141 lines
4.4 KiB
JavaScript
141 lines
4.4 KiB
JavaScript
'use strict';
|
||
|
||
const { Controller } = require('egg');
|
||
|
||
/**
|
||
* @controller PrincipalExternalApiController 主体外部接口管理
|
||
*/
|
||
class PrincipalExternalApiController extends Controller {
|
||
/**
|
||
* @summary 外部接口列表
|
||
* @description 分页查询主体外部接口配置,支持按主体、系统标识与状态过滤
|
||
* @router get /api/principal-external-apis
|
||
* @apikey
|
||
* @request query integer page 页码,默认 1
|
||
* @request query integer pageSize 每页条数,默认 20
|
||
* @request query integer principal_id 所属主体ID(必填,需当前账号已绑定该主体)
|
||
* @request query string code 外部系统标识,如 wms / erp
|
||
* @request query integer status 状态:1启用 0禁用,不传则不过滤
|
||
* @response 200 externalApiListResponse success
|
||
*/
|
||
async index() {
|
||
const { ctx } = this;
|
||
const page = Number(ctx.query.page || 1);
|
||
const pageSize = Number(ctx.query.pageSize || 20);
|
||
const { code, status } = ctx.query;
|
||
const principalId = ctx.state.principalId;
|
||
|
||
const where = {};
|
||
where.principal_id = principalId;
|
||
if (code) where.code = code;
|
||
if (status !== undefined && status !== '') where.status = Number(status);
|
||
|
||
const { count, rows } = await ctx.model.PrincipalExternalApi.findAndCountAll({
|
||
where,
|
||
offset: (page - 1) * pageSize,
|
||
limit: pageSize,
|
||
order: [[ 'id', 'DESC' ]],
|
||
});
|
||
|
||
ctx.body = { code: 0, message: 'success', data: { list: rows, total: count, page, pageSize } };
|
||
}
|
||
|
||
/**
|
||
* @summary 外部接口详情
|
||
* @description 按主键查询接口配置,含密钥等敏感字段,注意权限控制
|
||
* @router get /api/principal-external-apis/{id}
|
||
* @apikey
|
||
* @request path integer *id 接口配置主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async show() {
|
||
const { ctx } = this;
|
||
const api = await ctx.model.PrincipalExternalApi.findByPk(ctx.params.id);
|
||
|
||
if (!api) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '接口配置不存在' };
|
||
return;
|
||
}
|
||
|
||
ctx.body = { code: 0, message: 'success', data: api };
|
||
}
|
||
|
||
/**
|
||
* @summary 新增外部接口配置
|
||
* @description 同一主体下系统标识 code 唯一(如 wms)
|
||
* @router post /api/principal-external-apis
|
||
* @apikey
|
||
* @request body createExternalApiRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async create() {
|
||
const { ctx } = this;
|
||
const { principal_id: principalId, code, name } = ctx.request.body;
|
||
|
||
if (!principalId || !code || !name) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: 'principal_id、code、name 为必填项' };
|
||
return;
|
||
}
|
||
|
||
const exist = await ctx.model.PrincipalExternalApi.findOne({ where: { principal_id: principalId, code } });
|
||
if (exist) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: '该主体下该系统标识已配置' };
|
||
return;
|
||
}
|
||
|
||
const api = await ctx.model.PrincipalExternalApi.create(ctx.request.body);
|
||
ctx.body = { code: 0, message: 'success', data: { id: api.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 更新外部接口配置
|
||
* @description 未传入的字段保持原值;app_secret 建议加密存储
|
||
* @router put /api/principal-external-apis/{id}
|
||
* @apikey
|
||
* @request path integer *id 接口配置主键ID
|
||
* @request body updateExternalApiRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async update() {
|
||
const { ctx } = this;
|
||
const api = await ctx.model.PrincipalExternalApi.findByPk(ctx.params.id);
|
||
|
||
if (!api) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '接口配置不存在' };
|
||
return;
|
||
}
|
||
|
||
delete ctx.request.body.principal_id;
|
||
await api.update(ctx.request.body);
|
||
ctx.body = { code: 0, message: 'success', data: { id: api.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 删除外部接口配置
|
||
* @description 物理删除接口配置
|
||
* @router delete /api/principal-external-apis/{id}
|
||
* @apikey
|
||
* @request path integer *id 接口配置主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async destroy() {
|
||
const { ctx } = this;
|
||
const api = await ctx.model.PrincipalExternalApi.findByPk(ctx.params.id);
|
||
|
||
if (!api) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '接口配置不存在' };
|
||
return;
|
||
}
|
||
|
||
await api.destroy();
|
||
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
|
||
}
|
||
}
|
||
|
||
module.exports = PrincipalExternalApiController;
|