From e8526e2bef4f2c8bf7b96e1fad90dcdcad2537aa Mon Sep 17 00:00:00 2001 From: liufumingren Date: Wed, 23 Sep 2026 02:22:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=8A=A8=E5=87=BA=E5=BA=93?= =?UTF-8?q?=E8=A7=84=E5=88=99=E6=94=AF=E6=8C=81=E5=BD=93=E5=A4=A9=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E9=99=90=E5=88=B6=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 allow_same_day_outbound(出库当天订单,默认否) - 新增 same_day_outbound_deadline_time(允许当天出库最后时间,默认 06:00;复核时间早于该时间的订单不受关闭影响) - 新增 same_day_outbound_release_time(当天放开时间,默认 22:00;当天到达该时间后放开当天复核订单) - 保存校验:允许当天出库最后时间不能大于当天放开时间 - 出库扫描按 recheck_time 过滤:到达放开时间前,当天最后时间之后复核的订单不自动出库(北京时间边界,UTC 存储换算) --- app/model/store.js | 63 ++++++++++++++ app/schedule/auto_outbound_order.js | 6 ++ app/service/systems/order_auto_outbound.js | 98 ++++++++++++++++++++++ app/service/systems/setting/warehouse.js | 51 ++++++++++- 4 files changed, 217 insertions(+), 1 deletion(-) diff --git a/app/model/store.js b/app/model/store.js index 239afd1..089a1e5 100644 --- a/app/model/store.js +++ b/app/model/store.js @@ -28,6 +28,9 @@ module.exports = app => { */ const getDefaultInventoryWarningRules = () => DEFAULT_INVENTORY_WARNING_RULES.map(rule => ({ ...rule })); + const DEFAULT_SAME_DAY_OUTBOUND_DEADLINE_TIME = '06:00'; + const DEFAULT_SAME_DAY_OUTBOUND_RELEASE_TIME = '22:00'; + const DEFAULT_AUTO_OUTBOUND_RULES = Object.freeze({ enabled: false, require_recheck: true, @@ -36,6 +39,9 @@ module.exports = app => { require_sync: true, require_print: true, require_pick: true, + allow_same_day_outbound: false, + same_day_outbound_deadline_time: '06:00', + same_day_outbound_release_time: '22:00', }); const DEFAULT_CUTOFF_ORDER_TIME = '18:00'; @@ -176,6 +182,52 @@ module.exports = app => { }); }; + /** + * 规范化 HH:mm 时间文本。 + * 缺省或格式非法时回退指定默认值。 + * + * @param {string|undefined|null} value 原始时间文本。 + * @param {string} defaultValue 默认 HH:mm。 + * @return {string} 规范化后的 HH:mm 文本。 + */ + const normalizeHourMinuteTime = (value, defaultValue) => { + if (value === undefined || value === null || value === '') { + return defaultValue; + } + const text = String(value).trim(); + return /^([01]\d|2[0-3]):[0-5]\d$/.test(text) ? text : defaultValue; + }; + + /** + * 将 HH:mm 文本解析为总分钟数(用于时间先后比较)。 + * + * @param {string} timeText HH:mm 文本。 + * @return {number} 总分钟数。 + */ + const parseHourMinuteToMinutes = timeText => { + const match = /^(\d{2}):(\d{2})$/.exec(String(timeText || '').trim()); + if (!match) return 0; + return Number(match[1]) * 60 + Number(match[2]); + }; + + /** + * 规范化「允许当天出库最后时间」配置(HH:mm)。 + * 缺省或格式非法时回退默认 06:00。 + * + * @param {string|undefined|null} value 原始时间文本。 + * @return {string} 规范化后的 HH:mm 文本。 + */ + const normalizeSameDayOutboundDeadlineTime = value => normalizeHourMinuteTime(value, DEFAULT_SAME_DAY_OUTBOUND_DEADLINE_TIME); + + /** + * 规范化「当天放开时间」配置(HH:mm)。 + * 缺省或格式非法时回退默认 22:00。 + * + * @param {string|undefined|null} value 原始时间文本。 + * @return {string} 规范化后的 HH:mm 文本。 + */ + const normalizeSameDayOutboundReleaseTime = value => normalizeHourMinuteTime(value, DEFAULT_SAME_DAY_OUTBOUND_RELEASE_TIME); + /** * 规范化自动化出库规则配置。 * 确保 enabled/require_recheck/require_pack/require_weigh/require_sync/require_print/require_pick 等字段存在且类型正确。 @@ -201,6 +253,14 @@ module.exports = app => { return { ...DEFAULT_AUTO_OUTBOUND_RULES }; } + const sameDayOutboundDeadlineTime = normalizeSameDayOutboundDeadlineTime(parsed.same_day_outbound_deadline_time); + const sameDayOutboundReleaseTime = normalizeSameDayOutboundReleaseTime(parsed.same_day_outbound_release_time); + + // 保存时校验:允许当天出库最后时间不能大于当天放开时间 + if (parseHourMinuteToMinutes(sameDayOutboundDeadlineTime) > parseHourMinuteToMinutes(sameDayOutboundReleaseTime)) { + throw new Error('自动出库规则:允许当天出库最后时间不能大于当天放开时间'); + } + return { enabled: normalizeBooleanFlag(parsed.enabled, 'auto_outbound_rules.enabled') === true, require_recheck: parsed.require_recheck !== false, @@ -209,6 +269,9 @@ module.exports = app => { require_sync: parsed.require_sync !== false, require_print: parsed.require_print !== false, require_pick: parsed.require_pick !== false, + allow_same_day_outbound: normalizeBooleanFlag(parsed.allow_same_day_outbound, 'auto_outbound_rules.allow_same_day_outbound') === true, + same_day_outbound_deadline_time: sameDayOutboundDeadlineTime, + same_day_outbound_release_time: sameDayOutboundReleaseTime, }; }; diff --git a/app/schedule/auto_outbound_order.js b/app/schedule/auto_outbound_order.js index 148a670..5a20f1e 100644 --- a/app/schedule/auto_outbound_order.js +++ b/app/schedule/auto_outbound_order.js @@ -14,6 +14,12 @@ * 6. 已拣货:pick_status = 'fin' * * 各条件是否必须可由仓库配置独立控制。 + * + * 当天订单限制(allow_same_day_outbound,默认 false): + * 未开启时,在当天放开时间(same_day_outbound_release_time,默认北京时间 22:00)前, + * 北京时间当天「允许当天出库最后时间」(same_day_outbound_deadline_time,默认 06:00) + * 起复核(recheck_time)的订单不自动出库; + * 到达放开时间后当天复核订单恢复自动出库。开启则全程不受限制。 */ module.exports = { schedule: { diff --git a/app/service/systems/order_auto_outbound.js b/app/service/systems/order_auto_outbound.js index 6647379..8bcbf44 100644 --- a/app/service/systems/order_auto_outbound.js +++ b/app/service/systems/order_auto_outbound.js @@ -117,6 +117,15 @@ class OrderAutoOutboundService extends MainService { * - {boolean} [rules.require_sync] 是否要求已同步(sync_status='fin') * - {boolean} [rules.require_print] 是否要求已打印(print_status='fin') * - {boolean} [rules.require_pick] 是否要求已拣货(pick_status='fin') + * - {boolean} [rules.allow_same_day_outbound] 是否允许当天复核订单当天出库(默认 false); + * 为 false 时,在到达当天放开时间(same_day_outbound_release_time,默认北京时间 22:00)前, + * 北京时间当天「允许当天出库最后时间」(same_day_outbound_deadline_time,默认 06:00)起复核(recheck_time)的订单当天不参与自动出库; + * recheck_time 早于该最后时间或为 null 的订单不受影响; + * 到达放开时间后当天复核订单恢复自动出库。 + * - {string} [rules.same_day_outbound_deadline_time] 允许当天出库最后时间(HH:mm,默认 '06:00'), + * 复核时间早于该时间的订单不受「出库当天订单」关闭的影响。 + * - {string} [rules.same_day_outbound_release_time] 当天放开时间(HH:mm,默认 '22:00'), + * 仅在 allow_same_day_outbound 为 false 时生效。 * @return {Object} Sequelize where 条件对象。 */ buildAutoOutboundWhere(rules) { @@ -132,6 +141,24 @@ class OrderAutoOutboundService extends MainService { where.recheck_status = 'fin'; } + // 未开启「出库当天订单」时:当天到达放开时间(默认北京时间 22:00)前, + // 排除当天「允许当天出库最后时间」(默认 06:00)起复核的订单(recheck_time 为 null 不排除); + // 到达放开时间后不再限制当天复核订单,当天即可出库。 + // 注意:应用按 UTC 读写 DATETIME,recheck_time 存储为 UTC 时刻,边界需按北京时间(UTC+8)换算。 + if (rules.allow_same_day_outbound !== true) { + const now = new Date(); + const releaseUtc = this.buildSameDayReleaseUtc(now, rules.same_day_outbound_release_time); + if (now.getTime() < releaseUtc.getTime()) { + const sameDayCutoffUtc = this.buildSameDayOutboundCutoffUtc(now, rules.same_day_outbound_deadline_time); + where.recheck_time = { + [Op.or]: [ + { [Op.lt]: sameDayCutoffUtc }, + { [Op.is]: null }, + ], + }; + } + } + if (rules.require_pack) { where.pack_status = 'fin'; } @@ -192,6 +219,77 @@ class OrderAutoOutboundService extends MainService { return orders; } + /** + * 计算「允许当天出库最后时间」边界(UTC 时刻)。 + * + * 边界为北京时间当天配置时刻(默认 06:00,UTC+8): + * - recheck_time(UTC 存储)早于该边界 → 不受「出库当天订单」关闭影响,当天允许自动出库; + * - recheck_time 晚于或等于该边界 → 在到达当天放开时间前不自动出库, + * 到达放开时间(默认 22:00)后放开。 + * + * 用 UTC 显式换算,不依赖 Node 进程时区(生产服务器 TZ 可能与北京时间不一致)。 + * + * @param {Date} [now] 当前时刻(用于计算“今天”,默认 new Date())。 + * @param {string} [timeText] 允许当天出库最后时间(HH:mm,默认 '06:00')。 + * @return {Date} 北京时间当天该时刻对应的 UTC Date。 + */ + buildSameDayOutboundCutoffUtc(now = new Date(), timeText) { + const DAY_MS = 24 * 3600 * 1000; + const BEIJING_OFFSET_MS = 8 * 3600 * 1000; + const { hour, minute } = this.resolveSameDayTime(timeText, 6, 0); + // 当前时刻换算为北京时刻后,取北京当天 0 点的毫秒基准 + const beijingNowMs = now.getTime() + BEIJING_OFFSET_MS; + const beijingMidnightMs = Math.floor(beijingNowMs / DAY_MS) * DAY_MS; + return new Date(beijingMidnightMs + hour * 3600 * 1000 + minute * 60 * 1000 - BEIJING_OFFSET_MS); + } + + /** + * 解析 HH:mm 时间配置为小时/分钟,缺省或格式非法时回退指定默认值。 + * + * @param {string} [timeText] 时间文本,如 '22:00'。 + * @param {number} defaultHour 默认小时。 + * @param {number} defaultMinute 默认分钟。 + * @return {{hour: number, minute: number}} 小时与分钟。 + */ + resolveSameDayTime(timeText, defaultHour, defaultMinute) { + const text = typeof timeText === 'string' ? timeText.trim() : ''; + const match = /^([01]\d|2[0-3]):([0-5]\d)$/.exec(text); + if (!match) { + return { hour: defaultHour, minute: defaultMinute }; + } + return { hour: Number(match[1]), minute: Number(match[2]) }; + } + + /** + * 解析「当天放开时间」配置为小时/分钟(HH:mm)。 + * 缺省或格式非法时回退默认 22:00。 + * + * @param {string} [timeText] 时间文本,如 '22:00'。 + * @return {{hour: number, minute: number}} 小时与分钟。 + */ + resolveSameDayReleaseTime(timeText) { + return this.resolveSameDayTime(timeText, 22, 0); + } + + /** + * 计算北京时间当天「放开时间」对应的 UTC Date。 + * + * 仅在未开启「出库当天订单」时使用:当前时刻早于该边界时, + * 当天 06:00 后复核的订单不自动出库;晚于或等于该边界时放开。 + * + * @param {Date} [now] 当前时刻(默认 new Date())。 + * @param {string} [timeText] 放开时间(HH:mm,默认 22:00)。 + * @return {Date} 北京时间当天放开时间对应的 UTC Date。 + */ + buildSameDayReleaseUtc(now = new Date(), timeText) { + const DAY_MS = 24 * 3600 * 1000; + const BEIJING_OFFSET_MS = 8 * 3600 * 1000; + const { hour, minute } = this.resolveSameDayReleaseTime(timeText); + const beijingNowMs = now.getTime() + BEIJING_OFFSET_MS; + const beijingMidnightMs = Math.floor(beijingNowMs / DAY_MS) * DAY_MS; + return new Date(beijingMidnightMs + hour * 3600 * 1000 + minute * 60 * 1000 - BEIJING_OFFSET_MS); + } + /** * 查找有称重记录的订单ID列表。 * diff --git a/app/service/systems/setting/warehouse.js b/app/service/systems/setting/warehouse.js index fee843a..677d997 100644 --- a/app/service/systems/setting/warehouse.js +++ b/app/service/systems/setting/warehouse.js @@ -226,7 +226,7 @@ const WAREHOUSE_SETTING_FIELDS = Object.freeze([ key: 'auto_outbound_rules', name: '自动出库规则', type: 'object', - default: Object.freeze({ enabled: false, require_recheck: true, require_pack: true, require_weigh: true, require_sync: true, require_print: true, require_pick: true }), + default: Object.freeze({ enabled: false, require_recheck: true, require_pack: true, require_weigh: true, require_sync: true, require_print: true, require_pick: true, allow_same_day_outbound: false, same_day_outbound_deadline_time: '06:00', same_day_outbound_release_time: '22:00' }), required: false, description: '满足条件自动标记出库。全部条件满足时自动标记待出库', item_keys: Object.freeze([ @@ -237,6 +237,9 @@ const WAREHOUSE_SETTING_FIELDS = Object.freeze([ Object.freeze({ key: 'require_sync', name: '需已同步', type: 'flag', required: false, description: '是否要求 sync_status=fin' }), Object.freeze({ key: 'require_print', name: '需已打印', type: 'flag', required: false, description: '是否要求 print_status=fin' }), Object.freeze({ key: 'require_pick', name: '需已拣货', type: 'flag', required: false, description: '是否要求 pick_status=fin' }), + Object.freeze({ key: 'allow_same_day_outbound', name: '出库当天订单', type: 'flag', required: false, description: '是否允许当天复核的订单当天自动出库' }), + Object.freeze({ key: 'same_day_outbound_deadline_time', name: '允许当天出库最后时间', type: 'string', required: false, default: '06:00', maxLength: 5, min: '00:00', max: '23:59', description: '复核时间早于该时间的订单不受「出库当天订单」关闭的影响,格式 HH:mm,不能大于当天放开时间' }), + Object.freeze({ key: 'same_day_outbound_release_time', name: '当天放开时间', type: 'string', required: false, default: '22:00', maxLength: 5, min: '00:00', max: '23:59', description: '未开启「出库当天订单」时,当天到达该时间后不再限制当天复核订单的自动出库,格式 HH:mm' }), ]), }), Object.freeze({ @@ -977,6 +980,52 @@ class SettingWarehouseService extends Service { return this.buildFrontConfigOutput(record, warehouseId); } + /** + * 获取当前仓库启用的微信小程序授权配置(wms_wechat_app)。 + * + * 只拉取一条启用记录(is_enabled=1,按 id 升序取第一条),返回值为对象; + * 无可用记录时返回 null(接口 data 为空)。 + * 出于安全考虑不返回 authorizer_refresh_token 长期令牌。 + * + * @return {Promise} 微信小程序配置对象;无可用记录时 null。 + * @throws {Error} 未识别到当前仓库时抛出,状态码 400。 + */ + async getWechatApp() { + const warehouseId = this.getCurrentWarehouseId(); + + const wechatAppModel = this.ctx.model && this.ctx.model.WechatApp; + if (!wechatAppModel || typeof wechatAppModel.findOne !== 'function') { + throw this.createError('WechatApp model 不可用', 500); + } + + const record = await wechatAppModel.findOne({ + where: { + warehouse_id: warehouseId, + is_enabled: 1, + }, + order: [[ 'id', 'ASC' ]], + }); + + if (!record) { + return null; + } + + const plain = record.get ? record.get({ plain: true }) : record; + return { + id: plain.id, + warehouse_id: plain.warehouse_id, + appid: plain.appid, + nickname: plain.nickname, + user_name: plain.user_name, + head_img: plain.head_img, + auth_time: plain.auth_time, + func_info: plain.func_info, + is_enabled: plain.is_enabled, + created_at: plain.created_at, + updated_at: plain.updated_at, + }; + } + /** * 构建前端配置公共拉取接口的缓存键(按域名)。 *