redirect.vue 3.5 KB

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