wechatLogin.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. console.log('page', page);
  18. let local = window.location.href;
  19. let locationLocaturl = window.location.search;
  20. // 微信返回的回调地址
  21. let backUrl = getUrlParams(local, 'backUrl');
  22. if (backUrl) {
  23. this.backUrl = decodeURIComponent(backUrl);
  24. }
  25. console.log('backUrl', decodeURIComponent(backUrl));
  26. let loginType = getUrlParams(local, 'loginType');
  27. if (loginType) {
  28. this.loginType = loginType;
  29. }
  30. console.log('loginType', loginType);
  31. let code = getUrlParams(locationLocaturl, 'code');
  32. console.log('code', code);
  33. if (code) {
  34. this.code = code;
  35. // 判断重复回调问题
  36. let wechatLoginKey = localStorage.getItem('wechatLoginKey');
  37. if (wechatLoginKey) {
  38. wechatLoginKey = JSON.parse(wechatLoginKey);
  39. if (code === wechatLoginKey.code) {
  40. this.getUserInfo(wechatLoginKey.openId);
  41. } else {
  42. this.handleGetWXInfo(this.code);
  43. }
  44. } else {
  45. this.handleGetWXInfo(this.code);
  46. }
  47. } else {
  48. location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?
  49. appid=${this.config.wxAppid}&
  50. redirect_uri=${encodeURIComponent(local)}&
  51. response_type=code&
  52. scope=snsapi_userinfo&
  53. state=STATE#wechat_redirect`;
  54. }
  55. },
  56. methods: {
  57. // 通过code获取 openId等用户信息
  58. handleGetWXInfo(code) {
  59. uni.showLoading({
  60. title: '加载中'
  61. });
  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
  89. .sendSmsCodeV2api({
  90. openId
  91. })
  92. .then((res) => {
  93. if (res.code === 200) {
  94. this.$u.vuex('vuex_token', res.data.accessToken);
  95. this.$u.vuex('vuex_hasLogin', true);
  96. if (res.data.needVerify) {
  97. localStorage.setItem('backUrl', this.backUrl);
  98. this.$u.route({
  99. url: 'pages/center/phoneLogin/phoneLogin',
  100. type: 'reLaunch'
  101. });
  102. } else {
  103. this.$u.vuex('vuex_user', res.data);
  104. if (this.backUrl.indexOf('wechatLogin') > -1) {
  105. location.href = '/';
  106. } else {
  107. location.href = this.backUrl;
  108. }
  109. }
  110. uni.hideLoading();
  111. } else {
  112. this.$refs.uToast.show({
  113. title: res.msg || '获取用户信息失败!',
  114. type: 'error'
  115. });
  116. uni.hideLoading();
  117. }
  118. });
  119. },
  120. /**
  121. * @description:
  122. * @param {*} openId
  123. * @return {*}
  124. */
  125. async getUserInfo(openId) {
  126. try {
  127. const queryParams = {
  128. loginType: this.loginType,
  129. openid: openId,
  130. nickName: this?.vuex_wxinfo?.nickname,
  131. avatar: this?.vuex_wxinfo?.headImgUrl
  132. };
  133. const userInfo = await this.$u.api.userLoginApi.openidLoginApi({ ...queryParams });
  134. const { accessToken, needVerify } = userInfo.data;
  135. this.$u.vuex('vuex_token', accessToken);
  136. this.$u.vuex('vuex_hasLogin', true);
  137. if (needVerify) {
  138. localStorage.setItem('backUrl', this.backUrl);
  139. this.$u.route({
  140. url: 'pages/center/phoneLogin/phoneLogin',
  141. type: 'reLaunch'
  142. });
  143. } else {
  144. this.$u.vuex('vuex_user', userInfo.data);
  145. if (this.backUrl.indexOf('wechatLogin') > -1) {
  146. location.href = '/';
  147. } else {
  148. location.href = this.backUrl;
  149. }
  150. }
  151. uni.hideLoading();
  152. } catch (error) {
  153. this.$refs.uToast.show({
  154. title: '获取用户信息失败!',
  155. type: 'error'
  156. });
  157. uni.hideLoading();
  158. }
  159. }
  160. }
  161. };
  162. </script>