redirect.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <view>
  3. </view>
  4. </template>
  5. <script>
  6. import { getUserInfo } from '@/api/login.js';
  7. import { mapMutations,mapState } from 'vuex';
  8. import { wechatAuth } from '@/api/wx';
  9. export default {
  10. computed: {
  11. ...mapState(['urlFile'])
  12. },
  13. onLoad(option) {
  14. let obj = this;
  15. // 判断是否需要保存定向地址
  16. this.loadH5()
  17. },
  18. methods: {
  19. ...mapMutations('user',['login', 'setUserInfo']),
  20. loadH5() {
  21. let obj = this;
  22. let url = window.location.href;
  23. let code = url.match(/code=([0-9]|[a-z]|[A-Z])*/g)[0].replace('code=', '');
  24. let spread = uni.getStorageSync('spread')||'';
  25. wechatAuth({
  26. code: code,
  27. spread:spread,
  28. }).then(({ data }) => {
  29. obj.wchatAuth(data);
  30. }).catch((e) => {
  31. uni.showModal({
  32. title: '错误',
  33. content: JSON.stringify(e),
  34. showCancel: false,
  35. });
  36. });;
  37. },
  38. wchatAuth(data) {
  39. let obj = this;
  40. // 保存token
  41. uni.setStorageSync('token', data.token);
  42. // 获取用户基础信息
  43. getUserInfo({}).then(e => {
  44. obj.login();
  45. // 保存返回用户数据
  46. obj.setUserInfo(e.data);
  47. let ur = uni.getStorageSync('present')|| '/pages/index/index';
  48. // 用于处理缓存bug
  49. if (ur=='pages/product/product') {
  50. ur = '/pages/index/index'
  51. }
  52. uni.switchTab({
  53. url: ur,
  54. fail(e) {
  55. uni.navigateTo({
  56. url: ur,
  57. fail(e) {
  58. uni.navigateTo({
  59. url: '/pages/index/index',
  60. });
  61. }
  62. });
  63. }
  64. });
  65. }).catch((e) => {
  66. uni.showModal({
  67. title: '错误',
  68. content: JSON.stringify(e),
  69. showCancel: false,
  70. });
  71. });;
  72. }
  73. }
  74. };
  75. </script>
  76. <style></style>