chore: init Egg.js OMS backend with sequelize, redis, swagger and eslint

This commit is contained in:
liufumingren 2026-09-09 15:58:56 +08:00
commit 7fc414e06f
25 changed files with 14135 additions and 0 deletions

5
.eslintignore Normal file
View File

@ -0,0 +1,5 @@
node_modules/
coverage/
logs/
run/
.nyc_output/

19
.eslintrc Normal file
View File

@ -0,0 +1,19 @@
{
"extends": "eslint-config-egg",
"rules": {
"jsdoc/check-tag-names": [
"warn",
{
"definedTags": [ "controller", "router", "request", "response" ]
}
]
},
"overrides": [
{
"files": [ "scripts/**/*.js" ],
"rules": {
"no-restricted-modules": "off"
}
}
]
}

10
.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
node_modules/
logs/
swagger.json
run/
coverage/
.nyc_output/
.idea/
.vscode/
.DS_Store
*.log

10
.sequelizerc Normal file
View File

@ -0,0 +1,10 @@
'use strict';
const path = require('path');
module.exports = {
config: path.join(__dirname, 'database/config.js'),
'migrations-path': path.join(__dirname, 'database/migrations'),
'seeders-path': path.join(__dirname, 'database/seeders'),
'models-path': path.join(__dirname, 'app/model'),
};

16
app/contract/index.js Normal file
View File

@ -0,0 +1,16 @@
'use strict';
/**
* Swagger 契约(数据结构)定义。
* 在 Controller 注释中通过 `@Request` / `@Response` 引用这里的 key。
*/
module.exports = {
baseResponse: {
code: { type: 'integer', description: '业务状态码,0 表示成功' },
message: { type: 'string', description: '提示信息' },
data: { type: 'string', description: '业务数据(JSON)' },
},
homeResponse: {
text: { type: 'string', description: '欢迎文本' },
},
};

22
app/controller/home.js Normal file
View File

@ -0,0 +1,22 @@
'use strict';
const { Controller } = require('egg');
/**
* @controller HomeController 首页
*/
class HomeController extends Controller {
/**
* @summary 首页
* @description 示例接口:返回欢迎文本,可在 Swagger UI 中直接调试
* @router get /
* @request query string keyword 搜索关键词(示例参数,可不传)
* @response 200 baseResponse success
*/
async index() {
const { ctx } = this;
ctx.body = 'hi, oms';
}
}
module.exports = HomeController;

21
app/model/user.js Normal file
View File

@ -0,0 +1,21 @@
'use strict';
/**
* 示例 Model,演示 egg-sequelize 用法,可删除后按业务创建。
* 目录约定:app/model/** 下每个文件导出 define 的 model,
* 通过 app.model.xxx 访问(此处文件名为 user.js => app.model.User)。
* @param {Egg.Application} app egg application
* @return {object} user model
*/
module.exports = app => {
const { STRING, INTEGER, DATE } = app.Sequelize;
const User = app.model.define('user', {
id: { type: INTEGER, primaryKey: true, autoIncrement: true, comment: '主键' },
name: { type: STRING(50), allowNull: false, comment: '用户名' },
created_at: { type: DATE, allowNull: false, comment: '创建时间' },
updated_at: { type: DATE, allowNull: false, comment: '更新时间' },
});
return User;
};

9
app/router.js Normal file
View File

@ -0,0 +1,9 @@
'use strict';
/**
* @param {Egg.Application} app - egg application
*/
module.exports = app => {
const { router, controller } = app;
router.get('/', controller.home.index);
};

73
config/config.default.js Normal file
View File

