pageUtils.ts
3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { history } from 'umi';
const eq = (source: any, comparison: any) => {
if (Object.keys(source).length !== Object.keys(comparison).length) {
return false;
}
return (
JSON.stringify(source) === JSON.stringify({ ...source, ...comparison })
);
};
/**
* URL参数设置
* @param params
* @param form `import {useTable} from 'table-render'; useTable()`
*/
const urlParamSet = (params: any = {}) => {
const urlParams = {};
Object.keys(params).forEach((key: string) => {
urlParams[key] = params[key]?.toString() ?? '';
});
if (eq(history.location.query, urlParams)) {
return;
}
// const historyType = history.location.search ? 'push' : 'replace';
history['replace']({
query: urlParams,
});
};
/**
* 分页参数名重设
* `current` -> pageNum
* @param params
*/
const paginationSet = (params: any) => {
if (JSON.parse(JSON.stringify(params)).hasOwnProperty('current')) {
// eslint-disable-next-line no-param-reassign
params.pageNum = params.current;
// eslint-disable-next-line no-param-reassign
delete params.current;
}
};
export function paramsProcess(
params: any,
settings?: any,
resetQuery: boolean = true,
) {
let query = { ...params };
Object.keys(params)
.filter((key) => params[key] === '')
.forEach((key) => delete query[key]);
if (settings && Object.keys(settings).length) {
Object.keys(params)
.filter(
(key) => settings.hasOwnProperty(key) && settings[key].type === 'range',
)
.forEach((key) => {
delete query[key];
[query[`${key}Start`], query[`${key}End`]] = params[key];
});
} else {
query = params;
}
paginationSet(query);
if (resetQuery) {
urlParamSet(query);
}
return query;
}
export function resetQueryParams(params: any, settings: any) {
const restParams = {};
Object.keys(params)
.filter(
(key) => key !== 'pageNum' && key !== 'pageSize' && key !== 'current',
)
.forEach((key) => {
let flag = '';
if (key.endsWith('Start')) {
flag = 'start';
} else if (key.endsWith('End')) {
flag = 'end';
}
let propertyKey = key;
if (!settings.hasOwnProperty(propertyKey)) {
if (!flag) {
return;
}
propertyKey =
flag === 'start'
? propertyKey.slice(0, -5)
: propertyKey.slice(0, -3);
if (!settings.hasOwnProperty(propertyKey)) {
return;
}
}
const querySetting = settings[propertyKey];
if (querySetting.type === 'boolean') {
restParams[propertyKey] = params[key].toString() === 'true';
} else if (
querySetting.type === 'number' ||
querySetting.type === 'integer'
) {
restParams[propertyKey] = parseFloat(params[key].toString());
} else if (querySetting.type === 'range') {
const range = restParams.hasOwnProperty(propertyKey)
? restParams[propertyKey]
: ['', ''];
if (flag === 'start') {
range[0] = params[key];
} else {
range[1] = params[key];
}
restParams[propertyKey] = range;
} else {
restParams[propertyKey] = params[key];
}
});
return restParams;
}
/**
* 将 query 重置为默认字段
*/
export function resetToSysFields(query: Record<string, any> = {}) {
const params = {};
//重置后需要保留的字段
const FIXED_FIELDS = [
'_code',
'appId',
'pageSize',
'viewCode',
'viewType',
'_filter',
];
Object.keys(query).forEach((key) => {
if (FIXED_FIELDS.indexOf(key) > -1 && query[key]) {
params[key] = query[key];
}
});
return params;
}