import { defineStore } from 'pinia' import { ref } from 'vue' import { reqLogin } from '@/api/auth.js' 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) /** * 登出 */ const logout = () => { token.value = "" userInfo.value = null loginForm.value = { account: '', password: '', Telephone: '', } 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, setCurrentProject } }, { persist: { enabled: true, // 开启持久化 strategies: [{ key: 'user', // 自定义存储键 paths: ['loginForm', 'userInfo', 'token', 'currentProject'], // 仅持久化token字段 }] }, })