@ -0,0 +1,73 @@
'use strict';
const db = require('../database/config').development;
/**
* @param {Egg.EggAppInfo} appInfo app info
*/
module.exports = appInfo => {
/**
* built-in config
* @type {Egg.EggAppConfig}
**/
const config = exports = {};
// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1720000000000_1234';
// add your middleware config here
config.middleware = [];
// close csrf for easy debugging, please enable it in production if needed
config.security = {
csrf: {
enable: false,
},
};
// sequelize / mysql config,与 sequelize-cli 迁移共用 database/config.js
config.sequelize = {
...db,
define: {
underscored: true,
charset: 'utf8mb4',
dialectOptions: {
collate: 'utf8mb4_unicode_ci',
},
},
pool: {
max: 10,
min: 0,
idle: 10000,
},
};
// swagger 文档:UI 访问 /swagger-ui.html,JSON 访问 /swagger-doc
config.swaggerdoc = {
dirScanner: './app/controller',
basePath: '/',
apiInfo: {
title: 'OMS API',
description: 'OMS 后端接口文档',
version: '1.0.0',
},
schemes: [ 'http', 'https' ],
consumes: [ 'application/json' ],
produces: [ 'application/json' ],
securityDefinitions: {
apikey: {
type: 'apiKey',
name: 'Authorization',
in: 'header',
},
},
enableSecurity: false,
routerMap: false,
// 生产环境关闭文档
enable: appInfo.env !== 'prod',
};
return {
...config,
};
};

38
config/config.local.js Normal file
View File

@ -0,0 +1,38 @@
'use strict';
const db = require('../database/config').development;
/**
* 本地开发环境配置(npm run dev 自动加载)。
* 覆盖 config.default.js 中的数据库连接,并配置 Redis。
*/
module.exports = () => {
const config = {};
config.sequelize = {
...db,
define: {
underscored: true,
charset: 'utf8mb4',
dialectOptions: {
collate: 'utf8mb4_unicode_ci',
},
},
pool: {
max: 10,
min: 0,
idle: 10000,
},
};
config.redis = {
client: {
host: process.env.REDIS_HOST || '127.0.0.1',
port: Number(process.env.REDIS_PORT || 6379),
password: process.env.REDIS_PASSWORD || 'qwe2019',
db: 0,
},
};
return config;
};

21
config/plugin.js Normal file
View File

@ -0,0 +1,21 @@
'use strict';
/** @type Egg.EggPlugin */
module.exports = {
// had enabled by egg
// static: {
// enable: true,
// },
sequelize: {
enable: true,
package: 'egg-sequelize',
},
redis: {
enable: true,
package: 'egg-redis',
},
swaggerdoc: {
enable: true,
package: 'egg-swagger-doc',
},
};

25
database/config.js Normal file
View File

@ -0,0 +1,25 @@
'use strict';
/**
* sequelize-cli 迁移工具使用的数据库连接配置。
* 同时被 config/config.local.js 复用,保证应用与迁移使用同一套连接。
* 敏感信息建议通过环境变量覆盖。
*/
const development = {
username: process.env.MYSQL_USER || 'oms',
password: process.env.MYSQL_PASSWORD || 'NfNRTxmDbtRrjaAn',
database: process.env.MYSQL_DATABASE || 'oms',
host: process.env.MYSQL_HOST || '127.0.0.1',
port: Number(process.env.MYSQL_PORT || 3307),
dialect: 'mysql',
timezone: '+08:00',
migrationStorageTableName: 'sequelize_meta',
};
module.exports = {
development,
test: {
...development,
database: process.env.MYSQL_TEST_DATABASE || 'oms_test',
},
};

View File

@ -0,0 +1,44 @@
'use strict';
/** 1. 用户表(密码加盐存储) */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'users',
{
id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
username: { type: Sequelize.STRING(50), allowNull: false, comment: '登录账号' },
password: { type: Sequelize.STRING(255), allowNull: false, comment: '加盐哈希后的密码' },
salt: { type: Sequelize.STRING(32), allowNull: false, comment: '密码盐值' },
nickname: { type: Sequelize.STRING(50), allowNull: true, comment: '昵称' },
avatar: { type: Sequelize.STRING(255), allowNull: true, comment: '头像' },
email: { type: Sequelize.STRING(100), allowNull: true, comment: '邮箱' },
phone: { type: Sequelize.STRING(20), allowNull: true, comment: '手机号' },
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
last_login_at: { type: Sequelize.DATE, allowNull: true, comment: '最后登录时间' },
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '用户表' }
);
await queryInterface.addIndex('users', { fields: [ 'username' ], unique: true, name: 'uk_users_username' });
},
async down(queryInterface) {
await queryInterface.dropTable('users');
},
};

