redirect.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. obj.login();
  104. // 保存返回用户数据
  105. obj.setUserInfo(e.data);
  106. let ur = uni.getStorageSync('present') || '/pages/index/index';
  107. // 用于处理缓存bug
  108. if (ur == 'pages/product/product') {
  109. ur = '/pages/index/index'
  110. }
  111. if (e.data.wx_qr) {
  112. uni.switchTab({
  113. url: ur,
  114. fail(e) {
  115. uni.navigateTo({
  116. url: ur,
  117. fail(e) {
  118. uni.navigateTo({
  119. url: '/pages/index/index',
  120. });
  121. }
  122. });
  123. }
  124. });
  125. } else {
  126. obj.$api.msg('请完善用户信息');
  127. setTimeout(() => {
  128. uni.navigateTo({
  129. url: '/pages/set/userinfo'
  130. });
  131. }, 1000);
  132. }
  133. // uni.switchTab({
  134. // url: ur,
  135. // fail(e) {
  136. // uni.navigateTo({
  137. // url: ur,
  138. // fail(e) {
  139. // uni.navigateTo({
  140. // url: '/pages/index/index',
  141. // });
  142. // }
  143. // });
  144. // }
  145. // });
  146. }).catch((e) => {
  147. uni.showModal({
  148. title: '错误',
  149. content: JSON.stringify(e),
  150. showCancel: false,
  151. });
  152. });;
  153. }
  154. }
  155. };
  156. </script>
  157. <style></style>