feat: userStore 新增用户信息数据存储
This commit is contained in:
parent
d37f6f5cb8
commit
4570f38018
@ -1,117 +1,125 @@
|
|||||||
import { defineStore } from 'pinia'
|
import {defineStore} from 'pinia'
|
||||||
import { ref } from 'vue'
|
import {ref} from 'vue'
|
||||||
import config from '@/config'
|
import config from '@/config'
|
||||||
import storage from '@/utils/storage'
|
import storage from '@/utils/storage'
|
||||||
import constant from '@/utils/constant'
|
import constant from '@/utils/constant'
|
||||||
import { isHttp, isEmpty } from "@/utils/validate"
|
import {isEmpty, isHttp} from "@/utils/validate"
|
||||||
import { getInfo, login, logout } from '@/api/login'
|
import {getInfo, login, logout} from '@/api/login'
|
||||||
import { getToken, removeToken, setToken } from '@/utils/auth'
|
import {getToken, removeToken, setToken} from '@/utils/auth'
|
||||||
import defAva from '@/static/images/profile.jpg'
|
import defAva from '@/static/images/profile.jpg'
|
||||||
|
|
||||||
const baseUrl = config.baseUrl
|
const baseUrl = config.baseUrl
|
||||||
|
|
||||||
export const useUserStore = defineStore('user', () => {
|
export const useUserStore = defineStore('user', () => {
|
||||||
const token = ref(getToken())
|
const token = ref(getToken())
|
||||||
const id = ref(storage.get(constant.id))
|
const id = ref(storage.get(constant.id))
|
||||||
const name = ref(storage.get(constant.name))
|
const name = ref(storage.get(constant.name))
|
||||||
const avatar = ref(storage.get(constant.avatar))
|
const avatar = ref(storage.get(constant.avatar))
|
||||||
const roles = ref(storage.get(constant.roles))
|
const roles = ref(storage.get(constant.roles))
|
||||||
const permissions = ref(storage.get(constant.permissions))
|
const permissions = ref(storage.get(constant.permissions))
|
||||||
|
const userInfo = ref(storage.get(constant.userInfo))
|
||||||
|
|
||||||
const SET_TOKEN = (val) => {
|
const SET_TOKEN = (val) => {
|
||||||
token.value = val
|
token.value = val
|
||||||
}
|
}
|
||||||
const SET_ID = (val) => {
|
const SET_ID = (val) => {
|
||||||
id.value = val
|
id.value = val
|
||||||
storage.set(constant.id, val)
|
storage.set(constant.id, val)
|
||||||
}
|
}
|
||||||
const SET_NAME = (val) => {
|
const SET_NAME = (val) => {
|
||||||
name.value = val
|
name.value = val
|
||||||
storage.set(constant.name, val)
|
storage.set(constant.name, val)
|
||||||
}
|
}
|
||||||
const SET_AVATAR = (val) => {
|
const SET_AVATAR = (val) => {
|
||||||
avatar.value = val
|
avatar.value = val
|
||||||
storage.set(constant.avatar, val)
|
storage.set(constant.avatar, val)
|
||||||
}
|
}
|
||||||
const SET_ROLES = (val) => {
|
const SET_ROLES = (val) => {
|
||||||
roles.value = val
|
roles.value = val
|
||||||
storage.set(constant.roles, val)
|
storage.set(constant.roles, val)
|
||||||
}
|
}
|
||||||
const SET_PERMISSIONS = (val) => {
|
const SET_PERMISSIONS = (val) => {
|
||||||
permissions.value = val
|
permissions.value = val
|
||||||
storage.set(constant.permissions, val)
|
storage.set(constant.permissions, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 登录
|
const SET_USER_INFO = (val) => {
|
||||||
const loginAction = (userInfo) => {
|
userInfo.value = val
|
||||||
const username = userInfo.username.trim()
|
storage.set(constant.userInfo, val)
|
||||||
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.token)
|
|
||||||
SET_TOKEN(res.token)
|
|
||||||
resolve()
|
|
||||||
}).catch(error => {
|
|
||||||
reject(error)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取用户信息
|
// 登录
|
||||||
const getInfoAction = () => {
|
const loginAction = (userInfo) => {
|
||||||
return new Promise((resolve, reject) => {
|
const username = userInfo.username.trim()
|
||||||
getInfo().then(res => {
|
const password = userInfo.password
|
||||||
const user = res.user
|
const code = userInfo.code
|
||||||
let avatar = user.avatar || ""
|
const uuid = userInfo.uuid
|
||||||
if (!isHttp(avatar)) {
|
return new Promise((resolve, reject) => {
|
||||||
avatar = (isEmpty(avatar)) ? defAva : baseUrl + avatar
|
login(username, password, code, uuid).then(res => {
|
||||||
}
|
setToken(res.token)
|
||||||
const userid = (isEmpty(user) || isEmpty(user.userId)) ? "" : user.userId
|
SET_TOKEN(res.token)
|
||||||
const username = (isEmpty(user) || isEmpty(user.userName)) ? "" : user.userName
|
resolve()
|
||||||
if (res.roles && res.roles.length > 0) {
|
}).catch(error => {
|
||||||
SET_ROLES(res.roles)
|
reject(error)
|
||||||
SET_PERMISSIONS(res.permissions)
|
})
|
||||||
} else {
|
})
|
||||||
SET_ROLES(['ROLE_DEFAULT'])
|
}
|
||||||
}
|
|
||||||
SET_ID(userid)
|
|
||||||
SET_NAME(username)
|
|
||||||
SET_AVATAR(avatar)
|
|
||||||
resolve(res)
|
|
||||||
}).catch(error => {
|
|
||||||
reject(error)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 退出系统
|
// 获取用户信息
|
||||||
const logOutAction = () => {
|
const getInfoAction = () => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
logout(token.value).then(() => {
|
getInfo().then(res => {
|
||||||
SET_TOKEN('')
|
const user = res.user
|
||||||
SET_ROLES([])
|
let avatar = user.avatar || ""
|
||||||
SET_PERMISSIONS([])
|
if (!isHttp(avatar)) {
|
||||||
removeToken()
|
avatar = (isEmpty(avatar)) ? defAva : baseUrl + avatar
|
||||||
storage.clean()
|
}
|
||||||
resolve()
|
const userid = (isEmpty(user) || isEmpty(user.userId)) ? "" : user.userId
|
||||||
}).catch(error => {
|
const username = (isEmpty(user) || isEmpty(user.userName)) ? "" : user.userName
|
||||||
reject(error)
|
if (res.roles && res.roles.length > 0) {
|
||||||
})
|
SET_ROLES(res.roles)
|
||||||
})
|
SET_PERMISSIONS(res.permissions)
|
||||||
}
|
} else {
|
||||||
|
SET_ROLES(['ROLE_DEFAULT'])
|
||||||
|
}
|
||||||
|
SET_ID(userid)
|
||||||
|
SET_NAME(username)
|
||||||
|
SET_AVATAR(avatar)
|
||||||
|
SET_USER_INFO(user)
|
||||||
|
resolve(res)
|
||||||
|
}).catch(error => {
|
||||||
|
reject(error)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
// 退出系统
|
||||||
token,
|
const logOutAction = () => {
|
||||||
id,
|
return new Promise((resolve, reject) => {
|
||||||
name,
|
logout(token.value).then(() => {
|
||||||
avatar,
|
SET_TOKEN('')
|
||||||
roles,
|
SET_ROLES([])
|
||||||
permissions,
|
SET_PERMISSIONS([])
|
||||||
SET_AVATAR,
|
removeToken()
|
||||||
login: loginAction,
|
storage.clean()
|
||||||
getInfo: getInfoAction,
|
resolve()
|
||||||
logOut: logOutAction
|
}).catch(error => {
|
||||||
}
|
reject(error)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
token,
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
avatar,
|
||||||
|
roles,
|
||||||
|
permissions,
|
||||||
|
userInfo,
|
||||||
|
SET_AVATAR,
|
||||||
|
login: loginAction,
|
||||||
|
getInfo: getInfoAction,
|
||||||
|
logOut: logOutAction
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
const constant = {
|
const constant = {
|
||||||
avatar: 'user_avatar',
|
avatar: 'user_avatar',
|
||||||
id: 'user_id',
|
id: 'user_id',
|
||||||
name: 'user_name',
|
name: 'user_name',
|
||||||
roles: 'user_roles',
|
roles: 'user_roles',
|
||||||
permissions: 'user_permissions'
|
permissions: 'user_permissions',
|
||||||
}
|
userInfo: 'userInfo'
|
||||||
|
}
|
||||||
|
|
||||||
export default constant
|
export default constant
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user