yuang-framework-ui-common 1.0.110 → 1.0.111
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/hooks/uims/uimsUser.ts +34 -30
- package/package.json +1 -1
|
@@ -1,50 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { ref } from 'vue';
|
|
2
|
+
import { http } from '../../../lib/config/httpConfig';
|
|
3
|
+
|
|
3
4
|
export interface StatusEnumItem {
|
|
4
|
-
// 根据实际返回的字段补充类型,比如:
|
|
5
5
|
value: string;
|
|
6
6
|
name: string;
|
|
7
|
-
// 兼容对象中未显式定义的任意字段,避免 TS 报错
|
|
8
7
|
[key: string]: any;
|
|
9
8
|
}
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
// ==============================================
|
|
11
|
+
// 全局唯一单例(跨路由、跨组件、永远不重复)
|
|
12
|
+
// ==============================================
|
|
13
|
+
const GLOBAL_KEY = '__STATUS_ENUM_LIST__';
|
|
14
|
+
|
|
15
|
+
// 如果 window 上没有,才创建(保证全局唯一)
|
|
16
|
+
if (!(window as any)[GLOBAL_KEY]) {
|
|
17
|
+
(window as any)[GLOBAL_KEY] = {
|
|
18
|
+
statusEnumList: ref<StatusEnumItem[]>([]),
|
|
19
|
+
isLoadingEnum: ref(false),
|
|
20
|
+
isLoaded: false,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 永远拿全局同一个实例
|
|
25
|
+
const CACHE = (window as any)[GLOBAL_KEY];
|
|
13
26
|
|
|
14
|
-
/**
|
|
15
|
-
* 提供“获取状态枚举列表”的能力
|
|
16
|
-
*
|
|
17
|
-
* Vue3 官方明确规定,组合式函数必须以 use 开头(参考:Vue3 组合式函数文档),目的是让开发者一眼识别:“这是一个可复用的组合式逻辑,返回响应式能力”
|
|
18
|
-
*/
|
|
19
27
|
export const useStatusEnumList = () => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
* 获取状态枚举列表的核心方法
|
|
27
|
-
*/
|
|
28
|
-
const getStatusEnumList = async (): Promise<void> => {
|
|
28
|
+
const getStatusEnumList = async () => {
|
|
29
|
+
// 已经加载 → 直接返回
|
|
30
|
+
if (CACHE.isLoaded || CACHE.statusEnumList.value.length) return;
|
|
31
|
+
// 正在加载 → 防止并发
|
|
32
|
+
if (CACHE.isLoadingEnum.value) return;
|
|
33
|
+
|
|
29
34
|
try {
|
|
30
|
-
isLoadingEnum.value = true;
|
|
31
|
-
// 指定响应数据类型,TS会自动校验
|
|
35
|
+
CACHE.isLoadingEnum.value = true;
|
|
32
36
|
const res = await http.get('/framework-api/union/uims-user/getStatusEnumList', {});
|
|
33
|
-
statusEnumList.value = res.data.data;
|
|
37
|
+
CACHE.statusEnumList.value = res.data.data || [];
|
|
38
|
+
CACHE.isLoaded = true;
|
|
34
39
|
} catch (ex) {
|
|
35
|
-
console.error('
|
|
36
|
-
statusEnumList.value = [];
|
|
40
|
+
console.error('获取状态枚举失败', ex);
|
|
41
|
+
CACHE.statusEnumList.value = [];
|
|
37
42
|
} finally {
|
|
38
|
-
isLoadingEnum.value = false;
|
|
43
|
+
CACHE.isLoadingEnum.value = false;
|
|
39
44
|
}
|
|
40
45
|
};
|
|
41
46
|
|
|
42
|
-
//
|
|
47
|
+
// 自动加载
|
|
43
48
|
getStatusEnumList();
|
|
44
49
|
|
|
45
|
-
// 返回响应式数据和方法,供组件使用
|
|
46
50
|
return {
|
|
47
|
-
statusEnumList,
|
|
48
|
-
isLoadingEnum
|
|
51
|
+
statusEnumList: CACHE.statusEnumList,
|
|
52
|
+
isLoadingEnum: CACHE.isLoadingEnum,
|
|
49
53
|
};
|
|
50
54
|
};
|