@fairys/taro-tools-react 0.0.7 → 0.0.9
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/esm/context/page.data.instance.d.ts +95 -19
- package/esm/context/page.data.instance.js +213 -42
- package/esm/context/page.info.data.instance.d.ts +31 -10
- package/esm/context/page.info.data.instance.js +82 -23
- package/esm/utils/valtio/instance.d.ts +2 -0
- package/esm/utils/valtio/instance.js +2 -0
- package/package.json +1 -1
- package/src/context/page.data.instance.tsx +477 -0
- package/src/context/page.info.data.instance.tsx +274 -0
- package/src/utils/valtio/instance.ts +4 -2
- package/src/context/page.data.instance.ts +0 -215
- package/src/context/page.info.data.instance.ts +0 -182
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { proxy, ref, useSnapshot } from 'valtio';
|
|
2
|
+
import { ProxyInstanceObjectBase } from 'utils/valtio/instance';
|
|
3
|
+
import { createContext, useContext, useEffect, useMemo, useRef } from 'react';
|
|
4
|
+
import navigate from '../utils/navigate';
|
|
5
|
+
import { globalDataInstance } from './global.data.instance';
|
|
6
|
+
import { globalSettingDataInstance } from './global.setting.data.instance';
|
|
7
|
+
import { PageDataInstance } from './page.data.instance';
|
|
8
|
+
import { useDidShow } from '@tarojs/taro';
|
|
9
|
+
import Taro from '@tarojs/taro';
|
|
10
|
+
|
|
11
|
+
export interface PageInfoDataInstanceState extends Object {
|
|
12
|
+
/**loading存储*/
|
|
13
|
+
loading?: Record<string, boolean>;
|
|
14
|
+
/**编辑表单数据*/
|
|
15
|
+
editFormData?: Record<string, any>;
|
|
16
|
+
/**编辑类型*/
|
|
17
|
+
editType?: 'add' | 'edit' | 'info';
|
|
18
|
+
/**查询详情是否成功*/
|
|
19
|
+
isInfoSuccess?: boolean;
|
|
20
|
+
/**数据默认值不使用*/
|
|
21
|
+
__defaultValue?: string;
|
|
22
|
+
[s: string]: any;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class PageInfoDataInstance<
|
|
26
|
+
T extends PageInfoDataInstanceState = PageInfoDataInstanceState,
|
|
27
|
+
> extends ProxyInstanceObjectBase<T> {
|
|
28
|
+
notRefFields = ['loading'];
|
|
29
|
+
/**默认值*/
|
|
30
|
+
defaultInital = {
|
|
31
|
+
/**编辑类型*/
|
|
32
|
+
editType: 'add',
|
|
33
|
+
/**编辑表单数据*/
|
|
34
|
+
editFormData: ref({}),
|
|
35
|
+
/**查询详情是否成功*/
|
|
36
|
+
isInfoSuccess: false,
|
|
37
|
+
/**加载状态*/
|
|
38
|
+
loading: { pageLoading: false },
|
|
39
|
+
} as unknown as T;
|
|
40
|
+
|
|
41
|
+
requestInfoConfig?: {
|
|
42
|
+
/**详情查询-请求之前处理参数*/
|
|
43
|
+
onBefore?: (store: T, instance: PageInfoDataInstance<T>) => Promise<Partial<T>> | Partial<T>;
|
|
44
|
+
/**详情查询-请求接口*/
|
|
45
|
+
getInfo?: (
|
|
46
|
+
payload: any,
|
|
47
|
+
instance: PageInfoDataInstance<T>,
|
|
48
|
+
) => Promise<{ code?: number; data?: any; message?: string }>;
|
|
49
|
+
/**详情查询-请求之后处理返回值进行存储*/
|
|
50
|
+
onAfter?: (data: Record<string, any>, instance: PageInfoDataInstance<T>) => Partial<T>;
|
|
51
|
+
/**详情查询-code!==1 时 触发*/
|
|
52
|
+
onError?: (data: Record<string, any>, instance: PageInfoDataInstance<T>) => void;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
requestSaveInfoConfig?: {
|
|
56
|
+
/**详情保存-请求之前处理参数*/
|
|
57
|
+
onSaveBefore?: (store: T, instance: PageInfoDataInstance<T>) => Promise<Record<string, any>> | Record<string, any>;
|
|
58
|
+
/**详情保存接口*/
|
|
59
|
+
onSaveInfo?: (
|
|
60
|
+
payload: any,
|
|
61
|
+
instance: PageInfoDataInstance<T>,
|
|
62
|
+
) => Promise<{ code?: number; data?: any; message?: string }>;
|
|
63
|
+
/**详情保存-请求之后处理返回值进行存储*/
|
|
64
|
+
onSaveAfter?: (data: Record<string, any>, instance: PageInfoDataInstance<T>) => void;
|
|
65
|
+
/**详情查询-code!==1 时 触发*/
|
|
66
|
+
onSaveError?: (data: Record<string, any>, instance: PageInfoDataInstance<T>) => void;
|
|
67
|
+
/**保存成功-跳转页面*/
|
|
68
|
+
saveSuccessNavigate?: string | 'navigateBack' | number | ((data: any) => void);
|
|
69
|
+
/**是否显示成功提示*/
|
|
70
|
+
isShowSuccessMessage?: boolean;
|
|
71
|
+
} = {};
|
|
72
|
+
|
|
73
|
+
ctor(options?: PageInfoDataOptions<T>) {
|
|
74
|
+
if (options?.isProxy) {
|
|
75
|
+
this.notRefFields = ['editFormData', 'loading'];
|
|
76
|
+
this.store.editFormData = {};
|
|
77
|
+
}
|
|
78
|
+
if (options?.initialValues) {
|
|
79
|
+
this.main_page_store(options.initialValues);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
store = proxy<T>({ ...this.defaultInital } as T);
|
|
84
|
+
|
|
85
|
+
/**初始化状态值*/
|
|
86
|
+
main_page_store = (initalValues: Partial<T> = {}, file?: string[]) => {
|
|
87
|
+
const newStore = { ...this.defaultInital, ...initalValues } as unknown as T;
|
|
88
|
+
return this._ctor(newStore, [...(file || []), 'loading']);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/**更新页面级的 pageLoading */
|
|
92
|
+
updatedLoading = (loading: boolean = true) => {
|
|
93
|
+
if (typeof this.store?.loading === 'object') {
|
|
94
|
+
this.store.loading.pageLoading = loading;
|
|
95
|
+
} else {
|
|
96
|
+
this.store.loading = { pageLoading: loading };
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**内置——查询详细信息*/
|
|
101
|
+
main_getInfo = async () => {
|
|
102
|
+
if (!this.requestInfoConfig?.getInfo) {
|
|
103
|
+
console.error('未配置 requestInfoConfig.getInfo 请求方法');
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
Taro.showLoading({ title: '加载中...' });
|
|
107
|
+
try {
|
|
108
|
+
this.updatedLoading(true);
|
|
109
|
+
let newParams = {} as any;
|
|
110
|
+
if (this.requestInfoConfig?.onBefore) {
|
|
111
|
+
newParams = await this.requestInfoConfig.onBefore(this.store, this);
|
|
112
|
+
}
|
|
113
|
+
const result = await this.requestInfoConfig.getInfo?.(newParams, this);
|
|
114
|
+
this.updatedLoading(false);
|
|
115
|
+
if (result && result.code === globalSettingDataInstance.store.requestSuccessCode) {
|
|
116
|
+
let saveData: Partial<T> = {};
|
|
117
|
+
if (this.requestInfoConfig?.onAfter) {
|
|
118
|
+
saveData = this.requestInfoConfig.onAfter(result, this);
|
|
119
|
+
} else {
|
|
120
|
+
saveData = { editFormData: { ...result?.data } } as Partial<T>;
|
|
121
|
+
}
|
|
122
|
+
if (saveData) this._setValues({ ...saveData, isInfoSuccess: true });
|
|
123
|
+
} else if (this.requestInfoConfig?.onError) {
|
|
124
|
+
this.requestInfoConfig.onError(result, this);
|
|
125
|
+
}
|
|
126
|
+
} catch (error) {
|
|
127
|
+
console.log(error);
|
|
128
|
+
this.updatedLoading(false);
|
|
129
|
+
}
|
|
130
|
+
Taro.hideLoading();
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/**保存数据*/
|
|
134
|
+
main_saveInfo = async () => {
|
|
135
|
+
if (this.requestSaveInfoConfig?.onSaveInfo) {
|
|
136
|
+
console.error('未配置 requestSaveInfoConfig.onSaveInfo 请求方法');
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
this.updatedLoading(true);
|
|
141
|
+
Taro.showLoading({ title: '保存中...' });
|
|
142
|
+
const newParams =
|
|
143
|
+
(await this.requestSaveInfoConfig?.onSaveBefore?.(this.store, this)) || this.store.editFormData || {};
|
|
144
|
+
const result = await this.requestSaveInfoConfig.onSaveInfo?.(newParams, this);
|
|
145
|
+
Taro.hideLoading();
|
|
146
|
+
this.updatedLoading(false);
|
|
147
|
+
if (result && result.code === globalSettingDataInstance.store.requestSuccessCode) {
|
|
148
|
+
if (this.requestSaveInfoConfig?.isShowSuccessMessage === false) {
|
|
149
|
+
globalDataInstance.showMessage({ content: result.message || '保存成功' });
|
|
150
|
+
} else {
|
|
151
|
+
Taro.showToast({ title: result.message || '保存成功', icon: 'none' });
|
|
152
|
+
}
|
|
153
|
+
if (this.requestSaveInfoConfig?.onSaveAfter) {
|
|
154
|
+
this.requestSaveInfoConfig.onSaveAfter(result, this);
|
|
155
|
+
}
|
|
156
|
+
const saveSuccessNavigate = this.requestSaveInfoConfig?.saveSuccessNavigate;
|
|
157
|
+
if (saveSuccessNavigate) {
|
|
158
|
+
if (saveSuccessNavigate === 'navigateBack') {
|
|
159
|
+
navigate.navigateBack();
|
|
160
|
+
} else if (typeof saveSuccessNavigate === 'function') {
|
|
161
|
+
saveSuccessNavigate(result);
|
|
162
|
+
} else if (typeof saveSuccessNavigate === 'number') {
|
|
163
|
+
navigate.navigateBack({ delta: saveSuccessNavigate });
|
|
164
|
+
} else if (typeof saveSuccessNavigate === 'string') {
|
|
165
|
+
navigate.navigateTo({ url: saveSuccessNavigate });
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
} else if (this.requestSaveInfoConfig?.onSaveError) {
|
|
169
|
+
this.requestSaveInfoConfig.onSaveError(result, this);
|
|
170
|
+
}
|
|
171
|
+
} catch (error) {
|
|
172
|
+
console.log(error);
|
|
173
|
+
this.updatedLoading(false);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface PageInfoDataOptions<T extends PageInfoDataInstanceState = PageInfoDataInstanceState> {
|
|
179
|
+
/**详情查询请求配置*/
|
|
180
|
+
requestInfoConfig?: PageInfoDataInstance<T>['requestInfoConfig'];
|
|
181
|
+
/**详情保存请求配置*/
|
|
182
|
+
requestSaveInfoConfig?: PageInfoDataInstance<T>['requestSaveInfoConfig'];
|
|
183
|
+
/**初始值*/
|
|
184
|
+
initialValues?: Partial<T>;
|
|
185
|
+
/**editFormData是否使用valtio proxy 存储*/
|
|
186
|
+
isProxy?: boolean;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**初始化实例*/
|
|
190
|
+
export const usePageInfoDataInstance = <T extends PageInfoDataInstanceState = PageInfoDataInstanceState>(
|
|
191
|
+
instance: PageInfoDataInstance<T>,
|
|
192
|
+
) => {
|
|
193
|
+
const ref = useRef<PageInfoDataInstance<T>>();
|
|
194
|
+
if (!ref.current) {
|
|
195
|
+
if (instance) {
|
|
196
|
+
ref.current = instance;
|
|
197
|
+
} else {
|
|
198
|
+
ref.current = new PageInfoDataInstance<T>();
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return ref.current;
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/**页面级数据状态管理上下文*/
|
|
205
|
+
export const PageInfoDataInstanceContext = createContext<PageInfoDataInstance>(new PageInfoDataInstance());
|
|
206
|
+
|
|
207
|
+
/**获取上下文实例*/
|
|
208
|
+
export const usePageInfoDataInstanceContext = <T extends PageInfoDataInstanceState = PageInfoDataInstanceState>() => {
|
|
209
|
+
const PageInfoDataInstance = useContext(PageInfoDataInstanceContext) as PageInfoDataInstance<T>;
|
|
210
|
+
return PageInfoDataInstance;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
export interface PageInfoDataInstanceContextProviderProps<
|
|
214
|
+
T extends PageInfoDataInstanceState = PageInfoDataInstanceState,
|
|
215
|
+
> extends PageInfoDataOptions<T> {
|
|
216
|
+
instance?: PageInfoDataInstance<T>;
|
|
217
|
+
children: React.ReactNode;
|
|
218
|
+
/**页面标题*/
|
|
219
|
+
title?: string;
|
|
220
|
+
/**页面一加载是否请求详情接口*/
|
|
221
|
+
isMountRequestInfo?: boolean;
|
|
222
|
+
/**自定义hooks,挂载参数和设置完初始值后执行*/
|
|
223
|
+
useHooks?: (instance: PageInfoDataInstance<T>) => void;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**页面级数据状态管理上下文提供者*/
|
|
227
|
+
export function PageInfoDataInstanceContextProvider<T extends PageInfoDataInstanceState = PageInfoDataInstanceState>(
|
|
228
|
+
props: PageInfoDataInstanceContextProviderProps<T>,
|
|
229
|
+
) {
|
|
230
|
+
const {
|
|
231
|
+
instance,
|
|
232
|
+
children,
|
|
233
|
+
initialValues,
|
|
234
|
+
requestInfoConfig,
|
|
235
|
+
requestSaveInfoConfig,
|
|
236
|
+
isProxy,
|
|
237
|
+
title,
|
|
238
|
+
isMountRequestInfo,
|
|
239
|
+
useHooks,
|
|
240
|
+
} = props;
|
|
241
|
+
const pageInfoInstance = usePageInfoDataInstance(instance);
|
|
242
|
+
pageInfoInstance.requestInfoConfig = requestInfoConfig;
|
|
243
|
+
pageInfoInstance.requestSaveInfoConfig = requestSaveInfoConfig;
|
|
244
|
+
|
|
245
|
+
useMemo(() => pageInfoInstance.ctor({ initialValues, isProxy }), [pageInfoInstance]);
|
|
246
|
+
|
|
247
|
+
useHooks?.(pageInfoInstance);
|
|
248
|
+
|
|
249
|
+
useDidShow(() => {
|
|
250
|
+
if (title) {
|
|
251
|
+
// 列表查询才调用
|
|
252
|
+
Taro.setNavigationBarTitle({ title });
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
useEffect(() => {
|
|
257
|
+
if (isMountRequestInfo) {
|
|
258
|
+
pageInfoInstance.main_getInfo();
|
|
259
|
+
}
|
|
260
|
+
}, []);
|
|
261
|
+
|
|
262
|
+
return (
|
|
263
|
+
<PageInfoDataInstanceContext.Provider value={pageInfoInstance}>{children}</PageInfoDataInstanceContext.Provider>
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 页面级数据状态管理
|
|
269
|
+
*/
|
|
270
|
+
export const usePageInfoDataInstanceState = <T extends PageInfoDataInstanceState = PageInfoDataInstanceState>() => {
|
|
271
|
+
const PageInfoDataInstance = usePageInfoDataInstanceContext<T>();
|
|
272
|
+
const store = useSnapshot(PageInfoDataInstance.store, { sync: true }) as T;
|
|
273
|
+
return [store, PageInfoDataInstance, store.__defaultValue] as [T, PageInfoDataInstance<T>, string | undefined];
|
|
274
|
+
};
|
|
@@ -5,15 +5,15 @@ import React from 'react';
|
|
|
5
5
|
* 单个proxy对象数据基础实例封装
|
|
6
6
|
*/
|
|
7
7
|
export class ProxyInstanceObjectBase<T extends Object = any> {
|
|
8
|
+
/**不使用ref存储的字段*/
|
|
9
|
+
notRefFields: string[] = [];
|
|
8
10
|
/**proxy 可状态更新字段 */
|
|
9
11
|
store = proxy<T>({} as T);
|
|
10
|
-
|
|
11
12
|
/**初始化存储值*/
|
|
12
13
|
_ctor = (inital?: Partial<T>, fields?: string[]) => {
|
|
13
14
|
this._setValues(inital || {}, fields);
|
|
14
15
|
return this;
|
|
15
16
|
};
|
|
16
|
-
|
|
17
17
|
/**更新store数据 循环对象进行存储,当值是对象的时候存储为ref*/
|
|
18
18
|
_setValues = <K = T>(values: Partial<K>, fields?: string[]) => {
|
|
19
19
|
if (!this.store) {
|
|
@@ -23,6 +23,8 @@ export class ProxyInstanceObjectBase<T extends Object = any> {
|
|
|
23
23
|
const value = values[key];
|
|
24
24
|
if (Array.isArray(fields) && fields.includes(key)) {
|
|
25
25
|
this.store[key] = values[key];
|
|
26
|
+
} else if (Array.isArray(this.notRefFields) && this.notRefFields.includes(key)) {
|
|
27
|
+
this.store[key as keyof T] = value;
|
|
26
28
|
} else if (React.isValidElement(value) || typeof value === 'function') {
|
|
27
29
|
this.store[key] = ref(values[key]);
|
|
28
30
|
} else if (typeof value === 'object' && value !== null) {
|
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
import { proxy, ref, useSnapshot } from 'valtio';
|
|
2
|
-
import { ProxyInstanceObjectBase } from 'utils/valtio/instance';
|
|
3
|
-
import { useRef } from 'react';
|
|
4
|
-
import { globalSettingDataInstance } from './global.setting.data.instance';
|
|
5
|
-
|
|
6
|
-
export interface PageDataInstanceState extends Object {
|
|
7
|
-
/**loading存储*/
|
|
8
|
-
loading?: Record<string, boolean>;
|
|
9
|
-
/**当前页*/
|
|
10
|
-
page?: number;
|
|
11
|
-
/**分页数*/
|
|
12
|
-
pageSize?: number;
|
|
13
|
-
/**总数*/
|
|
14
|
-
total?: number;
|
|
15
|
-
/**是否最后一页*/
|
|
16
|
-
hasLastPage?: boolean;
|
|
17
|
-
/**查询条件*/
|
|
18
|
-
search?: Object;
|
|
19
|
-
/**表格数据*/
|
|
20
|
-
dataList?: any[];
|
|
21
|
-
/**选择行数据*/
|
|
22
|
-
selectedRows?: any[];
|
|
23
|
-
selectedRowKeys?: any[];
|
|
24
|
-
/**数据默认值不使用*/
|
|
25
|
-
__defaultValue?: string;
|
|
26
|
-
[s: string]: any;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export class PageDataInstance<
|
|
30
|
-
T extends PageDataInstanceState = PageDataInstanceState,
|
|
31
|
-
> extends ProxyInstanceObjectBase<T> {
|
|
32
|
-
/**是否滚动加载分页*/
|
|
33
|
-
is_scroll_page = true;
|
|
34
|
-
/**默认值*/
|
|
35
|
-
defaultInital = {
|
|
36
|
-
/**当前页*/
|
|
37
|
-
page: 1,
|
|
38
|
-
/**分页数*/
|
|
39
|
-
pageSize: 10,
|
|
40
|
-
/**总数*/
|
|
41
|
-
total: 0,
|
|
42
|
-
/**查询条件*/
|
|
43
|
-
search: ref({}),
|
|
44
|
-
/**表格数据*/
|
|
45
|
-
dataList: ref([]),
|
|
46
|
-
/**选择行数据*/
|
|
47
|
-
selectedRows: ref([]),
|
|
48
|
-
selectedRowKeys: ref([]),
|
|
49
|
-
/**加载状态*/
|
|
50
|
-
loading: { pageLoading: false },
|
|
51
|
-
/**是否最后一页*/
|
|
52
|
-
hasLastPage: false,
|
|
53
|
-
} as unknown as T;
|
|
54
|
-
|
|
55
|
-
requestConfig?: {
|
|
56
|
-
/**请求之前处理参数*/
|
|
57
|
-
onBefore?: (payload: any, store: T) => Partial<T>;
|
|
58
|
-
/**请求接口*/
|
|
59
|
-
getList?: (payload: any) => Promise<{ code?: number; data?: any; message?: string }>;
|
|
60
|
-
/**请求之后处理返回值进行存储*/
|
|
61
|
-
onAfter?: (data: Record<string, any>) => Partial<T>;
|
|
62
|
-
/** code!==1 时 触发*/
|
|
63
|
-
onError?: (data: Record<string, any>) => void;
|
|
64
|
-
} = {};
|
|
65
|
-
|
|
66
|
-
constructor(options?: PageDataOptions<T>) {
|
|
67
|
-
super();
|
|
68
|
-
if (options?.initialValues) {
|
|
69
|
-
this.main_page_store(options.initialValues);
|
|
70
|
-
}
|
|
71
|
-
if (options.requestConfig) {
|
|
72
|
-
this.requestConfig = options.requestConfig || {};
|
|
73
|
-
}
|
|
74
|
-
if (options?.is_scroll_page !== undefined) {
|
|
75
|
-
this.is_scroll_page = options?.is_scroll_page;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
store = proxy<T>({ ...this.defaultInital } as T);
|
|
80
|
-
|
|
81
|
-
/**初始化状态值*/
|
|
82
|
-
main_page_store = (initalValues: Partial<T> = {}, file?: string[]) => {
|
|
83
|
-
const newStore = { ...this.defaultInital, ...initalValues } as unknown as T;
|
|
84
|
-
return this._ctor(newStore, [...(file || []), 'loading']);
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
/**更新页面级的 pageLoading */
|
|
88
|
-
updatedLoading = (loading: boolean = true) => {
|
|
89
|
-
if (typeof this.store?.loading === 'object') {
|
|
90
|
-
this.store.loading.pageLoading = loading;
|
|
91
|
-
} else {
|
|
92
|
-
this.store.loading = { pageLoading: loading };
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
/**内置——查询列表*/
|
|
97
|
-
main_getList = async () => {
|
|
98
|
-
if (!this.requestConfig?.getList) {
|
|
99
|
-
console.error('未配置 requestConfig.getList 请求方法');
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
try {
|
|
103
|
-
this.updatedLoading(true);
|
|
104
|
-
const payload = {
|
|
105
|
-
...this.store.search,
|
|
106
|
-
page: this.store.page,
|
|
107
|
-
pageSize: this.store.pageSize,
|
|
108
|
-
};
|
|
109
|
-
let newParams = { ...payload } as any;
|
|
110
|
-
if (this.requestConfig?.onBefore) {
|
|
111
|
-
newParams = this.requestConfig.onBefore(payload, this.store);
|
|
112
|
-
}
|
|
113
|
-
const result = await this.requestConfig.getList?.(newParams);
|
|
114
|
-
this.updatedLoading(false);
|
|
115
|
-
this.store.loading.loadMore = false;
|
|
116
|
-
|
|
117
|
-
if (result && result.code === globalSettingDataInstance.store.requestSuccessCode) {
|
|
118
|
-
let saveData: Partial<T> = {};
|
|
119
|
-
if (this.requestConfig?.onAfter) {
|
|
120
|
-
saveData = this.requestConfig.onAfter(result);
|
|
121
|
-
} else {
|
|
122
|
-
const dataList = result?.data?.list || result?.data?.rows || result?.data?.records || [];
|
|
123
|
-
/**如果是第一页则直接返回数据,否则进行拼接数据*/
|
|
124
|
-
let newDataList = [];
|
|
125
|
-
if (this.store.page === 1) {
|
|
126
|
-
newDataList = dataList;
|
|
127
|
-
} else if (this.is_scroll_page) {
|
|
128
|
-
newDataList = [...this.store.dataList, ...dataList];
|
|
129
|
-
}
|
|
130
|
-
saveData = {
|
|
131
|
-
dataList: newDataList,
|
|
132
|
-
total: result?.data?.total || 0,
|
|
133
|
-
} as Partial<T>;
|
|
134
|
-
// 第一页清理
|
|
135
|
-
if (this.store.page === 1) {
|
|
136
|
-
saveData.selectedRows = ref([]);
|
|
137
|
-
saveData.selectedRowKeys = ref([]);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
if (saveData) this._setValues(saveData);
|
|
141
|
-
} else if (this.requestConfig?.onError) {
|
|
142
|
-
this.requestConfig.onError(result);
|
|
143
|
-
}
|
|
144
|
-
} catch (error) {
|
|
145
|
-
console.log(error);
|
|
146
|
-
this.store.loading.loadMore = false;
|
|
147
|
-
this.updatedLoading(false);
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
/**内置——翻页*/
|
|
152
|
-
main_onPageChange = (page: number) => {
|
|
153
|
-
this._setValues({ page });
|
|
154
|
-
this.main_getList();
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
/**内置——翻页切换*/
|
|
158
|
-
main_onShowSizeChange = (_: number, pageSize: number) => {
|
|
159
|
-
this._setValues({ page: 1, pageSize });
|
|
160
|
-
this.main_getList();
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
/**内置——查询方法*/
|
|
164
|
-
main_onSearch = () => {
|
|
165
|
-
this.main_onPageChange(1);
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
/**加载更多*/
|
|
169
|
-
main_onLoadMore = () => {
|
|
170
|
-
if (this.store.loading?.pageLoading) {
|
|
171
|
-
// 加载中,不进行请求
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
const total = this.store.total || 0;
|
|
175
|
-
const page = this.store.page || 1;
|
|
176
|
-
const pageSize = this.store.pageSize || 20;
|
|
177
|
-
const count = Math.ceil(total / pageSize);
|
|
178
|
-
let hasLastPage = false;
|
|
179
|
-
if (page >= count && total) {
|
|
180
|
-
// 已经最后一页数据了
|
|
181
|
-
hasLastPage = true;
|
|
182
|
-
this._setValues({ hasLastPage });
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
const nextPage = page + 1;
|
|
186
|
-
if (nextPage >= count && total) {
|
|
187
|
-
// 当前是最后一页数据
|
|
188
|
-
hasLastPage = true;
|
|
189
|
-
}
|
|
190
|
-
this.store.loading.loadMore = true;
|
|
191
|
-
// 判断是否最后一页数据
|
|
192
|
-
this._setValues({ page: nextPage, hasLastPage });
|
|
193
|
-
this.main_getList();
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export interface PageDataOptions<T extends PageDataInstanceState = PageDataInstanceState> {
|
|
198
|
-
/**请求配置*/
|
|
199
|
-
requestConfig?: PageDataInstance<T>['requestConfig'];
|
|
200
|
-
/**初始值*/
|
|
201
|
-
initialValues?: Partial<T>;
|
|
202
|
-
/**是否滚动加载更多*/
|
|
203
|
-
is_scroll_page?: boolean;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* 页面级数据状态管理
|
|
208
|
-
*/
|
|
209
|
-
export const usePageData = <T extends PageDataInstanceState = PageDataInstanceState>(
|
|
210
|
-
options: PageDataOptions<T> = {},
|
|
211
|
-
) => {
|
|
212
|
-
const pageDataInstance = useRef(new PageDataInstance<T>(options)).current;
|
|
213
|
-
const store = useSnapshot(pageDataInstance.store) as T;
|
|
214
|
-
return [store, pageDataInstance, store.__defaultValue] as [T, PageDataInstance<T>, string | undefined];
|
|
215
|
-
};
|
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import { proxy, useSnapshot } from 'valtio';
|
|
2
|
-
import { ProxyInstanceObjectBase } from 'utils/valtio/instance';
|
|
3
|
-
import { useRef } from 'react';
|
|
4
|
-
import navigate from './../utils/navigate';
|
|
5
|
-
import { globalDataInstance } from './global.data.instance';
|
|
6
|
-
import { globalSettingDataInstance } from './global.setting.data.instance';
|
|
7
|
-
|
|
8
|
-
export interface PageInfoDataInstanceState extends Object {
|
|
9
|
-
/**loading存储*/
|
|
10
|
-
loading?: Record<string, boolean>;
|
|
11
|
-
/**编辑表单数据*/
|
|
12
|
-
editFormData?: Record<string, any>;
|
|
13
|
-
/**编辑类型*/
|
|
14
|
-
editType?: 'add' | 'edit' | 'info';
|
|
15
|
-
/**查询详情是否成功*/
|
|
16
|
-
isInfoSuccess?: boolean;
|
|
17
|
-
/**数据默认值不使用*/
|
|
18
|
-
__defaultValue?: string;
|
|
19
|
-
[s: string]: any;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export class PageInfoDataInstance<
|
|
23
|
-
T extends PageInfoDataInstanceState = PageInfoDataInstanceState,
|
|
24
|
-
> extends ProxyInstanceObjectBase<T> {
|
|
25
|
-
/**默认值*/
|
|
26
|
-
defaultInital = {
|
|
27
|
-
/**编辑类型*/
|
|
28
|
-
editType: 'add',
|
|
29
|
-
/**编辑表单数据*/
|
|
30
|
-
editFormData: {},
|
|
31
|
-
/**查询详情是否成功*/
|
|
32
|
-
isInfoSuccess: false,
|
|
33
|
-
/**加载状态*/
|
|
34
|
-
loading: { pageLoading: false },
|
|
35
|
-
} as unknown as T;
|
|
36
|
-
|
|
37
|
-
requestInfoConfig?: {
|
|
38
|
-
/**详情查询-请求之前处理参数*/
|
|
39
|
-
onBefore?: (store: T) => Promise<Partial<T>> | Partial<T>;
|
|
40
|
-
/**详情查询-请求接口*/
|
|
41
|
-
getInfo?: (payload: any) => Promise<{ code?: number; data?: any; message?: string }>;
|
|
42
|
-
/**详情查询-请求之后处理返回值进行存储*/
|
|
43
|
-
onAfter?: (data: Record<string, any>) => Partial<T>;
|
|
44
|
-
/**详情查询-code!==1 时 触发*/
|
|
45
|
-
onError?: (data: Record<string, any>) => void;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
requestSaveInfoConfig?: {
|
|
49
|
-
/**详情保存-请求之前处理参数*/
|
|
50
|
-
onSaveBefore?: (store: T) => Promise<Record<string, any>> | Record<string, any>;
|
|
51
|
-
/**详情保存接口*/
|
|
52
|
-
onSaveInfo?: (payload: any) => Promise<{ code?: number; data?: any; message?: string }>;
|
|
53
|
-
/**详情保存-请求之后处理返回值进行存储*/
|
|
54
|
-
onSaveAfter?: (data: Record<string, any>) => void;
|
|
55
|
-
/**详情查询-code!==1 时 触发*/
|
|
56
|
-
onSaveError?: (data: Record<string, any>) => void;
|
|
57
|
-
/**保存成功-跳转页面*/
|
|
58
|
-
saveSuccessNavigate?: string | 'navigateBack' | number | ((data: any) => void);
|
|
59
|
-
/**是否显示成功提示*/
|
|
60
|
-
isShowSuccessMessage?: boolean;
|
|
61
|
-
} = {};
|
|
62
|
-
|
|
63
|
-
constructor(options?: PageInfoDataOptions<T>) {
|
|
64
|
-
super();
|
|
65
|
-
if (options?.initialValues) {
|
|
66
|
-
this.main_page_store(options.initialValues);
|
|
67
|
-
}
|
|
68
|
-
if (options.requestInfoConfig) {
|
|
69
|
-
this.requestInfoConfig = options.requestInfoConfig || {};
|
|
70
|
-
}
|
|
71
|
-
if (options.requestSaveInfoConfig) {
|
|
72
|
-
this.requestSaveInfoConfig = options.requestSaveInfoConfig || {};
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
store = proxy<T>({ ...this.defaultInital } as T);
|
|
77
|
-
|
|
78
|
-
/**初始化状态值*/
|
|
79
|
-
main_page_store = (initalValues: Partial<T> = {}, file?: string[]) => {
|
|
80
|
-
const newStore = { ...this.defaultInital, ...initalValues } as unknown as T;
|
|
81
|
-
return this._ctor(newStore, [...(file || []), 'loading']);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
/**更新页面级的 pageLoading */
|
|
85
|
-
updatedLoading = (loading: boolean = true) => {
|
|
86
|
-
if (typeof this.store?.loading === 'object') {
|
|
87
|
-
this.store.loading.pageLoading = loading;
|
|
88
|
-
} else {
|
|
89
|
-
this.store.loading = { pageLoading: loading };
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
/**内置——查询详细信息*/
|
|
94
|
-
main_getInfo = async () => {
|
|
95
|
-
if (!this.requestInfoConfig?.getInfo) {
|
|
96
|
-
console.error('未配置 requestInfoConfig.getInfo 请求方法');
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
try {
|
|
100
|
-
this.updatedLoading(true);
|
|
101
|
-
let newParams = {} as any;
|
|
102
|
-
if (this.requestInfoConfig?.onBefore) {
|
|
103
|
-
newParams = await this.requestInfoConfig.onBefore(this.store);
|
|
104
|
-
}
|
|
105
|
-
const result = await this.requestInfoConfig.getInfo?.(newParams);
|
|
106
|
-
this.updatedLoading(false);
|
|
107
|
-
if (result && result.code === globalSettingDataInstance.store.requestSuccessCode) {
|
|
108
|
-
let saveData: Partial<T> = {};
|
|
109
|
-
if (this.requestInfoConfig?.onAfter) {
|
|
110
|
-
saveData = this.requestInfoConfig.onAfter(result);
|
|
111
|
-
} else {
|
|
112
|
-
saveData = { editFormData: { ...result?.data } } as Partial<T>;
|
|
113
|
-
}
|
|
114
|
-
if (saveData) this._setValues({ ...saveData, isInfoSuccess: true });
|
|
115
|
-
} else if (this.requestInfoConfig?.onError) {
|
|
116
|
-
this.requestInfoConfig.onError(result);
|
|
117
|
-
}
|
|
118
|
-
} catch (error) {
|
|
119
|
-
console.log(error);
|
|
120
|
-
this.updatedLoading(false);
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
/**保存数据*/
|
|
125
|
-
main_saveInfo = async () => {
|
|
126
|
-
try {
|
|
127
|
-
if (this.requestSaveInfoConfig?.onSaveInfo) {
|
|
128
|
-
console.error('未配置 requestSaveInfoConfig.onSaveInfo 请求方法');
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
this.updatedLoading(true);
|
|
132
|
-
const newParams = (await this.requestSaveInfoConfig?.onSaveBefore?.(this.store)) || this.store.editFormData || {};
|
|
133
|
-
const result = await this.requestSaveInfoConfig.onSaveInfo?.(newParams);
|
|
134
|
-
this.updatedLoading(false);
|
|
135
|
-
if (result && result.code === globalSettingDataInstance.store.requestSuccessCode) {
|
|
136
|
-
if (this.requestSaveInfoConfig?.isShowSuccessMessage !== false) {
|
|
137
|
-
globalDataInstance.showMessage({ content: result.message || '保存成功' });
|
|
138
|
-
}
|
|
139
|
-
if (this.requestSaveInfoConfig?.onSaveAfter) {
|
|
140
|
-
this.requestSaveInfoConfig.onSaveAfter(result);
|
|
141
|
-
}
|
|
142
|
-
const saveSuccessNavigate = this.requestSaveInfoConfig?.saveSuccessNavigate;
|
|
143
|
-
if (saveSuccessNavigate) {
|
|
144
|
-
if (saveSuccessNavigate === 'navigateBack') {
|
|
145
|
-
navigate.navigateBack();
|
|
146
|
-
} else if (typeof saveSuccessNavigate === 'function') {
|
|
147
|
-
saveSuccessNavigate(result);
|
|
148
|
-
} else if (typeof saveSuccessNavigate === 'number') {
|
|
149
|
-
navigate.navigateBack({ delta: saveSuccessNavigate });
|
|
150
|
-
} else if (typeof saveSuccessNavigate === 'string') {
|
|
151
|
-
navigate.navigateTo({ url: saveSuccessNavigate });
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
} else if (this.requestSaveInfoConfig?.onSaveError) {
|
|
155
|
-
this.requestSaveInfoConfig.onSaveError(result);
|
|
156
|
-
}
|
|
157
|
-
} catch (error) {
|
|
158
|
-
console.log(error);
|
|
159
|
-
this.updatedLoading(false);
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export interface PageInfoDataOptions<T extends PageInfoDataInstanceState = PageInfoDataInstanceState> {
|
|
165
|
-
/**详情查询请求配置*/
|
|
166
|
-
requestInfoConfig?: PageInfoDataInstance<T>['requestInfoConfig'];
|
|
167
|
-
/**详情保存请求配置*/
|
|
168
|
-
requestSaveInfoConfig?: PageInfoDataInstance<T>['requestSaveInfoConfig'];
|
|
169
|
-
/**初始值*/
|
|
170
|
-
initialValues?: Partial<T>;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* 页面级数据状态管理
|
|
175
|
-
*/
|
|
176
|
-
export const usePageInfoData = <T extends PageInfoDataInstanceState = PageInfoDataInstanceState>(
|
|
177
|
-
options: PageInfoDataOptions<T> = {},
|
|
178
|
-
) => {
|
|
179
|
-
const pageDataInstance = useRef(new PageInfoDataInstance<T>(options)).current;
|
|
180
|
-
const store = useSnapshot(pageDataInstance.store) as T;
|
|
181
|
-
return [store, pageDataInstance, store.__defaultValue] as [T, PageInfoDataInstance<T>, string | undefined];
|
|
182
|
-
};
|