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 { 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 { EnumContentType } from '@/enum';
import qs from 'qs'; import qs from 'qs';
@ -18,14 +18,16 @@ export function showError(error: Service.RequestError) {
*/ */
export async function transformRequestData(requestData: any, contentType?: string) { export async function transformRequestData(requestData: any, contentType?: string) {
// application/json类型不处理 // application/json类型不处理
let data = requestData; let data = clearInvalidParameters(requestData);
// let data = requestData;
// form类型转换 // form类型转换
if (contentType === EnumContentType.formUrlencoded) { if (contentType === EnumContentType.formUrlencoded) {
data = qs.stringify(requestData); data = qs.stringify(data);
} }
// form-data类型转换 // form-data类型转换
if (contentType === EnumContentType.formData) { if (contentType === EnumContentType.formData) {
data = await handleFormData(requestData); data = await handleFormData(data);
} }
return data; return data;
@ -67,3 +69,16 @@ async function transformFile(formData: FormData, key: string, file: File[] | Fil
formData.append(key, file); 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() { async initDynamicRoute() {
// 根据用户id来获取用户的路由 // 根据用户id来获取用户的路由
const { userId } = getUserInfo(); const { userId } = getUserInfo();
const { data: routes } = await fetchUserRoutes(userId); const { data: routes } = await fetchUserRoutes({ userId });
// 根据用户返回的路由表来生成真实路由 // 根据用户返回的路由表来生成真实路由
const appRoutes = await createDynamicRoutes(routes); const appRoutes = await createDynamicRoutes(routes);
// 生成侧边菜单 // 生成侧边菜单

View File

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

View File

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