fix: 移动端驳回支持传图、422 报错可读化、修复详情页图片破图

车间的驳回操作不再强制拍照(编号错误等场景无需照片),移动端同步放开。

- utils/request.js: 新增 extractErrorDetail 统一解析后端 detail 的三种形态
  (字符串 / FastAPI 校验错误数组 / 对象),新增 422 拦截分支,并把
  400/403/409 及兜底分支的 `res.data?.detail || x` 全部换掉 —— 原写法在
  detail 是数组时会把整个数组丢给 showToast,弹出 [object Object]
- pages/scan/detail.vue: 驳回弹窗新增与「追加记录」一致的选图/传图 UI,
  doReject 携带 { reason, images }(允许空数组);选图上传流程抽成通用的
  pickAndUploadImages,与追加记录共用
- pages/scan/detail.vue: 新增 imageUrl() 补全后端返回的相对路径
  (/api/v1/upload/files/xxx.jpg 直接给 <image> 会破图),缩略图渲染与
  previewImage 预览均已接入;逻辑与 records.vue / TaskTreeNode.vue 对齐
This commit is contained in:
2026-09-15 10:57:49 +08:00
parent 0d1e45e3fb
commit 6b3f14e8fd
2 changed files with 93 additions and 11 deletions

View File

@ -82,6 +82,39 @@ async function refreshAccessToken() {
});
}
// ============================================================
// 错误文案提取 — 把后端 detail 统一转成能读的中文,杜绝 [object Object]
// ============================================================
//
// FastAPI 的 detail 有三种形态,直接丢给 showToast 会变成 [object Object]:
// 1. 字符串: {"detail": "任务状态为 COMPLETED,无法驳回"}
// 2. 校验错误数组(422): {"detail": [{"loc": ["body","images"], "msg": "Value error, 驳回必须...", "type": "value_error"}]}
// 3. 对象: {"detail": {"msg": "..."}}
export function extractErrorDetail(data, fallback = "") {
const detail = data && data.detail;
if (!detail) return (data && data.msg) || fallback;
if (typeof detail === "string") return detail || fallback;
if (Array.isArray(detail)) {
const msgs = detail.map((item) => {
if (typeof item === "string") return item;
if (!item || typeof item !== "object") return "";
// Pydantic v2 自定义校验器抛的错带 "Value error, " 前缀,读起来噪音大,去掉
const msg = String(item.msg || item.message || "").replace(/^Value error,\s*/, "");
const field = Array.isArray(item.loc)
? item.loc.filter((p) => !["body", "query", "path"].includes(p)).join(".")
: "";
return field && msg ? `${field}: ${msg}` : msg;
}).filter(Boolean);
return msgs.length ? msgs.join(";") : fallback;
}
if (typeof detail === "object") {
return detail.msg || detail.message || JSON.stringify(detail);
}
return String(detail) || fallback;
}
export default function request(options) {
return new Promise((resolve, reject) => {
const url = options.url.startsWith("http") ? options.url : getBaseUrl() + options.url;
@ -137,10 +170,14 @@ export default function request(options) {
return;
}
if (code === 403) uni.showToast({ title: res.data?.detail || "无权操作", icon: "none", duration: 3000 });
else if (code === 400) uni.showToast({ title: res.data?.detail || "请求参数有误", icon: "none", duration: 2500 });
else if (code === 409) uni.showToast({ title: res.data?.detail || "操作冲突", icon: "none", duration: 3000 });
else { const d = res.data?.detail || ""; uni.showToast({ title: d || `请求失败 (${code})`, icon: "none", duration: 3000 }); }
if (code === 403) uni.showToast({ title: extractErrorDetail(res.data, "无权操作"), icon: "none", duration: 3000 });
else if (code === 400) uni.showToast({ title: extractErrorDetail(res.data, "请求参数有误"), icon: "none", duration: 2500 });
else if (code === 409) uni.showToast({ title: extractErrorDetail(res.data, "操作冲突"), icon: "none", duration: 3000 });
else if (code === 422) {
// 表单/参数校验失败(Pydantic)—— detail 是校验项数组,必须解析出 msg 再提示
uni.showToast({ title: extractErrorDetail(res.data, "提交内容不符合要求"), icon: "none", duration: 3000 });
}
else uni.showToast({ title: extractErrorDetail(res.data, `请求失败 (${code})`), icon: "none", duration: 3000 });
reject(res);
},
fail() { uni.showToast({ title: "网络连接失败", icon: "none" }); reject(new Error("network")); },