redirect.vue 3.4 KB

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