redirect.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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', 'logout']),
  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. // 修改当前页面
  53. history.replaceState({ name: '首页' }, '首页', obj.urlFile+'/pages/index/index');
  54. }).catch((e) => {
  55. uni.showModal({
  56. title: '错误',
  57. content: JSON.stringify(e),
  58. showCancel: false,
  59. });
  60. });;
  61. },
  62. // #endif
  63. // #ifdef MP-WEIXIN
  64. loadMp(option) {
  65. let obj = this;
  66. // 获取登录授权页数据
  67. let user = obj.$api.prePage().userInfo;
  68. // #ifndef MP
  69. // 获取推广人id
  70. let spread_spid = uni.getStorageSync('spread') || '';
  71. // #endif
  72. // #ifdef MP
  73. // 小程序推广人
  74. let spread_code = uni.getStorageSync('spread_code') || '';
  75. // #endif
  76. wechatMpAuth({
  77. code: option.code,
  78. iv: user.target.iv,
  79. encryptedData: user.target.encryptedData,
  80. // #ifndef MP
  81. spread_spid: spread_spid,
  82. // #endif
  83. // #ifdef MP
  84. spread_code: spread_code
  85. // #endif
  86. }).then(({
  87. data
  88. }) => {
  89. obj.wchatAuth(data);
  90. }).catch((e) => {
  91. uni.showModal({
  92. title: '错误',
  93. content: JSON.stringify(e),
  94. showCancel: false,
  95. });
  96. });
  97. },
  98. // #endif
  99. wchatAuth(data) {
  100. let obj = this;
  101. // 保存token
  102. uni.setStorageSync('token', data.token);
  103. // 获取用户基础信息
  104. getUserInfo({}).then(e => {
  105. obj.login();
  106. // 保存返回用户数据
  107. obj.setUserInfo(e.data);
  108. let ur = uni.getStorageSync('present') || '/pages/index/index';
  109. // 用于处理缓存bug
  110. if (ur == 'pages/product/product') {
  111. ur = '/pages/index/index'
  112. }
  113. if (!e.data.phone) {
  114. uni.showModal({
  115. title: '提示',
  116. content: '请先绑定手机号',
  117. showCancel: false,
  118. success: res => {
  119. if (res.confirm) {
  120. uni.navigateTo({
  121. url: '/pages/set/phone',
  122. });
  123. }
  124. },
  125. });
  126. } else {
  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. }
  141. }).catch((e) => {
  142. uni.showModal({
  143. title: '错误',
  144. content: JSON.stringify(e),
  145. showCancel: false,
  146. });
  147. });;
  148. }
  149. }
  150. };
  151. </script>
  152. <style></style>