wechatLogin.vue 3.0 KB

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