- 登录/邮箱验证码/密码流程(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 表
171 lines
5.5 KiB
JavaScript
171 lines
5.5 KiB
JavaScript
'use strict';
|
||
|
||
const { Controller } = require('egg');
|
||
const { parsePagination, buildPageResult } = require('../lib/pagination');
|
||
|
||
/**
|
||
* @controller ExpressCompanyController 快递公司字典管理
|
||
* 通用字典(不区分主体),供发货单等单据选择物流公司;code 为行业/WMS 标准编码
|
||
*/
|
||
class ExpressCompanyController extends Controller {
|
||
/**
|
||
* @summary 快递公司列表
|
||
* @description 分页查询快递公司字典,支持按编码、名称与状态过滤
|
||
* @router get /api/express-companies
|
||
* @apikey
|
||
* @request query integer page 页码,默认 1
|
||
* @request query integer pageSize 每页条数,默认 20
|
||
* @request query string code 快递编码,支持模糊匹配
|
||
* @request query string name 快递公司名称,支持模糊匹配
|
||
* @request query integer status 状态:1启用 0禁用
|
||
* @response 200 expressCompanyListResponse success
|
||
*/
|
||
async index() {
|
||
const { ctx } = this;
|
||
const Op = this.app.Sequelize.Op;
|
||
const { page, pageSize, offset, limit } = parsePagination(ctx.query);
|
||
const { code, name, status } = ctx.query;
|
||
|
||
const where = {};
|
||
if (code) where.code = { [Op.like]: `%${String(code).trim().toUpperCase()}%` };
|
||
if (name) where.name = { [Op.like]: `%${name}%` };
|
||
if (status !== undefined && status !== '') where.status = Number(status);
|
||
|
||
const { count, rows } = await ctx.model.ExpressCompany.findAndCountAll({
|
||
where,
|
||
offset,
|
||
limit,
|
||
order: [[ 'sort', 'ASC' ], [ 'id', 'ASC' ]],
|
||
});
|
||
|
||
ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) };
|
||
}
|
||
|
||
/**
|
||
* @summary 快递公司下拉(仅启用)
|
||
* @description 供发货单等表单选择物流公司,按 sort 升序返回
|
||
* @router get /api/express-companies/select
|
||
* @apikey
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async select() {
|
||
const { ctx } = this;
|
||
const rows = await ctx.model.ExpressCompany.findAll({
|
||
where: { status: 1 },
|
||
attributes: [ 'id', 'code', 'name' ],
|
||
order: [[ 'sort', 'ASC' ], [ 'id', 'ASC' ]],
|
||
});
|
||
ctx.body = { code: 0, message: 'success', data: rows };
|
||
}
|
||
|
||
/**
|
||
* @summary 快递公司详情
|
||
* @router get /api/express-companies/{id}
|
||
* @apikey
|
||
* @request path integer *id 快递公司主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async show() {
|
||
const { ctx } = this;
|
||
const item = await ctx.model.ExpressCompany.findByPk(ctx.params.id);
|
||
if (!item) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '快递公司不存在' };
|
||
return;
|
||
}
|
||
ctx.body = { code: 0, message: 'success', data: item };
|
||
}
|
||
|
||
/**
|
||
* @summary 新增快递公司
|
||
* @description 编码全局唯一(统一转大写),名称与编码为必填项
|
||
* @router post /api/express-companies
|
||
* @apikey
|
||
* @request body createExpressCompanyRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async create() {
|
||
const { ctx } = this;
|
||
const body = ctx.request.body || {};
|
||
const code = String(body.code || '').trim().toUpperCase();
|
||
const name = String(body.name || '').trim();
|
||
|
||
if (!code || !name) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: 'code(快递编码)与 name(快递公司名称)为必填项' };
|
||
return;
|
||
}
|
||
|
||
const exist = await ctx.model.ExpressCompany.findOne({ where: { code } });
|
||
if (exist) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: `快递编码 ${code} 已存在` };
|
||
return;
|
||
}
|
||
|
||
const item = await ctx.model.ExpressCompany.create({ ...body, code, name });
|
||
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 更新快递公司
|
||
* @description 未传入的字段保持原值;编码唯一(统一转大写)
|
||
* @router put /api/express-companies/{id}
|
||
* @apikey
|
||
* @request path integer *id 快递公司主键ID
|
||
* @request body updateExpressCompanyRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async update() {
|
||
const { ctx } = this;
|
||
const item = await ctx.model.ExpressCompany.findByPk(ctx.params.id);
|
||
if (!item) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '快递公司不存在' };
|
||
return;
|
||
}
|
||
|
||
const body = { ...(ctx.request.body || {}) };
|
||
if (body.code !== undefined) {
|
||
const code = String(body.code).trim().toUpperCase();
|
||
if (!code) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: 'code(快递编码)不能为空' };
|
||
return;
|
||
}
|
||
const exist = await ctx.model.ExpressCompany.findOne({ where: { code } });
|
||
if (exist && Number(exist.id) !== Number(item.id)) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: `快递编码 ${code} 已存在` };
|
||
return;
|
||
}
|
||
body.code = code;
|
||
}
|
||
|
||
await item.update(body);
|
||
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 删除快递公司
|
||
* @description 物理删除字典项;如已停用建议改用 status=0 而非删除
|
||
* @router delete /api/express-companies/{id}
|
||
* @apikey
|
||
* @request path integer *id 快递公司主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async destroy() {
|
||
const { ctx } = this;
|
||
const item = await ctx.model.ExpressCompany.findByPk(ctx.params.id);
|
||
if (!item) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '快递公司不存在' };
|
||
return;
|
||
}
|
||
await item.destroy();
|
||
ctx.body = { code: 0, message: 'success' };
|
||
}
|
||
}
|
||
|
||
module.exports = ExpressCompanyController;
|