redirect.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. let spread = uni.getStorageSync('spread') || '';
  35. console.log(spread,'邀请人ID')
  36. wechatAuth({
  37. code: code,
  38. spread: spread
  39. })
  40. .then(({ data }) => {
  41. obj.wchatAuth(data);
  42. })
  43. .catch(e => {
  44. obj.wchatAuth(data);
  45. uni.showModal({
  46. title: '提示',
  47. content: '您未绑定手机号,请您先去绑定手机号',
  48. showCancel: false,
  49. success: function(res) {
  50. if (res.confirm) {
  51. uni.navigateTo({
  52. url: '/pages/set/phone'
  53. });
  54. }
  55. }
  56. });
  57. });
  58. },
  59. // #endif
  60. // #ifdef MP-WEIXIN
  61. loadMp(option) {
  62. let obj = this;
  63. // 获取登录授权页数据
  64. let user = obj.$api.prePage().userInfo;
  65. // #ifndef MP
  66. // 获取推广人id
  67. let spread_spid = uni.getStorageSync('spread') || '';
  68. // #endif
  69. // #ifdef MP
  70. // 小程序推广人
  71. let spread_code = uni.getStorageSync('spread_code') || '';
  72. // #endif
  73. wechatMpAuth({
  74. code: option.code,
  75. iv: user.target.iv,
  76. encryptedData: user.target.encryptedData,
  77. // #ifndef MP
  78. spread_spid: spread_spid,
  79. // #endif
  80. // #ifdef MP
  81. spread_code: spread_code
  82. // #endif
  83. })
  84. .then(({ data }) => {
  85. obj.wchatAuth(data);
  86. })
  87. .catch(e => {
  88. uni.showModal({
  89. title: '错误',
  90. content: JSON.stringify(e),
  91. showCancel: false
  92. });
  93. });
  94. },
  95. // #endif
  96. wchatAuth(data) {
  97. let obj = this;
  98. // 保存token
  99. uni.setStorageSync('token', data.token);
  100. // 获取用户基础信息
  101. console.log('获取用户信息');
  102. getUserInfo({})
  103. .then(e => {
  104. obj.login();
  105. // 保存返回用户数据
  106. obj.setUserInfo(e.data);
  107. let ur = uni.getStorageSync('present') || '/pages/index/index';
  108. // 用于处理缓存bug
  109. if (ur == 'pages/product/product') {
  110. ur = '/pages/index/index';
  111. }
  112. console.log('跳转');
  113. uni.switchTab({
  114. url: ur,
  115. fail(e) {
  116. uni.navigateTo({
  117. url: ur,
  118. fail(e) {
  119. uni.navigateTo({
  120. url: '/pages/index/index'
  121. });
  122. }
  123. });
  124. }
  125. });
  126. })
  127. .catch(e => {
  128. console.log(e, '123456789');
  129. uni.showModal({
  130. title: '提示',
  131. content: '请先绑定手机号',
  132. showCancel: false,
  133. success: res => {
  134. uni.navigateTo({
  135. url: '/pages/set/phone'
  136. });
  137. },
  138. fail(e) {
  139. console.log(e);
  140. }
  141. });
  142. });
  143. }
  144. }
  145. };
  146. </script>
  147. <style></style>