redirect.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <view>
  3. </view>
  4. </template>
  5. <script>
  6. import { getUserInfo } from '@/api/user.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('user',['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);
  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. }).catch((e) => {
  76. uni.showModal({
  77. title: '错误',
  78. content: JSON.stringify(e),
  79. showCancel: false,
  80. });
  81. });
  82. },
  83. // #endif
  84. wchatAuth(data) {
  85. let obj = this;
  86. // 保存token
  87. uni.setStorageSync('token', data.token);
  88. // 获取用户基础信息
  89. getUserInfo({}).then(e => {
  90. obj.login();
  91. // 保存返回用户数据
  92. obj.setUserInfo(e.data);
  93. let ur = uni.getStorageSync('present')|| '/pages/index/index';
  94. // 用于处理缓存bug
  95. if (ur=='pages/product/product') {
  96. ur = '/pages/index/index'
  97. }
  98. uni.switchTab({
  99. url: ur,
  100. fail(e) {
  101. uni.navigateTo({
  102. url: ur,
  103. fail(e) {
  104. uni.navigateTo({
  105. url: '/pages/index/index',
  106. });
  107. }
  108. });
  109. }
  110. });
  111. }).catch((e) => {
  112. uni.showModal({
  113. title: '错误',
  114. content: JSON.stringify(e),
  115. showCancel: false,
  116. });
  117. });;
  118. }
  119. }
  120. };
  121. </script>
  122. <style></style>