View File

@ -0,0 +1,42 @@
'use strict';
/** 2. 主体表:独立经营/组织主体,业务数据(商品、订单等)均挂在主体下 */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'principals',
{
id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
code: { type: Sequelize.STRING(50), allowNull: false, comment: '主体编码' },
name: { type: Sequelize.STRING(100), allowNull: false, comment: '主体名称' },
type: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '主体类型:1自营 2加盟 3外部' },
contact_name: { type: Sequelize.STRING(50), allowNull: true, comment: '联系人' },
contact_phone: { type: Sequelize.STRING(20), allowNull: true, comment: '联系电话' },
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注' },
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '主体表' }
);
await queryInterface.addIndex('principals', { fields: [ 'code' ], unique: true, name: 'uk_principals_code' });
},
async down(queryInterface) {
await queryInterface.dropTable('principals');
},
};

View File

@ -0,0 +1,55 @@
'use strict';
/** 3. 主体用户关联表:一个用户可属于多个主体,一个主体可有多个用户 */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'principal_users',
{
id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
principal_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '主体ID',
references: { model: 'principals', key: 'id' },
},
user_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '用户ID',
references: { model: 'users', key: 'id' },
},
role: { type: Sequelize.STRING(50), allowNull: true, comment: '在该主体下的角色标识' },
is_default: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '是否默认主体:1是 0否' },
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '主体用户关联表' }
);
await queryInterface.addIndex('principal_users', {
fields: [ 'principal_id', 'user_id' ],
unique: true,
name: 'uk_principal_users',
});
await queryInterface.addIndex('principal_users', { fields: [ 'user_id' ], name: 'idx_principal_users_user' });
},
async down(queryInterface) {
await queryInterface.dropTable('principal_users');
},
};

View File

@ -0,0 +1,56 @@
'use strict';
/** 4. 商品表:普通商品,归属于具体主体 */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'products',
{
id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
principal_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '所属主体ID',
references: { model: 'principals', key: 'id' },
},
code: { type: Sequelize.STRING(50), allowNull: false, comment: '商品编码' },
name: { type: Sequelize.STRING(100), allowNull: false, comment: '商品名称' },
type: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '商品类型' },
category: { type: Sequelize.STRING(50), allowNull: true, comment: '分类' },
spec: { type: Sequelize.STRING(100), allowNull: true, comment: '规格' },
unit: { type: Sequelize.STRING(20), allowNull: true, comment: '单位' },
price: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '销售价' },
cost_price: { type: Sequelize.DECIMAL(12, 2), allowNull: true, comment: '成本价' },
image: { type: Sequelize.STRING(255), allowNull: true, comment: '图片' },
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1上架 0下架' },
remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注' },
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '商品表' }
);
await queryInterface.addIndex('products', {
fields: [ 'principal_id', 'code' ],
unique: true,
name: 'uk_products_principal_code',
});
},
async down(queryInterface) {
await queryInterface.dropTable('products');
},
};

View File

@ -0,0 +1,55 @@
'use strict';
/** 5. 组合商品表:多个普通商品的组合(套餐),归属于具体主体 */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'combo_products',
{
id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
principal_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '所属主体ID',
references: { model: 'principals', key: 'id' },
},
code: { type: Sequelize.STRING(50), allowNull: false, comment: '组合商品编码' },
name: { type: Sequelize.STRING(100), allowNull: false, comment: '组合商品名称' },
price: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '组合商品售价' },
items: {
type: Sequelize.JSON,
allowNull: true,
comment: '组合明细:[{ "product_id": 1, "quantity": 2 }],关联 products.id',
},
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1上架 0下架' },
remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注' },
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '组合商品表' }
);
await queryInterface.addIndex('combo_products', {
fields: [ 'principal_id', 'code' ],
unique: true,
name: 'uk_combo_products_principal_code',
});
},
async down(queryInterface) {
await queryInterface.dropTable('combo_products');
},
};

