redirect.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // if (e.data.phone) {
  101. // uni.switchTab({
  102. // url: ur,
  103. // fail(e) {
  104. // console.log(e);
  105. // uni.navigateTo({
  106. // url: ur,
  107. // fail(e) {
  108. // console.log(e);
  109. // uni.switchTab({
  110. // url: '/pages/index/index'
  111. // });
  112. // }
  113. // });
  114. // }
  115. // });
  116. // } else {
  117. // uni.showModal({
  118. // title: '提示',
  119. // content: '请先绑定手机号',
  120. // showCancel: false,
  121. // success: res => {
  122. // uni.navigateTo({
  123. // url: '/pages/set/phone'
  124. // });
  125. // },
  126. // fail(e) {
  127. // console.log(e);
  128. // }
  129. // });
  130. // }
  131. uni.switchTab({
  132. url: ur,
  133. fail(e) {
  134. uni.navigateTo({
  135. url: ur,
  136. fail(e) {
  137. uni.navigateTo({
  138. url: '/pages/index/index'
  139. });
  140. }
  141. });
  142. }
  143. });
  144. })
  145. .catch(e => {
  146. uni.showModal({
  147. title: '错误',
  148. content: JSON.stringify(e),
  149. showCancel: false
  150. });
  151. });
  152. }
  153. }
  154. };
  155. </script>
  156. <style></style>