redirect.vue 3.3 KB

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