redirect.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <view>
  3. </view>
  4. </template>
  5. <script>
  6. import { getUserInfo } from '@/api/login.js';
  7. import { mapMutations,mapState } from 'vuex';
  8. // #ifdef H5
  9. import { wechatAuth } from '@/api/wx';
  10. // #endif
  11. // #ifdef MP-WEIXIN
  12. import { wechatMpAuth } from '@/api/wx';
  13. // #endif
  14. export default {
  15. computed: {
  16. ...mapState(['urlFile'])
  17. },
  18. onLoad(option) {
  19. let obj = this;
  20. // 判断是否需要保存定向地址
  21. // #ifdef H5
  22. this.loadH5()
  23. // #endif
  24. // #ifdef MP-WEIXIN
  25. this.loadMp(option)
  26. // #endif
  27. },
  28. methods: {
  29. ...mapMutations('user',['login', 'setUserInfo']),
  30. // #ifdef H5
  31. loadH5() {
  32. let obj = this;
  33. let url = window.location.href;
  34. let code = url.match(/code=([0-9]|[a-z]|[A-Z])*/g)[0].replace('code=', '');
  35. let spread = uni.getStorageSync('spread')||'';
  36. wechatAuth({
  37. code: code,
  38. spread:spread,
  39. }).then(({ data }) => {
  40. obj.wchatAuth(data.userinfo);
  41. }).catch((e) => {
  42. uni.showModal({
  43. title: '错误',
  44. content: JSON.stringify(e),
  45. showCancel: false,
  46. });
  47. });;
  48. },
  49. // #endif
  50. // #ifdef MP-WEIXIN
  51. loadMp(option) {
  52. let obj = this;
  53. // 获取登录授权页数据
  54. let user = obj.$api.prePage().userInfo;
  55. // #ifndef MP
  56. // 获取推广人id
  57. let spread_spid = uni.getStorageSync('spread') || '';
  58. // #endif
  59. // #ifdef MP
  60. // 小程序推广人
  61. let spread_code = uni.getStorageSync('spread_code') || '';
  62. // #endif
  63. wechatMpAuth({
  64. code: option.code,
  65. iv: user.target.iv,
  66. encryptedData: user.target.encryptedData,
  67. // #ifndef MP
  68. spread_spid: spread_spid,
  69. // #endif
  70. // #ifdef MP
  71. spread_code:spread_code
  72. // #endif
  73. }).then(({ data }) => {
  74. obj.wchatAuth(data);
  75. // 设置成功后修改当前页面
  76. history.replaceState({ name: '我的' }, '我的', obj.urlFile+'/#/pages/user/user');
  77. }).catch((e) => {
  78. uni.showModal({
  79. title: '错误',
  80. content: JSON.stringify(e),
  81. showCancel: false,
  82. });
  83. });
  84. },
  85. // #endif
  86. wchatAuth(data) {
  87. console.log(data)
  88. let obj = this;
  89. // 保存token
  90. uni.setStorageSync('token', data.token);
  91. // 获取用户基础信息
  92. getUserInfo({}).then(e => {
  93. obj.login();
  94. // 保存返回用户数据
  95. obj.setUserInfo(e.data.user);
  96. let ur = uni.getStorageSync('present')|| '/pages/user/user';
  97. // 用于处理缓存bug
  98. if (ur=='pages/product/product') {
  99. ur = '/pages/user/user'
  100. }
  101. uni.switchTab({
  102. url: ur,
  103. fail(e) {
  104. uni.navigateTo({
  105. url: ur,
  106. fail(e) {
  107. uni.navigateTo({
  108. url: '/pages/user/user',
  109. });
  110. }
  111. });
  112. }
  113. });
  114. }).catch((e) => {
  115. uni.showModal({
  116. title: '错误',
  117. content: JSON.stringify(e),
  118. showCancel: false,
  119. });
  120. });;
  121. }
  122. }
  123. };
  124. </script>
  125. <style></style>