redirect.vue 3.4 KB

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