redirect.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. history.replaceState({ name: '首页' }, '首页', obj.urlFile+'/#/pages/index/index');
  36. let spread = uni.getStorageSync('spread')||'';
  37. wechatAuth({
  38. code: code,
  39. spread:spread,
  40. }).then(({ data }) => {
  41. obj.wchatAuth(data);
  42. }).catch((e) => {
  43. uni.showModal({
  44. title: '错误',
  45. content: JSON.stringify(e),
  46. showCancel: false,
  47. });
  48. });;
  49. },
  50. // #endif
  51. // #ifdef MP-WEIXIN
  52. loadMp(option) {
  53. let obj = this;
  54. // 获取登录授权页数据
  55. let user = obj.$api.prePage().userInfo;
  56. // #ifndef MP
  57. // 获取推广人id
  58. let spread_spid = uni.getStorageSync('spread') || '';
  59. // #endif
  60. // #ifdef MP
  61. // 小程序推广人
  62. let spread_code = uni.getStorageSync('spread_code') || '';
  63. // #endif
  64. wechatMpAuth({
  65. code: option.code,
  66. iv: user.target.iv,
  67. encryptedData: user.target.encryptedData,
  68. // #ifndef MP
  69. spread_spid: spread_spid,
  70. // #endif
  71. // #ifdef MP
  72. spread_code:spread_code
  73. // #endif
  74. }).then(({ data }) => {
  75. obj.wchatAuth(data);
  76. }).catch((e) => {
  77. uni.showModal({
  78. title: '错误',
  79. content: JSON.stringify(e),
  80. showCancel: false,
  81. });
  82. });
  83. },
  84. // #endif
  85. wchatAuth(data) {
  86. let obj = this;
  87. // 保存token
  88. uni.setStorageSync('token', data.token);
  89. // 获取用户基础信息
  90. getUserInfo({}).then(e => {
  91. obj.login();
  92. // 保存返回用户数据
  93. obj.setUserInfo(e.data);
  94. let ur = uni.getStorageSync('present')|| '/pages/index/index';
  95. // 用于处理缓存bug
  96. if (ur=='pages/product/product') {
  97. ur = '/pages/index/index'
  98. }
  99. uni.switchTab({
  100. url: ur,
  101. fail(e) {
  102. console.log(e);
  103. uni.navigateTo({
  104. url: ur,
  105. fail(e) {
  106. console.log(e);
  107. uni.navigateTo({
  108. url: '/pages/index/index',
  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>