redirect.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <view></view>
  3. </template>
  4. <script>
  5. import { getUserInfo } from '@/api/user.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. computed: {
  15. ...mapState(['urlFile'])
  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. methods: {
  28. ...mapMutations('user', ['login', 'setUserInfo']),
  29. // #ifdef H5
  30. loadH5() {
  31. let obj = this;
  32. let url = window.location.href;
  33. let code = url.match(/code=([0-9]|[a-z]|[A-Z])*/g)[0].replace('code=', '');
  34. wechatAuth({
  35. code: code
  36. })
  37. .then(({ data }) => {
  38. obj.wchatAuth(data);
  39. })
  40. .catch(e => {
  41. uni.showModal({
  42. title: '错误',
  43. content: JSON.stringify(e),
  44. showCancel: false
  45. });
  46. });
  47. },
  48. // #endif
  49. // #ifdef MP-WEIXIN
  50. loadMp(option) {
  51. let obj = this;
  52. // 获取登录授权页数据
  53. let user = obj.$api.prePage().userInfo;
  54. // #ifndef MP
  55. // 获取推广人id
  56. let spread_spid = uni.getStorageSync('spread') || '';
  57. // #endif
  58. // #ifdef MP
  59. // 小程序推广人
  60. let spread_code = uni.getStorageSync('spread_code') || '';
  61. // #endif
  62. wechatMpAuth({
  63. code: option.code,
  64. iv: user.target.iv,
  65. encryptedData: user.target.encryptedData,
  66. // #ifndef MP
  67. spread_spid: spread_spid,
  68. // #endif
  69. // #ifdef MP
  70. spread_code: spread_code
  71. // #endif
  72. })
  73. .then(({ data }) => {
  74. obj.wchatAuth(data);
  75. })
  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({})
  91. .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. })
  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>