redirect.vue 2.9 KB

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