wxLogin.vue 4.3 KB

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