redirect.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. 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', 'userInfo']),
  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. try {
  36. wechatAuth({
  37. code: code,
  38. spread: spread
  39. })
  40. .then(({ data }) => {
  41. obj.wchatAuth(data);
  42. // 设置成功后修改当前页面
  43. // history.replaceState({ name: '首页' }, '首页', obj.urlFile + '/pages/index/index');
  44. })
  45. .catch(e => {});
  46. } catch (e) {}
  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. })
  62. .then(({ data }) => {
  63. obj.wchatAuth(data);
  64. })
  65. .catch(e => {
  66. uni.showModal({
  67. title: '错误',
  68. content: JSON.stringify(e) + option.code,
  69. showCancel: false
  70. });
  71. });
  72. },
  73. // #endif
  74. wchatAuth(data) {
  75. let obj = this;
  76. // 保存token
  77. uni.setStorageSync('token', data.token);
  78. // 获取用户基础信息
  79. getUserInfo({})
  80. .then(e => {
  81. uni.showModal({
  82. title: '获取用户信息',
  83. content: 'getUserInfo'
  84. });
  85. obj.login();
  86. // 保存返回用户数据
  87. obj.setUserInfo(e.data);
  88. let ur = uni.getStorageSync('present') || '/pages/index/index';
  89. // 用于处理缓存bug
  90. if (ur == 'pages/product/product') {
  91. ur = '/pages/index/index';
  92. }
  93. if (ur.indexOf('public/wxLogin') > -1) {
  94. uni.showModal({
  95. title: '跳转页面',
  96. content: ur
  97. });
  98. uni.switchTab({
  99. url: '/pages/index/index'
  100. });
  101. return false;
  102. }
  103. if (e.data.phone) {
  104. uni.switchTab({
  105. url: ur,
  106. fail(e) {
  107. console.log(e);
  108. uni.navigateTo({
  109. url: ur,
  110. fail(e) {
  111. console.log(e);
  112. uni.switchTab({
  113. url: '/pages/index/index'
  114. });
  115. }
  116. });
  117. }
  118. });
  119. } else {
  120. uni.showModal({
  121. title: '提示',
  122. content: '请先绑定手机号',
  123. showCancel: false,
  124. success: res => {
  125. uni.navigateTo({
  126. url: '/pages/set/phone'
  127. });
  128. },
  129. fail(e) {
  130. console.log(e);
  131. }
  132. });
  133. }
  134. })
  135. .catch(e => {
  136. console.log(e);
  137. });
  138. }
  139. }
  140. };
  141. </script>
  142. <style></style>