wxLogin.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <view class="content">
  3. <!-- #ifndef H5 -->
  4. <!-- <image class="bg-img" :src="baseURL+urlFile+'/img/img09.png'" mode=" scaleToFill"></image> -->
  5. <view class="logo-img-box">
  6. <image class="logo-img" src="../../static/img/logo.png" mode=" aspectFit"></image>
  7. <!-- 获取头像登录 -->
  8. <!-- <button class="userInfo" type="warn" @click="isclick?'':userInfoData()" :class="{'nocaction': isclick}">
  9. <text class="iconfont iconweixin"></text>
  10. <text>
  11. 微信授权登录
  12. </text>
  13. </button> -->
  14. <!-- 获取手机号登录 -->
  15. <template>
  16. <button class="userInfo" type="warn" :class="{ nocaction: isclick }" @click="isclick ? '' : userInfoData()">
  17. <text class="iconfont iconweixin"></text>
  18. <text>微信授权登录</text>
  19. </button>
  20. </template>
  21. <!-- 550*450 -->
  22. <uni-popup ref="popupsq" type="center">
  23. <view class="popupsq-wrap">
  24. <view class="" style="text-align: center;font-size: 32rpx;font-weight: bold;padding: 40rpx 0;color: #000;">亲家母优选</view>
  25. <view class="" style="font-size: 26rpx;padding-bottom: 35rpx;color: #000;">该程序获取以下授权:</view>
  26. <view class="" style="font-size: 26rpx;color:#b7b7b7;">获取您的公开信息(昵称,头像),以便于个性话头像和昵称</view>
  27. <view class="flex" style="padding-top: 40rpx;">
  28. <view class="btn qx" style="" @click="close()">取消</view>
  29. <view class="btn qr" style="" @click="isclick ? '' : userInfoData()">确认授权</view>
  30. </view>
  31. </view>
  32. </uni-popup>
  33. </view>
  34. <!-- #endif -->
  35. </view>
  36. </template>
  37. <script>
  38. import { getUserInfo } from '@/api/login.js';
  39. // #ifdef H5
  40. import { loginWinxin } from '@/utils/wxAuthorized';
  41. // #endif
  42. // #ifdef MP-WEIXIN
  43. import { loginWinxinMp } from '@/utils/wxMinProgram';
  44. import { wechatMpAuth } from '@/api/wx';
  45. // #endif
  46. import { mapMutations, mapState } from 'vuex';
  47. export default {
  48. data() {
  49. return {
  50. userInfo: {}, //授权用户信息
  51. code: '', //授权code
  52. isclick: false, //是否点击了
  53. iv1: '',
  54. encryptedData1: '',
  55. hasphone: false
  56. };
  57. },
  58. onLoad() {
  59. this.loadData();
  60. },
  61. computed: {
  62. // ...mapState(['baseURL','urlFile'])
  63. },
  64. methods: {
  65. // ...mapMutations(['login', 'setUserInfo']),
  66. ...mapMutations('user', ['login', 'setUserInfo']),
  67. close() {
  68. this.$refs.popupsq.close();
  69. },
  70. getPhoneNumber(e) {
  71. let obj = this;
  72. console.log(e, 'e+++++++++++++++');
  73. // 用户同意授权
  74. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  75. console.log('chenggong++++++++++++++');
  76. obj.hasphone = true;
  77. obj.iv1 = e.detail.iv;
  78. obj.encryptedData1 = e.detail.encryptedData;
  79. obj.$refs.popupsq.open();
  80. } else {
  81. obj.isclick = false;
  82. uni.showToast({
  83. title: '您拒绝了请求,不能正常使用小程序',
  84. icon: 'error',
  85. duration: 2000
  86. });
  87. return;
  88. }
  89. },
  90. loadData() {
  91. let obj = this;
  92. // #ifdef H5
  93. loginWinxin();
  94. // #endif
  95. // #ifdef MP-WEIXIN
  96. loginWinxinMp().then(e => {
  97. console.log(e, 'ddddddddddddddddddddddd');
  98. // obj.code = e.code
  99. wx.login({
  100. success(e) {
  101. console.log(e, 'loginWinxinMp');
  102. obj.code = e.code;
  103. console.log(obj.code, 'wxlogin.code++++++');
  104. },
  105. fill: function(e) {
  106. console.log(e);
  107. }
  108. });
  109. });
  110. // #endif
  111. },
  112. // 用户确认授权
  113. userInfoData() {
  114. let that = this;
  115. if (that.isclick) {
  116. return;
  117. }
  118. that.isclick = true;
  119. wx.getUserProfile({
  120. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  121. success: res => {
  122. console.log(res, 'that.userInfo+++++++++++++++++++');
  123. that.userInfo = res;
  124. uni.showLoading({
  125. title: '授权中',
  126. mask: true
  127. });
  128. that.loadMp();
  129. },
  130. fail: err => {
  131. that.isclick = false;
  132. uni.showToast({
  133. title: '您拒绝了请求,不能正常使用小程序',
  134. icon: 'error',
  135. duration: 2000
  136. });
  137. return;
  138. }
  139. });
  140. // this.userInfo = e;
  141. // console.log(e,'用户确认授权')
  142. // this.loadMp()
  143. },
  144. // #ifdef MP-WEIXIN
  145. loadMp() {
  146. let obj = this;
  147. // 获取登录授权页数据
  148. let user = obj.userInfo;
  149. console.log(user);
  150. // 获取推广人id
  151. let spread_spid = uni.getStorageSync('spread') || '';
  152. // #ifdef MP
  153. let spread_code = uni.getStorageSync('spread_code') || '';
  154. // #endif
  155. wechatMpAuth({
  156. code: obj.code,
  157. iv: user.iv,
  158. iv1: obj.iv1,
  159. encryptedData1: obj.encryptedData1,
  160. encryptedData: user.encryptedData,
  161. spread_spid: spread_spid,
  162. // #ifdef MP
  163. spread_code: spread_code
  164. // #endif
  165. })
  166. .then(({ data }) => {
  167. obj.wchatAuth(data);
  168. console.log(data, 'wechatMpAuth++++++++++++++++++++++++++');
  169. })
  170. .catch(err => {
  171. // obj.loding = false;
  172. // uni.hideLoading();
  173. });
  174. },
  175. // #endif
  176. wchatAuth(data) {
  177. let obj = this;
  178. // 保存token
  179. uni.setStorageSync('token', data.token);
  180. console.log(data.token, 'token++++++++++++++');
  181. // 获取用户基础信息
  182. getUserInfo({}).then(e => {
  183. console.log('userInfo+++++++++++', e);
  184. obj.login();
  185. uni.hideLoading();
  186. // 保存返回用户数据
  187. obj.setUserInfo(e.data);
  188. let ur = uni.getStorageSync('present') || '/pages/index/index';
  189. // 用于处理缓存bug
  190. if (ur == 'pages/shop/product') {
  191. ur = '/pages/index/index';
  192. }
  193. uni.switchTab({
  194. url: ur,
  195. fail(e) {
  196. uni.navigateTo({
  197. url: ur,
  198. fail(e) {
  199. uni.navigateTo({
  200. url: '/pages/index/index'
  201. });
  202. }
  203. });
  204. }
  205. });
  206. });
  207. }
  208. }
  209. };
  210. </script>
  211. <style lang="scss">
  212. page,
  213. .content {
  214. height: 100%;
  215. background-color: #fff;
  216. }
  217. .bg-img,
  218. .logo-img-box {
  219. position: absolute;
  220. top: 0;
  221. left: 0;
  222. width: 100%;
  223. height: 100%;
  224. }
  225. .logo-img {
  226. margin-top: 20vh;
  227. margin-left: 176rpx;
  228. width: 385rpx;
  229. height: 394rpx;
  230. }
  231. .userInfo {
  232. margin: 0 100rpx;
  233. margin-top: 50rpx;
  234. color: #ffffff;
  235. border-radius: 99rpx;
  236. background-color: #000000 !important;
  237. }
  238. .nocaction {
  239. background-color: #999;
  240. }
  241. .popupsq-wrap {
  242. width: 550rpx;
  243. height: 450rpx;
  244. background-color: #fff;
  245. border-radius: 20rpx;
  246. padding: 0 30rpx;
  247. .btn {
  248. width: 240rpx;
  249. height: 90rpx;
  250. text-align: center;
  251. line-height: 90rpx;
  252. font-size: 30rpx;
  253. border-radius: 45rpx;
  254. font-weight: bold;
  255. }
  256. .qx {
  257. color: #000;
  258. border: #f0f0f0 1px solid;
  259. }
  260. .qr {
  261. color: #fff;
  262. background-color: #24a17d;
  263. }
  264. }
  265. </style>