129 lines
4.2 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', 'callback_functions', '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); }
}
/**
* 拉取出站回调 method 注册表(管理端多选列表)。
* @return {Promise<Object>} { list: [{method,name,description}], total }。
*/
async listCallbackFunctions() {
try {
const result = this.service.systems.apiApp.listCallbackFunctions();
return this.success('获取成功', result);
} catch (e) { return this.fail(e.message); }
}
}
module.exports = ApiAppService;