wechatLogin.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. this.handleGetWxOpenId(this.code);
  47. }
  48. } else {
  49. // this.handleGetWXInfo(this.code);
  50. this.handleGetWxOpenId(this.code);
  51. }
  52. } else {
  53. // scope (snsapi_userinfo 获取用户基本信息 snsapi_base 只获取OpenId)
  54. location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?
  55. appid=${this.config.wxAppid}&
  56. redirect_uri=${encodeURIComponent(local)}&
  57. response_type=code&
  58. scope=snsapi_base&
  59. state=STATE#wechat_redirect`;
  60. }
  61. },
  62. methods: {
  63. // 通过code获取 openId等用户信息
  64. handleGetWXInfo(code) {
  65. this.$u.api
  66. .getWXInfo(code)
  67. .then((res) => {
  68. if (res.code === 200) {
  69. this.$u.vuex('vuex_wxinfo', res.data);
  70. localStorage.setItem(
  71. 'wechatLoginKey',
  72. JSON.stringify({
  73. code,
  74. openId: res.data.openId
  75. })
  76. );
  77. this.getUserInfo(res.data.openId);
  78. } else {
  79. uni.hideLoading();
  80. }
  81. })
  82. .catch((err) => {
  83. uni.hideLoading();
  84. });
  85. },
  86. /**
  87. * @description: 通过code获取OpenId
  88. * @param {*} code
  89. * @return {*}
  90. */
  91. handleGetWxOpenId(code) {
  92. this.$u.api
  93. .getWxOpenidApi({ code })
  94. .then((res) => {
  95. if (res.code === 200) {
  96. this.$u.vuex('vuex_wxinfo', res.data);
  97. localStorage.setItem(
  98. 'wechatLoginKey',
  99. JSON.stringify({
  100. code,
  101. openId: res.data.openId
  102. })
  103. );
  104. this.getUserInfo(res.data.openId);
  105. } else {
  106. uni.hideLoading();
  107. }
  108. })
  109. .catch((err) => {
  110. uni.hideLoading();
  111. });
  112. },
  113. /**
  114. * 通过openId获取token
  115. * @param {Object} openId
  116. */
  117. getToken(openId) {
  118. this.$u.api.codeV2Api.sendSmsCodeV2api({ openId }).then((res) => {
  119. if (res.code === 200) {
  120. this.$u.vuex('vuex_token', res.data.accessToken);
  121. this.$u.vuex('vuex_hasLogin', true);
  122. if (res.data.needVerify) {
  123. localStorage.setItem('backUrl', this.backUrl);
  124. this.$u.route({
  125. url: 'pages/center/phoneLogin/phoneLogin',
  126. type: 'reLaunch'
  127. });
  128. } else {
  129. this.$u.vuex('vuex_user', res.data);
  130. if (this.backUrl.indexOf('wechatLogin') > -1) {
  131. location.href = '/';
  132. } else {
  133. location.href = this.backUrl;
  134. }
  135. }
  136. uni.hideLoading();
  137. } else {
  138. this.$refs.uToast.show({
  139. title: res.msg || '获取用户信息失败!',
  140. type: 'error'
  141. });
  142. uni.hideLoading();
  143. }
  144. });
  145. },
  146. /**
  147. * @description:
  148. * @param {*} openId
  149. * @return {*}
  150. */
  151. async getUserInfo(openId) {
  152. try {
  153. const queryParams = {
  154. loginType: this.loginType,
  155. openid: openId,
  156. nickName: this?.vuex_wxinfo?.nickname,
  157. avatar: this?.vuex_wxinfo?.headImgUrl
  158. };
  159. const userInfo = await this.$u.api.userLoginApi.openidLoginApi({ ...queryParams });
  160. const { accessToken, needVerify } = userInfo.data;
  161. this.$u.vuex('vuex_token', accessToken);
  162. this.$u.vuex('vuex_hasLogin', true);
  163. if (needVerify) {
  164. localStorage.setItem('backUrl', this.backUrl);
  165. this.$u.route({
  166. url: 'pages/center/phoneLogin/phoneLogin',
  167. type: 'reLaunch'
  168. });
  169. } else {
  170. this.$u.vuex('vuex_user', userInfo.data);
  171. if (this.backUrl.indexOf('wechatLogin') > -1) {
  172. location.href = '/';
  173. } else {
  174. location.href = this.backUrl;
  175. }
  176. }
  177. uni.hideLoading();
  178. } catch (error) {
  179. this.$refs.uToast.show({
  180. title: '获取用户信息失败!',
  181. type: 'error'
  182. });
  183. uni.hideLoading();
  184. }
  185. }
  186. }
  187. };
  188. </script>