WMS_MAIN/app/model/api_app.js
liufumingren 948f600689 feat(inbox): 回调推送、出站文档、搜索增强、日志与推送状态完善
出站回调:
- 新增 warehouse_notify.json 文档(notify.ping/notify.reply),与 warehouse.json 互跳
- 回调体统一 { method, code, data },method 放 query,body 为业务数据
- MD5 签名: sign=MD5(appSecret+timestamp+bodyJson)
- 回调响应: {code:200} 成功,非200/非code:200 视为失败
- docs 页面支持模拟推送调试

搜索增强(searchStock/searchWarehouseStock):
- 直接查 stock_warehouse,不再依赖 stock_store
- 新增字段: gross_weight/box_spec/packing_requirements/prohibited_type
- 管理标识: is_sn_mgmt/is_shelf_life_mgmt/is_batch_mgmt
- 条码关联: wms_stock_barcodes
- 图片: wms_file_reference(business_type=stock)+wms_file,返回数组
- id 修正为 stock_warehouse.id

推送状态完善:
- handleApiMessage 增加 push_status 更新(2成功/3失败)
- 未配回调地址 → push_status=3 + error 原因
- 回调异常 catch 也更新 push_status=3 + 写日志
- 出站日志写入 wms_warehouse_log 表(WN-前缀)

消息列表:
- keyword 兼容 content/keyword,支持数组
- keyword 搜索 content/group_name/user_name 三列
- 支持 where+operator 结构化查询(parseWhereConditions)
- storeId=-1 查询无货主消息; storeId=0→IS NULL
- 日期范围: startDate/endDate

其他:
- silent 动作: AI 识别无需回复时跳过回执
- ai_enabled 灰度开关(群级)
- bindCode 文档修正
- store_name 改用 StoreWarehouse.store_remask
2026-08-08 00:17:38 +08:00

