64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
import { defineStore } from "pinia";
|
|
import _hook from "@/hooks/index.js";
|
|
import { login, getUserInfo, getNotices } from "@/api/user.js";
|
|
|
|
export const useUser = defineStore("useUser", {
|
|
state: () => {
|
|
return {
|
|
userInfo: {},
|
|
token: _hook.useLocalStorage.get("token") || "",
|
|
notices: {
|
|
num: 0,
|
|
list: []
|
|
}
|
|
};
|
|
},
|
|
getters: {},
|
|
actions: {
|
|
// 设置用户信息
|
|
async setUserInfo() {
|
|
if (_hook.useLocalStorage.get("userInfo")) {
|
|
this.userInfo = _hook.useLocalStorage.get("userInfo");
|
|
} else {
|
|
this.userInfo = await this.getUserInfo();
|
|
}
|
|
},
|
|
/**
|
|
* @description: 用户登录
|
|
* @param {*} param: 登录的参数
|
|
*/
|
|
userlogin(param) {
|
|
return login(param).then((res) => {
|
|
// console.log(res);
|
|
this.userInfo = res;
|
|
_hook.useLocalStorage.set("token", this.userInfo.token);
|
|
_hook.useLocalStorage.set("userInfo", this.userInfo.user);
|
|
return this.userInfo;
|
|
});
|
|
},
|
|
/**
|
|
* @description: 获取用户信息
|
|
*/
|
|
getUserInfo() {
|
|
return getUserInfo().then((res) => {
|
|
return res.userInfo;
|
|
});
|
|
},
|
|
/**
|
|
* 获取消息列表
|
|
*/
|
|
async getNotices() {
|
|
try {
|
|
const res = await getNotices({
|
|
currPage: 1,
|
|
size: 10
|
|
})
|
|
this.notices.num = res.unread
|
|
this.notices.list = res.list.list
|
|
} catch (error) {
|
|
console.error('获取消息列表error=', error)
|
|
}
|
|
}
|
|
},
|
|
});
|