1) 仓库接口配置 - 新增时校验同一仓库只允许一份接口配置(含唯一索引兜底,迁移会先清理历史重复数据) - 接口配置页在有配置时禁用「新增配置」并提示 2) 员工档案微信绑定 - 列表返回 wechat_bound / 脱敏 openid / 绑定时间,页面新增「微信」列 - 新增员工微信绑定码、轮询结果、解绑接口(按员工系统账号绑定,绑定码仅操作人可轮询) - 导出增加「微信绑定」列 3) 停用员工后不再展示该主体 - 主体ID列表与可选主体列表只取启用(status=1)的关联,停用后登录不显示该主体且无该主体权限 - 权限解析对停用关联直接返回空权限兜底 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
223 lines
8.5 KiB
JavaScript
223 lines
8.5 KiB
JavaScript
'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;
|
||
}
|
||
|
||
// 每个仓库只能存在一个接口配置:仓库与外部 WMS 是一对一关系,多份配置无法确定用哪一份
|
||
const exist = await ctx.model.WarehouseApi.findOne({
|
||
where: { principal_id: principalId, warehouse_id: Number(warehouseId) },
|
||
attributes: [ 'id', 'name' ],
|
||
});
|
||
if (exist) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: `该仓库已存在接口配置(${exist.name}),每个仓库只能配置一个接口` };
|
||
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;
|