wxLogin.vue 4.3 KB

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