fix:syncBook请求加自动重试,解决unexpected end of stream
This commit is contained in:
parent
07d2277664
commit
1de5078104
@ -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))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user