redirect.vue 3.5 KB

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