/* * @Author: wjc * @Date: 2024-06-05 17:13:30 * @LastEditors: wjc * @LastEditTime: 2024-07-02 16:42:44 * @Description: */ import { defineStore } from 'pinia' import { UserState, ILogin, LoginRes, Employees } from '@/models/userTypes' import { login, logout, getUserInfo } from '@/api/userApi' export const useUserStore = defineStore('user', { state: (): UserState => { return { isPrivacyShowed: false, isInstall: false, token: '', userInfo: new Employees({}), storageLoginInfo: undefined, storageUserId: '', } }, persist: true, // persist: { // paths: ['isPrivacyShowed', 'isInstall', 'storageUserId', 'storageLoginInfo'], // }, getters: {}, actions: { loginAction(data: ILogin): Promise { return new Promise((resolve, reject) => { login(data) .then((res) => { if (res && res.data) { this.isPrivacyShowed = true this.isInstall = true this.storageLoginInfo = data this.storageUserId = res.data.id resolve(res.data) } }) .catch((error) => { reject(error) }) }) }, logoutAction(): Promise { return new Promise((resolve, reject) => { logout() .then((res) => { if (res && res.data) { this.$reset() resolve(res.data) } }) .catch((error) => { reject(error) }) }) }, getUserInfoAction(): Promise { return new Promise((resolve, reject) => { if (this.storageUserId) { getUserInfo(this.storageUserId) .then((res) => { if (res && res.data) { this.userInfo = res.data resolve(res.data) } }) .catch((error) => { reject(error) }) } }) }, }, })