299 lines
7.0 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';
module.exports = app => {
const { BIGINT, STRING, INTEGER, JSON, TINYINT, DATE } = app.Sequelize;
const ApiApp = app.model.define('api_app', {
id: {
type: BIGINT(20),
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '应用ID',
},
app_name: {
type: STRING(200),
allowNull: false,
validate: {
notEmpty: true,
len: [ 1, 200 ],
},
comment: '应用名称',
},
interface_type_id: {
type: INTEGER(10),
allowNull: false,
comment: '接口类型ID',
references: {
model: 'interface_type',
key: 'id',
},
},
contract_id: {
type: BIGINT(20),
allowNull: true,
comment: '合约ID,关联wms_store_warehouse.id(货主接口使用)',
references: {
model: 'store_warehouse',
key: 'id',
},
},
warehouse_id: {
type: BIGINT(20),
allowNull: true,
comment: '仓库ID(仓库接口使用)',
},
self_app_key: {
type: STRING(100),
allowNull: true,
unique: 'uk_self_app_key',
validate: {
len: [ 0, 100 ],
},
comment: '自身AppKey(开放接口使用,系统生成)',
},
self_app_secret: {
type: STRING(500),
allowNull: true,
validate: {
len: [ 0, 500 ],
},
comment: '自身AppSecret(开放接口使用,系统生成)',
},
self_ip_whitelist: {
type: JSON,
allowNull: true,
comment: '自身IP白名单',
},
callback_url: {
type: STRING(500),
allowNull: true,
comment: 'API回调通知URL',
},
qimen_app_key: {
type: STRING(100),
allowNull: true,
validate: {
len: [ 0, 100 ],
},
comment: '奇门对方AppKey',
},
qimen_customer_id: {
type: STRING(100),
allowNull: true,
validate: {
len: [ 0, 100 ],
},
comment: '奇门客户ID',
},
qimen_warehouse_code: {
type: STRING(100),
allowNull: true,
validate: {
len: [ 0, 100 ],
},
comment: '奇门仓库编码',
},
api_version: {
type: STRING(50),
allowNull: true,
defaultValue: '1.0',
validate: {
len: [ 0, 50 ],
},
comment: 'API版本',
},
sign_method: {
type: STRING(50),
allowNull: true,
defaultValue: 'md5',
validate: {
isIn: [[ 'md5' ]],
len: [ 0, 50 ],
},
comment: '签名方法(当前仅支持 md5)',
},
rate_limit: {
type: INTEGER(11),
allowNull: true,
defaultValue: 1000,
validate: {
min: 1,
max: 100000,
},
comment: '速率限制(次/分钟)',
},
timeout_seconds: {
type: INTEGER(11),
allowNull: true,
defaultValue: 30,
validate: {
min: 1,
max: 300,
},
comment: '超时时间(秒)',
},
contact_person: {
type: STRING(100),
allowNull: true,
validate: {
len: [ 0, 100 ],
},
comment: '联系人',
},
contact_phone: {
type: STRING(20),
allowNull: true,
validate: {
len: [ 0, 20 ],
},
comment: '联系电话',
},
email: {
type: STRING(100),
allowNull: true,
validate: {
isEmail: true,
len: [ 0, 100 ],
},
comment: '邮箱',
},
is_enabled: {
type: TINYINT(1),
allowNull: false,
defaultValue: 1,
validate: {
isIn: [[ 0, 1 ]],
},
comment: '是否启用',
},
created_at: {
type: DATE,
allowNull: false,
comment: '创建时间',
},
updated_at: {
type: DATE,
allowNull: false,
comment: '更新时间',
},
}, {
tableName: 'wms_api_app',
freezeTableName: true,
underscored: true,
timestamps: true,
paranoid: false,
createdAt: 'created_at',
updatedAt: 'updated_at',
comment: 'API应用配置表',
indexes: [
{
name: 'uk_app_name_interface',
unique: true,
fields: [ 'app_name', 'interface_type_id' ],
},
{
name: 'uk_self_app_key',
unique: true,
fields: [ 'self_app_key' ],
},
{
name: 'uk_interface_warehouse',
unique: true,
fields: [ 'qimen_app_key', 'qimen_warehouse_code' ],
},
{
name: 'uk_interface_contract',
unique: true,
fields: [ 'interface_type_id', 'contract_id' ],
},
{
name: 'idx_interface_type_id',
fields: [ 'interface_type_id' ],
},
{
name: 'idx_warehouse_id',
fields: [ 'warehouse_id' ],
},
{
name: 'idx_is_enabled',
fields: [ 'is_enabled' ],
},
{
name: 'idx_created_at',
fields: [ 'created_at' ],
},
{
name: 'idx_interface_enabled',
fields: [ 'interface_type_id', 'is_enabled' ],
},
],
});
// 定义关联关系
ApiApp.associate = function() {
const { InterfaceType, StoreWarehouse, Store } = app.model;
// 关联接口类型
ApiApp.belongsTo(InterfaceType, {
foreignKey: 'interface_type_id',
targetKey: 'id',
as: 'interface_type',
});
// 关联合约(商家仓库授权关系)
ApiApp.belongsTo(StoreWarehouse, {
foreignKey: 'contract_id',
targetKey: 'id',
as: 'contract',
});
// 关联仓库
ApiApp.belongsTo(Store, {
foreignKey: 'warehouse_id',
targetKey: 'id',
as: 'warehouse',
});
};
// 实例方法 - 检查应用是否可用
ApiApp.prototype.isAvailable = function() {
return this.is_enabled === 1;
};
// 实例方法 - 检查IP是否在白名单中
ApiApp.prototype.isIpAllowed = function(ip) {
if (!this.self_ip_whitelist || !Array.isArray(this.self_ip_whitelist)) {
return true; // 如果没有设置白名单,允许所有IP
}
return this.self_ip_whitelist.includes(ip);
};
// 实例方法 - 生成签名(示例)
ApiApp.prototype.generateSignature = function(params) {
const crypto = require('crypto');
const sortedParams = Object.keys(params).sort().map(key => `${key}${params[key]}`)
.join('');
switch (this.sign_method) {
case 'md5':
return crypto.createHash('md5').update(sortedParams + this.self_app_secret).digest('hex');
case 'hmac':
return crypto.createHmac('sha256', this.self_app_secret).update(sortedParams).digest('hex');
case 'sha256':
return crypto.createHash('sha256').update(sortedParams + this.self_app_secret).digest('hex');
default:
return crypto.createHash('md5').update(sortedParams + this.self_app_secret).digest('hex');
}
};
// 实例方法 - 验证签名
ApiApp.prototype.verifySignature = function(params, signature) {
const generatedSignature = this.generateSignature(params);
return generatedSignature === signature;
};
return ApiApp;
};