diff --git a/src/service/http/utils.ts b/src/service/http/utils.ts index cbfc7fb..37b72c6 100644 --- a/src/service/http/utils.ts +++ b/src/service/http/utils.ts @@ -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) { + const result: Record = {}; + for (const key in requestData) { + if (isEmpty(requestData[key]) || isNullOrUnDef(requestData[key])) continue; + result[key] = requestData[key]; + } + return result; +} diff --git a/src/store/modules/route.ts b/src/store/modules/route.ts index ed266a1..1bfbd4d 100644 --- a/src/store/modules/route.ts +++ b/src/store/modules/route.ts @@ -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); // 生成侧边菜单 diff --git a/src/views/test/test1/index.vue b/src/views/test/test1/index.vue index 117c240..9df337c 100644 --- a/src/views/test/test1/index.vue +++ b/src/views/test/test1/index.vue @@ -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; diff --git a/tsconfig.json b/tsconfig.json index 2ec5817..2f1927e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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"] }