mixin.js 913 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {
  2. mapState
  3. } from 'vuex'
  4. export default {
  5. computed: {
  6. ...mapState({
  7. userInfo: state => state.user.userInfo,
  8. })
  9. },
  10. async onReady() {
  11. const whiteList = ['pages/error/403/403'] // 路由白名单
  12. const pages = getCurrentPages() // 获取栈实例
  13. const page = pages[0] || {}
  14. // set code
  15. const code = page?.options?.code || ''
  16. if (code) {
  17. console.log('code: ', code)
  18. this.$store.commit('user/setCode', code)
  19. // code 重新登录
  20. await this.$store.dispatch('user/login', {
  21. test: 111
  22. })
  23. }
  24. // 白名单 无需登录
  25. if (whiteList.indexOf(page.route) >= 0) {
  26. return
  27. }
  28. setTimeout(function() {
  29. const token = uni.getStorageSync('TOKEN')
  30. // 获取用户信息
  31. token && !this.userInfo.studentName && this.$store.dispatch('user/updateUserInfo')
  32. // 获取token
  33. !token && this.$store.dispatch('user/login', {
  34. test: 222
  35. })
  36. }, 500);
  37. }
  38. }