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