2026-01-16 15:09:19 +08:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
|
import { wxLogin, wxGetUserProfile } from "@/utils/login.js";
|
|
|
|
|
import { getPlatformProvider } from "@/utils/system";
|
2026-01-09 11:24:40 +08:00
|
|
|
|
2026-01-16 15:09:19 +08:00
|
|
|
export const useUserStore = defineStore("user", {
|
2026-01-09 11:24:40 +08:00
|
|
|
state: () => ({
|
2026-01-16 15:09:19 +08:00
|
|
|
token: "",
|
|
|
|
|
userInfo: {},
|
2026-01-09 11:24:40 +08:00
|
|
|
}),
|
|
|
|
|
actions: {
|
|
|
|
|
setToken(token) {
|
2026-01-16 15:09:19 +08:00
|
|
|
this.token = token;
|
|
|
|
|
uni.setStorageSync("token", token); // 持久化 tokenx
|
2026-01-09 11:24:40 +08:00
|
|
|
},
|
|
|
|
|
setUserInfo(userInfo) {
|
2026-01-16 15:09:19 +08:00
|
|
|
this.userInfo = userInfo;
|
|
|
|
|
uni.setStorageSync("userInfo", JSON.stringify(userInfo)); // 持久化用户信息
|
2026-01-09 11:24:40 +08:00
|
|
|
},
|
|
|
|
|
loadFromStorage() {
|
2026-01-16 15:09:19 +08:00
|
|
|
const token = uni.getStorageSync("token");
|
|
|
|
|
const userInfoStr = uni.getStorageSync("userInfo");
|
|
|
|
|
if (token) this.token = token;
|
2026-01-09 11:24:40 +08:00
|
|
|
if (userInfoStr) {
|
|
|
|
|
try {
|
2026-01-16 15:09:19 +08:00
|
|
|
this.userInfo = JSON.parse(userInfoStr);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.userInfo = {};
|
2026-01-09 11:24:40 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
clear() {
|
2026-01-16 15:09:19 +08:00
|
|
|
this.token = "";
|
|
|
|
|
this.userInfo = {};
|
|
|
|
|
uni.removeStorageSync("token");
|
|
|
|
|
uni.removeStorageSync("userInfo");
|
2026-01-09 11:24:40 +08:00
|
|
|
},
|
2026-01-16 15:09:19 +08:00
|
|
|
loadUserInfo() {
|
|
|
|
|
const userStr = uni.getStorageSync("userInfo");
|
|
|
|
|
if (userStr) {
|
|
|
|
|
try {
|
|
|
|
|
this.userInfo = JSON.parse(userStr);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.userInfo = {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
logout() {
|
|
|
|
|
this.userInfo = {};
|
|
|
|
|
this.token = "";
|
|
|
|
|
uni.removeStorageSync("userInfo");
|
|
|
|
|
uni.removeStorageSync("token");
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|