index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <view class="auth">
  3. <image src="/static/logo.jpg" mode="aspectFill"></image>
  4. <text class="margin-top-3 auth-title">欢迎使用农工笔记小程序</text>
  5. <text class="margin-top-3 auth-content">此页面是微信授权页面,授权之后你可以获取更优质的服务,您的隐私将会受到保护</text>
  6. <view class="margin-top-3">
  7. <button type='default' class="refuse" @click="refuseBtn">暂不授权</button>
  8. <button type='primary' class="allow" @click="getUserInfo()" v-if="!isAuth">登录授权</button>
  9. <button type="primary" class="allow" open-type="getPhoneNumber" style="font-size: 12px;"
  10. @getphonenumber="getPhoneNumber" v-if="isAuth && !isNeedPhone">手机号码授权</button>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import md5 from '@/common/md5.js';
  16. export default {
  17. data(){
  18. return {
  19. iv:'',
  20. encryptedData:'',
  21. isNeedPhone:getApp().globalData.user_phone,
  22. isAuth:getApp().globalData.isAuth
  23. }
  24. },
  25. onLoad() {
  26. this.getUserInfoData()
  27. },
  28. methods:{
  29. getUserInfoData(){
  30. let md5Sign = md5("method="+'user'+"&timestamp="+getApp().globalData.globalTimestamp
  31. +"&secret="+getApp().globalData.secret)
  32. let url = getApp().globalData.shareUrl+'api/api.php'+'?method=user&action=info_by_openid&timestamp='
  33. +getApp().globalData.globalTimestamp +'&sign='+md5Sign
  34. uni.request({
  35. url:url,
  36. method: 'POST',
  37. header: {
  38. 'content-type': 'application/x-www-form-urlencoded'
  39. },
  40. data: {
  41. openId:getApp().globalData.open_id
  42. },
  43. success: (res) => {
  44. if(res.data.code === 200){
  45. if(res.data.data.nickname){
  46. getApp().globalData.isAuth = true;
  47. getApp().globalData.globalAuth = true;
  48. getApp().globalData.user_phone = false;
  49. getApp().globalData.user_name = res.data.data.nickname;
  50. this.isAuth = true;
  51. this.isNeedPhone = false;
  52. }else{
  53. getApp().globalData.user_name = '';
  54. getApp().globalData.isAuth = false;
  55. getApp().globalData.globalAuth = false;
  56. this.isAuth = false;
  57. }
  58. }
  59. },
  60. fail: () => {
  61. console.log("连接失败");
  62. }
  63. });
  64. },
  65. getUserInfo() {
  66. uni.getUserProfile({
  67. desc:'登录',
  68. success:(res)=> {
  69. getApp().globalData.user_headUrl = res.userInfo.avatarUrl;
  70. getApp().globalData.user_name = res.userInfo.nickName;
  71. this.iv = res.iv;
  72. this.encryptedData = res.encryptedData;
  73. this.isAuth = true;
  74. getApp().globalData.isAuth = true;
  75. getApp().globalData.globalAuth = true;
  76. this.loginUserInfo()
  77. },
  78. fail:(err)=> {
  79. getApp().globalData.isAuth = false;
  80. getApp().globalData.globalAuth = false;
  81. this.isAuth = false;
  82. }
  83. })
  84. // if (e.detail.errMsg == "getUserInfo:ok") {
  85. // getApp().globalData.user_headUrl = e.detail.userInfo.avatarUrl;
  86. // getApp().globalData.user_name = e.detail.userInfo.nickName;
  87. // this.iv = e.detail.iv;
  88. // this.encryptedData = e.detail.encryptedData;
  89. // console.log(e.detail)
  90. // //this.loginUserInfo()
  91. // } else {
  92. // console.log("用户信息授权失败");
  93. // getApp().globalData.isAuth = false;
  94. // }
  95. },
  96. getPhoneNumber(e){
  97. let that = this;
  98. if (e.detail.errMsg == 'getPhoneNumber:ok') { //允许授权执行跳转
  99. console.log(e.detail)
  100. that.phoneRequest(e.detail.iv, e.detail.encryptedData, getApp().globalData.session_key)
  101. } else { //
  102. that.isNeedPhone = false;
  103. }
  104. },
  105. phoneRequest(myIv,myEncryptedData,sKey){
  106. let that = this;
  107. let md5Sign = md5("method="+'user'+"&timestamp="+getApp().globalData.globalTimestamp+"&secret="+getApp().globalData.secret)
  108. let url = getApp().globalData.shareUrl+'api/api.php'+'?method=user&source=user&action=phone&timestamp='+getApp().globalData.globalTimestamp +'&sign='+md5Sign
  109. uni.request({
  110. url:url,
  111. method: 'POST',
  112. header: {
  113. 'content-type': 'application/x-www-form-urlencoded'
  114. },
  115. data: {
  116. iv:myIv,
  117. sessionKey:sKey,
  118. encryptedData:myEncryptedData,
  119. openId:getApp().globalData.open_id
  120. },
  121. success: (res) => {
  122. console.log(res)
  123. if(res.data.code === 200){
  124. getApp().globalData.user_phone = res.data.data;
  125. setTimeout(()=>{
  126. that.refuseBtn();
  127. },300)
  128. }
  129. },
  130. fail: () => {
  131. console.log("连接失败");
  132. }
  133. });
  134. },
  135. refuseBtn(){
  136. uni.navigateBack({
  137. });
  138. },
  139. loginUserInfo(){
  140. let md5Sign = md5(
  141. "method=" +
  142. "user" +
  143. "&timestamp=" +
  144. getApp().globalData.globalTimestamp +
  145. "&secret=" +
  146. getApp().globalData.secret
  147. );
  148. let url =
  149. getApp().globalData.shareUrl +
  150. "api/api.php" +
  151. "?method=user&action=update&timestamp=" +
  152. getApp().globalData.globalTimestamp +
  153. "&sign=" +
  154. md5Sign;
  155. uni.request({
  156. url: url,
  157. method: "POST",
  158. header: {
  159. "content-type": "application/x-www-form-urlencoded",
  160. },
  161. data: {
  162. nickname:getApp().globalData.user_name,
  163. headimg:getApp().globalData.user_headUrl,
  164. openid:getApp().globalData.open_id
  165. },
  166. success: (res) => {
  167. if (res.data.code === 200) {
  168. }
  169. },
  170. fail: () => {
  171. console.log("连接失败");
  172. },
  173. });
  174. },
  175. }
  176. }
  177. </script>
  178. <style>
  179. .auth{
  180. margin-top: 0;
  181. text-align: center;
  182. display: flex;
  183. flex-direction: column;
  184. justify-content: center;
  185. align-items: center;
  186. padding: 100upx;
  187. vertical-align: middle;
  188. }
  189. .auth image{
  190. width: 200upx;
  191. height: 200upx;
  192. }
  193. .auth view {
  194. display: flex;
  195. width: 490upx;
  196. justify-content: space-between;
  197. }
  198. .auth-title {
  199. font-size: 32upx;
  200. }
  201. .auth-content {
  202. font-size: 26upx;
  203. color: #a7aaa9;
  204. }
  205. .allow {
  206. background-color: #27BCEF;
  207. margin: 20rpx 0 200rpx;
  208. text-align: center;
  209. vertical-align: middle;
  210. touch-action: manipulation;
  211. cursor: pointer;
  212. background-image: none;
  213. white-space: nowrap;
  214. user-select: none;
  215. font-size: 14px;
  216. border: 0 !important;
  217. position: relative;
  218. text-decoration: none;
  219. height: 44px;
  220. width: 250rpx;
  221. line-height: 44px;
  222. box-shadow: inset 0 0 0 1px #27BCEF;
  223. background: #fff !important;
  224. color: #27BCEF !important;
  225. display: inline-block;
  226. border-radius: 10rpx;
  227. }
  228. .refuse {
  229. background-color: #19be6b;
  230. margin: 20rpx 20rpx 200rpx 20rpx;
  231. text-align: center;
  232. vertical-align: middle;
  233. touch-action: manipulation;
  234. cursor: pointer;
  235. background-image: none;
  236. white-space: nowrap;
  237. user-select: none;
  238. font-size: 14px;
  239. border: 0 !important;
  240. position: relative;
  241. text-decoration: none;
  242. height: 44px;
  243. width: 250rpx;
  244. line-height: 44px;
  245. box-shadow: inset 0 0 0 1px #8a8a8a;
  246. background: #fff !important;
  247. color: #8a8a8a !important;
  248. display: inline-block;
  249. border-radius: 10rpx;
  250. }
  251. .margin-top-3{
  252. margin-top: 10%;
  253. }
  254. </style>