wip: 临时提交
This commit is contained in:
@@ -9,6 +9,5 @@ export function setupStore(app: App<Element>) {
|
||||
}
|
||||
|
||||
export * from "./modules/user";
|
||||
export * from "./modules/dict";
|
||||
export * from "./modules/theme";
|
||||
export { store };
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
import { defineStore } from "pinia";
|
||||
import DictAPI, { type DictItemOption } from "@/api/system/dict";
|
||||
|
||||
const DICT_CACHE_KEY = "dict_cache";
|
||||
|
||||
export const useDictStore = defineStore("dict", () => {
|
||||
// 字典数据缓存
|
||||
const dictCache = ref<Record<string, DictItemOption[]>>(uni.getStorageSync(DICT_CACHE_KEY) || {});
|
||||
|
||||
// 监听dictCache变化,同步到本地存储
|
||||
watch(
|
||||
dictCache,
|
||||
(newVal) => {
|
||||
uni.setStorageSync(DICT_CACHE_KEY, newVal);
|
||||
},
|
||||
{ deep: true }
|
||||
);
|
||||
|
||||
// 请求队列(防止重复请求)
|
||||
const requestQueue: Record<string, Promise<void>> = {};
|
||||
|
||||
/**
|
||||
* 缓存字典数据
|
||||
* @param dictCode 字典编码
|
||||
* @param data 字典项列表
|
||||
*/
|
||||
const cacheDictItems = (dictCode: string, data: DictItemOption[]) => {
|
||||
dictCache.value[dictCode] = data;
|
||||
};
|
||||
|
||||
/**
|
||||
* 加载字典数据(如果缓存中没有则请求)
|
||||
* @param dictCode 字典编码
|
||||
*/
|
||||
const loadDictItems = async (dictCode: string) => {
|
||||
if (dictCache.value[dictCode]) return;
|
||||
|
||||
// 防止重复请求
|
||||
if (!requestQueue[dictCode]) {
|
||||
requestQueue[dictCode] = DictAPI.getDictItems(dictCode).then((data) => {
|
||||
cacheDictItems(dictCode, data);
|
||||
Reflect.deleteProperty(requestQueue, dictCode);
|
||||
});
|
||||
}
|
||||
await requestQueue[dictCode];
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取字典项列表
|
||||
* @param dictCode 字典编码
|
||||
* @returns 字典项列表
|
||||
*/
|
||||
const getDictItems = (dictCode: string): DictItemOption[] => {
|
||||
return dictCache.value[dictCode] || [];
|
||||
};
|
||||
|
||||
/**
|
||||
* 移除指定字典项
|
||||
* @param dictCode 字典编码
|
||||
*/
|
||||
const removeDictItem = (dictCode: string) => {
|
||||
if (dictCache.value[dictCode]) {
|
||||
Reflect.deleteProperty(dictCache.value, dictCode);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 清空字典缓存
|
||||
*/
|
||||
const clearDictCache = () => {
|
||||
dictCache.value = {};
|
||||
};
|
||||
|
||||
return {
|
||||
loadDictItems,
|
||||
getDictItems,
|
||||
removeDictItem,
|
||||
clearDictCache,
|
||||
};
|
||||
});
|
||||
@@ -1,7 +1,6 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { ref } from "vue";
|
||||
import { applyThemeToMiniProgram } from "@/utils/theme";
|
||||
import { getLighterColor, getDarkerColor } from "@/utils/colorUtils";
|
||||
|
||||
// 从缓存获取主题色
|
||||
const getThemeColor = (): string => {
|
||||
@@ -28,11 +27,9 @@ export const useThemeStore = defineStore("theme", () => {
|
||||
// H5环境
|
||||
document.documentElement.style.setProperty("--primary-color", color);
|
||||
|
||||
// 计算衍生色
|
||||
const lighterColor = getLighterColor(color, 0.8);
|
||||
const darkerColor = getDarkerColor(color, 0.8);
|
||||
document.documentElement.style.setProperty("--primary-color-light", lighterColor);
|
||||
document.documentElement.style.setProperty("--primary-color-dark", darkerColor);
|
||||
// 设置简单的衍生色(不依赖外部工具函数)
|
||||
document.documentElement.style.setProperty("--primary-color-light", color + "80"); // 添加透明度
|
||||
document.documentElement.style.setProperty("--primary-color-dark", color);
|
||||
} else {
|
||||
// 小程序环境
|
||||
applyThemeToMiniProgram(color);
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { defineStore } from "pinia";
|
||||
import AuthAPI, { type LoginFormData } from "@/api/auth";
|
||||
import UserAPI, { type UserInfo } from "@/api/system/user";
|
||||
import { setToken, getUserInfo, setUserInfo, clearAll } from "@/utils/cache";
|
||||
import UserAPI, { type UserInfo } from "@/api/user";
|
||||
import { setAccessToken, clearTokens } from "@/utils/auth";
|
||||
import { getUserInfo, setUserInfo } from "@/utils/storage";
|
||||
import { USER_INFO_KEY } from "@/constants";
|
||||
import { Storage } from "@/utils/storage";
|
||||
|
||||
export const useUserStore = defineStore("user", () => {
|
||||
const userInfo = ref<UserInfo | undefined>(getUserInfo());
|
||||
@@ -11,7 +14,7 @@ export const useUserStore = defineStore("user", () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
AuthAPI.login(data)
|
||||
.then((data) => {
|
||||
setToken(data.accessToken);
|
||||
setAccessToken(data.accessToken);
|
||||
resolve(data);
|
||||
})
|
||||
.catch((error) => {
|
||||
@@ -26,7 +29,7 @@ export const useUserStore = defineStore("user", () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
AuthAPI.wechatLogin(code)
|
||||
.then((data) => {
|
||||
setToken(data.accessToken);
|
||||
setAccessToken(data.accessToken);
|
||||
resolve(data);
|
||||
})
|
||||
.catch((error) => {
|
||||
@@ -36,6 +39,21 @@ export const useUserStore = defineStore("user", () => {
|
||||
});
|
||||
};
|
||||
|
||||
// 微信小程序增强登录
|
||||
const loginByWechatMini = (data: any): Promise<any> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
AuthAPI.wechatMiniLogin(data)
|
||||
.then((result) => {
|
||||
setAccessToken(result.accessToken);
|
||||
resolve(result);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("微信小程序登录失败", error);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// 获取用户信息
|
||||
const getInfo = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -59,7 +77,8 @@ export const useUserStore = defineStore("user", () => {
|
||||
} catch (error) {
|
||||
console.error("登出失败", error);
|
||||
} finally {
|
||||
clearAll(); // 清除本地的 token 和用户信息缓存
|
||||
clearTokens(); // 清除本地的 token
|
||||
Storage.remove(USER_INFO_KEY); // 清除用户信息缓存
|
||||
userInfo.value = undefined; // 清空用户信息
|
||||
}
|
||||
};
|
||||
@@ -75,6 +94,7 @@ export const useUserStore = defineStore("user", () => {
|
||||
userInfo,
|
||||
login,
|
||||
loginByWechat,
|
||||
loginByWechatMini,
|
||||
logout,
|
||||
getInfo,
|
||||
isUserInfoComplete,
|
||||
|
||||
Reference in New Issue
Block a user