WMS_MAIN/app/model/api_app.js
2026-03-23 17:10:07 +08:00

293 lines
6.8 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白名单',
},
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', 'hmac', 'sha256' ]],
len: [ 0, 50 ],
},
comment: '签名方法',
},
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,
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_customer_id' ],
},
{
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, InterfaceLog } = 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;
};