init
This commit is contained in:
44
src/store/user.js
Normal file
44
src/store/user.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import { defineStore } from "pinia";
|
||||
import _hook from "@/hooks/index.js";
|
||||
import { login, getUserInfo } from "@/api/user.js";
|
||||
|
||||
export const useUser = defineStore("useUser", {
|
||||
state: () => {
|
||||
return {
|
||||
userInfo: {},
|
||||
token: _hook.useLocalStorage.get("token") || "",
|
||||
};
|
||||
},
|
||||
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;
|
||||
});
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user