liufumingren 76e25db7da feat(apiApp): 新增仓库 API 应用管理模块
- 新增 systems/apiApp.js 业务层:CRUD + AppKey/Secret 自动生成 + 仓库隔离
- 新增 systemMethods/apiApp.js 入口层:list/detail/create/update/delete/regenerateSecret
- 注册 methods/system.json API 文档 + changes 变更记录
- 新增迁移脚本:wms_permission 固定 ID 464-470 + 客服组主管及以上角色授权
2026-08-01 03:39:41 +08:00

118 lines
3.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';
const MainService = require('../main');
/**
* 仓库 API 应用管理 Service
* 管理当前仓库的外部 API 应用(AppKey/Secret 生成、IP 白名单、限流等)。
* 仓库隔离:所有操作自动按 ctx.user.store_id 过滤。
*/
class ApiAppService extends MainService {
constructor(ctx) {
super(ctx);
const _noNeedLogin = '';
const _noNeedRight = '';
const _noNeedLog = '';
const _noNeedAuth = '';
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
}
/**
* 查询当前仓库的 API 应用列表(分页)。
* @param {Object} data 查询参数。
* @return {Promise<Object>} 分页结果。
*/
async list(data) {
try {
const result = await this.service.systems.apiApp.list(data || {});
return this.success('获取成功', result);
} catch (e) { return this.fail(e.message); }
}
/**
* 查询单个 API 应用详情。
* @param {Object} data 查询参数。
* @param {number|string} data.id 应用 ID。
* @return {Promise<Object>} 应用详情。
*/
async detail(data) {
try {
const { id } = data || {};
if (!id) return this.fail('ID不能为空');
const { ctx } = this;
const warehouseId = Number(ctx.user && ctx.user.store_id || 0);
const defaultTypeId = await this.service.systems.apiApp.getDefaultInterfaceTypeId();
const record = await ctx.model.ApiApp.findOne({
attributes: [
'id', 'app_name', 'warehouse_id', 'self_app_key', 'self_app_secret',
'self_ip_whitelist', 'callback_url', 'interface_type_id',
'rate_limit', 'sign_method', 'is_enabled', 'created_at', 'updated_at',
],
where: { id: Number(id), warehouse_id: warehouseId, interface_type_id: defaultTypeId },
raw: true,
});
if (!record) return this.fail('API应用不存在');
record.self_app_secret = record.self_app_secret
? String(record.self_app_secret).substring(0, 8) + '****'
: '';
return this.success('获取成功', record);
} catch (e) { return this.fail(e.message); }
}
/**
* 创建 API 应用,自动生成 AppKey 和 AppSecret。
* @param {Object} data 创建参数。
* @return {Promise<Object>} 创建结果(含完整 secret)。
*/
async create(data) {
try {
const result = await this.service.systems.apiApp.create(data || {});
return this.success('创建成功', result);
} catch (e) { return this.fail(e.message); }
}
/**
* 更新 API 应用。
* @param {Object} data 更新参数。
* @return {Promise<Object>} 更新结果。
*/
async update(data) {
try {
const { id, ...fields } = data || {};
if (!id) return this.fail('ID不能为空');
const result = await this.service.systems.apiApp.update(id, fields);
return this.success('更新成功', result);
} catch (e) { return this.fail(e.message); }
}
/**
* 删除/停用 API 应用。
* @param {Object} data 删除参数。
* @return {Promise<Object>} 删除结果。
*/
async delete(data) {
try {
const { id } = data || {};
if (!id) return this.fail('ID不能为空');
await this.service.systems.apiApp.delete(id);
return this.success('删除成功');
} catch (e) { return this.fail(e.message); }
}
/**
* 重新生成 AppSecret。
* @param {Object} data 参数。
* @return {Promise<Object>} 新 secret。
*/
async regenerateSecret(data) {
try {
const { id } = data || {};
if (!id) return this.fail('ID不能为空');
const result = await this.service.systems.apiApp.regenerateSecret(id);
return this.success('重新生成成功', result);
} catch (e) { return this.fail(e.message); }
}
}
module.exports = ApiAppService;