redirect.vue 3.1 KB

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