wxLogin.vue 4.5 KB

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