修改上传图片安全验证问题

This commit is contained in:
张成
2026-02-14 12:59:21 +08:00
parent 8688b6b82d
commit 64f0267457
54 changed files with 239 additions and 240 deletions

View File

@@ -39,19 +39,28 @@ export interface uploadFileResponseData {
updated_at: string,
}
// 从上传错误中取出可展示的文案
function get_upload_error_msg(error: any): string {
if (!error) return "上传失败";
const msg =
error?.message ||
(typeof error?.error === "string" ? error.error : error?.error?.message) ||
(error?.data?.message ?? error?.data?.msg) ||
"";
return (msg && String(msg).trim()) || "上传失败";
}
// 发布球局类
class UploadApi {
async upload(req: UploadFilesData): Promise<{ id: string, data: uploadFileResponseData }> {
let fullUrl = `${envConfig.apiBaseURL}/api/gallery/upload`
const authHeader = tokenManager.getAuthHeader()
const { id, ...rest } = req
const fullUrl = `${envConfig.apiBaseURL}/api/gallery/upload`;
const authHeader = tokenManager.getAuthHeader();
const { id, ...rest } = req;
try {
const res = await Taro.uploadFile({
url: fullUrl,
filePath: rest.filePath,
name: 'file',
name: "file",
formData: {
description: rest.description,
tags: rest.tags,
@@ -59,12 +68,17 @@ class UploadApi {
},
header: authHeader,
});
return {
id,
data: JSON.parse(res.data).data,
const parsed = JSON.parse(res.data);
if (parsed.code !== 0) {
const msg = get_upload_error_msg(parsed);
Taro.showToast({ title: msg, icon: "none" });
throw new Error(msg);
}
return { id, data: parsed.data };
} catch (error) {
throw { id, error }
const msg = get_upload_error_msg(error);
Taro.showToast({ title: msg, icon: "none" });
throw { id, error };
}
}
@@ -103,7 +117,7 @@ class UploadApi {
throw new Error(result.message || '上传失败');
}
} catch (error) {
console.error('上传图片失败:', error);
console.warn('上传图片失败:', error);
throw error;
}
}