wxLogin.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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="https://hongmd.liuniu946.com/static/img/hmdlogo.png" mode=" aspectFit"></image>
  7. <button class="userInfo" type="warn" @click="isclick?'':userInfoData()" :class="{'nocaction': isclick}">
  8. <text class="iconfont iconweixin"></text>
  9. <text>
  10. 微信授权登录
  11. </text>
  12. </button>
  13. </view>
  14. <!-- #endif -->
  15. </view>
  16. </template>
  17. <script>
  18. import {
  19. getUserInfo
  20. } from '@/api/login.js';
  21. // #ifdef H5
  22. import {
  23. loginWinxin
  24. } from '@/utils/wxAuthorized';
  25. // #endif
  26. // #ifdef MP-WEIXIN
  27. import {
  28. loginWinxinMp
  29. } from '@/utils/wxMinProgram';
  30. import {
  31. wechatMpAuth
  32. } from '@/api/wx';
  33. // #endif
  34. import {
  35. mapMutations,
  36. mapState
  37. } from 'vuex';
  38. export default {
  39. data() {
  40. return {
  41. userInfo: {}, //授权用户信息
  42. code: '', //授权code
  43. isclick: false, //是否点击了
  44. };
  45. },
  46. onLoad(option) {
  47. this.loadData();
  48. },
  49. computed: {
  50. // ...mapState(['baseURL','urlFile'])
  51. },
  52. methods: {
  53. // ...mapMutations(['login', 'setUserInfo']),
  54. ...mapMutations('user', ['login', 'setUserInfo']),
  55. loadData() {
  56. let obj = this;
  57. // #ifdef H5
  58. loginWinxin();
  59. // #endif
  60. // #ifdef MP-WEIXIN
  61. loginWinxinMp().then(() => {
  62. wx.login({
  63. success(e) {
  64. console.log(e, 'loginWinxinMp');
  65. obj.code = e.code;
  66. },
  67. fill: function(e) {
  68. console.log(e)
  69. }
  70. })
  71. });
  72. // #endif
  73. },
  74. // 用户确认授权
  75. userInfoData() {
  76. let that = this
  77. if (that.isclick) {
  78. return
  79. }
  80. that.isclick = true
  81. wx.getUserProfile({
  82. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  83. success: res => {
  84. console.log(res, 'that.userInfo+++++++++++++++++++')
  85. that.userInfo = res;
  86. uni.showLoading({
  87. title: '授权中',
  88. mask: true
  89. });
  90. that.loadMp();
  91. },
  92. fail: err => {
  93. that.isclick = false
  94. uni.showToast({
  95. title: '您拒绝了请求,不能正常使用小程序',
  96. icon: 'error',
  97. duration: 2000
  98. });
  99. return;
  100. }
  101. });
  102. // this.userInfo = e;
  103. // console.log(e,'用户确认授权')
  104. // this.loadMp()
  105. },
  106. // #ifdef MP-WEIXIN
  107. loadMp() {
  108. let obj = this;
  109. // 获取登录授权页数据
  110. let user = obj.userInfo;
  111. console.log(user)
  112. // 获取推广人id
  113. let spread_spid = uni.getStorageSync('spread') || '';
  114. // #ifdef MP
  115. let spread_code = uni.getStorageSync('spread_code') || '';
  116. // #endif
  117. wechatMpAuth({
  118. code: obj.code,
  119. iv: user.iv,
  120. encryptedData: user.encryptedData,
  121. spread_spid: spread_spid,
  122. // #ifdef MP
  123. spread_code: spread_code,
  124. // #endif
  125. }).then(({
  126. data
  127. }) => {
  128. obj.wchatAuth(data);
  129. console.log(data, 'wechatMpAuth++++++++++++++++++++++++++')
  130. }).catch(err => {
  131. // obj.loding = false;
  132. // uni.hideLoading();
  133. });
  134. },
  135. // #endif
  136. wchatAuth(data) {
  137. let obj = this;
  138. // 保存token
  139. uni.setStorageSync('token', data.token);
  140. console.log(data.token, 'token++++++++++++++')
  141. // 获取用户基础信息
  142. getUserInfo({}).then(e => {
  143. console.log('userInfo+++++++++++', e)
  144. obj.login();
  145. uni.hideLoading();
  146. // 保存返回用户数据
  147. obj.setUserInfo(e.data);
  148. let ur = uni.getStorageSync('present') || '/pages/index/index';
  149. // 用于处理缓存bug
  150. if (ur == 'pages/shop/product') {
  151. ur = '/pages/index/index'
  152. }
  153. uni.switchTab({
  154. url: ur,
  155. fail(e) {
  156. uni.navigateTo({
  157. url: ur,
  158. fail(e) {
  159. uni.navigateTo({
  160. url: '/pages/index/index',
  161. });
  162. }
  163. });
  164. }
  165. });
  166. });
  167. }
  168. }
  169. };
  170. </script>
  171. <style lang="scss">
  172. page,
  173. .content {
  174. height: 100%;
  175. background-color: #fff;
  176. }
  177. .bg-img,
  178. .logo-img-box {
  179. position: absolute;
  180. top: 0;
  181. left: 0;
  182. width: 100%;
  183. height: 100%;
  184. }
  185. .logo-img {
  186. margin-top: 20vh;
  187. margin-left: 176rpx;
  188. width: 385rpx;
  189. height: 394rpx;
  190. }
  191. .userInfo {
  192. margin: 0 100rpx;
  193. margin-top: 50rpx;
  194. color: #FFFFFF;
  195. border-radius: 99rpx;
  196. background-color: $base-color !important;
  197. }
  198. .nocaction {
  199. background-color: #999;
  200. }
  201. </style>