redirect.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <view></view>
  3. </template>
  4. <script>
  5. import { getUserInfo } from '@/api/login.js';
  6. import { mapMutations,mapState } from 'vuex';
  7. // #ifdef H5
  8. import { wechatAuth } from '@/api/wx';
  9. // #endif
  10. // #ifdef MP-WEIXIN
  11. import { wechatMpAuth } from '@/api/wx';
  12. // #endif
  13. export default {
  14. data() {
  15. return {};
  16. },
  17. onLoad(option) {
  18. let obj = this;
  19. // 判断是否需要保存定向地址
  20. // #ifdef H5
  21. this.loadH5();
  22. // #endif
  23. // #ifdef MP-WEIXIN
  24. this.loadMp(option);
  25. // #endif
  26. },
  27. computed: {
  28. ...mapState(['urlFile'])
  29. },
  30. methods: {
  31. ...mapMutations(['login', 'setUserInfo']),
  32. // #ifdef H5
  33. loadH5() {
  34. let obj = this;
  35. let url = window.location.href;
  36. let code = url.match(/code=([0-9]|[a-z]|[A-Z])*/g)[0].replace('code=', '');
  37. history.replaceState({ name: '首页' }, '首页', obj.urlFile+'/pages/index/index');
  38. let spread = uni.getStorageSync('spread') || '';
  39. let spreadId = uni.getStorageSync('spread_uid') || '';
  40. wechatAuth({
  41. code: code,
  42. spread: spread,
  43. spread_uid: spreadId,
  44. }).then(({ data }) => {
  45. obj.wchatAuth(data);
  46. });
  47. },
  48. // #endif
  49. // #ifdef MP-WEIXIN
  50. loadMp(option) {
  51. let obj = this;
  52. // 获取登录授权页数据
  53. let user = obj.$api.prePage().userInfo;
  54. // 获取推广人id
  55. let spread_spid = uni.getStorageSync('spread') || '';
  56. wechatMpAuth({
  57. code: option.code,
  58. iv: user.target.iv,
  59. encryptedData: user.target.encryptedData,
  60. spread_spid: spread_spid
  61. }).then(({ data }) => {
  62. obj.wchatAuth(data);
  63. });
  64. },
  65. // #endif
  66. wchatAuth(data) {
  67. let obj = this;
  68. // 保存token
  69. uni.setStorageSync('token', data.token);
  70. // 获取用户基础信息
  71. getUserInfo({}).then(e => {
  72. obj.login();
  73. // 保存返回用户数据
  74. obj.setUserInfo(e.data);
  75. let ur = uni.getStorageSync('present') || '/pages/index/index';
  76. // 用于处理缓存bug
  77. if (ur=='pages/product/product') {
  78. ur = '/pages/index/index'
  79. }
  80. uni.switchTab({
  81. url: ur,
  82. fail(e) {
  83. uni.navigateTo({
  84. url: ur,
  85. fail(e) {
  86. uni.navigateTo({
  87. url: '/pages/index/index',
  88. });
  89. }
  90. });
  91. }
  92. });
  93. });
  94. }
  95. }
  96. };
  97. </script>
  98. <style></style>