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))