From c85281736320fe880c6c1bcdcea5934adac73789 Mon Sep 17 00:00:00 2001 From: Blizzard Date: Fri, 31 Jul 2026 08:32:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(fe):=20=E4=BF=AE=E7=A4=BE=E5=8C=BA=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E6=92=91=E7=A0=B4=E7=BD=91=E6=A0=BC=EF=BC=9B=E5=8F=91?= =?UTF-8?q?=E5=B8=96=E9=99=90=203=20=E5=BC=A0=EF=BC=9B=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=81=B6=E5=8F=91=E5=A4=B1=E8=B4=A5=E8=87=AA=E5=8A=A8=E9=87=8D?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - .photo-tile 补 width:100%+min-width:0:微信 image 默认 320x240, 不给宽高会冲破 photo-grid(社区帖子图片"爆炸") - 发帖图片上限从 9 张改成 3 张 - uploadFile 三重兜底:冷启动没 token 先续期、40100 续期重试、 网络/网关瞬时错误自动重试一次(后端 MD5 去重,重传安全) Co-Authored-By: Claude Opus 5 --- pets-fe/app.wxss | 5 +- .../components/bottom-sheet/bottom-sheet.js | 4 +- pets-fe/utils/request.js | 56 ++++++++++++------- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/pets-fe/app.wxss b/pets-fe/app.wxss index b418ae5..3fb35a8 100644 --- a/pets-fe/app.wxss +++ b/pets-fe/app.wxss @@ -391,7 +391,10 @@ page{scrollbar-width:none;-ms-overflow-style:none} .photo-grid{display:grid;grid-template-columns:repeat(3,1fr);gap:var(--sp-1);margin-bottom:var(--sp-3)} .photo-tile{ - aspect-ratio:1;border-radius:var(--r-sm);background:var(--primary-soft); + /* 微信 image 默认 320x240,不显式给宽高会撑破网格。width:100% + min-width:0 + 让它缩进 1fr 格子,aspect-ratio 保持正方形,overflow 裁掉溢出 */ + width:100%;min-width:0;aspect-ratio:1;overflow:hidden; + border-radius:var(--r-sm);background:var(--primary-soft); display:flex;align-items:center;justify-content:center;font-size:56rpx; } diff --git a/pets-fe/components/bottom-sheet/bottom-sheet.js b/pets-fe/components/bottom-sheet/bottom-sheet.js index e059234..9d9c383 100644 --- a/pets-fe/components/bottom-sheet/bottom-sheet.js +++ b/pets-fe/components/bottom-sheet/bottom-sheet.js @@ -539,8 +539,8 @@ Component({ api.getTasks(id).then((tasks) => this.setData({ manageTasks: tasks || [] })).catch(() => {}); }, onPickPostImages() { - const left = 9 - this.data.postImages.length; - if (left <= 0) return wx.showToast({ title: '最多 9 张', icon: 'none' }); + const left = 3 - this.data.postImages.length; + if (left <= 0) return wx.showToast({ title: '最多 3 张', icon: 'none' }); upload .chooseAndUploadImages(left) .then((list) => this.setData({ postImages: this.data.postImages.concat(list) })) diff --git a/pets-fe/utils/request.js b/pets-fe/utils/request.js index bbfe6a5..4a62872 100644 --- a/pets-fe/utils/request.js +++ b/pets-fe/utils/request.js @@ -106,19 +106,34 @@ function request(options, _retried) { }); } -// 文件上传(multipart);token 过期同样先续期再重试一次 +// 文件上传(multipart)。三重兜底,应对「第一次报错、重试就好」的偶发: +// 1) 上传前若还没 token(冷启动竞态),先续期拿到再传; +// 2) token 过期(40100)→ 续期后重试; +// 3) 网络 fail / 网关 5xx(响应非 JSON)等瞬时错误 → 自动重试一次。 +// 后端按 MD5 去重,重复上传同一张图返回同一条记录,重试是安全的。 function uploadFile(filePath, _retried) { return new Promise((resolve, reject) => { - const token = getToken(); - wx.uploadFile({ - url: BASE_URL + '/api/upload', - filePath, - name: 'file', - timeout: 60000, - header: token ? { Authorization: 'Bearer ' + token } : {}, - success(res) { - try { - const body = JSON.parse(res.data); + // 瞬时错误:还没重试过就延后再传一次,否则直接失败 + const softRetry = (err) => { + if (_retried) return reject(err); + setTimeout(() => uploadFile(filePath, true).then(resolve, reject), 500); + }; + const fire = () => { + const token = getToken(); + wx.uploadFile({ + url: BASE_URL + '/api/upload', + filePath, + name: 'file', + timeout: 60000, + header: token ? { Authorization: 'Bearer ' + token } : {}, + success(res) { + let body; + try { + body = JSON.parse(res.data); + } catch (e) { + // 非 JSON 多为网关 5xx / 服务重启窗口,按瞬时错误重试 + return softRetry(new Error('上传失败,请重试')); + } if (body.code === 0) return resolve(body.data); if (body.code === 40100 && !_retried) { return recoverAuth() @@ -126,14 +141,17 @@ function uploadFile(filePath, _retried) { .catch(() => reject(new Error('登录已过期,请重进小程序'))); } reject(new Error(body.message || '上传失败')); - } catch (e) { - reject(new Error('上传响应解析失败')); - } - }, - fail(err) { - reject(new Error((err && err.errMsg) || '上传失败')); - }, - }); + }, + fail(err) { + softRetry(new Error((err && err.errMsg) || '上传失败')); + }, + }); + }; + // 冷启动竞态:点得太快、登录还没完成就上传,先把 token 拿到再传 + if (!getToken() && !_retried) { + return recoverAuth().then(fire, fire); + } + fire(); }); }