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="https://chaomd.liuniu946.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('spread_code') || '';
  112. // #endif
  113. this.$api
  114. .wechatMpAuthLogin({
  115. sessionKey: obj.sessionKey,
  116. openid: obj.openid,
  117. iv: user.iv,
  118. encryptedData: user.encryptedData
  119. })
  120. .then(({ data }) => {
  121. obj.wchatAuth(data);
  122. console.log(data, 'wechatMpAuth++++++++++++++++++++++++++');
  123. })
  124. .catch(err => {
  125. console.log(err);
  126. // obj.loding = false;
  127. // uni.hideLoading();
  128. });
  129. },
  130. // #endif
  131. wchatAuth(data) {
  132. let obj = this;
  133. // 保存token
  134. uni.setStorageSync('token', data.token);
  135. // 获取用户基础信息
  136. this.$api.userinfo().then(e => {
  137. uni.hideLoading();
  138. // 保存返回用户数据
  139. let ur = uni.getStorageSync('present') || '/pages/tabbar/index';
  140. // 用于处理缓存bug
  141. if (ur == 'pages/shop/product') {
  142. ur = '/pages/tabbar/index';
  143. }
  144. console.log(ur, '1111');
  145. uni.switchTab({
  146. url: ur,
  147. fail(e) {
  148. uni.navigateTo({
  149. url: ur,
  150. fail(e) {
  151. uni.navigateTo({
  152. url: '/pages/tabbar/index'
  153. });
  154. }
  155. });
  156. }
  157. });
  158. });
  159. }
  160. }
  161. };
  162. </script>
  163. <style lang="scss">
  164. page,
  165. .content {
  166. height: 100%;
  167. background-color: #fff;
  168. }
  169. .bg-img,
  170. .logo-img-box {
  171. position: absolute;
  172. top: 0;
  173. left: 0;
  174. width: 100%;
  175. height: 100%;
  176. }
  177. .logo-img {
  178. // background: #000;
  179. margin-top: 20vh;
  180. margin-left: 176rpx;
  181. width: 385rpx;
  182. height: 394rpx;
  183. }
  184. .userInfo {
  185. margin: 0 100rpx;
  186. margin-top: 50rpx;
  187. color: #3374ff;
  188. border-radius: 99rpx;
  189. background-color: #6bc6fe !important;
  190. }
  191. .nocaction {
  192. background-color: #999;
  193. }
  194. </style>