2025-04-29 08:37:17 +00:00
|
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
|
import { ref } from 'vue'
|
2025-05-09 01:42:19 +00:00
|
|
|
|
import { queryDepByOrgCodeApi } from '@/api/system/department'
|
2025-04-29 08:37:17 +00:00
|
|
|
|
|
|
|
|
|
const initState = {
|
2025-05-09 01:42:19 +00:00
|
|
|
|
token: '',
|
|
|
|
|
userid: '',
|
|
|
|
|
username: '',
|
|
|
|
|
realname: '',
|
|
|
|
|
avatar: '',
|
|
|
|
|
tenantId: 0,
|
|
|
|
|
phone: '',
|
|
|
|
|
email: '',
|
|
|
|
|
// sex: 1,
|
|
|
|
|
post: '',
|
|
|
|
|
orgCode: '',
|
|
|
|
|
workNo: '',
|
|
|
|
|
// 本地存储时间
|
|
|
|
|
localStorageTime: 0,
|
2025-04-29 08:37:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useUserStore = defineStore(
|
2025-05-09 01:42:19 +00:00
|
|
|
|
'user',
|
|
|
|
|
() => {
|
|
|
|
|
const userInfo = ref<IUserInfo>({ ...initState })
|
|
|
|
|
const setUserInfo = (val : IUserInfo) => {
|
|
|
|
|
userInfo.value = val
|
|
|
|
|
if (val.orgCode) {
|
|
|
|
|
queryDepByOrgCodeApi(val.orgCode).then((res : any) => {
|
|
|
|
|
userInfo.value.department = res.result; // 假设 API 返回 { result: "部门名称" }
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const clearUserInfo = () => {
|
|
|
|
|
userInfo.value = { ...initState }
|
|
|
|
|
}
|
|
|
|
|
const editUserInfo = (options) => {
|
|
|
|
|
userInfo.value = { ...userInfo.value, ...options }
|
|
|
|
|
}
|
|
|
|
|
// 一般没有reset需求,不需要的可以删除
|
|
|
|
|
const reset = () => {
|
|
|
|
|
userInfo.value = { ...initState }
|
|
|
|
|
}
|
|
|
|
|
const isLogined = computed(() => !!userInfo.value.token)
|
|
|
|
|
return {
|
|
|
|
|
userInfo,
|
|
|
|
|
setUserInfo,
|
|
|
|
|
clearUserInfo,
|
|
|
|
|
isLogined,
|
|
|
|
|
editUserInfo,
|
|
|
|
|
reset,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// 如果需要持久化就写 true, 不需要持久化就写 false(或者去掉这个配置项)
|
|
|
|
|
persist: true,
|
|
|
|
|
},
|
|
|
|
|
)
|