wechatLogin.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <view>
  3. <u-toast ref="uToast" />
  4. </view>
  5. </template>
  6. <script>
  7. import getUrlParams from '@/utils/getUrlParams.js';
  8. export default {
  9. data() {
  10. return {
  11. backUrl: '',
  12. code: '',
  13. loginType: 2
  14. };
  15. },
  16. onLoad(page) {
  17. uni.showLoading({
  18. title: '登录中...',
  19. mask: true
  20. });
  21. let local = window.location.href;
  22. let locationLocaturl = window.location.search;
  23. // 微信返回的回调地址
  24. let backUrl = getUrlParams(local, 'backUrl');
  25. if (backUrl) {
  26. this.backUrl = decodeURIComponent(backUrl);
  27. }
  28. console.log('backUrl', decodeURIComponent(backUrl));
  29. let loginType = getUrlParams(local, 'loginType');
  30. if (loginType) {
  31. this.loginType = loginType;
  32. }
  33. console.log('loginType', loginType);
  34. let code = getUrlParams(locationLocaturl, 'code');
  35. console.log('code', code);
  36. if (code) {
  37. this.code = code;
  38. // 判断重复回调问题
  39. let wechatLoginKey = localStorage.getItem('wechatLoginKey');
  40. if (wechatLoginKey) {
  41. wechatLoginKey = JSON.parse(wechatLoginKey);
  42. if (code === wechatLoginKey.code) {
  43. this.getUserInfo(wechatLoginKey.openId);
  44. } else {
  45. this.handleGetWXInfo(this.code);
  46. }
  47. } else {
  48. this.handleGetWXInfo(this.code);
  49. }
  50. } else {
  51. location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?
  52. appid=${this.config.wxAppid}&
  53. redirect_uri=${encodeURIComponent(local)}&
  54. response_type=code&
  55. scope=snsapi_userinfo&
  56. state=STATE#wechat_redirect`;
  57. }
  58. },
  59. methods: {
  60. // 通过code获取 openId等用户信息
  61. handleGetWXInfo(code) {
  62. this.$u.api
  63. .getWXInfo(code)
  64. .then((res) => {
  65. if (res.code === 200) {
  66. this.$u.vuex('vuex_wxinfo', res.data);
  67. localStorage.setItem(
  68. 'wechatLoginKey',
  69. JSON.stringify({
  70. code,
  71. openId: res.data.openId
  72. })
  73. );
  74. this.getUserInfo(res.data.openId);
  75. } else {
  76. uni.hideLoading();
  77. }
  78. })
  79. .catch((err) => {
  80. uni.hideLoading();
  81. });
  82. },
  83. /**
  84. * 通过openId获取token
  85. * @param {Object} openId
  86. */
  87. getToken(openId) {
  88. this.$u.api.codeV2Api.sendSmsCodeV2api({ openId }).then((res) => {
  89. if (res.code === 200) {
  90. this.$u.vuex('vuex_token', res.data.accessToken);
  91. this.$u.vuex('vuex_hasLogin', true);
  92. if (res.data.needVerify) {
  93. localStorage.setItem('backUrl', this.backUrl);
  94. this.$u.route({
  95. url: 'pages/center/phoneLogin/phoneLogin',
  96. type: 'reLaunch'
  97. });
  98. } else {
  99. this.$u.vuex('vuex_user', res.data);
  100. if (this.backUrl.indexOf('wechatLogin') > -1) {
  101. location.href = '/';
  102. } else {
  103. location.href = this.backUrl;
  104. }
  105. }
  106. uni.hideLoading();
  107. } else {
  108. this.$refs.uToast.show({
  109. title: res.msg || '获取用户信息失败!',
  110. type: 'error'
  111. });
  112. uni.hideLoading();
  113. }
  114. });
  115. },
  116. /**
  117. * @description:
  118. * @param {*} openId
  119. * @return {*}
  120. */
  121. async getUserInfo(openId) {
  122. try {
  123. const queryParams = {
  124. loginType: this.loginType,
  125. openid: openId,
  126. nickName: this?.vuex_wxinfo?.nickname,
  127. avatar: this?.vuex_wxinfo?.headImgUrl
  128. };
  129. const userInfo = await this.$u.api.userLoginApi.openidLoginApi({ ...queryParams });
  130. const { accessToken, needVerify } = userInfo.data;
  131. this.$u.vuex('vuex_token', accessToken);
  132. this.$u.vuex('vuex_hasLogin', true);
  133. if (needVerify) {
  134. localStorage.setItem('backUrl', this.backUrl);
  135. this.$u.route({
  136. url: 'pages/center/phoneLogin/phoneLogin',
  137. type: 'reLaunch'
  138. });
  139. } else {
  140. this.$u.vuex('vuex_user', userInfo.data);
  141. if (this.backUrl.indexOf('wechatLogin') > -1) {
  142. location.href = '/';
  143. } else {
  144. location.href = this.backUrl;
  145. }
  146. }
  147. uni.hideLoading();
  148. } catch (error) {
  149. this.$refs.uToast.show({
  150. title: '获取用户信息失败!',
  151. type: 'error'
  152. });
  153. uni.hideLoading();
  154. }
  155. }
  156. }
  157. };
  158. </script>