login.ts 2.33 KB
import { defineStore } from 'pinia'
import router from '@/router';
import { logout } from '@/api/user/login';

interface LoginUser {
  token: string,
  organizationId?: number,
  id?: number,
  userName: string,
  organizationType?: number | null,
  organizationName?: string,
  organizationLogo?: string,
  oId?: number,
  oName?: string,
  oLogo?: string,
  userId?: number,
  oType?: number,
  menuList?: any[]
}

export const useLoginStore = defineStore({
  id: 'login',
  state: () => {
    return {
      token: '',
      organizationId: null,
      id: null,
      userName: '',
      organizationType: null,
      organizationName: '',
      organizationLogo: '',
      menuList: []
    }
  },
  actions: {
    setUserInfo(obj: LoginUser) {
      this.token = obj.token? obj.token: this.token
      this.organizationId = obj.organizationId? obj.organizationId: obj.oId
      this.id = obj.id? obj.id: obj.userId
      this.userName = obj.userName
      this.organizationName = obj.organizationName? obj.organizationName: obj.oName
      this.organizationType = obj.organizationType? obj.organizationType: obj.oType
      this.organizationLogo = obj.organizationLogo? obj.organizationLogo: obj.oLogo
    },
    setMenuList (data: any[]) {
      this.menuList = data
    },
    removeToken() {
      logout()
      this.token = ''
      let loginPath: string = window.localStorage.getItem('isCnlive') === 'true'? '/enter': '/login'
      console.log(loginPath)
      window.localStorage.clear()
      window.localStorage.removeItem('pinia')
      setTimeout(() => {
        router.replace(loginPath)
      }, 500)
    }
  },
  getters: {
    getToken(state) {
      // 记录登录状态的标识
      return state.token
    },
    getOid(state) {
      // 当前登录人的组织id
      return state.organizationId
    },
    getId(state) {
      // 当前登录人的id
      return state.id
    },
    getUserName(state) {
      // 当前登录人的用户名
      return state.userName
    },
    getOtype (state) {
      // 1 物业管理员,2 公益管理员
      return state.organizationType
    },
    getOname (state) {
      // 当前登录的组织名称
      return state.organizationName
    },
    getOlogo (state) {
      // 当前登陆的组织logo
      return state.organizationLogo
    },
    getMenuList (state) {
      return state.menuList
    }
  }
})