oms/backend/app/controller/principalExternalApi.js
liufumingren 8597cf5ac9 feat(backend): 用户鉴权、菜单权限、主体与系统基础模块
- 登录/邮箱验证码/密码流程(controller/auth.js、lib/emailCode.js、middleware/jwt_auth.js)
- 主体(principal)隔离与主体用户、对外接口配置模块及 principalCheck 中间件
- 菜单、用户、文件上传、快递公司字典(express_companies)等基础模块
- 通用工具:分页(lib/pagination.js)、缓存(service/cache.js)
- 全量接口契约(contract/index.js)与路由注册(router.js)、config.default.js 配置
- 相关迁移:菜单组件调整与菜单种子、创建 files 表
2026-09-13 02:23:34 +08:00

141 lines
4.4 KiB
JavaScript
Raw Permalink 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 { Controller } = require('egg');
const { parsePagination, buildPageResult } = require('../lib/pagination');
/**
* @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, pageSize, offset, limit } = parsePagination(ctx.query);
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,
limit,
order: [[ 'id', 'DESC' ]],
});
ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, 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;