-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
122 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { ERROR_MSG_DURATION, ERROR_NO_TIP_STATUS } from '@/config'; | ||
import { isArray, isFile } from '@/utils'; | ||
import { EnumContentType } from '@/enum'; | ||
import qs from 'qs'; | ||
|
||
export function showError(error: Service.RequestError) { | ||
// 如果error不需要提示,则跳过 | ||
const code = Number(error.code); | ||
if (ERROR_NO_TIP_STATUS.includes(code)) return; | ||
|
||
window.console.warn(error.code, error.msg); | ||
window.$message?.error(error.msg, { duration: ERROR_MSG_DURATION }); | ||
} | ||
/** | ||
* 请求数据的转换 | ||
* @param requestData - 请求数据 | ||
* @param contentType - 请求头的Content-Type | ||
*/ | ||
export async function transformRequestData(requestData: any, contentType?: string) { | ||
// application/json类型不处理 | ||
let data = requestData; | ||
// form类型转换 | ||
if (contentType === EnumContentType.formUrlencoded) { | ||
data = qs.stringify(requestData); | ||
} | ||
// form-data类型转换 | ||
if (contentType === EnumContentType.formData) { | ||
data = await handleFormData(requestData); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
async function handleFormData(data: Record<string, any>) { | ||
const formData = new FormData(); | ||
const entries = Object.entries(data); | ||
|
||
entries.forEach(async ([key, value]) => { | ||
const isFileType = isFile(value) || (isArray(value) && value.length && isFile(value[0])); | ||
|
||
if (isFileType) { | ||
await transformFile(formData, key, value); | ||
} else { | ||
formData.append(key, value); | ||
} | ||
}); | ||
|
||
return formData; | ||
} | ||
|
||
/** | ||
* 接口为上传文件的类型时数据转换 | ||
* @param key - 文件的属性名 | ||
* @param file - 单文件或多文件 | ||
*/ | ||
async function transformFile(formData: FormData, key: string, file: File[] | File) { | ||
if (isArray(file)) { | ||
// 多文件 | ||
await Promise.all( | ||
(file as File[]).map((item) => { | ||
formData.append(key, item); | ||
return true; | ||
}) | ||
); | ||
} else { | ||
// 单文件 | ||
formData.append(key, file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
014f728
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ench-admin – ./
ench-admin-git-main-whyhenin.vercel.app
ench-admin.vercel.app
ench-admin-whyhenin.vercel.app