auth.global.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. export default defineNuxtRouteMiddleware((to, from) => {
  2. if (import.meta.server) return
  3. const authStore = useAuthStore()
  4. const { token } = storeToRefs(authStore)
  5. const chatStore = useChatStore()
  6. const { user } = storeToRefs(chatStore)
  7. // 建立链接
  8. async function getUserInfo() {
  9. const { data } = await request('/website/tourism/user/view')
  10. chatStore.user = data
  11. user.value = data
  12. console.log(data, 'createConnection')
  13. await chatStore.createConnection(data.pass)
  14. // chatStore.reqChatList()
  15. // console.log('用户信息:', chatStore.user)
  16. // console.log('会话列表:', chatStore.chatList.value)
  17. }
  18. if (token.value) {
  19. getUserInfo()
  20. return
  21. }
  22. if (to.fullPath.includes('/profile')) {
  23. return navigateTo('/login', {
  24. replace: true,
  25. query: {
  26. redirect: to.fullPath
  27. }
  28. })
  29. }
  30. if (to.fullPath.includes('/note-create')) {
  31. return navigateTo('/login', {
  32. replace: true,
  33. query: {
  34. redirect: to.fullPath
  35. }
  36. })
  37. }
  38. return
  39. })