user.js 4.89 KB
import { login, logout, getInfo, loginByPhone } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import defAva from '@/assets/images/profile.jpg'
import { getMyOwnMemberInfo } from '@/api/system/userInfo.js'
import { defineStore } from 'pinia'
import aes from '@/utils/aes'
import { loginZtx } from '@/apiPc/login.js'
import { getRemindCount } from '@/api/system/user'
import { ElMessageBox } from 'element-plus'

const useUserStore = defineStore(
  'user',
  {
    state: () => ({
      token: getToken(),
      user: null,
      name: '',
      nickName: '',
      avatar: '',
      roles: [],
      permissions: [],
      memberInfo: {},
      authenticationStatus: '', // 是否已认证
      deptType: '',
      showPrice: false,
      perId: '',
      personInfo: '',
      unit: '',
      isExam: '1', // 是否为考点,0:是;1:否,
      genFlag: '', // 是否是自动的协会
      badge: {},
      reLogin: false,
      language: 0
    }),
    actions: {
      // 登录
      login(userInfo) {
        const username = userInfo.username.trim()
        const password = userInfo.password
        const code = userInfo.code
        const uuid = userInfo.uuid
        return new Promise((resolve, reject) => {
          login(username, password, code, uuid).then(res => {
            setToken(res.data.token)
            this.token = res.data.token
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      loginByPhone(userInfo) {
        const phonenumber = userInfo.telNo.trim()
        const code = userInfo.code
        return new Promise((resolve, reject) => {
          loginByPhone(phonenumber, code).then(res => {
            setToken(res.data.token)
            this.token = res.data.token
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      loginPc(userInfo) {
        const username = userInfo.username
        const password = userInfo.password
        const uuid = userInfo.uuid
        const code = userInfo.code
        return new Promise((resolve, reject) => {
          loginZtx(username, password, code, uuid).then(res => {
            setToken(res.data.token)
            this.token = res.data.token
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 获取用户信息
      getInfo() {
        return new Promise((resolve, reject) => {
          getInfo().then(res => {
            const user = res.data.user
            const personInfo = res.data.personInfo
            const avatar = (user.avatar == '' || user.avatar == null) ? defAva : user.avatar
            if (res.data.roles && res.data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
              this.roles = res.data.roles
              this.permissions = res.data.permissions
            } else {
              this.roles = ['ROLE_DEFAULT']
            }
            this.user = user
            this.name = user.userName
            this.nickName = user.nickName
            this.deptType = user.dept.deptType
            this.genFlag = user.dept.genFlag
            this.showPrice = ['1', '2', '3'].indexOf(this.deptType) > -1
            this.dept = user.dept
            this.avatar = avatar
            if (personInfo) {
              this.perId = aes.encrypt(personInfo.perId)
              this.personInfo = personInfo
            }
            resolve(res.data)
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 退出系统
      logOut() {
        return new Promise((resolve, reject) => {
          logout(this.token).then(() => {
            this.token = ''
            this.roles = []
            this.permissions = []
            this.perId = ''
            removeToken()
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 获取道馆信息
      getMemInfo() {
        return getMyOwnMemberInfo().then(res => {
          this.authenticationStatus = res.data.authenticationStatus
          this.memberInfo = res.data.memberInfo
          this.isExam = res.data?.dept?.isExam
        })
      },
      getRemindInfo() {
        getRemindCount().then((res) => {
          this.badge = res.data
        })
      },
      setReLogin() {
        this.reLogin = true
        setTimeout(() => {
          this.reLogin = false
        }, 1000)
      },
      checkAndLogin() {
        if (!this.perId) {
          ElMessageBox.alert('请先登录账号', '系统提示')
            .then(() => {
              this.setReLogin()
            })

          return false
        } else {
          return true
        }
      }
    }
  })

export default useUserStore