Initial project commit
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { getMyInfo } from "@/api/user";
|
||||
|
||||
export const useUserStore = defineStore("user", {
|
||||
state: () => ({
|
||||
token: uni.getStorageSync("access_token") || "",
|
||||
refreshToken: uni.getStorageSync("refresh_token") || "",
|
||||
userInfo: null,
|
||||
isLoggedIn: !!uni.getStorageSync("access_token"),
|
||||
}),
|
||||
actions: {
|
||||
setTokens(accessToken, refreshToken) {
|
||||
this.token = accessToken;
|
||||
this.refreshToken = refreshToken;
|
||||
this.isLoggedIn = true;
|
||||
uni.setStorageSync("access_token", accessToken);
|
||||
uni.setStorageSync("refresh_token", refreshToken);
|
||||
},
|
||||
async fetchUserInfo() {
|
||||
try {
|
||||
const data = await getMyInfo();
|
||||
this.userInfo = data;
|
||||
} catch (e) {
|
||||
console.error("获取用户信息失败", e);
|
||||
}
|
||||
},
|
||||
logout() {
|
||||
this.token = "";
|
||||
this.refreshToken = "";
|
||||
this.userInfo = null;
|
||||
this.isLoggedIn = false;
|
||||
uni.removeStorageSync("access_token");
|
||||
uni.removeStorageSync("refresh_token");
|
||||
uni.reLaunch({ url: "/pages/login/index" });
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user