user.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { defineStore } from 'pinia'
  2. import { useVisitedRowState } from './visitedRow'
  3. interface UserState {
  4. username: string
  5. isFirstLogin: boolean
  6. }
  7. export const useUserStore = defineStore('user', {
  8. state: (): UserState => ({
  9. username: localStorage.getItem('username') || '',
  10. isFirstLogin: localStorage.getItem('isFirstLogin')
  11. ? JSON.parse(localStorage.getItem('isFirstLogin'))
  12. : false
  13. }),
  14. getters: {},
  15. actions: {
  16. setUsername(username: any, isFirstLogin?: boolean) {
  17. localStorage.setItem('username', username)
  18. this.username = username
  19. if (isFirstLogin !== undefined) {
  20. localStorage.setItem('isFirstLogin', JSON.stringify(isFirstLogin))
  21. this.isFirstLogin = isFirstLogin
  22. }
  23. },
  24. async logout(isNeedLogout: boolean = true) {
  25. if (isNeedLogout) {
  26. await $api.logout().then(() => {})
  27. }
  28. localStorage.removeItem('username')
  29. this.username = ''
  30. if (localStorage.getItem('isFirstLogin')) {
  31. localStorage.removeItem('isFirstLogin')
  32. }
  33. this.isFirstLogin = false
  34. useVisitedRowState().clearVisitedRow()
  35. }
  36. }
  37. })