redirect.vue 2.8 KB

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