wxLogin.vue 4.2 KB

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