feat(service): 去除请求虚值字段

This commit is contained in:
Coffee-crocodile 2023-02-14 17:34:54 +08:00
parent a1ffb3c884
commit 2dbea4b4cd
4 changed files with 48 additions and 28 deletions

View File

@ -1,5 +1,5 @@
import { ERROR_MSG_DURATION, ERROR_NO_TIP_STATUS } from '@/config';
import { isArray, isFile } from '@/utils';
import { isArray, isFile, isEmpty, isNullOrUnDef } from '@/utils';
import { EnumContentType } from '@/enum';
import qs from 'qs';
@ -18,14 +18,16 @@ export function showError(error: Service.RequestError) {
*/
export async function transformRequestData(requestData: any, contentType?: string) {
// application/json类型不处理
let data = requestData;
let data = clearInvalidParameters(requestData);
// let data = requestData;
// form类型转换
if (contentType === EnumContentType.formUrlencoded) {
data = qs.stringify(requestData);
data = qs.stringify(data);
}
// form-data类型转换
if (contentType === EnumContentType.formData) {
data = await handleFormData(requestData);
data = await handleFormData(data);
}
return data;
@ -67,3 +69,16 @@ async function transformFile(formData: FormData, key: string, file: File[] | Fil
formData.append(key, file);
}
}
/**
*
* @param requestData -
*/
function clearInvalidParameters(requestData: Record<string, any>) {
const result: Record<string, any> = {};
for (const key in requestData) {
if (isEmpty(requestData[key]) || isNullOrUnDef(requestData[key])) continue;
result[key] = requestData[key];
}
return result;
}

View File

@ -80,7 +80,7 @@ export const useRouteStore = defineStore('route-store', {
async initDynamicRoute() {
// 根据用户id来获取用户的路由
const { userId } = getUserInfo();
const { data: routes } = await fetchUserRoutes(userId);
const { data: routes } = await fetchUserRoutes({ userId });
// 根据用户返回的路由表来生成真实路由
const appRoutes = await createDynamicRoutes(routes);
// 生成侧边菜单

View File

@ -142,6 +142,11 @@ const delete2 = () => {
const post = () => {
const params = {
data: '2022-2-2',
data1: [],
data2: {},
data3: '',
data4: null,
data5: undefined,
};
fetachPost(params).then((res) => {
msg.value = res;

View File

@ -1,25 +1,25 @@
{
"compilerOptions": {
"baseUrl": ".",
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"moduleResolution": "Node",
"strict": true,
"jsx": "preserve",
// "sourceMap": true,
"removeComments": false, //
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"lib": ["ESNext", "DOM"],
"skipLibCheck": true,
"paths": {
"~/*": ["./*"],
"@/*": ["./src/*"]
},
"types": ["node", "vite/client", "naive-ui/volar"]
},
// "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"],
"exclude": ["node_modules", "dist"]
"compilerOptions": {
"baseUrl": ".",
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"moduleResolution": "Node",
"strict": true,
"jsx": "preserve",
// "sourceMap": true,
"removeComments": false, //
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"lib": ["ESNext", "DOM"],
"skipLibCheck": true,
"paths": {
"~/*": ["./*"],
"@/*": ["./src/*"]
},
"types": ["node", "vite/client", "naive-ui/volar"]
},
// "include": ["src/**/*.d.ts", "src/**/*.vue", "./*.d.ts"]
"exclude": ["node_modules", "dist"]
}