redirect.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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);
  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/index/index');
  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. let obj = this;
  88. // 保存token
  89. uni.setStorageSync('token', data.token);
  90. // 获取用户基础信息
  91. getUserInfo({}).then(e => {
  92. obj.login();
  93. // 保存返回用户数据
  94. obj.setUserInfo(e.data);
  95. let ur = uni.getStorageSync('present')|| '/pages/index/index';
  96. // 用于处理缓存bug
  97. if (ur=='pages/product/product') {
  98. ur = '/pages/index/index'
  99. }
  100. uni.switchTab({
  101. url: ur,
  102. fail(e) {
  103. uni.navigateTo({
  104. url: ur,
  105. fail(e) {
  106. uni.navigateTo({
  107. url: '/pages/index/index',
  108. });
  109. }
  110. });
  111. }
  112. });
  113. }).catch((e) => {
  114. uni.showModal({
  115. title: '错误',
  116. content: JSON.stringify(e),
  117. showCancel: false,
  118. });
  119. });;
  120. }
  121. }
  122. };
  123. </script>
  124. <style></style>