diff --git a/core/request/request.js b/core/request/request.js index 227b5fc..ba4bd12 100644 --- a/core/request/request.js +++ b/core/request/request.js @@ -113,13 +113,45 @@ export default class Request { mergeUrl += mergeUrl.indexOf('?') === -1 ? `?${query}` : `&${query}` } options.url = mergeUrl + // 硬超时兜底:uni.request 的 timeout 在弱网/部分安卓 WebView 下可能不触发, + // 用独立定时器保证请求 Promise 必定 settle,超时主动 abort 并走拦截器,避免界面无限卡住 + const timeoutMs = Number(options.timeout) > 0 ? Number(options.timeout) : 20000; + let settled = false; + let task = null; + const timer = setTimeout(() => { + if (settled) return; + settled = true; + if (task && typeof task.abort === 'function') { + try { task.abort(); } catch (e) {} + } + // 构造符合响应拦截器预期的结构,使 toastAfter/隐藏 loading/错误提示链路完整生效 + const timeoutRes = { + errMsg: `request:fail timeout(${timeoutMs}ms)`, + statusCode: 0, + header: {}, + data: { flag: 'failure', message: '网络超时,请检查网络后重试' } + }; + let handled = timeoutRes; + try { + handled = this.requestAfter(timeoutRes); + } catch (e) {} + reject(handled); + }, timeoutMs + 2000); + const settle = (fn, value) => { + if (settled) return; + settled = true; + clearTimeout(timer); + fn(value); + }; options.success = res => { - resolve(this.requestAfter(res)) + settle(resolve, this.requestAfter(res)) } options.fail = err => { - reject(this.requestAfter(err)) + const handled = this.requestAfter(err); + // 原生失败(超时/断网/abort)时透传原始 errMsg,便于调用方识别失败原因 + settle(reject, handled === undefined || handled === null ? err : handled) } - uni.request(options) + task = uni.request(options) }) } diff --git a/pages/packing/packMode.vue b/pages/packing/packMode.vue index e82f62f..6f6fa91 100644 --- a/pages/packing/packMode.vue +++ b/pages/packing/packMode.vue @@ -81,6 +81,10 @@ + + + 打包上报失败,扫描商品条码或长按商品重新上报 + {{ orderQueryIsSandan ? '运单' : '波次' }} {{ orderQueryIsSandan ? orderWaybillNo : orderWaveNo }} @@ -307,6 +311,29 @@ + + + + 打包上报失败 + + {{ retryPopupText }} + + + 关闭 + + {{ retrySubmitting ? '重新上传中...' : '重新上传' }} + + + + + 0 && this.groupVerifiedCount < items.length; }, + // 团购提交失败待重传:全部商品已核实、已自动提交过但无进行中打包任务(即上次上报未成功) + groupPackRetryReady() { + if (this.orderQueryIsSandan) return false; + if (!this.orderQueryResult) return false; + if (!this.groupAllVerified) return false; + if (!this.groupAutoSubmitted) return false; + if (this.hasActivePack) return false; + return !this.groupSubmitting; + }, // 输入框占位:按实际流程提示(未绑定操作员 → 扫工牌绑定;团购核实中 → 扫商品条码;否则 → 运单查询) orderScanPlaceholder() { if (!this.boundOperators.length) return '扫描/输入工牌绑定操作员'; + if (this.groupPackRetryReady) return '扫描商品条码重新上报'; if (this.groupVerifyingActive && !this.orderBlocked) return '扫描商品条码核实'; return '扫描/输入运单号查询'; }, @@ -859,9 +902,16 @@ export default { this.handleBadgeScan(c); return; } + // 团购提交失败待重传:扫描任一商品条码直接重传开始打包(保持核实进度,不重新查单) + if (this.groupPackRetryReady && this.matchesAnyGroupItemCodeAny(c)) { + this.retryGroupPackSubmit(); + return; + } // 与上一次扫描内容完全一致 → 跳过(防止运单/商品条码重复触发查询/核实) if (this.lastScannedCode === c) return; this.lastScannedCode = c; + // 手动扫码重新查单:重置打包上下文 key 失效自动重查计数(用户主动换单/重扫视为新的一轮) + this.packKeyRetryCount = 0; if (!this.boundOperators.length) { // 未绑定操作员:输入/扫描值走绑定操作员流程(识别工牌 → 自动绑定,识别失败由 queryOperatorUser 提示) this.opScanCode = c; @@ -935,6 +985,18 @@ export default { } return false; }, + // 判断条码是否为当前团购订单中任一商品的可扫条码(含已核实商品,重传触发用) + matchesAnyGroupItemCodeAny(code) { + const c = String(code || '').trim(); + if (!c || this.orderQueryIsSandan || !this.orderQueryResult) return false; + const items = this.groupItems; + for (let i = 0; i < items.length; i++) { + if (this.groupItemCodes(items[i]).indexOf(c) !== -1) { + return true; + } + } + return false; + }, // 商品是否需要复核:need_recheck 为 'N' 视为免验(无需复核),其余(含缺省)按需要复核(Y) isItemNeedRecheck(item) { const v = String((item && item.need_recheck) || 'Y').trim().toUpperCase(); @@ -1016,6 +1078,11 @@ export default { // 长按复核:团单任意未核实商品可通过“无条码复核”人工标记已核实 handleGroupItemLongPress(item, idx) { if (this.orderQueryIsSandan) return; + // 提交失败待重传:长按任一商品直接重传开始打包 + if (this.groupPackRetryReady) { + this.retryGroupPackSubmit(); + return; + } if (this.isItemVerified(item, idx)) return; const name = String(item.item_name || '该商品').trim(); const qty = Math.max(1, Number(item.plan_qty) || 1); @@ -1081,6 +1148,81 @@ export default { this.groupSubmitting = false; } }, + // 打包上报失败后的手动重传:扫描商品条码/长按商品触发(保持核实状态,凭原 key 重新上报) + retryGroupPackSubmit() { + if (!this.groupPackRetryReady) return; + this.speakText('重新上报开始打包'); + uni.showToast({ title: '重新上报开始打包', icon: 'none', duration: 2000 }); + this.handleSubmitGroupPack(); + }, + // 提交开始打包服务器返回错误时:按最近查询条码自动重查订单获取新 key 并重新提交 + async retryQueryForNewKey(failText = '') { + if (this.packKeyRetrying) return; + // 自动重查仅尝试一次,避免反复失败造成死循环;再次失败改为弹窗提示 + if (this.packKeyRetryCount >= 1) { + this.openRetryPopup(failText || '打包上报失败,请重新扫码查询'); + return; + } + this.packKeyRetrying = true; + try { + this.packKeyRetryCount += 1; + const r = this.orderQueryResult; + const barcode = String((r && (r.waybill_code || r.barcode)) || this.orderBarcode || '').trim(); + if (!barcode) { + // 无条码可重查:提示手动重新扫码 + this.openRetryPopup(failText || '打包上报失败,请重新扫码查询'); + return; + } + // 重查前记录团购核实进度(重查会重置核实状态,成功后恢复并重新提交) + const wasGroupAllVerified = !this.orderQueryIsSandan && this.groupAllVerified; + this.speakText('上报失败,重新查询订单'); + uni.showToast({ title: '上报失败,正在重新查询订单', icon: 'none', duration: 2500 }); + await this.queryOrder(barcode); + if (this.orderQueryState !== 'success' || !this.orderQueryResult) { + // 重查失败:弹出重传弹窗提示手动处理 + this.openRetryPopup('重新查询订单失败,请检查网络后重新扫码'); + return; + } + if (!this.orderQueryIsSandan && wasGroupAllVerified && this.groupItems.length) { + // 团购:恢复全部商品已核实并自动重新提交开始打包 + const items = this.groupItems; + const verified = {}; + items.forEach((it, i) => { + verified[this.groupItemKey(it, i)] = Math.max(1, Number(it?.plan_qty) || 1); + }); + // 先置 groupAutoSubmitted 再写 groupVerified,避免 groupAllVerified 变化触发重复提交 + this.groupAutoSubmitted = true; + this.groupVerified = verified; + this.handleSubmitGroupPack(); + } + // 散单:queryOrder 成功后已自动上报;无商品明细团购也已自动提交,无需额外处理 + } finally { + this.packKeyRetrying = false; + } + }, + // 打开打包上报失败弹窗(可点击「重新上传」重传) + openRetryPopup(text) { + this.retryPopupText = String(text || '打包上报失败,请重新上传'); + this.retryPopupOpen = true; + this.speakText('打包上报失败'); + }, + // 关闭重新上传弹窗(用户选择重新扫码等其它操作) + handleCloseRetryPopup() { + this.retryPopupOpen = false; + }, + // 弹窗「重新上传」:重传开始打包,成功后自动关闭弹窗 + async handleRetryPackReport() { + if (this.retrySubmitting) return; + this.retrySubmitting = true; + try { + const ok = await this.reportPackOrder(); + if (ok) { + this.retryPopupOpen = false; + } + } finally { + this.retrySubmitting = false; + } + }, // 顶部操作员姓名按钮:打开操作员弹窗(展示在线成员列表,支持扫码追加绑定 / 下线成员) handleUserInfo() { this.userDrawerOpen = true; @@ -1187,7 +1329,8 @@ export default { uni.showLoading({ title: '查询中...', mask: true }); try { // toastAfter=false:失败提示由本方法自行控制,避免拦截器重复弹 toast - const res = await this.$http('workbench.queryOrder', { token, barcode: code }, '', false); + // 弱网下显式 15s 超时,配合请求层硬超时兜底,失败后界面必定恢复可操作 + const res = await this.$http('workbench.queryOrder', { token, barcode: code }, '', false, 'text', 15000); const invalid = res?.flag === 'failure' || [0, 401, 403].indexOf(Number(res?.code)) !== -1; if (invalid) { // 401:工作台 token 失效,退出工作台模式并清除 token 回登录页 @@ -1235,6 +1378,8 @@ export default { // 先置 groupAutoSubmitted 再写 groupVerified,避免 groupAllVerified 变化触发重复提交打包 this.groupAutoSubmitted = true; this.groupVerified = dupVerified; + // 新查询进入另一订单:关闭打包上报失败弹窗 + this.retryPopupOpen = false; this.speakText('正在打包'); uni.showToast({ title: '订单打包中,请勿重复上报', icon: 'none' }); return; @@ -1243,6 +1388,8 @@ export default { this.orderQueryState = 'success'; // 新查询正常进入,清除恢复态 this.packRestored = false; + // 新查询进入另一订单:关闭打包上报失败弹窗 + this.retryPopupOpen = false; // 打包上下文 key:packOrder 凭 key 由服务端解析订单信息,前端不回传订单数据 this.packContextKey = String(data?.key || '').trim(); // 查询成功:清空输入框,方便连续扫描下一单 @@ -1271,7 +1418,10 @@ export default { } catch (error) { console.error('[packMode] 查询订单失败:', error); this.orderQueryState = 'error'; - this.orderQueryError = error?.message || '查询失败,请重试'; + const errMsg = String(error && (error.message || error.errMsg) || ''); + this.orderQueryError = errMsg.indexOf('timeout') !== -1 || errMsg.indexOf('超时') !== -1 + ? '网络超时,请检查网络后重试' + : (error?.message || '查询失败,请重试'); } finally { uni.hideLoading(); this.orderQuerying = false; @@ -1402,17 +1552,14 @@ export default { } // toastAfter=false:失败提示由本方法自行控制 - const res = await this.$http('workbench.packOrder', payload, '', false); + // 弱网下显式 15s 超时,配合请求层硬超时兜底,失败后界面必定恢复可操作 + const res = await this.$http('workbench.packOrder', payload, '', false, 'text', 15000); const invalid = res?.flag === 'failure' || [0, 400, 401, 403].indexOf(Number(res?.code)) !== -1; if (invalid) { // 401:工作台 token 失效,退出工作台模式并清除 token 回登录页 if (this.handleWorkbenchAuthExpired(res?.code)) return false; - // 400:打包上下文 key 缺失或过期(跨仓库/跨工作台消费他人 key 为 403),提示重新扫码 - if (Number(res?.code) === 400) { - uni.showToast({ title: res?.message || '打包上下文已失效,请重新扫码查询', icon: 'none' }); - return false; - } - uni.showToast({ title: res?.message || '打包上报失败', icon: 'none' }); + // 服务器返回错误(含打包上下文 key 失效等):自动重查订单获取新 key 并重新提交 + this.retryQueryForNewKey(res?.message || ''); return false; } const data = (res && typeof res === 'object' && res.data && typeof res.data === 'object') ? res.data : res; @@ -1438,7 +1585,12 @@ export default { return true; } catch (error) { console.error('[packMode] 打包上报失败:', error); - uni.showToast({ title: error?.message || '打包上报失败', icon: 'none' }); + // 弱网超时/断网时给出明确失败提示并弹出重新上传弹窗 + const errMsg = String(error && (error.message || error.errMsg) || ''); + const title = errMsg.indexOf('timeout') !== -1 || errMsg.indexOf('超时') !== -1 + ? '网络超时,打包上报失败' + : (error?.message || '打包上报失败'); + this.openRetryPopup(title); return false; } finally { uni.hideLoading(); @@ -1452,8 +1604,8 @@ export default { if (this.finishPacking) return { ok: false, busy: true }; this.finishPacking = true; try { - // toastAfter=false:失败提示由调用方自行控制 - const res = await this.$http('workbench.finishPack', { token }, '', false); + // toastAfter=false:失败提示由调用方自行控制(弱网 15s 超时,避免退出时卡住) + const res = await this.$http('workbench.finishPack', { token }, '', false, 'text', 15000); const invalid = res?.flag === 'failure' || [401, 403].indexOf(Number(res?.code)) !== -1; if (invalid) { // 401:工作台 token 失效,退出工作台模式并清除 token 回登录页 @@ -2832,6 +2984,22 @@ export default { color: #ffffff; } +/* 打包上报失败提示条:浅红底红字 */ +.order-summary__row--retry { + background: #fdeceb; + border-radius: 8rpx; + padding: 10rpx 12rpx; + justify-content: center; +} + +.order-summary__retry-text { + font-size: 28rpx; + font-weight: 600; + color: #e64340; + line-height: 1.5; + text-align: center; +} + .order-block { margin-top: 12rpx; background: #f8f9fb; @@ -3461,6 +3629,67 @@ export default { color: #ffffff; } + /* 打包上报失败弹窗(参考取消核实弹窗) */ + .retry-popup { + width: 640rpx; + padding: 40rpx 32rpx 32rpx; + background: #ffffff; + border-radius: 20rpx; + box-sizing: border-box; + } + + .retry-popup__title { + font-size: 42rpx; + font-weight: 600; + color: #e64340; + text-align: center; + margin-bottom: 32rpx; + } + + .retry-popup__body { + margin-bottom: 36rpx; + padding: 24rpx; + background: #f8f9fa; + border-radius: 12rpx; + } + + .retry-popup__text { + font-size: 34rpx; + color: #303133; + line-height: 1.6; + } + + .retry-popup__actions { + display: flex; + align-items: center; + } + + .retry-popup__btn { + flex: 1; + height: 88rpx; + border-radius: 12rpx; + display: flex; + align-items: center; + justify-content: center; + font-size: 36rpx; + font-weight: 500; + } + + .retry-popup__btn:not(:last-child) { + margin-right: 20rpx; + } + + .retry-popup__btn--cancel { + background: #f5f7fa; + color: #606266; + border: 1rpx solid #dcdfe6; + } + + .retry-popup__btn--confirm { + background: #2979ff; + color: #ffffff; + } + /* 退出工作台弹窗(参考取消核实弹窗) */ .exit-popup { width: 640rpx;