View File

@ -0,0 +1,48 @@
'use strict';
/** 6. 菜单表:通用菜单/权限资源,不区分主体 */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'menus',
{
id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
parent_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: '父菜单ID,0 表示根节点',
},
name: { type: Sequelize.STRING(50), allowNull: false, comment: '菜单名称' },
type: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '类型:1目录 2菜单 3按钮' },
path: { type: Sequelize.STRING(200), allowNull: true, comment: '前端路由/地址' },
icon: { type: Sequelize.STRING(100), allowNull: true, comment: '图标' },
permission: { type: Sequelize.STRING(100), allowNull: true, comment: '权限标识,如 order:create' },
sort: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '排序' },
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '菜单表' }
);
await queryInterface.addIndex('menus', { fields: [ 'parent_id' ], name: 'idx_menus_parent' });
},
async down(queryInterface) {
await queryInterface.dropTable('menus');
},
};

View File

@ -0,0 +1,52 @@
'use strict';
/** 7. 主体外部接口表:主体对接外部系统(WMS/ERP/平台等)的接口凭据配置 */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'principal_external_apis',
{
id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
principal_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '所属主体ID',
references: { model: 'principals', key: 'id' },
},
code: { type: Sequelize.STRING(50), allowNull: false, comment: '外部系统标识,如 wms / erp' },
name: { type: Sequelize.STRING(100), allowNull: false, comment: '接口名称' },
base_url: { type: Sequelize.STRING(255), allowNull: true, comment: '接口基础地址' },
app_key: { type: Sequelize.STRING(100), allowNull: true, comment: '应用Key' },
app_secret: { type: Sequelize.STRING(255), allowNull: true, comment: '应用密钥(建议加密存储)' },
extra: { type: Sequelize.JSON, allowNull: true, comment: '扩展配置' },
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '主体外部接口表' }
);
await queryInterface.addIndex('principal_external_apis', {
fields: [ 'principal_id', 'code' ],
unique: true,
name: 'uk_principal_external_apis',
});
},
async down(queryInterface) {
await queryInterface.dropTable('principal_external_apis');
},
};

View File

