From 1de507810467eaab7d5976ba926b8910cfc54f78 Mon Sep 17 00:00:00 2001 From: ShenQiLun <97694732@qq.com> Date: Fri, 26 Jun 2026 13:04:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:syncBook=E8=AF=B7=E6=B1=82=E5=8A=A0?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=87=8D=E8=AF=95,=E8=A7=A3=E5=86=B3unexpect?= =?UTF-8?q?ed=20end=20of=20stream?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/upload/upload.vue | 66 ++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/pages/upload/upload.vue b/pages/upload/upload.vue index 56b606b..29651dd 100644 --- a/pages/upload/upload.vue +++ b/pages/upload/upload.vue @@ -2161,18 +2161,14 @@ export default { console.log('【syncBook】请求地址:', apiUrl) console.log('【syncBook】请求参数:', params) - const res = await new Promise(function (resolve, reject) { - uni.request({ - url: apiUrl, - method: 'POST', - header: { - 'Content-Type': 'application/x-www-form-urlencoded', - 'Authorization': 'Bearer ' + token - }, - data: formBody, - success: function (r) { resolve(r) }, - fail: function (e) { reject(e) } - }) + const res = await this.requestWithRetry({ + url: apiUrl, + method: 'POST', + header: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'Authorization': 'Bearer ' + token + }, + data: formBody }) console.log('【syncBook】返回值:', res.statusCode, res.data) @@ -2330,18 +2326,14 @@ export default { console.log('【syncBook】请求地址:', apiUrl) console.log('【syncBook】请求参数:', params) - const res = await new Promise(function (resolve, reject) { - uni.request({ - url: apiUrl, - method: 'POST', - header: { - 'Content-Type': 'application/x-www-form-urlencoded', - 'Authorization': 'Bearer ' + token - }, - data: formBody, - success: function (r) { resolve(r) }, - fail: function (e) { reject(e) } - }) + const res = await this.requestWithRetry({ + url: apiUrl, + method: 'POST', + header: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'Authorization': 'Bearer ' + token + }, + data: formBody }) console.log('【syncBook】返回值:', res.statusCode, res.data) @@ -2546,6 +2538,32 @@ export default { } }, + // 带自动重试的请求封装(网络抖动时自动重试2次) + requestWithRetry(options, maxRetries) { + if (maxRetries === undefined) maxRetries = 2 + var that = this + return new Promise(function (resolve, reject) { + var doRequest = function (attempt) { + uni.request({ + url: options.url, + method: options.method, + header: options.header, + data: options.data, + success: function (r) { resolve(r) }, + fail: function (e) { + if (attempt < maxRetries) { + console.log('【请求重试】第' + (attempt + 1) + '次失败,1秒后重试...', e.errMsg) + setTimeout(function () { doRequest(attempt + 1) }, 1000) + } else { + reject(e) + } + } + }) + } + doRequest(0) + }) + }, + // 确认上传时创建/追加波次(首次创建+release,后续release追加) async appendWaveItem(warehouseData, productId, stock, price) { var timestamp = String(Math.floor(Date.now() / 1000))