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-02-24 23:30:36 +08:00
|
|
|
import { getUserInfo, getUserAsset } from "@/api/auth.js";
|
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 = {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2026-01-30 17:48:48 +08:00
|
|
|
async fetchUserInfo() {
|
|
|
|
|
try {
|
|
|
|
|
const res = await getUserInfo();
|
|
|
|
|
if (res) {
|
|
|
|
|
this.setUserInfo(res);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("fetchUserInfo error", e);
|
|
|
|
|
}
|
|
|
|
|
},
|
2026-02-24 23:30:36 +08:00
|
|
|
async fetchUserAssets() {
|
|
|
|
|
try {
|
|
|
|
|
const res = await getUserAsset();
|
|
|
|
|
if (res) {
|
|
|
|
|
const newInfo = { ...this.userInfo, ...res };
|
|
|
|
|
if (res.points !== undefined) {
|
|
|
|
|
newInfo.points = res.points;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (res.exp !== undefined) {
|
|
|
|
|
newInfo.exp = res.exp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (res.level !== undefined) {
|
|
|
|
|
newInfo.level = res.level;
|
|
|
|
|
}
|
|
|
|
|
this.setUserInfo(newInfo);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("fetchUserAssets error", e);
|
|
|
|
|
}
|
|
|
|
|
},
|
2026-01-16 15:09:19 +08:00
|
|
|
logout() {
|
|
|
|
|
this.userInfo = {};
|
|
|
|
|
this.token = "";
|
|
|
|
|
uni.removeStorageSync("userInfo");
|
|
|
|
|
uni.removeStorageSync("token");
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|