@ -0,0 +1,79 @@
'use strict';
/**
* 8. 订单表:归属于具体主体;
* 包含本系统订单主状态、外部 WMS 回传状态、以及"上交"(上报/交接给上级或平台)状态。
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'orders',
{
id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
order_no: { type: Sequelize.STRING(50), allowNull: false, comment: '业务订单号' },
principal_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '所属主体ID',
references: { model: 'principals', key: 'id' },
},
order_type: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '订单类型' },
// OMS 本系统主状态
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '主状态:1待处理 2处理中 3已完成 4已取消' },
// 外部 WMS 相关状态
wms_status: {
type: Sequelize.STRING(50),
allowNull: true,
comment: '外部WMS状态(如 CREATED/PICKING/SHIPPED,由回调回写)',
},
wms_order_no: { type: Sequelize.STRING(50), allowNull: true, comment: '外部WMS单号' },
wms_synced_at: { type: Sequelize.DATE, allowNull: true, comment: '最近一次WMS同步时间' },
// 上交(上报/交接)相关状态
submit_status: {
type: Sequelize.TINYINT,
allowNull: false,
defaultValue: 0,
comment: '上交状态:0未上交 1上交中 2已上交 3上交失败',
},
submit_order_no: { type: Sequelize.STRING(50), allowNull: true, comment: '上交后返回的单号' },
submit_at: { type: Sequelize.DATE, allowNull: true, comment: '上交时间' },
submit_msg: { type: Sequelize.STRING(255), allowNull: true, comment: '上交结果/失败原因' },
// 金额与收货信息
total_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '订单金额' },
pay_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: true, comment: '实付金额' },
receiver: {
type: Sequelize.JSON,
allowNull: true,
comment: '收货人信息 JSON:{ name, phone, address }',
},
remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注' },
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '订单表' }
);
await queryInterface.addIndex('orders', { fields: [ 'order_no' ], unique: true, name: 'uk_orders_order_no' });
await queryInterface.addIndex('orders', { fields: [ 'principal_id' ], name: 'idx_orders_principal' });
await queryInterface.addIndex('orders', { fields: [ 'principal_id', 'status' ], name: 'idx_orders_principal_status' });
await queryInterface.addIndex('orders', { fields: [ 'wms_status' ], name: 'idx_orders_wms_status' });
await queryInterface.addIndex('orders', { fields: [ 'submit_status' ], name: 'idx_orders_submit_status' });
},
async down(queryInterface) {
await queryInterface.dropTable('orders');
},
};

13324
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

39
package.json Normal file
View File

@ -0,0 +1,39 @@
{
"name": "oms",
"version": "1.0.0",
"description": "OMS 后端服务(Egg.js)",
"private": true,
"scripts": {
"dev": "egg-bin dev",
"start": "egg-scripts start --daemon --title=egg-server-oms",
"stop": "egg-scripts stop --title=egg-server-oms",
"test": "npm run lint -- --fix && npm run test-local",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"lint": "eslint .",
"ci": "npm run lint && npm run cov",
"migration:up": "sequelize-cli db:migrate",
"migration:down": "sequelize-cli db:migrate:undo",
"migration:down:all": "sequelize-cli db:migrate:undo:all",
"migration:create": "sequelize-cli migration:generate --name",
"swagger:export": "node scripts/export-swagger.js"
},
"dependencies": {
"egg": "^3.34.0",
"egg-redis": "^2.6.1",
"egg-scripts": "^3.1.0",
"egg-sequelize": "^6.0.0",
"egg-swagger-doc": "^2.3.2",
"mysql2": "^3.24.4"
},
"devDependencies": {
"egg-bin": "^6.13.0",
"egg-mock": "^5.12.0",
"eslint": "^8.57.0",
"eslint-config-egg": "^14.1.0",
"sequelize-cli": "^6.6.5"
},
"engines": {
"node": ">=16.0.0"
}
}

32
scripts/export-swagger.js Normal file
View File

@ -0,0 +1,32 @@
'use strict';
/**
* 导出 Swagger 文档为 swagger.json,便于导入 Apifox / Postman 给前端调试。
* 用法:npm run swagger:export
*/
const fs = require('fs');
const path = require('path');
const mm = require('egg-mock');
const output = process.env.SWAGGER_OUTPUT
? path.resolve(process.env.SWAGGER_OUTPUT)
: path.join(__dirname, '../swagger.json');
(async () => {
const app = mm.app();
await app.ready();
const res = await app.httpRequest().get('/swagger-doc');
if (res.status !== 200) {
throw new Error(`获取 swagger 文档失败,HTTP ${res.status}`);
}
fs.writeFileSync(output, JSON.stringify(res.body, null, 2));
console.log(`swagger 文档已导出:${output}`);
await app.close();
process.exit(0);
})().catch(err => {
console.error('导出失败:', err);
process.exit(1);
});

View File

@ -0,0 +1,20 @@
'use strict';
const { app, assert } = require('egg-mock/bootstrap');
describe('test/app/controller/home.test.js', () => {
it('should assert', async () => {
const pkg = require('../../../package.json');
assert(app.config.keys.startsWith(pkg.name));
// const ctx = app.mockContext({});
// yield ctx.service.xx();
});
it('should GET /', async () => {
return app.httpRequest()
.get('/')
.expect('hi, oms')
.expect(200);
});
});

View File

@ -0,0 +1,20 @@
'use strict';
const { app, assert } = require('egg-mock/bootstrap');
describe('test/app/swagger-doc.test.js', () => {
it('should GET /swagger-doc', async () => {
const res = await app.httpRequest()
.get('/swagger-doc')
.expect(200);
assert(res.body.info.title === 'OMS API');
assert(res.body.paths['/']);
});
it('should GET /swagger-ui.html', async () => {
await app.httpRequest()
.get('/swagger-ui.html')
.expect(200);
});
});