redirect.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view>
  3. </view>
  4. </template>
  5. <script>
  6. import {
  7. getUserInfo
  8. } from '@/api/login.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. history.replaceState({
  45. name: '首页'
  46. }, '首页', obj.urlFile + '/#/pages/index/index');
  47. let spread = uni.getStorageSync('spread') || '';
  48. wechatAuth({
  49. code: code,
  50. spread: spread,
  51. }).then(({
  52. data
  53. }) => {
  54. obj.wchatAuth(data);
  55. }).catch((e) => {
  56. uni.showModal({
  57. title: '错误',
  58. content: JSON.stringify(e),
  59. showCancel: false,
  60. });
  61. });;
  62. },
  63. // #endif
  64. // #ifdef MP-WEIXIN
  65. loadMp(option) {
  66. let obj = this;
  67. // 获取登录授权页数据
  68. let user = obj.$api.prePage().userInfo;
  69. // #ifndef MP
  70. // 获取推广人id
  71. let spread_spid = uni.getStorageSync('spread') || '';
  72. // #endif
  73. // #ifdef MP
  74. // 小程序推广人
  75. let spread_code = uni.getStorageSync('spread_code') || '';
  76. // #endif
  77. wechatMpAuth({
  78. code: option.code,
  79. iv: user.target.iv,
  80. encryptedData: user.target.encryptedData,
  81. // #ifndef MP
  82. spread_spid: spread_spid,
  83. // #endif
  84. // #ifdef MP
  85. spread_code: spread_code
  86. // #endif
  87. }).then(({
  88. data
  89. }) => {
  90. obj.wchatAuth(data);
  91. }).catch((e) => {
  92. uni.showModal({
  93. title: '错误',
  94. content: JSON.stringify(e),
  95. showCancel: false,
  96. });
  97. });
  98. },
  99. // #endif
  100. wchatAuth(data) {
  101. let obj = this;
  102. // 保存token
  103. uni.setStorageSync('token', data.token);
  104. // 获取用户基础信息
  105. getUserInfo({}).then(e => {
  106. // uni.showModal({
  107. // title: '登录',
  108. // content: JSON.stringify(e),
  109. // });
  110. obj.login();
  111. // 保存返回用户数据
  112. obj.setUserInfo(e.data);
  113. let ur = uni.getStorageSync('present') || '/pages/index/index';
  114. // 用于处理缓存bug
  115. if (ur == 'pages/product/product') {
  116. ur = '/pages/index/index'
  117. }
  118. if (ur.indexOf('public/wxLogin') > -1) {
  119. uni.switchTab({
  120. url: '/pages/index/index',
  121. fail() {
  122. uni.navigateTo({
  123. url:'/pages/index/index'
  124. })
  125. }
  126. })
  127. return false;
  128. }
  129. uni.switchTab({
  130. url: ur,
  131. fail(e) {
  132. console.log(e);
  133. uni.navigateTo({
  134. url: ur,
  135. fail(e) {
  136. console.log(e);
  137. uni.navigateTo({
  138. url: '/pages/index/index'
  139. });
  140. }
  141. });
  142. }
  143. });
  144. // uni.showModal({
  145. // title: '跳转地址',
  146. // content: JSON.stringify(ur),
  147. // success: res => {
  148. // if (res.confirm) {
  149. // uni.switchTab({
  150. // url: ur,
  151. // fail(e) {
  152. // console.log(e);
  153. // uni.navigateTo({
  154. // url: ur,
  155. // fail(e) {
  156. // console.log(e);
  157. // uni.navigateTo({
  158. // url: '/pages/index/index',
  159. // });
  160. // }
  161. // });
  162. // }
  163. // });
  164. // }
  165. // },
  166. // fail: () => {},
  167. // complete: () => {}
  168. // });
  169. }).catch((e) => {
  170. // uni.showModal({
  171. // title: '错误',
  172. // content: JSON.stringify(e),
  173. // showCancel: false,
  174. // });
  175. uni.navigateTo({
  176. url:'/pages/index/index',
  177. fail() {
  178. uni.switchTab({
  179. url:'/pages/index/index'
  180. })
  181. }
  182. })
  183. });;
  184. }
  185. }
  186. };
  187. </script>
  188. <style></style>