2026-06-09 19:29:04 +08:00
|
|
|
import {
|
|
|
|
|
defineStore
|
|
|
|
|
} from 'pinia'
|
|
|
|
|
import {
|
2026-06-10 14:17:54 +08:00
|
|
|
ref,
|
|
|
|
|
watch
|
2026-06-09 19:29:04 +08:00
|
|
|
} from 'vue'
|
|
|
|
|
import {
|
|
|
|
|
reqLogin
|
|
|
|
|
} from '@/api/auth.js'
|
2026-06-10 14:17:54 +08:00
|
|
|
import { useMenuStore, useLargeStore } from '@/store'
|
2026-06-09 19:29:04 +08:00
|
|
|
export const useUserStore = defineStore('user', () => {
|
|
|
|
|
const token = ref("")
|
|
|
|
|
const userInfo = ref(null)
|
|
|
|
|
const loginForm = ref({
|
|
|
|
|
account: 'hfnbd',
|
|
|
|
|
password: '1111',
|
|
|
|
|
Telephone: '',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const projectList = ref([])
|
|
|
|
|
const currentProject = ref(null)
|
|
|
|
|
|
2026-06-10 14:17:54 +08:00
|
|
|
// 监听 currentProject 变化,获取菜单权限
|
|
|
|
|
watch(() => currentProject.value?.ProjectId, (newId) => {
|
|
|
|
|
if (newId) {
|
|
|
|
|
// 项目变化 请求菜单权限
|
|
|
|
|
const menuStore = useMenuStore()
|
|
|
|
|
const userId = userInfo.value?.PersonId
|
|
|
|
|
menuStore.fetchMenuPowerList(userId, newId)
|
|
|
|
|
// 项目变化 请求焊接首页数据
|
|
|
|
|
const largeStore = useLargeStore()
|
|
|
|
|
largeStore.fetchHJIndex(newId)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}, { immediate: true })
|
|
|
|
|
|
2026-06-09 19:29:04 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登出
|
|
|
|
|
*/
|
|
|
|
|
const logout = () => {
|
|
|
|
|
token.value = ""
|
|
|
|
|
userInfo.value = null
|
|
|
|
|
currentProject.value = null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录请求
|
|
|
|
|
*/
|
|
|
|
|
const fetchLogin = async () => {
|
|
|
|
|
try {
|
|
|
|
|
let res = await reqLogin(loginForm.value)
|
|
|
|
|
userInfo.value = res.data
|
|
|
|
|
token.value = res.data?.PersonId
|
|
|
|
|
currentProject.value = {
|
|
|
|
|
ProjectId: res.data?.LoginProjectId,
|
|
|
|
|
ProjectName: res.data?.LoginProjectName
|
|
|
|
|
}
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
uni.reLaunch({
|
|
|
|
|
url: '/pages/index/index'
|
|
|
|
|
})
|
|
|
|
|
}, 900)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户的项目列表
|
|
|
|
|
*/
|
|
|
|
|
const setProjectList = (list) => {
|
|
|
|
|
console.log(list)
|
|
|
|
|
projectList.value = list
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 选择当前的项目
|
|
|
|
|
*/
|
|
|
|
|
const setCurrentProject = (item) => {
|
|
|
|
|
currentProject.value = item ? { ...item } : null
|
|
|
|
|
console.log(11111111, currentProject.value)
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
token,
|
|
|
|
|
userInfo,
|
|
|
|
|
loginForm,
|
|
|
|
|
fetchLogin,
|
|
|
|
|
projectList,
|
|
|
|
|
currentProject,
|
|
|
|
|
setProjectList,
|
2026-06-10 14:17:54 +08:00
|
|
|
setCurrentProject,
|
|
|
|
|
logout
|
2026-06-09 19:29:04 +08:00
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
persist: {
|
|
|
|
|
enabled: true, // 开启持久化
|
|
|
|
|
strategies: [{
|
|
|
|
|
key: 'user', // 自定义存储键
|
|
|
|
|
paths: ['loginForm', 'userInfo', 'token', 'currentProject'], // 仅持久化token字段
|
|
|
|
|
}]
|
|
|
|
|
},
|
|
|
|
|
})
|