oms/backend/app/controller/warehouseApi.js
liufumingren cbd16752a5 feat(backend): 商品、店铺、组合商品、仓库与库存模块
- 商品/商品图片、店铺、店铺商品、组合商品及明细
- 仓库、仓库商品(WMS itemId 映射)、仓库接口配置与字段定义(warehouseApi/warehouseApiField)
- 库存:stock_items、product_stocks(ZC/CC 双维度)与库存重算工具(utils/recalcStock.js)
- 库存单据与对接日志:stock orders、api_outbound_logs
- 相关迁移:shops/shop_products/stock_orders/warehouses/warehouse_apis/warehouse_products/product_stocks/files-images 等表与字段
2026-09-13 02:23:39 +08:00

211 lines
7.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const { Controller } = require('egg');
const { parsePagination, buildPageResult } = require('../lib/pagination');
const { buildCallbackUrl } = require('../lib/openapiCallback');
/**
* 仓库接口的接口类型取值:当前系统只对接「类奇门JSON接口」这一种
* 后续若接入其他类型(标准奇门、自研协议等),在此扩展取值并同步 contracts/前端选项
*/
const API_TYPE_QIMEN = 'qimen';
/**
* @controller WarehouseApiController 仓库接口配置管理
* 仓库接口配置归属某个仓库,一个仓库可配置多条接口;接口类型由系统固定为类奇门JSON接口
*/
class WarehouseApiController extends Controller {
/**
* @summary 仓库接口配置列表
* @description 分页查询某仓库下的接口配置,支持按仓库、状态与关键词过滤
* @router get /api/warehouse-apis
* @apikey
* @request query integer page 页码,默认 1
* @request query integer pageSize 每页条数,默认 20
* @request query integer principal_id 所属主体ID(必填,需当前账号已绑定该主体)
* @request query integer warehouse_id 仓库ID,不传则返回该主体下全部配置
* @request query integer status 状态:1启用 0禁用,不传则不过滤
* @request query string keyword 关键词,匹配配置名称或基础地址
* @response 200 warehouseApiListResponse success
*/
async index() {
const { ctx } = this;
const Op = this.app.Sequelize.Op;
const { page, pageSize, offset, limit } = parsePagination(ctx.query);
const { warehouse_id: warehouseId, status, keyword } = ctx.query;
const where = { principal_id: ctx.state.principalId };
if (warehouseId) where.warehouse_id = Number(warehouseId);
if (status !== undefined && status !== '') where.status = Number(status);
if (keyword) {
where[Op.or] = [
{ name: { [Op.like]: `%${keyword}%` } },
{ base_url: { [Op.like]: `%${keyword}%` } },
];
}
const { count, rows } = await ctx.model.WarehouseApi.findAndCountAll({
where,
offset,
limit,
order: [[ 'id', 'DESC' ]],
include: [
{ model: ctx.model.Warehouse, as: 'warehouse', attributes: [ 'id', 'code', 'name' ] },
],
});
// 附带对外回调地址(外部系统据此回调 OMS,协议见 app/controller/openapi.js)
const list = rows.map(row => {
const plain = row.get({ plain: true });
plain.callback_url = buildCallbackUrl(ctx);
return plain;
});
ctx.body = { code: 0, message: 'success', data: buildPageResult(list, count, { page, pageSize }) };
}
/**
* @summary 仓库接口配置详情
* @router get /api/warehouse-apis/{id}
* @apikey
* @request path integer *id 接口配置主键ID
* @response 200 baseResponse success
*/
async show() {
const { ctx } = this;
const item = await ctx.model.WarehouseApi.findByPk(ctx.params.id);
if (!item) {
ctx.status = 404;
ctx.body = { code: 404, message: '接口配置不存在' };
return;
}
const data = item.get({ plain: true });
// 附带对外回调地址(外部系统据此回调 OMS)
data.callback_url = buildCallbackUrl(ctx);
ctx.body = { code: 0, message: 'success', data };
}
/**
* @summary 新增仓库接口配置
* @description warehouse_id 与 name 必填;仓库必须属于当前主体;接口类型由服务端固定为类奇门JSON接口(qimen);
* 配置项值不在此接口维护,请使用 PUT /api/warehouse-apis/{id}/fields
* @router post /api/warehouse-apis
* @apikey
* @request body createWarehouseApiRequest *body
* @response 200 baseResponse success
*/
async create() {
const { ctx } = this;
const { principal_id: principalId, warehouse_id: warehouseId, name } = ctx.request.body;
if (!warehouseId || !name) {
ctx.status = 400;
ctx.body = { code: 400, message: 'warehouse_id、name 为必填项' };
return;
}
const warehouse = await ctx.model.Warehouse.findOne({ where: { id: warehouseId, principal_id: principalId } });
if (!warehouse) {
ctx.status = 404;
ctx.body = { code: 404, message: '仓库不存在或不属于当前主体' };
return;
}
// 接口类型仅支持类奇门JSON接口,忽略入参由服务端固定写入;extra 由字段值接口维护
const payload = { ...ctx.request.body, principal_id: principalId, type: API_TYPE_QIMEN };
delete payload.extra;
const item = await ctx.model.WarehouseApi.create(payload);
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
}
/**
* @summary 更新仓库接口配置
* @description 未传入的字段保持原值;不支持通过更新变更归属仓库、主体、接口类型(服务端固定)与配置项值
* @router put /api/warehouse-apis/{id}
* @apikey
* @request path integer *id 接口配置主键ID
* @request body updateWarehouseApiRequest *body
* @response 200 baseResponse success
*/
async update() {
const { ctx } = this;
const item = await ctx.model.WarehouseApi.findByPk(ctx.params.id);
if (!item) {
ctx.status = 404;
ctx.body = { code: 404, message: '接口配置不存在' };
return;
}
delete ctx.request.body.principal_id;
delete ctx.request.body.warehouse_id;
// 接口类型由服务端固定,不随更新变更
delete ctx.request.body.type;
// 配置项值由配置项接口维护,不随接口配置更新
delete ctx.request.body.extra;
await item.update(ctx.request.body);
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
}
/**
* @summary 删除仓库接口配置
* @description 物理删除接口配置,并清空该仓库下的商品推送绑定(warehouse_products)
* @router delete /api/warehouse-apis/{id}
* @apikey
* @request path integer *id 接口配置主键ID
* @response 200 baseResponse success
*/
async destroy() {
const { ctx } = this;
const item = await ctx.model.WarehouseApi.findByPk(ctx.params.id);
if (!item) {
ctx.status = 404;
ctx.body = { code: 404, message: '接口配置不存在' };
return;
}
// 接口删除后该仓库的商品推送状态不再有效,一并清空绑定数据
await ctx.model.WarehouseProduct.destroy({ where: { warehouse_id: item.warehouse_id } });
await item.destroy();
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
}
/**
* @summary 测试连接(类奇门JSON接口)
* @description 调用 WMS 基础模块心跳接口 index.ping,验证 base_url 可达且 WMS 可用;
* 返回外层始终成功(code=0),由 data.ok 标识 ping 是否成功,便于前端直接提示
* @router post /api/warehouse-apis/{id}/test
* @apikey
* @request path integer *id 接口配置主键ID
* @response 200 warehouseApiTestResponse success
*/
async test() {
const { ctx } = this;
const id = Number(ctx.params.id);
const item = await ctx.model.WarehouseApi.findOne({ where: { id, principal_id: ctx.state.principalId } });
if (!item) {
ctx.status = 404;
ctx.body = { code: 404, message: '接口配置不存在' };
return;
}
if (!item.base_url || !item.app_key || !item.app_secret) {
ctx.status = 400;
ctx.body = { code: 400, message: '接口未配置完整(base_url / app_key / app_secret)' };
return;
}
const QimenWmsClient = require('../lib/wms/qimenClient');
const client = new QimenWmsClient(item, this.app);
try {
const resp = await client.ping();
ctx.body = {
code: 0,
message: 'success',
data: { ok: client.isSuccess(resp), code: resp.code, message: resp.message, wms: resp },
};
} catch (err) {
ctx.body = { code: 0, message: 'success', data: { ok: false, error: err.message } };
}
}
}
module.exports = WarehouseApiController;