mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-15 13:43:48 +08:00
发布v2.15.7版本,更新内容请查看:https://github.com/bufanyun/hotgo/tree/v2.0/docs/guide-zh-CN/addon-version-upgrade.md
This commit is contained in:
181
web/src/store/modules/dict.ts
Normal file
181
web/src/store/modules/dict.ts
Normal file
@@ -0,0 +1,181 @@
|
||||
import { computed, ComputedRef } from 'vue';
|
||||
import { defineStore } from 'pinia';
|
||||
import { createStorage, storage } from '@/utils/Storage';
|
||||
import { store } from '@/store';
|
||||
import { CURRENT_DICT } from '@/store/mutation-types';
|
||||
import { Dicts } from '@/api/dict/dict';
|
||||
import { SelectMixedOption } from 'naive-ui/es/select/src/interface';
|
||||
import { localDict } from '@/enums/localDictEnum';
|
||||
|
||||
const Storage = createStorage({ storage: localStorage });
|
||||
|
||||
// 默认字典类型,通过后台字典管理添加的
|
||||
export type DefaultDictType = 'sys_normal_disable' | 'sys_switch' | 'sys_user_sex';
|
||||
|
||||
// 枚举字典类型
|
||||
export type EnumsDictType = 'creditType' | 'creditGroup' | 'deptType';
|
||||
|
||||
// 方法字典类型
|
||||
export type FuncDictType = 'testCategoryOption';
|
||||
|
||||
// 可以把常用的字典类型加入进来,这样会有IDE提示
|
||||
export type DictType = DefaultDictType | EnumsDictType | FuncDictType | string;
|
||||
|
||||
export interface Option {
|
||||
label: string;
|
||||
value: string | number;
|
||||
key: string | number;
|
||||
listClass: 'default' | 'error' | 'primary' | 'info' | 'success' | 'warning';
|
||||
extra: any;
|
||||
}
|
||||
|
||||
export class DictOptions {
|
||||
[name: DictType]: Option[];
|
||||
}
|
||||
|
||||
export type IDictState = {
|
||||
options: DictOptions;
|
||||
};
|
||||
|
||||
export const useDictStore = defineStore({
|
||||
id: 'app-dict',
|
||||
state: (): IDictState => ({
|
||||
options: Storage.get(CURRENT_DICT, new DictOptions()),
|
||||
}),
|
||||
getters: {},
|
||||
actions: {
|
||||
// 加载字典数据选项
|
||||
loadOptions(types: DictType[]) {
|
||||
Dicts({
|
||||
types: types,
|
||||
}).then((res) => {
|
||||
if (!res) {
|
||||
return;
|
||||
}
|
||||
Object.keys(res).forEach((key) => {
|
||||
if (res[key]) {
|
||||
this.options[key] = res[key] as unknown as Option[];
|
||||
}
|
||||
});
|
||||
storage.set(CURRENT_DICT, this.options);
|
||||
});
|
||||
},
|
||||
|
||||
// 设置指定类型的字典选项
|
||||
setOption(type: DictType, opts: Option[]) {
|
||||
this.options[type] = opts;
|
||||
storage.set(CURRENT_DICT, this.options);
|
||||
},
|
||||
|
||||
// 获取指定类型的字典选项
|
||||
getOption(type: DictType): ComputedRef<SelectMixedOption[]> {
|
||||
return computed(() => {
|
||||
const opts = this.checkOptionValue(type) ?? [];
|
||||
return opts as unknown as SelectMixedOption[];
|
||||
});
|
||||
},
|
||||
|
||||
// 获取指定类型的字典选项
|
||||
getOptionUnRef(type: DictType): any[] {
|
||||
return this.checkOptionValue(type) ?? [];
|
||||
},
|
||||
|
||||
// 是否存在选项,不存在返回null,存在就返回选项
|
||||
checkOptionValue(type: DictType): null | Option[] {
|
||||
// 本地字典
|
||||
const local = localDict[type] ?? [];
|
||||
if (local && local.length > 0) {
|
||||
return local;
|
||||
}
|
||||
const opts = this.options[type] ?? [];
|
||||
if (opts === undefined || opts?.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return opts;
|
||||
},
|
||||
|
||||
// 获取选项名称
|
||||
getLabel(type: DictType, value: any) {
|
||||
const opts = this.checkOptionValue(type);
|
||||
if (!opts) {
|
||||
return ``;
|
||||
}
|
||||
for (const item of opts) {
|
||||
if (item.value.toString() === value.toString()) {
|
||||
return item.label;
|
||||
}
|
||||
}
|
||||
return ``;
|
||||
},
|
||||
|
||||
// 获取选项标签类型
|
||||
getType(
|
||||
type: DictType,
|
||||
value: any
|
||||
): 'default' | 'error' | 'primary' | 'info' | 'success' | 'warning' {
|
||||
const opts = this.checkOptionValue(type);
|
||||
if (!opts) {
|
||||
return `default`;
|
||||
}
|
||||
for (const item of opts) {
|
||||
if (item.value.toString() === value.toString()) {
|
||||
return item.listClass;
|
||||
}
|
||||
}
|
||||
return `default`;
|
||||
},
|
||||
|
||||
// 获取选项额外的数据配置
|
||||
getExtra(type: DictType, value: any): any {
|
||||
const opts = this.checkOptionValue(type);
|
||||
if (!opts) {
|
||||
return null;
|
||||
}
|
||||
for (const item of opts) {
|
||||
if (item.value.toString() === value.toString()) {
|
||||
return item?.extra;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
// 是否存在指定选项值
|
||||
hasValue(type: DictType, value: any): boolean {
|
||||
const opts = this.checkOptionValue(type);
|
||||
if (!opts) {
|
||||
return false;
|
||||
}
|
||||
for (const item of opts) {
|
||||
if (item.value.toString() === value.toString()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
// 获取指定类型的可选项数量
|
||||
getOptionLength(type: DictType): number {
|
||||
const opts = this.checkOptionValue(type);
|
||||
if (!opts) {
|
||||
return 0;
|
||||
}
|
||||
return opts?.length;
|
||||
},
|
||||
|
||||
// 生成单个选项
|
||||
genOption(key: any, label: string, type = 'default', extra: any = null): Option {
|
||||
return <Option>{
|
||||
label: label,
|
||||
value: key,
|
||||
key: key,
|
||||
listClass: type,
|
||||
extra: extra,
|
||||
};
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Need to be used outside the setup
|
||||
export function useDictStoreWidthOut() {
|
||||
return useDictStore(store);
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
// @ts-ignore
|
||||
const allModules = import.meta.globEager('./*/index.ts');
|
||||
const modules = {} as any;
|
||||
Object.keys(allModules).forEach((path) => {
|
||||
@@ -14,10 +15,13 @@ import user from './user';
|
||||
import tabsView from './tabs-view';
|
||||
// @ts-ignore
|
||||
import lockscreen from './lockscreen';
|
||||
// @ts-ignore
|
||||
import dict from './dict';
|
||||
|
||||
export default {
|
||||
asyncRoute,
|
||||
user,
|
||||
tabsView,
|
||||
lockscreen,
|
||||
dict,
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@ import { store } from '@/store';
|
||||
import {
|
||||
ACCESS_TOKEN,
|
||||
CURRENT_CONFIG,
|
||||
CURRENT_DICT,
|
||||
CURRENT_LOGIN_CONFIG,
|
||||
CURRENT_USER,
|
||||
IS_LOCKSCREEN,
|
||||
@@ -256,6 +257,7 @@ export const useUserStore = defineStore({
|
||||
this.setUserInfo(null);
|
||||
storage.remove(ACCESS_TOKEN);
|
||||
storage.remove(CURRENT_USER);
|
||||
storage.remove(CURRENT_DICT);
|
||||
}
|
||||
return Promise.resolve(response);
|
||||
} catch (e) {
|
||||
|
||||
@@ -4,3 +4,4 @@ export const CURRENT_CONFIG = 'CURRENT-CONFIG'; // 当前基础配置
|
||||
export const CURRENT_LOGIN_CONFIG = 'CURRENT-LOGIN-CONFIG'; // 当前登录配置
|
||||
export const IS_LOCKSCREEN = 'IS-LOCKSCREEN'; // 是否锁屏
|
||||
export const TABS_ROUTES = 'TABS-ROUTES'; // 标签页
|
||||
export const CURRENT_DICT = 'CURRENT-DICT'; // 当前用户字典配置
|
||||
|
||||
@@ -3,12 +3,14 @@ import { IUserState } from '@/store/modules/user';
|
||||
import { ILockscreenState } from '@/store/modules/lockscreen';
|
||||
import { ITabsViewState } from '@/store/modules/tabsView';
|
||||
import { INotificationStore } from '@/store/modules/notification';
|
||||
import { IDictState } from '@/store/modules/dict';
|
||||
|
||||
export interface IStore {
|
||||
asyncRoute: IAsyncRouteState;
|
||||
user: IUserState;
|
||||
lockscreen: ILockscreenState;
|
||||
tabsView: ITabsViewState;
|
||||
dict: IDictState;
|
||||
notification: INotificationStore;
|
||||
count: number;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user