diff --git a/app/service/systemMethods/returnUnpack.js b/app/service/systemMethods/returnUnpack.js index 38dbcaf..20c6c0e 100644 --- a/app/service/systemMethods/returnUnpack.js +++ b/app/service/systemMethods/returnUnpack.js @@ -138,6 +138,19 @@ class ReturnUnpackMethodService extends MainService { return this._call(() => this.service.systems.returnUnpack.submit({ id }), '提交确认失败'); } + /** + * 强制重新录制拆包视频(改,参考 order.packing.forceRecord)。 + * 校验仓库录制电脑在线后重置视频状态并推送打印服务 /record/force 触发重新录制。 + * + * @param {Object} data 请求体。 + * @param {number} data.id 拆包单ID。 + * @return {Promise} 统一 success 包装。 + */ + async forceRecord(data = {}) { + const { id } = data; + return this._call(() => this.service.systems.returnUnpack.forceRecord({ id }), '强制录制失败'); + } + /** * 分页查询拆包单列表(查,仓库隔离自动注入)。 * diff --git a/app/service/systems/return_unpack.js b/app/service/systems/return_unpack.js index 7ba4076..b5d1593 100644 --- a/app/service/systems/return_unpack.js +++ b/app/service/systems/return_unpack.js @@ -548,6 +548,77 @@ class ReturnUnpackService extends Service { }; } + /** + * 强制重新录制拆包视频:重置视频记录状态并推送打印服务强制录制。 + * 参考 order.packing.forceRecord:校验仓库录制电脑在线后,将视频记录重置为 + * created 并清空 video_url,再向打印服务 /record/force 推送 { unpack_id } 触发重新录制。 + * + * @param {Object} params 参数。 + * @param {number} params.id 拆包单ID。 + * @return {Promise} 推送结果 { unpack_id, computer_id, print_service_url, print_service_request, print_service_response }。 + */ + async forceRecord({ id }) { + const { ctx } = this; + + // 拆包单存在性 + 仓库隔离校验(不存在 404 / 跨仓库 403) + const unpack = await this._findByIdChecked(id); + const warehouseId = Number(unpack.warehouse_id || 0); + const unpackId = Number(id); + + // 1. 查找仓库的录制电脑 + const recordingComputer = await ctx.model.PrintComputer.findOne({ + where: { warehouse_id: warehouseId, is_recording: true, deleted_at: null }, + raw: true, + }); + if (!recordingComputer) { + throw this.createBusinessError('该仓库无录制电脑,无法强制录制', 400); + } + + // 2. 检查录制电脑是否在线(与 order_packing 相同的 Redis 在线键) + const redisPrinter = ctx.app.redis.get('printer'); + const onlineKey = `warehouse:${warehouseId}:printer:${recordingComputer.id}`; + const onlineInfo = await redisPrinter.get(onlineKey); + if (!onlineInfo) { + throw this.createBusinessError('该仓库录制电脑不在线,无法强制录制', 400); + } + + // 3. 重置视频记录状态为 created,触发重新录制 + await ctx.model.ReturnUnpackVideo.update( + { status: 'created', video_url: '' }, + { where: { unpack_id: unpackId, deleted_at: null } } + ); + + // 4. 通知打印服务推送强制录制任务 + const printServiceUrl = ctx.app.config.printService.url; + const requestData = { unpack_id: unpackId }; + let printServiceResponse = null; + try { + const res = await ctx.curl(`${printServiceUrl}/record/force`, { + method: 'POST', + contentType: 'json', + data: requestData, + dataType: 'json', + timeout: 10000, + }); + printServiceResponse = { + status: res.status, + headers: res.headers, + data: res.data, + }; + } catch (err) { + ctx.logger.warn(`[returnUnpack.forceRecord] 通知打印服务强制录制 unpack_id=${unpackId} 失败: ${err.message}`); + throw this.createBusinessError(`通知打印服务失败: ${err.message}`, 500); + } + + return { + unpack_id: unpackId, + computer_id: recordingComputer.id, + print_service_url: `${printServiceUrl}/record/force`, + print_service_request: requestData, + print_service_response: printServiceResponse, + }; + } + /** * 分页查询拆包单列表(仓库隔离由方法层注入 warehouse_id)。 * diff --git a/database/migrations/20260825120000-add-return-unpack-force-record-permission.js b/database/migrations/20260825120000-add-return-unpack-force-record-permission.js new file mode 100644 index 0000000..05f6fe6 --- /dev/null +++ b/database/migrations/20260825120000-add-return-unpack-force-record-permission.js @@ -0,0 +1,82 @@ +'use strict'; + +/** + * 退回拆包强制录制权限迁移(returnUnpack.forceRecord)。 + * + * 权限明细: + * 562 — returnUnpack.forceRecord API:退回拆包强制录制 + * + * 角色授权(与作业类 540-545 一致): + * → 仓库组-员工(27) → 仓库组-主管(26)(仓库员工及以上) + * 经理(8)/仓库管理员(2)/超级管理员(7) 已持有 * 通配符,自动拥有所有权限,无需处理 + * + * 幂等:INSERT IGNORE 创建权限记录;角色权限数组追加后去重排序,已存在跳过。 + */ + +const TABLE = 'wms_permission'; +const ROLE_TABLE = 'wms_role'; +const COLUMNS = 'id, path, component, name, redirect, meta_title, meta_icon, hidden, always_show, parent_id, type, sort, status, created_at, update_at'; + +/** 权限ID → 授权角色ID列表(不含通配符角色) */ +const PERMISSION_ROLES = { + 562: [ 27, 26 ], +}; + +module.exports = { + // eslint-disable-next-line no-unused-vars + async up(queryInterface, Sequelize) { + // ── 1. 创建 API 权限记录(固定 ID 562)── + await queryInterface.sequelize.query( + `INSERT IGNORE INTO \`${TABLE}\` (${COLUMNS}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())`, + { replacements: [ 562, 'returnUnpack.forceRecord', null, 'returnUnpack.forceRecord', null, 'API:退回拆包强制录制', null, 0, 0, null, 'api', 0, 1 ] } + ); + + // ── 2. 角色授权注入 ── + for (const permId of Object.keys(PERMISSION_ROLES).map(Number)) { + for (const roleId of PERMISSION_ROLES[permId]) { + const rows = await queryInterface.sequelize.query( + `SELECT id, permissions FROM \`${ROLE_TABLE}\` WHERE id = ?`, + { replacements: [ roleId ], type: queryInterface.sequelize.QueryTypes.SELECT } + ); + const record = Array.isArray(rows) ? rows[0] : null; + if (!record) { + continue; + } + + // 通配符角色跳过 + if (record.permissions === '*') { + continue; + } + + let perms; + try { + perms = JSON.parse(record.permissions || '[]'); + } catch (err) { + continue; + } + if (!Array.isArray(perms)) { + continue; + } + + if (perms.includes(permId)) { + continue; + } + + perms.push(permId); + perms.sort((a, b) => a - b); + + await queryInterface.sequelize.query( + `UPDATE \`${ROLE_TABLE}\` SET permissions = ? WHERE id = ?`, + { replacements: [ JSON.stringify(perms), roleId ] } + ); + } + } + }, + + // eslint-disable-next-line no-unused-vars + async down(queryInterface, Sequelize) { + await queryInterface.sequelize.query( + `DELETE FROM \`${TABLE}\` WHERE id = 562` + ); + }, +}; diff --git a/methods/changes/system.json b/methods/changes/system.json index 6464fae..dcb572f 100644 --- a/methods/changes/system.json +++ b/methods/changes/system.json @@ -1,4 +1,20 @@ [ + { + "version": "2026.08.25-returnUnpack.forceRecord", + "date": "2026-08-25", + "type": "added", + "summary": "新增 returnUnpack.forceRecord:强制重新录制拆包视频。参考 order.packing.forceRecord,入参 id(拆包单ID);校验仓库录制电脑(wms_print_computer is_recording=true)在线后,将 wms_return_unpack_video 视频记录重置为 created 并清空 video_url,再向打印服务 /record/force 推送 { unpack_id } 触发重新录制。", + "methods": [ + "returnUnpack.forceRecord" + ], + "notes": [ + "app/service/systems/return_unpack.js 新增 forceRecord:_findByIdChecked 仓库隔离校验(不存在 404 / 跨仓库 403)→ 查询录制电脑(无则 400)→ Redis 在线键 warehouse:{warehouseId}:printer:{computerId} 校验在线(不在线 400)→ 重置视频状态为 created 并清空 video_url → POST {printService.url}/record/force 推送 { unpack_id }(失败抛 500 业务错误)", + "app/service/systemMethods/returnUnpack.js 新增 forceRecord 包装方法(与 submit 同层,走 _call 统一错误包装)", + "database/migrations/20260825120000-add-return-unpack-force-record-permission.js 新增权限记录 562(returnUnpack.forceRecord,API:退回拆包强制录制),授权角色与作业类 540-545 一致:仓库组-员工(27)/仓库组-主管(26)", + "同步更新 methods/system.json 中 returnUnpack.forceRecord 的接口文档" + ], + "module": "returnUnpack" + }, { "version": "2026.08.24-storage.entry-receivingCompare-merge", "date": "2026-08-24", diff --git a/methods/system.json b/methods/system.json index 14fdc57..e78c540 100644 --- a/methods/system.json +++ b/methods/system.json @@ -36963,6 +36963,88 @@ } ] }, + { + "method": "returnUnpack.forceRecord", + "description": "强制重新录制拆包视频。校验仓库录制电脑在线后,将视频记录(wms_return_unpack_video)重置为 created 并清空 video_url,再向打印服务 /record/force 推送 { unpack_id } 触发重新录制(参考 order.packing.forceRecord)。仓库隔离校验(仅本仓库拆包单)。", + "isNeedAuth": true, + "isNeedLogin": true, + "headers": [], + "params": [ + { + "name": "id", + "type": "integer", + "required": true, + "source": "body", + "description": "拆包单ID" + } + ], + "response": [ + { + "name": "unpack_id", + "type": "integer", + "required": true, + "description": "拆包单ID" + }, + { + "name": "computer_id", + "type": "integer", + "required": true, + "description": "录制电脑ID(wms_print_computer.id,is_recording=true)" + }, + { + "name": "print_service_url", + "type": "string", + "required": true, + "description": "打印服务强制录制接口地址(POST {printService.url}/record/force)" + }, + { + "name": "print_service_request", + "type": "object", + "required": true, + "description": "推送给打印服务的请求体({ unpack_id })" + }, + { + "name": "print_service_response", + "type": "object", + "required": false, + "description": "打印服务响应(status/headers/data)" + } + ], + "errors": [ + { + "code": "400", + "message": "id 必填且必须为正整数" + }, + { + "code": "400", + "message": "该仓库无录制电脑,无法强制录制" + }, + { + "code": "400", + "message": "该仓库录制电脑不在线,无法强制录制" + }, + { + "code": "401", + "message": "未提供有效的认证令牌" + }, + { + "code": "403", + "message": "无权访问该拆包单" + }, + { + "code": "403", + "message": "无权访问该接口" + }, + { + "code": "404", + "message": "拆包单不存在" + }, + { + "code": "500", + "message": "通知打印服务失败" + } + ] + }, { "method": "returnUnpack.list", "description": "拆包单分页列表(主表)。仓库隔离:warehouse_id 由服务端自动注入 ctx.user.store_id,禁止前端传入。支持拆包单号/运单号模糊、日期/时间范围、状态、拆包人员、退回收到单、货主、原运单/原订单等筛选。",