redirect.vue 3.2 KB

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