redirect.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <view>
  3. </view>
  4. </template>
  5. <script>
  6. import { getUserInfo } from '@/api/user.js';
  7. import { mapMutations,mapState } from 'vuex';
  8. // #ifdef H5
  9. import { wechatAuth } from '@/api/wx';
  10. // #endif
  11. // #ifdef MP-WEIXIN
  12. import { wechatMpAuth } from '@/api/wx';
  13. // #endif
  14. export default {
  15. computed: {
  16. ...mapState(['urlFile'])
  17. },
  18. onLoad(option) {
  19. let obj = this;
  20. // 判断是否需要保存定向地址
  21. // #ifdef H5
  22. this.loadH5()
  23. // #endif
  24. // #ifdef MP-WEIXIN
  25. this.loadMp(option)
  26. // #endif
  27. },
  28. methods: {
  29. ...mapMutations('user',['login', 'setUserInfo']),
  30. // #ifdef H5
  31. loadH5() {
  32. let obj = this;
  33. let url = window.location.href;
  34. let code = url.match(/code=([0-9]|[a-z]|[A-Z])*/g)[0].replace('code=', '');
  35. let spread = uni.getStorageSync('spread') || ''
  36. wechatAuth({
  37. code: code,
  38. spread: spread
  39. }).then(({ data }) => {
  40. obj.wchatAuth(data);
  41. }).catch((e) => {
  42. uni.showModal({
  43. title: '错误',
  44. content: JSON.stringify(e),
  45. showCancel: false,
  46. });
  47. });;
  48. },
  49. // #endif
  50. // #ifdef MP-WEIXIN
  51. loadMp(option) {
  52. let obj = this;
  53. // 获取登录授权页数据
  54. let user = obj.$api.prePage().userInfo;
  55. // #ifndef MP
  56. // 获取推广人id
  57. let spread_spid = uni.getStorageSync('spread') || '';
  58. // #endif
  59. // #ifdef MP
  60. // 小程序推广人
  61. let spread_code = uni.getStorageSync('spread_code') || '';
  62. // #endif
  63. wechatMpAuth({
  64. code: option.code,
  65. iv: user.target.iv,
  66. encryptedData: user.target.encryptedData,
  67. // #ifndef MP
  68. spread_spid: spread_spid,
  69. // #endif
  70. // #ifdef MP
  71. spread_code:spread_code
  72. // #endif
  73. }).then(({ data }) => {
  74. obj.wchatAuth(data);
  75. }).catch((e) => {
  76. uni.showModal({
  77. title: '错误',
  78. content: JSON.stringify(e),
  79. showCancel: false,
  80. });
  81. });
  82. },
  83. // #endif
  84. wchatAuth(data) {
  85. let obj = this;
  86. // 保存token
  87. uni.setStorageSync('token', data.token);
  88. // 获取用户基础信息
  89. getUserInfo({}).then(e => {
  90. obj.login();
  91. // 保存返回用户数据
  92. obj.setUserInfo(e.data);
  93. let ur = uni.getStorageSync('present')|| '/pages/index/index';
  94. // 用于处理缓存bug
  95. if (ur=='pages/product/product') {
  96. ur = '/pages/index/index'
  97. }
  98. // if (e.data.phone) {
  99. uni.switchTab({
  100. url: ur,
  101. fail(e) {
  102. console.log(e);
  103. uni.navigateTo({
  104. url: ur,
  105. fail(e) {
  106. console.log(e);
  107. uni.switchTab({
  108. url: '/pages/index/index'
  109. });
  110. }
  111. });
  112. }
  113. });
  114. // } else {
  115. // uni.showModal({
  116. // title: '提示',
  117. // content: '请先绑定手机号',
  118. // showCancel: false,
  119. // success: res => {
  120. // uni.navigateTo({
  121. // url: '/pages/set/phone'
  122. // });
  123. // },
  124. // fail(e) {
  125. // console.log(e);
  126. // }
  127. // });
  128. // }
  129. // uni.switchTab({
  130. // url: ur,
  131. // fail(e) {
  132. // uni.navigateTo({
  133. // url: ur,
  134. // fail(e) {
  135. // uni.navigateTo({
  136. // url: '/pages/index/index',
  137. // });
  138. // }
  139. // });
  140. // }
  141. // });
  142. }).catch((e) => {
  143. uni.showModal({
  144. title: '错误',
  145. content: JSON.stringify(e),
  146. showCancel: false,
  147. });
  148. });;
  149. }
  150. }
  151. };
  152. </script>
  153. <style></style>