login.ts 2.05 KB
import router from '@/router';
import { logout } from '@/api/user/login';
import { ElMessage } from "element-plus";
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[],
}
const id: string = 'login'
const state: Function = () => ({
  token: '',
  organizationId: null,
  id: null,
  userName: '',
  organizationType: null,
  organizationName: '',
  organizationLogo: '',
  menuList: []
})
const getters = {
  getToken() {
    // 记录登录状态的标识
    return this.token
  },
  getOid() {
    // 当前登录人的组织id
    return this.organizationId
  },
  getId() {
    // 当前登录人的id
    return this.id
  },
  getUserName() {
    // 当前登录人的用户名
    return this.userName
  },
  getOtype () {
    // 1 物业管理员,2 公益管理员
    return this.organizationType
  },
  getOname () {
    // 当前登录的组织名称
    return this.organizationName
  },
  getOlogo () {
    // 当前登陆的组织logo
    return this.organizationLogo
  },
  getMenuList () {
    return this.menuList
  }
}
const 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(msg: string | null) {
    logout()
    this.token = ''
    window.localStorage.clear()
    setTimeout(() => {
      router.replace('/login')
    }, 500)
  }
}

export default {
  namespace: true,
  actions,
  getters,
  state,
  id
}