This commit is contained in:
zzc
2026-01-16 15:09:19 +08:00
parent 67684572da
commit 9d5b9b6812
6 changed files with 148 additions and 109 deletions

View File

@@ -1,54 +1,54 @@
import { defineStore } from 'pinia'
import { wxLogin, wxGetUserProfile } from '@/utils/login.js'
import {getPlatformProvider} from '@/utils/system'
import { defineStore } from "pinia";
import { wxLogin, wxGetUserProfile } from "@/utils/login.js";
import { getPlatformProvider } from "@/utils/system";
export const useUserStore = defineStore('user', {
export const useUserStore = defineStore("user", {
state: () => ({
token: '',
userInfo: {}
token: "",
userInfo: {},
}),
actions: {
setToken(token) {
this.token = token
uni.setStorageSync('token', token) // 持久化 tokenx
this.token = token;
uni.setStorageSync("token", token); // 持久化 tokenx
},
setUserInfo(userInfo) {
this.userInfo = userInfo
uni.setStorageSync('userInfo', JSON.stringify(userInfo)) // 持久化用户信息
this.userInfo = userInfo;
uni.setStorageSync("userInfo", JSON.stringify(userInfo)); // 持久化用户信息
},
loadFromStorage() {
const token = uni.getStorageSync('token')
const userInfoStr = uni.getStorageSync('userInfo')
if (token) this.token = token
const token = uni.getStorageSync("token");
const userInfoStr = uni.getStorageSync("userInfo");
if (token) this.token = token;
if (userInfoStr) {
try {
this.userInfo = JSON.parse(userInfoStr)
} catch {
this.userInfo = {}
this.userInfo = JSON.parse(userInfoStr);
} catch (e) {
this.userInfo = {};
}
}
},
clear() {
this.token = ''
this.userInfo = {}
uni.removeStorageSync('token')
uni.removeStorageSync('userInfo')
this.token = "";
this.userInfo = {};
uni.removeStorageSync("token");
uni.removeStorageSync("userInfo");
},
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')
}
}
})
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");
},
},
});