redirect.vue 2.9 KB

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