wechatLogin.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. };
  14. },
  15. onLoad(page) {
  16. console.log('page:', page);
  17. const locationLocaturl = location.search;
  18. // 微信返回的回调地址
  19. const backUrl = page?.backUrl;
  20. if (backUrl) {
  21. this.backUrl = backUrl;
  22. }
  23. console.log('backUrl', backUrl);
  24. let code = getUrlParams(locationLocaturl, 'code');
  25. console.log('code', code);
  26. if (code) {
  27. this.code = code;
  28. // 判断重复回调问题
  29. let wechatLoginKey = localStorage.getItem('wechatLoginKey');
  30. if (wechatLoginKey) {
  31. wechatLoginKey = JSON.parse(wechatLoginKey);
  32. if (code === wechatLoginKey.code) {
  33. this.getToken(wechatLoginKey.openId);
  34. } else {
  35. this.handleGetWXInfo(this.code);
  36. }
  37. } else {
  38. this.handleGetWXInfo(this.code);
  39. }
  40. } else {
  41. const local = location.href;
  42. location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?
  43. appid=${this.config.wxAppid}&
  44. redirect_uri=${encodeURIComponent(local)}&
  45. response_type=code&
  46. scope=snsapi_userinfo&
  47. state=STATE#wechat_redirect`;
  48. }
  49. },
  50. methods: {
  51. // 通过code获取 openId等用户信息
  52. handleGetWXInfo(code) {
  53. uni.showLoading({
  54. title: '加载中'
  55. });
  56. this.$u.api
  57. .getWXInfo(code)
  58. .then((res) => {
  59. if (res.code === 200) {
  60. this.$u.vuex('vuex_wxinfo', res.data);
  61. localStorage.setItem(
  62. 'wechatLoginKey',
  63. JSON.stringify({
  64. code,
  65. openId: res.data.openId
  66. })
  67. );
  68. this.getToken(res.data.openId);
  69. } else {
  70. this.$refs.uToast.show({
  71. title: res.msg || '获取用户信息失败!',
  72. type: 'error'
  73. });
  74. uni.hideLoading();
  75. }
  76. })
  77. .catch((err) => {
  78. uni.hideLoading();
  79. });
  80. },
  81. /**
  82. * 通过openId获取token
  83. * @param {Object} openId
  84. */
  85. getToken(openId) {
  86. this.$u.api.codeV2Api
  87. .sendSmsCodeV2api({
  88. openId
  89. })
  90. .then((res) => {
  91. if (res.code === 200) {
  92. this.$u.vuex('vuex_token', res.data.accessToken);
  93. this.$u.vuex('vuex_hasLogin', true);
  94. if (res.data.needVerify) {
  95. localStorage.setItem('backUrl', this.backUrl);
  96. this.$u.route({
  97. url: 'pages/center/phoneLogin/phoneLogin',
  98. type: 'reLaunch'
  99. });
  100. } else {
  101. this.$u.vuex('vuex_user', res.data);
  102. if (this.backUrl.indexOf('wechatLogin') > -1) {
  103. location.href = '/';
  104. } else {
  105. location.href = this.backUrl;
  106. }
  107. }
  108. uni.hideLoading();
  109. } else {
  110. this.$refs.uToast.show({
  111. title: res.msg || '获取用户信息失败!',
  112. type: 'error'
  113. });
  114. uni.hideLoading();
  115. }
  116. });
  117. }
  118. }
  119. };
  120. </script>
  121. <style></style>