@fairys/taro-tools-react 0.0.12 → 1.0.0
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.
|
@@ -58,6 +58,8 @@ export declare class PageDataInstance<T extends PageDataInstanceState = PageData
|
|
|
58
58
|
}>;
|
|
59
59
|
/**请求之后处理返回值进行存储*/
|
|
60
60
|
onAfter?: (data: Record<string, any>, instance: PageDataInstance<T>) => Partial<T>;
|
|
61
|
+
/**额外数据处理*/
|
|
62
|
+
onExtraData?: (data: Record<string, any>, instance: PageDataInstance<T>) => Record<string, any>;
|
|
61
63
|
/** code!==1 时 触发*/
|
|
62
64
|
onError?: (data: Record<string, any>, instance: PageDataInstance<T>) => void;
|
|
63
65
|
/**重置获取值的方法*/
|
|
@@ -124,6 +126,8 @@ export interface PageDataInstanceContextProviderProps<T extends PageDataInstance
|
|
|
124
126
|
getList?: PageDataInstance<T>['getList'];
|
|
125
127
|
/**请求之后处理返回值进行存储*/
|
|
126
128
|
onAfter?: PageDataInstance<T>['onAfter'];
|
|
129
|
+
/** 额外数据处理*/
|
|
130
|
+
onExtraData?: PageDataInstance<T>['onExtraData'];
|
|
127
131
|
/** code!== 200 时 触发*/
|
|
128
132
|
onError?: PageDataInstance<T>['onError'];
|
|
129
133
|
/**获取弹框内重置参数*/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
2
|
import { proxy, ref as external_valtio_ref, useSnapshot } from "valtio";
|
|
3
3
|
import { ProxyInstanceObjectBase } from "../utils/valtio/instance.js";
|
|
4
|
-
import { createContext, useContext, useMemo, useRef } from "react";
|
|
4
|
+
import { createContext, useContext, useEffect, useMemo, useRef } from "react";
|
|
5
5
|
import { globalSettingDataInstance } from "./global.setting.data.instance.js";
|
|
6
6
|
import taro, { useDidShow } from "@tarojs/taro";
|
|
7
7
|
class page_data_instance_PageDataInstance extends ProxyInstanceObjectBase {
|
|
@@ -12,6 +12,7 @@ class page_data_instance_PageDataInstance extends ProxyInstanceObjectBase {
|
|
|
12
12
|
onBefore;
|
|
13
13
|
getList;
|
|
14
14
|
onAfter;
|
|
15
|
+
onExtraData;
|
|
15
16
|
onError;
|
|
16
17
|
getResetValues;
|
|
17
18
|
defaultQuery = {};
|
|
@@ -97,7 +98,7 @@ class page_data_instance_PageDataInstance extends ProxyInstanceObjectBase {
|
|
|
97
98
|
let totalField = 'total';
|
|
98
99
|
let selectedRowsField = 'selectedRows';
|
|
99
100
|
let selectedRowKeysField = 'selectedRowKeys';
|
|
100
|
-
let _request;
|
|
101
|
+
let _request = this.getList;
|
|
101
102
|
let defaultQuery = {};
|
|
102
103
|
if (this.store.isTabs) {
|
|
103
104
|
const tabKey = this.store.tabKey;
|
|
@@ -110,7 +111,7 @@ class page_data_instance_PageDataInstance extends ProxyInstanceObjectBase {
|
|
|
110
111
|
totalField = `${tabKey}Total`;
|
|
111
112
|
selectedRowsField = `${tabKey}SelectedRows`;
|
|
112
113
|
selectedRowKeysField = `${tabKey}SelectedRowKeys`;
|
|
113
|
-
}
|
|
114
|
+
}
|
|
114
115
|
if (!_request) return void console.error("\u672A\u914D\u7F6E getList \u8BF7\u6C42\u65B9\u6CD5,\u8BF7\u68C0\u67E5\u662F\u5426\u914D\u7F6E\u4E86 getList \u65B9\u6CD5");
|
|
115
116
|
taro.showLoading({
|
|
116
117
|
title: "\u52A0\u8F7D\u4E2D..."
|
|
@@ -146,11 +147,18 @@ class page_data_instance_PageDataInstance extends ProxyInstanceObjectBase {
|
|
|
146
147
|
[dataListField]: newDataList,
|
|
147
148
|
[totalField]: result?.data?.total || 0
|
|
148
149
|
};
|
|
149
|
-
if (1 === this.store[pageField]) {
|
|
150
|
+
if (1 === this.store[pageField] || !this.is_scroll_page) {
|
|
150
151
|
saveData[selectedRowsField] = external_valtio_ref([]);
|
|
151
152
|
saveData[selectedRowKeysField] = external_valtio_ref([]);
|
|
152
153
|
}
|
|
153
154
|
}
|
|
155
|
+
if (this.onExtraData) {
|
|
156
|
+
const _temps = this.onExtraData(result, this);
|
|
157
|
+
saveData = {
|
|
158
|
+
...saveData,
|
|
159
|
+
..._temps || {}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
154
162
|
if (saveData) this._setValues(saveData);
|
|
155
163
|
} else if (this.onError) this.onError(result, this);
|
|
156
164
|
} catch (error) {
|
|
@@ -264,11 +272,12 @@ const usePageDataInstanceContext = ()=>{
|
|
|
264
272
|
return PageDataInstance;
|
|
265
273
|
};
|
|
266
274
|
function PageDataInstanceContextProvider(props) {
|
|
267
|
-
const { instance, children, initialValues, is_scroll_page, defaultTabKey, tabItems, defaultPageSize, onBefore, getList, onAfter, onError, getResetValues, defaultQuery, codeFields, valueFields, isMountLoad, title } = props;
|
|
275
|
+
const { instance, children, initialValues, is_scroll_page, defaultTabKey, tabItems, defaultPageSize, onBefore, getList, onAfter, onExtraData, onError, getResetValues, defaultQuery, codeFields, valueFields, isMountLoad, title } = props;
|
|
268
276
|
const pageInstance = usePageDataInstance(instance);
|
|
269
277
|
instance.onBefore = onBefore;
|
|
270
278
|
instance.getList = getList;
|
|
271
279
|
instance.onAfter = onAfter;
|
|
280
|
+
instance.onExtraData = onExtraData;
|
|
272
281
|
instance.onError = onError;
|
|
273
282
|
instance.getResetValues = getResetValues;
|
|
274
283
|
instance.codeFields = codeFields;
|
|
@@ -283,7 +292,7 @@ function PageDataInstanceContextProvider(props) {
|
|
|
283
292
|
}), [
|
|
284
293
|
pageInstance
|
|
285
294
|
]);
|
|
286
|
-
|
|
295
|
+
useEffect(()=>{
|
|
287
296
|
if (isMountLoad) pageInstance.main_onSearch();
|
|
288
297
|
}, []);
|
|
289
298
|
useDidShow(()=>{
|
package/esm/utils/request.js
CHANGED
|
@@ -92,6 +92,8 @@ class RequestInstance {
|
|
|
92
92
|
};
|
|
93
93
|
};
|
|
94
94
|
formatUrl = (url, module)=>{
|
|
95
|
+
if (!url) throw new Error("\u8BF7\u6C42\u7684url\u5FC5\u586B");
|
|
96
|
+
if (url && (url.startsWith('http://') || url.startsWith('https://'))) return url;
|
|
95
97
|
let { host, url: _url } = this.getProxyHost(url, module);
|
|
96
98
|
if (host) host = host.replace(/\/$/, '');
|
|
97
99
|
const newUrl = `${_url}`.replace(/^\//, '').replace(/\/$/, '');
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"author": "SunLxy <1011771396@qq.com>",
|
|
4
4
|
"description": "框架组件库",
|
|
5
5
|
"homepage": "https://github.com/autumn-fairy-tales/fairys-taro-react",
|
|
6
|
-
"version": "0.0
|
|
6
|
+
"version": "1.0.0",
|
|
7
7
|
"main": "lib/index.js",
|
|
8
8
|
"types": "esm/index.d.ts",
|
|
9
9
|
"module": "esm/index.js",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { proxy, ref, useSnapshot } from 'valtio';
|
|
2
2
|
import { ProxyInstanceObjectBase } from 'utils/valtio/instance';
|
|
3
|
-
import { createContext, useContext, useMemo, useRef } from 'react';
|
|
3
|
+
import { createContext, useContext, useEffect, useMemo, useRef } from 'react';
|
|
4
4
|
import { globalSettingDataInstance } from './global.setting.data.instance';
|
|
5
5
|
import Taro, { useDidShow } from '@tarojs/taro';
|
|
6
6
|
|
|
@@ -63,6 +63,8 @@ export class PageDataInstance<
|
|
|
63
63
|
getList?: (payload: any, instance: PageDataInstance<T>) => Promise<{ code?: number; data?: any; message?: string }>;
|
|
64
64
|
/**请求之后处理返回值进行存储*/
|
|
65
65
|
onAfter?: (data: Record<string, any>, instance: PageDataInstance<T>) => Partial<T>;
|
|
66
|
+
/**额外数据处理*/
|
|
67
|
+
onExtraData?: (data: Record<string, any>, instance: PageDataInstance<T>) => Record<string, any>;
|
|
66
68
|
/** code!==1 时 触发*/
|
|
67
69
|
onError?: (data: Record<string, any>, instance: PageDataInstance<T>) => void;
|
|
68
70
|
/**重置获取值的方法*/
|
|
@@ -173,7 +175,7 @@ export class PageDataInstance<
|
|
|
173
175
|
let totalField = 'total';
|
|
174
176
|
let selectedRowsField = 'selectedRows';
|
|
175
177
|
let selectedRowKeysField = 'selectedRowKeys';
|
|
176
|
-
let _request: Function | undefined;
|
|
178
|
+
let _request: Function | undefined = this.getList;
|
|
177
179
|
/**默认请求参数*/
|
|
178
180
|
let defaultQuery = {};
|
|
179
181
|
if (this.store.isTabs) {
|
|
@@ -189,8 +191,6 @@ export class PageDataInstance<
|
|
|
189
191
|
totalField = `${tabKey}Total`;
|
|
190
192
|
selectedRowsField = `${tabKey}SelectedRows`;
|
|
191
193
|
selectedRowKeysField = `${tabKey}SelectedRowKeys`;
|
|
192
|
-
} else {
|
|
193
|
-
_request = this.getList;
|
|
194
194
|
}
|
|
195
195
|
if (!_request) {
|
|
196
196
|
console.error('未配置 getList 请求方法,请检查是否配置了 getList 方法');
|
|
@@ -233,11 +233,15 @@ export class PageDataInstance<
|
|
|
233
233
|
[totalField]: result?.data?.total || 0,
|
|
234
234
|
};
|
|
235
235
|
// 第一页清理
|
|
236
|
-
if (this.store[pageField] === 1) {
|
|
236
|
+
if (this.store[pageField] === 1 || !this.is_scroll_page) {
|
|
237
237
|
saveData[selectedRowsField] = ref([]);
|
|
238
238
|
saveData[selectedRowKeysField] = ref([]);
|
|
239
239
|
}
|
|
240
240
|
}
|
|
241
|
+
if (this.onExtraData) {
|
|
242
|
+
const _temps = this.onExtraData(result, this);
|
|
243
|
+
saveData = { ...saveData, ...(_temps || {}) };
|
|
244
|
+
}
|
|
241
245
|
if (saveData) this._setValues(saveData);
|
|
242
246
|
} else if (this.onError) {
|
|
243
247
|
this.onError(result, this);
|
|
@@ -396,6 +400,8 @@ export interface PageDataInstanceContextProviderProps<T extends PageDataInstance
|
|
|
396
400
|
getList?: PageDataInstance<T>['getList'];
|
|
397
401
|
/**请求之后处理返回值进行存储*/
|
|
398
402
|
onAfter?: PageDataInstance<T>['onAfter'];
|
|
403
|
+
/** 额外数据处理*/
|
|
404
|
+
onExtraData?: PageDataInstance<T>['onExtraData'];
|
|
399
405
|
/** code!== 200 时 触发*/
|
|
400
406
|
onError?: PageDataInstance<T>['onError'];
|
|
401
407
|
/**获取弹框内重置参数*/
|
|
@@ -427,6 +433,7 @@ export function PageDataInstanceContextProvider<T extends PageDataInstanceState
|
|
|
427
433
|
onBefore,
|
|
428
434
|
getList,
|
|
429
435
|
onAfter,
|
|
436
|
+
onExtraData,
|
|
430
437
|
onError,
|
|
431
438
|
getResetValues,
|
|
432
439
|
defaultQuery,
|
|
@@ -440,6 +447,7 @@ export function PageDataInstanceContextProvider<T extends PageDataInstanceState
|
|
|
440
447
|
instance.onBefore = onBefore;
|
|
441
448
|
instance.getList = getList;
|
|
442
449
|
instance.onAfter = onAfter;
|
|
450
|
+
instance.onExtraData = onExtraData;
|
|
443
451
|
instance.onError = onError;
|
|
444
452
|
instance.getResetValues = getResetValues;
|
|
445
453
|
instance.codeFields = codeFields;
|
|
@@ -451,7 +459,7 @@ export function PageDataInstanceContextProvider<T extends PageDataInstanceState
|
|
|
451
459
|
[pageInstance],
|
|
452
460
|
);
|
|
453
461
|
|
|
454
|
-
|
|
462
|
+
useEffect(() => {
|
|
455
463
|
if (isMountLoad) {
|
|
456
464
|
pageInstance.main_onSearch();
|
|
457
465
|
}
|
package/src/utils/request.ts
CHANGED
|
@@ -178,6 +178,14 @@ export class RequestInstance {
|
|
|
178
178
|
|
|
179
179
|
/**格式化地址*/
|
|
180
180
|
formatUrl = (url: string, module?: string) => {
|
|
181
|
+
if (!url) {
|
|
182
|
+
throw new Error('请求的url必填');
|
|
183
|
+
}
|
|
184
|
+
/**如果是http或者https开头的,直接返回*/
|
|
185
|
+
if (url && (url.startsWith('http://') || url.startsWith('https://'))) {
|
|
186
|
+
return url;
|
|
187
|
+
}
|
|
188
|
+
|
|
181
189
|
let { host, url: _url } = this.getProxyHost(url, module);
|
|
182
190
|
if (host) {
|
|
183
191
|
host = host.replace(/\/$/, '');
|