redirect.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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']),
  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. }).catch((e) => {
  53. uni.showModal({
  54. title: '错误',
  55. content: JSON.stringify(e),
  56. showCancel: false,
  57. });
  58. });;
  59. },
  60. // #endif
  61. // #ifdef MP-WEIXIN
  62. loadMp(option) {
  63. let obj = this;
  64. // 获取登录授权页数据
  65. let user = obj.$api.prePage().userInfo;
  66. // #ifndef MP
  67. // 获取推广人id
  68. let spread_spid = uni.getStorageSync('spread') || '';
  69. // #endif
  70. // #ifdef MP
  71. // 小程序推广人
  72. let spread_code = uni.getStorageSync('spread_code') || '';
  73. // #endif
  74. wechatMpAuth({
  75. code: option.code,
  76. iv: user.target.iv,
  77. encryptedData: user.target.encryptedData,
  78. // #ifndef MP
  79. spread_spid: spread_spid,
  80. // #endif
  81. // #ifdef MP
  82. spread_code: spread_code
  83. // #endif
  84. }).then(({
  85. data
  86. }) => {
  87. obj.wchatAuth(data);
  88. }).catch((e) => {
  89. uni.showModal({
  90. title: '错误',
  91. content: JSON.stringify(e),
  92. showCancel: false,
  93. });
  94. });
  95. },
  96. // #endif
  97. wchatAuth(data) {
  98. let obj = this;
  99. // 保存token
  100. uni.setStorageSync('token', data.token);
  101. // 获取用户基础信息
  102. getUserInfo({}).then(e => {
  103. obj.login();
  104. // 保存返回用户数据
  105. obj.setUserInfo(e.data);
  106. let ur = uni.getStorageSync('present') || '/pages/index/index';
  107. // 用于处理缓存bug
  108. if (ur == 'pages/product/product') {
  109. ur = '/pages/index/index'
  110. }
  111. uni.switchTab({
  112. url: ur,
  113. fail(e) {
  114. uni.navigateTo({
  115. url: ur,
  116. fail(e) {
  117. uni.navigateTo({
  118. url: '/pages/index/index',
  119. });
  120. }
  121. });
  122. }
  123. });
  124. }).catch((e) => {
  125. uni.showModal({
  126. title: '错误',
  127. content: JSON.stringify(e),
  128. showCancel: false,
  129. });
  130. });;
  131. }
  132. }
  133. };
  134. </script>
  135. <style></style>