index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <view class="auth">
  3. <image src="/static/logo1.png" mode=""></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" open-type="getUserInfo" @getuserinfo="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. var md5 = require("../../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. },
  27. methods:{
  28. getUserInfo(e) {
  29. if (e.detail.errMsg == "getUserInfo:ok") {
  30. console.log(e)
  31. getApp().globalData.user_headUrl = e.detail.userInfo.avatarUrl;
  32. getApp().globalData.user_name = e.detail.userInfo.nickName;
  33. this.iv = e.detail.iv;
  34. this.encryptedData = e.detail.encryptedData;
  35. this.loginUserInfo()
  36. } else {
  37. console.log("用户信息授权失败");
  38. getApp().globalData.isAuth = false;
  39. }
  40. },
  41. getPhoneNumber(e){
  42. let that = this;
  43. if (e.detail.errMsg == 'getPhoneNumber:ok') { //允许授权执行跳转
  44. that.phoneRequest(e.detail.iv, e.detail.encryptedData, getApp().globalData.session_key)
  45. } else { //
  46. that.isNeedPhone = false;
  47. }
  48. },
  49. phoneRequest(myIv,myEncryptedData,sKey){
  50. let that = this;
  51. uni.request({
  52. url:getApp().globalData.shareUrl, //需要设置为全局
  53. method: 'POST',
  54. header: {
  55. 'content-type': 'application/x-www-form-urlencoded'
  56. },
  57. data: {
  58. method: 'getPhoneNumber',
  59. timestamp: getApp().globalData.globalTimestamp, //Date.now()
  60. uid:getApp().globalData.user_id,
  61. sign: md5('getPhoneNumber' + getApp().globalData.globalTimestamp),
  62. iv:myIv,
  63. sessionKey:sKey,
  64. encryptedData:myEncryptedData,
  65. },
  66. success: res => {
  67. getApp().globalData.user_phone = res.data.phoneNumber;
  68. that.refuseBtn();
  69. }
  70. });
  71. },
  72. refuseBtn(){
  73. uni.navigateBack({
  74. });
  75. },
  76. loginUserInfo(){
  77. let that = this;
  78. uni.request({
  79. url:getApp().globalData.shareUrl, //需要设置为全局
  80. method: 'POST',
  81. header: {
  82. 'content-type': 'application/x-www-form-urlencoded'
  83. },
  84. data: {
  85. method: 'auth',
  86. timestamp: getApp().globalData.globalTimestamp, //Date.now()
  87. id:getApp().globalData.user_id,
  88. sign: md5('auth' + getApp().globalData.globalTimestamp),
  89. nickname:getApp().globalData.user_name,
  90. headimg:getApp().globalData.user_headUrl,
  91. // iv:that.iv,
  92. // session_key:getApp().globalData.session_key,
  93. // encryptedData:that.encryptedData,
  94. auth_status:1,
  95. shangjiid:getApp().globalData.lastId?getApp().globalData.lastId:getApp().globalData.user_id
  96. },
  97. success: res => {
  98. if(res.data.code === 200){
  99. getApp().globalData.user_id = res.data.msg.id;
  100. getApp().globalData.open_id = res.data.msg.openid;
  101. getApp().globalData.isAuth = true;
  102. getApp().globalData.user_headUrl = res.data.msg.headimg;
  103. getApp().globalData.user_name = res.data.msg.name;
  104. getApp().globalData.user_status = res.data.msg.status;
  105. getApp().globalData.user_phone = res.data.msg.phone;
  106. that.isNeedPhone = res.data.msg.phone;
  107. that.isAuth = getApp().globalData.isAuth;
  108. }
  109. else {
  110. uni.showToast({
  111. title: res.data.msg,
  112. icon: 'none',
  113. duration:2000
  114. });
  115. }
  116. // uni.navigateBack({
  117. // });
  118. }
  119. });
  120. },
  121. }
  122. }
  123. </script>
  124. <style>
  125. .auth{
  126. margin-top: 0;
  127. text-align: center;
  128. display: flex;
  129. flex-direction: column;
  130. justify-content: center;
  131. align-items: center;
  132. padding: 100upx;
  133. vertical-align: middle;
  134. }
  135. .auth image{
  136. width: 200upx;
  137. height: 200upx;
  138. }
  139. .auth view {
  140. display: flex;
  141. width: 490upx;
  142. justify-content: space-between;
  143. }
  144. .auth-title {
  145. font-size: 32upx;
  146. }
  147. .auth-content {
  148. font-size: 26upx;
  149. color: #a7aaa9;
  150. }
  151. .allow {
  152. background-color: #27BCEF;
  153. margin: 20rpx 0 200rpx;
  154. text-align: center;
  155. vertical-align: middle;
  156. touch-action: manipulation;
  157. cursor: pointer;
  158. background-image: none;
  159. white-space: nowrap;
  160. user-select: none;
  161. font-size: 14px;
  162. border: 0 !important;
  163. position: relative;
  164. text-decoration: none;
  165. height: 44px;
  166. width: 250rpx;
  167. line-height: 44px;
  168. box-shadow: inset 0 0 0 1px #27BCEF;
  169. background: #fff !important;
  170. color: #27BCEF !important;
  171. display: inline-block;
  172. border-radius: 10rpx;
  173. }
  174. .refuse {
  175. background-color: #19be6b;
  176. margin: 20rpx 20rpx 200rpx 20rpx;
  177. text-align: center;
  178. vertical-align: middle;
  179. touch-action: manipulation;
  180. cursor: pointer;
  181. background-image: none;
  182. white-space: nowrap;
  183. user-select: none;
  184. font-size: 14px;
  185. border: 0 !important;
  186. position: relative;
  187. text-decoration: none;
  188. height: 44px;
  189. width: 250rpx;
  190. line-height: 44px;
  191. box-shadow: inset 0 0 0 1px #8a8a8a;
  192. background: #fff !important;
  193. color: #8a8a8a !important;
  194. display: inline-block;
  195. border-radius: 10rpx;
  196. }
  197. .margin-top-3{
  198. margin-top: 10%;
  199. }
  200. </style>