@fairys/taro-tools-react 0.0.1
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/README.md +1 -0
- package/esm/components/EnterLoading/index.d.ts +14 -0
- package/esm/components/EnterLoading/index.js +65 -0
- package/esm/components/MainPage/index.d.ts +10 -0
- package/esm/components/MainPage/index.js +49 -0
- package/esm/components/Mesage/index.d.ts +48 -0
- package/esm/components/Mesage/index.js +122 -0
- package/esm/components/Portal/index.d.ts +8 -0
- package/esm/components/Portal/index.js +11 -0
- package/esm/components/Toast/index.d.ts +1 -0
- package/esm/components/Toast/index.js +20 -0
- package/esm/components/connectToastMessage/index.d.ts +10 -0
- package/esm/components/connectToastMessage/index.js +28 -0
- package/esm/components/index.d.ts +6 -0
- package/esm/components/index.js +6 -0
- package/esm/context/global.data.instance.d.ts +5411 -0
- package/esm/context/global.data.instance.js +66 -0
- package/esm/context/global.setting.data.instance.d.ts +28 -0
- package/esm/context/global.setting.data.instance.js +20 -0
- package/esm/context/index.d.ts +4 -0
- package/esm/context/index.js +4 -0
- package/esm/context/page.data.instance.d.ts +71 -0
- package/esm/context/page.data.instance.js +139 -0
- package/esm/context/page.info.data.instance.d.ts +72 -0
- package/esm/context/page.info.data.instance.js +107 -0
- package/esm/index.d.ts +3 -0
- package/esm/index.js +3 -0
- package/esm/styles/index.css +711 -0
- package/esm/utils/index.d.ts +4 -0
- package/esm/utils/index.js +4 -0
- package/esm/utils/navigate.d.ts +109 -0
- package/esm/utils/navigate.js +45 -0
- package/esm/utils/request.d.ts +112 -0
- package/esm/utils/request.js +212 -0
- package/esm/utils/useId.d.ts +2 -0
- package/esm/utils/useId.js +13 -0
- package/esm/utils/valtio/index.d.ts +9 -0
- package/esm/utils/valtio/index.js +23 -0
- package/esm/utils/valtio/instance.d.ts +15 -0
- package/esm/utils/valtio/instance.js +34 -0
- package/lib/index.js +78 -0
- package/package.json +40 -0
- package/src/components/EnterLoading/index.tsx +57 -0
- package/src/components/MainPage/index.tsx +72 -0
- package/src/components/Mesage/index.tsx +217 -0
- package/src/components/Portal/index.tsx +18 -0
- package/src/components/Toast/index.tsx +22 -0
- package/src/components/connectToastMessage/index.tsx +41 -0
- package/src/components/index.ts +6 -0
- package/src/context/global.data.instance.ts +106 -0
- package/src/context/global.setting.data.instance.ts +36 -0
- package/src/context/index.ts +4 -0
- package/src/context/page.data.instance.ts +215 -0
- package/src/context/page.info.data.instance.ts +182 -0
- package/src/index.ts +3 -0
- package/src/styles/index.css +515 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/navigate.ts +159 -0
- package/src/utils/request.ts +319 -0
- package/src/utils/useId.ts +14 -0
- package/src/utils/valtio/index.ts +23 -0
- package/src/utils/valtio/instance.ts +59 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { proxy, ref, useSnapshot } from "valtio";
|
|
2
|
+
import utils_navigate from "../utils/navigate.js";
|
|
3
|
+
import { createUseId } from "../utils/useId.js";
|
|
4
|
+
import { ProxyInstanceObjectBase } from "../utils/valtio/instance.js";
|
|
5
|
+
class GlobalDataInstance extends ProxyInstanceObjectBase {
|
|
6
|
+
loginPageRoute = 'pages/login/index';
|
|
7
|
+
store = proxy({
|
|
8
|
+
messageData: ref([]),
|
|
9
|
+
toastData: void 0
|
|
10
|
+
});
|
|
11
|
+
showMessage = (options, timeout = 3000)=>{
|
|
12
|
+
const _that = this;
|
|
13
|
+
let newItem = {
|
|
14
|
+
...options
|
|
15
|
+
};
|
|
16
|
+
if (!_that.store.messageData) _that.store.messageData = ref([]);
|
|
17
|
+
newItem.visible = true;
|
|
18
|
+
if (!newItem.__id) newItem.__id = `${new Date().valueOf()}__${_that.store.messageData.length + 1}__` + createUseId('message');
|
|
19
|
+
_that.store.messageData = ref([
|
|
20
|
+
..._that.store.messageData
|
|
21
|
+
].concat([
|
|
22
|
+
newItem
|
|
23
|
+
]));
|
|
24
|
+
if (timeout) {
|
|
25
|
+
const timer = setTimeout(()=>{
|
|
26
|
+
newItem.visible = false;
|
|
27
|
+
_that.store.messageData = ref((_that.store.messageData || []).filter((it)=>it.__id !== newItem.__id));
|
|
28
|
+
clearTimeout(timer);
|
|
29
|
+
}, timeout);
|
|
30
|
+
}
|
|
31
|
+
return newItem.__id;
|
|
32
|
+
};
|
|
33
|
+
hideMessage = (id)=>{
|
|
34
|
+
this.store.messageData = ref((this.store.messageData || []).filter((it)=>it.__id !== id));
|
|
35
|
+
};
|
|
36
|
+
showToast = (config = {})=>{
|
|
37
|
+
this.store.toastData = ref({
|
|
38
|
+
visible: true,
|
|
39
|
+
...config
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
hideToast = ()=>{
|
|
43
|
+
this.store.toastData = ref({
|
|
44
|
+
...this.store.toastData,
|
|
45
|
+
visible: false
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
toLoginPage = ()=>{
|
|
49
|
+
const isLoginPage = utils_navigate.isCurrentPage(this.loginPageRoute || '');
|
|
50
|
+
const _loginPageRoute = `${this.loginPageRoute || ''}`.replace(/^\//, '');
|
|
51
|
+
if (isLoginPage) return;
|
|
52
|
+
utils_navigate.navigateTo({
|
|
53
|
+
url: `/${_loginPageRoute}`
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const globalDataInstance = new GlobalDataInstance();
|
|
58
|
+
const useGlobalData = ()=>{
|
|
59
|
+
const store = useSnapshot(globalDataInstance.store);
|
|
60
|
+
return [
|
|
61
|
+
store,
|
|
62
|
+
globalDataInstance,
|
|
63
|
+
store.__defaultValue
|
|
64
|
+
];
|
|
65
|
+
};
|
|
66
|
+
export { GlobalDataInstance, globalDataInstance, useGlobalData };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ProxyInstanceObjectBase } from '../utils/valtio/instance';
|
|
2
|
+
export interface GlobalSettingDataInstanceState {
|
|
3
|
+
/**
|
|
4
|
+
* 请求成功返回code
|
|
5
|
+
* @default 200
|
|
6
|
+
*/
|
|
7
|
+
requestSuccessCode?: number;
|
|
8
|
+
/**数据默认值不使用*/
|
|
9
|
+
__defaultValue?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare class GlobalSettingDataInstance extends ProxyInstanceObjectBase<GlobalSettingDataInstanceState> {
|
|
12
|
+
store: GlobalSettingDataInstanceState;
|
|
13
|
+
/**
|
|
14
|
+
* 扩展全局设置数据状态
|
|
15
|
+
*/
|
|
16
|
+
extendStore: (state: Partial<GlobalSettingDataInstanceState>) => void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 全局设置数据实例
|
|
20
|
+
*/
|
|
21
|
+
export declare const globalSettingDataInstance: GlobalSettingDataInstance;
|
|
22
|
+
/**
|
|
23
|
+
* 全局设置数据状态管理
|
|
24
|
+
*/
|
|
25
|
+
export declare const useGlobalSettingData: () => readonly [{
|
|
26
|
+
readonly requestSuccessCode?: number;
|
|
27
|
+
readonly __defaultValue?: string;
|
|
28
|
+
}, GlobalSettingDataInstance, string];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { proxy, useSnapshot } from "valtio";
|
|
2
|
+
import { ProxyInstanceObjectBase } from "../utils/valtio/instance.js";
|
|
3
|
+
class GlobalSettingDataInstance extends ProxyInstanceObjectBase {
|
|
4
|
+
store = proxy({
|
|
5
|
+
requestSuccessCode: 200
|
|
6
|
+
});
|
|
7
|
+
extendStore = (state)=>{
|
|
8
|
+
this._setValues(state);
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
const globalSettingDataInstance = new GlobalSettingDataInstance();
|
|
12
|
+
const useGlobalSettingData = ()=>{
|
|
13
|
+
const store = useSnapshot(globalSettingDataInstance.store);
|
|
14
|
+
return [
|
|
15
|
+
store,
|
|
16
|
+
globalSettingDataInstance,
|
|
17
|
+
store.__defaultValue
|
|
18
|
+
];
|
|
19
|
+
};
|
|
20
|
+
export { GlobalSettingDataInstance, globalSettingDataInstance, useGlobalSettingData };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ProxyInstanceObjectBase } from '../utils/valtio/instance';
|
|
2
|
+
export interface PageDataInstanceState extends Object {
|
|
3
|
+
/**loading存储*/
|
|
4
|
+
loading?: Record<string, boolean>;
|
|
5
|
+
/**当前页*/
|
|
6
|
+
page?: number;
|
|
7
|
+
/**分页数*/
|
|
8
|
+
pageSize?: number;
|
|
9
|
+
/**总数*/
|
|
10
|
+
total?: number;
|
|
11
|
+
/**是否最后一页*/
|
|
12
|
+
hasLastPage?: boolean;
|
|
13
|
+
/**查询条件*/
|
|
14
|
+
search?: Object;
|
|
15
|
+
/**表格数据*/
|
|
16
|
+
dataList?: any[];
|
|
17
|
+
/**选择行数据*/
|
|
18
|
+
selectedRows?: any[];
|
|
19
|
+
selectedRowKeys?: any[];
|
|
20
|
+
/**数据默认值不使用*/
|
|
21
|
+
__defaultValue?: string;
|
|
22
|
+
[s: string]: any;
|
|
23
|
+
}
|
|
24
|
+
export declare class PageDataInstance<T extends PageDataInstanceState = PageDataInstanceState> extends ProxyInstanceObjectBase<T> {
|
|
25
|
+
/**是否滚动加载分页*/
|
|
26
|
+
is_scroll_page: boolean;
|
|
27
|
+
/**默认值*/
|
|
28
|
+
defaultInital: T;
|
|
29
|
+
requestConfig?: {
|
|
30
|
+
/**请求之前处理参数*/
|
|
31
|
+
onBefore?: (payload: any, store: T) => Partial<T>;
|
|
32
|
+
/**请求接口*/
|
|
33
|
+
getList?: (payload: any) => Promise<{
|
|
34
|
+
code?: number;
|
|
35
|
+
data?: any;
|
|
36
|
+
message?: string;
|
|
37
|
+
}>;
|
|
38
|
+
/**请求之后处理返回值进行存储*/
|
|
39
|
+
onAfter?: (data: Record<string, any>) => Partial<T>;
|
|
40
|
+
/** code!==1 时 触发*/
|
|
41
|
+
onError?: (data: Record<string, any>) => void;
|
|
42
|
+
};
|
|
43
|
+
constructor(options?: PageDataOptions<T>);
|
|
44
|
+
store: T;
|
|
45
|
+
/**初始化状态值*/
|
|
46
|
+
main_page_store: (initalValues?: Partial<T>, file?: string[]) => this;
|
|
47
|
+
/**更新页面级的 pageLoading */
|
|
48
|
+
updatedLoading: (loading?: boolean) => void;
|
|
49
|
+
/**内置——查询列表*/
|
|
50
|
+
main_getList: () => Promise<void>;
|
|
51
|
+
/**内置——翻页*/
|
|
52
|
+
main_onPageChange: (page: number) => void;
|
|
53
|
+
/**内置——翻页切换*/
|
|
54
|
+
main_onShowSizeChange: (_: number, pageSize: number) => void;
|
|
55
|
+
/**内置——查询方法*/
|
|
56
|
+
main_onSearch: () => void;
|
|
57
|
+
/**加载更多*/
|
|
58
|
+
main_onLoadMore: () => void;
|
|
59
|
+
}
|
|
60
|
+
export interface PageDataOptions<T extends PageDataInstanceState = PageDataInstanceState> {
|
|
61
|
+
/**请求配置*/
|
|
62
|
+
requestConfig?: PageDataInstance<T>['requestConfig'];
|
|
63
|
+
/**初始值*/
|
|
64
|
+
initialValues?: Partial<T>;
|
|
65
|
+
/**是否滚动加载更多*/
|
|
66
|
+
is_scroll_page?: boolean;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 页面级数据状态管理
|
|
70
|
+
*/
|
|
71
|
+
export declare const usePageData: <T extends PageDataInstanceState = PageDataInstanceState>(options?: PageDataOptions<T>) => readonly [T, PageDataInstance<T>, string];
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { proxy, ref, useSnapshot } from "valtio";
|
|
2
|
+
import { ProxyInstanceObjectBase } from "../utils/valtio/instance.js";
|
|
3
|
+
import { useRef } from "react";
|
|
4
|
+
import { globalSettingDataInstance } from "./global.setting.data.instance.js";
|
|
5
|
+
class PageDataInstance extends ProxyInstanceObjectBase {
|
|
6
|
+
is_scroll_page = true;
|
|
7
|
+
defaultInital = {
|
|
8
|
+
page: 1,
|
|
9
|
+
pageSize: 10,
|
|
10
|
+
total: 0,
|
|
11
|
+
search: ref({}),
|
|
12
|
+
dataList: ref([]),
|
|
13
|
+
selectedRows: ref([]),
|
|
14
|
+
selectedRowKeys: ref([]),
|
|
15
|
+
loading: {
|
|
16
|
+
pageLoading: false
|
|
17
|
+
},
|
|
18
|
+
hasLastPage: false
|
|
19
|
+
};
|
|
20
|
+
requestConfig = {};
|
|
21
|
+
constructor(options){
|
|
22
|
+
super();
|
|
23
|
+
if (options?.initialValues) this.main_page_store(options.initialValues);
|
|
24
|
+
if (options.requestConfig) this.requestConfig = options.requestConfig || {};
|
|
25
|
+
if (options?.is_scroll_page !== void 0) this.is_scroll_page = options?.is_scroll_page;
|
|
26
|
+
}
|
|
27
|
+
store = proxy({
|
|
28
|
+
...this.defaultInital
|
|
29
|
+
});
|
|
30
|
+
main_page_store = (initalValues = {}, file)=>{
|
|
31
|
+
const newStore = {
|
|
32
|
+
...this.defaultInital,
|
|
33
|
+
...initalValues
|
|
34
|
+
};
|
|
35
|
+
return this._ctor(newStore, [
|
|
36
|
+
...file || [],
|
|
37
|
+
'loading'
|
|
38
|
+
]);
|
|
39
|
+
};
|
|
40
|
+
updatedLoading = (loading = true)=>{
|
|
41
|
+
if ('object' == typeof this.store?.loading) this.store.loading.pageLoading = loading;
|
|
42
|
+
else this.store.loading = {
|
|
43
|
+
pageLoading: loading
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
main_getList = async ()=>{
|
|
47
|
+
if (!this.requestConfig?.getList) return void console.error("\u672A\u914D\u7F6E requestConfig.getList \u8BF7\u6C42\u65B9\u6CD5");
|
|
48
|
+
try {
|
|
49
|
+
this.updatedLoading(true);
|
|
50
|
+
const payload = {
|
|
51
|
+
...this.store.search,
|
|
52
|
+
page: this.store.page,
|
|
53
|
+
pageSize: this.store.pageSize
|
|
54
|
+
};
|
|
55
|
+
let newParams = {
|
|
56
|
+
...payload
|
|
57
|
+
};
|
|
58
|
+
if (this.requestConfig?.onBefore) newParams = this.requestConfig.onBefore(payload, this.store);
|
|
59
|
+
const result = await this.requestConfig.getList?.(newParams);
|
|
60
|
+
this.updatedLoading(false);
|
|
61
|
+
this.store.loading.loadMore = false;
|
|
62
|
+
if (result && result.code === globalSettingDataInstance.store.requestSuccessCode) {
|
|
63
|
+
let saveData = {};
|
|
64
|
+
if (this.requestConfig?.onAfter) saveData = this.requestConfig.onAfter(result);
|
|
65
|
+
else {
|
|
66
|
+
const dataList = result?.data?.list || result?.data?.records || [];
|
|
67
|
+
let newDataList = [];
|
|
68
|
+
if (1 === this.store.page) newDataList = dataList;
|
|
69
|
+
else if (this.is_scroll_page) newDataList = [
|
|
70
|
+
...this.store.dataList,
|
|
71
|
+
...dataList
|
|
72
|
+
];
|
|
73
|
+
saveData = {
|
|
74
|
+
dataList: newDataList,
|
|
75
|
+
total: result?.data?.total || 0
|
|
76
|
+
};
|
|
77
|
+
if (1 === this.store.page) {
|
|
78
|
+
saveData.selectedRows = ref([]);
|
|
79
|
+
saveData.selectedRowKeys = ref([]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (saveData) this._setValues(saveData);
|
|
83
|
+
} else if (this.requestConfig?.onError) this.requestConfig.onError(result);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
console.log(error);
|
|
86
|
+
this.store.loading.loadMore = false;
|
|
87
|
+
this.updatedLoading(false);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
main_onPageChange = (page)=>{
|
|
91
|
+
this._setValues({
|
|
92
|
+
page
|
|
93
|
+
});
|
|
94
|
+
this.main_getList();
|
|
95
|
+
};
|
|
96
|
+
main_onShowSizeChange = (_, pageSize)=>{
|
|
97
|
+
this._setValues({
|
|
98
|
+
page: 1,
|
|
99
|
+
pageSize
|
|
100
|
+
});
|
|
101
|
+
this.main_getList();
|
|
102
|
+
};
|
|
103
|
+
main_onSearch = ()=>{
|
|
104
|
+
this.main_onPageChange(1);
|
|
105
|
+
};
|
|
106
|
+
main_onLoadMore = ()=>{
|
|
107
|
+
if (this.store.loading?.pageLoading) return;
|
|
108
|
+
const total = this.store.total || 0;
|
|
109
|
+
const page = this.store.page || 1;
|
|
110
|
+
const pageSize = this.store.pageSize || 20;
|
|
111
|
+
const count = Math.ceil(total / pageSize);
|
|
112
|
+
let hasLastPage = false;
|
|
113
|
+
if (page >= count && total) {
|
|
114
|
+
hasLastPage = true;
|
|
115
|
+
this._setValues({
|
|
116
|
+
hasLastPage
|
|
117
|
+
});
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const nextPage = page + 1;
|
|
121
|
+
if (nextPage >= count && total) hasLastPage = true;
|
|
122
|
+
this.store.loading.loadMore = true;
|
|
123
|
+
this._setValues({
|
|
124
|
+
page: nextPage,
|
|
125
|
+
hasLastPage
|
|
126
|
+
});
|
|
127
|
+
this.main_getList();
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const usePageData = (options = {})=>{
|
|
131
|
+
const pageDataInstance = useRef(new PageDataInstance(options)).current;
|
|
132
|
+
const store = useSnapshot(pageDataInstance.store);
|
|
133
|
+
return [
|
|
134
|
+
store,
|
|
135
|
+
pageDataInstance,
|
|
136
|
+
store.__defaultValue
|
|
137
|
+
];
|
|
138
|
+
};
|
|
139
|
+
export { PageDataInstance, usePageData };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { ProxyInstanceObjectBase } from '../utils/valtio/instance';
|
|
2
|
+
export interface PageInfoDataInstanceState extends Object {
|
|
3
|
+
/**loading存储*/
|
|
4
|
+
loading?: Record<string, boolean>;
|
|
5
|
+
/**编辑表单数据*/
|
|
6
|
+
editFormData?: Record<string, any>;
|
|
7
|
+
/**编辑类型*/
|
|
8
|
+
editType?: 'add' | 'edit' | 'info';
|
|
9
|
+
/**查询详情是否成功*/
|
|
10
|
+
isInfoSuccess?: boolean;
|
|
11
|
+
/**数据默认值不使用*/
|
|
12
|
+
__defaultValue?: string;
|
|
13
|
+
[s: string]: any;
|
|
14
|
+
}
|
|
15
|
+
export declare class PageInfoDataInstance<T extends PageInfoDataInstanceState = PageInfoDataInstanceState> extends ProxyInstanceObjectBase<T> {
|
|
16
|
+
/**默认值*/
|
|
17
|
+
defaultInital: T;
|
|
18
|
+
requestInfoConfig?: {
|
|
19
|
+
/**详情查询-请求之前处理参数*/
|
|
20
|
+
onBefore?: (store: T) => Promise<Partial<T>> | Partial<T>;
|
|
21
|
+
/**详情查询-请求接口*/
|
|
22
|
+
getInfo?: (payload: any) => Promise<{
|
|
23
|
+
code?: number;
|
|
24
|
+
data?: any;
|
|
25
|
+
message?: string;
|
|
26
|
+
}>;
|
|
27
|
+
/**详情查询-请求之后处理返回值进行存储*/
|
|
28
|
+
onAfter?: (data: Record<string, any>) => Partial<T>;
|
|
29
|
+
/**详情查询-code!==1 时 触发*/
|
|
30
|
+
onError?: (data: Record<string, any>) => void;
|
|
31
|
+
};
|
|
32
|
+
requestSaveInfoConfig?: {
|
|
33
|
+
/**详情保存-请求之前处理参数*/
|
|
34
|
+
onSaveBefore?: (store: T) => Promise<Record<string, any>> | Record<string, any>;
|
|
35
|
+
/**详情保存接口*/
|
|
36
|
+
onSaveInfo?: (payload: any) => Promise<{
|
|
37
|
+
code?: number;
|
|
38
|
+
data?: any;
|
|
39
|
+
message?: string;
|
|
40
|
+
}>;
|
|
41
|
+
/**详情保存-请求之后处理返回值进行存储*/
|
|
42
|
+
onSaveAfter?: (data: Record<string, any>) => void;
|
|
43
|
+
/**详情查询-code!==1 时 触发*/
|
|
44
|
+
onSaveError?: (data: Record<string, any>) => void;
|
|
45
|
+
/**保存成功-跳转页面*/
|
|
46
|
+
saveSuccessNavigate?: string | 'navigateBack' | number | ((data: any) => void);
|
|
47
|
+
/**是否显示成功提示*/
|
|
48
|
+
isShowSuccessMessage?: boolean;
|
|
49
|
+
};
|
|
50
|
+
constructor(options?: PageInfoDataOptions<T>);
|
|
51
|
+
store: T;
|
|
52
|
+
/**初始化状态值*/
|
|
53
|
+
main_page_store: (initalValues?: Partial<T>, file?: string[]) => this;
|
|
54
|
+
/**更新页面级的 pageLoading */
|
|
55
|
+
updatedLoading: (loading?: boolean) => void;
|
|
56
|
+
/**内置——查询详细信息*/
|
|
57
|
+
main_getInfo: () => Promise<void>;
|
|
58
|
+
/**保存数据*/
|
|
59
|
+
main_saveInfo: () => Promise<void>;
|
|
60
|
+
}
|
|
61
|
+
export interface PageInfoDataOptions<T extends PageInfoDataInstanceState = PageInfoDataInstanceState> {
|
|
62
|
+
/**详情查询请求配置*/
|
|
63
|
+
requestInfoConfig?: PageInfoDataInstance<T>['requestInfoConfig'];
|
|
64
|
+
/**详情保存请求配置*/
|
|
65
|
+
requestSaveInfoConfig?: PageInfoDataInstance<T>['requestSaveInfoConfig'];
|
|
66
|
+
/**初始值*/
|
|
67
|
+
initialValues?: Partial<T>;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 页面级数据状态管理
|
|
71
|
+
*/
|
|
72
|
+
export declare const usePageInfoData: <T extends PageInfoDataInstanceState = PageInfoDataInstanceState>(options?: PageInfoDataOptions<T>) => readonly [T, PageInfoDataInstance<T>, string];
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { proxy, useSnapshot } from "valtio";
|
|
2
|
+
import { ProxyInstanceObjectBase } from "../utils/valtio/instance.js";
|
|
3
|
+
import { useRef } from "react";
|
|
4
|
+
import utils_navigate from "../utils/navigate.js";
|
|
5
|
+
import { globalDataInstance } from "./global.data.instance.js";
|
|
6
|
+
import { globalSettingDataInstance } from "./global.setting.data.instance.js";
|
|
7
|
+
class PageInfoDataInstance extends ProxyInstanceObjectBase {
|
|
8
|
+
defaultInital = {
|
|
9
|
+
editType: 'add',
|
|
10
|
+
editFormData: {},
|
|
11
|
+
isInfoSuccess: false,
|
|
12
|
+
loading: {
|
|
13
|
+
pageLoading: false
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
requestInfoConfig;
|
|
17
|
+
requestSaveInfoConfig = {};
|
|
18
|
+
constructor(options){
|
|
19
|
+
super();
|
|
20
|
+
if (options?.initialValues) this.main_page_store(options.initialValues);
|
|
21
|
+
if (options.requestInfoConfig) this.requestInfoConfig = options.requestInfoConfig || {};
|
|
22
|
+
if (options.requestSaveInfoConfig) this.requestSaveInfoConfig = options.requestSaveInfoConfig || {};
|
|
23
|
+
}
|
|
24
|
+
store = proxy({
|
|
25
|
+
...this.defaultInital
|
|
26
|
+
});
|
|
27
|
+
main_page_store = (initalValues = {}, file)=>{
|
|
28
|
+
const newStore = {
|
|
29
|
+
...this.defaultInital,
|
|
30
|
+
...initalValues
|
|
31
|
+
};
|
|
32
|
+
return this._ctor(newStore, [
|
|
33
|
+
...file || [],
|
|
34
|
+
'loading'
|
|
35
|
+
]);
|
|
36
|
+
};
|
|
37
|
+
updatedLoading = (loading = true)=>{
|
|
38
|
+
if ('object' == typeof this.store?.loading) this.store.loading.pageLoading = loading;
|
|
39
|
+
else this.store.loading = {
|
|
40
|
+
pageLoading: loading
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
main_getInfo = async ()=>{
|
|
44
|
+
if (!this.requestInfoConfig?.getInfo) return void console.error("\u672A\u914D\u7F6E requestInfoConfig.getInfo \u8BF7\u6C42\u65B9\u6CD5");
|
|
45
|
+
try {
|
|
46
|
+
this.updatedLoading(true);
|
|
47
|
+
let newParams = {};
|
|
48
|
+
if (this.requestInfoConfig?.onBefore) newParams = await this.requestInfoConfig.onBefore(this.store);
|
|
49
|
+
const result = await this.requestInfoConfig.getInfo?.(newParams);
|
|
50
|
+
this.updatedLoading(false);
|
|
51
|
+
if (result && result.code === globalSettingDataInstance.store.requestSuccessCode) {
|
|
52
|
+
let saveData = {};
|
|
53
|
+
saveData = this.requestInfoConfig?.onAfter ? this.requestInfoConfig.onAfter(result) : {
|
|
54
|
+
editFormData: {
|
|
55
|
+
...result?.data
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
if (saveData) this._setValues({
|
|
59
|
+
...saveData,
|
|
60
|
+
isInfoSuccess: true
|
|
61
|
+
});
|
|
62
|
+
} else if (this.requestInfoConfig?.onError) this.requestInfoConfig.onError(result);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
console.log(error);
|
|
65
|
+
this.updatedLoading(false);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
main_saveInfo = async ()=>{
|
|
69
|
+
try {
|
|
70
|
+
if (this.requestSaveInfoConfig?.onSaveInfo) return void console.error("\u672A\u914D\u7F6E requestSaveInfoConfig.onSaveInfo \u8BF7\u6C42\u65B9\u6CD5");
|
|
71
|
+
this.updatedLoading(true);
|
|
72
|
+
const newParams = await this.requestSaveInfoConfig?.onSaveBefore?.(this.store);
|
|
73
|
+
const result = await this.requestSaveInfoConfig.onSaveInfo?.(newParams);
|
|
74
|
+
this.updatedLoading(false);
|
|
75
|
+
if (result && result.code === globalSettingDataInstance.store.requestSuccessCode) {
|
|
76
|
+
if (this.requestSaveInfoConfig?.isShowSuccessMessage !== false) globalDataInstance.showMessage({
|
|
77
|
+
content: result.message || "\u4FDD\u5B58\u6210\u529F"
|
|
78
|
+
});
|
|
79
|
+
if (this.requestSaveInfoConfig?.onSaveAfter) this.requestSaveInfoConfig.onSaveAfter(result);
|
|
80
|
+
const saveSuccessNavigate = this.requestSaveInfoConfig?.saveSuccessNavigate;
|
|
81
|
+
if (saveSuccessNavigate) {
|
|
82
|
+
if ('navigateBack' === saveSuccessNavigate) utils_navigate.navigateBack();
|
|
83
|
+
else if ('function' == typeof saveSuccessNavigate) saveSuccessNavigate(result);
|
|
84
|
+
else if ('number' == typeof saveSuccessNavigate) utils_navigate.navigateBack({
|
|
85
|
+
delta: saveSuccessNavigate
|
|
86
|
+
});
|
|
87
|
+
else if ('string' == typeof saveSuccessNavigate) utils_navigate.navigateTo({
|
|
88
|
+
url: saveSuccessNavigate
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
} else if (this.requestSaveInfoConfig?.onSaveError) this.requestSaveInfoConfig.onSaveError(result);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.log(error);
|
|
94
|
+
this.updatedLoading(false);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const usePageInfoData = (options = {})=>{
|
|
99
|
+
const pageDataInstance = useRef(new PageInfoDataInstance(options)).current;
|
|
100
|
+
const store = useSnapshot(pageDataInstance.store);
|
|
101
|
+
return [
|
|
102
|
+
store,
|
|
103
|
+
pageDataInstance,
|
|
104
|
+
store.__defaultValue
|
|
105
|
+
];
|
|
106
|
+
};
|
|
107
|
+
export { PageInfoDataInstance, usePageInfoData };
|
package/esm/index.d.ts
ADDED
package/esm/index.js
ADDED