架构:eggjs + egg-jwt + egg-sequelize + egg-validate
数据库:mysql
实现功能:登录、验证码、权限菜单、鉴权、角色、用户、上传、下载、错误统一处理
api格式: restful + json
项目目录
app->controller->base_controller.js
const { Controller } = require('egg');
class BaseController extends Controller {
// get user() {
// return this.ctx.session.user;
// }
success(msg = '', status = 200, data = {}) {
const { ctx } = this;
ctx.body = {
msg,
status,
data
};
}
error(msg = '', status = 500, data = {}) {
const { ctx } = this;
ctx.body = {
msg,
status,
data
};
}
// notFound(msg) {
// msg = msg || 'not found';
// this.ctx.throw(404, msg);
// }
}
module.exports = BaseController;
app->controller->home.js
'use strict';
const Controller = require('./base_controller');
class HomeController extends Controller {
async index() {
this.success('hi, egg!', 200);
}
}
module.exports = HomeController;
app->controller->menu.js
'use strict';
const Controller = require('./base_controller');
class menuController extends Controller {
async index() {
const { ctx } = this;
ctx.validate({ isMenu: 'boolean' });
const { isMenu } = ctx.request.body;
this.success('查询成功', 200, {
list: await ctx.service.menu.getMenu(isMenu)
});
}
async update() {
const { ctx } = this;
ctx.validate({ name: 'string', pid: 'number' });
ctx.validate({ id: 'string' }, ctx.params);
const { name, pid } = ctx.request.body;
await ctx.service.menu.editMenu(ctx.params.id, name, pid);
if (ctx.status === 400) {
this.error('编辑失败', 400);
} else {
this.success('编辑成功', 200);
}
}
}
module.exports = menuController;
app->controller->role.js
'use strict';
const Controller = require('./base_controller');
class RoleController extends Controller {
async index() {
const { ctx } = this;
this.success('查询成功', 200, {
list: await ctx.service.role.getRole()
});
}
async show() {
const { ctx } = this;
ctx.validate({ id: 'string' }, ctx.params);
this.success('查询成功', 200, {
list: await ctx.service.role.getRole(ctx.params.id)
});
}
// 插入角色
async create() {
const { ctx } = this;
ctx.validate({ name: 'string', pid: 'number'});
const { name, pid } = ctx.request.body;
await ctx.service.role.addRole(name, pid);
this.success('新增成功', 200);
}
// 更新角色
async update() {
const { ctx } = this;
ctx.validate({ name: 'string' });
ctx.validate({ pid: 'string' }, ctx.params);
const { name, pid } = ctx.request.body;
await ctx.service.role.editRole(ctx.params.id, name, pid);
if (ctx.status === 400) {
this.error('修改失败', 400);
} else {
this.success('修改成功', 200);
}
}
// 移除角色
async remove() {
const { ctx } = this;
ctx.validate({ id: 'string' }, ctx.params);
await ctx.service.role.removeRole(ctx.params.id);
if (ctx.status === 400) {
this.error('删除失败', 400);
} else {
this.success('删除成功', 200);
}
}
}
module.exports = RoleController;
app->controller->upload.js
const Controller = require('./base_controller');
const path = require('path');
const fs = require("fs");
const dayjs = require('dayjs');
function mkdirsSync(dirname) {
if (fs.existsSync(dirname)) {
return true;
} else {
if (mkdirsSync(path.dirname(dirname))) {
fs.mkdirSync(dirname);
return true;
}
}
}
module.exports = class extends Controller {
// 上传单个文件
async uploadOne() {
const { ctx } = this;
const file = ctx.request.files[0]
console.log('-----------获取数据 start--------------');
console.log(file);
console.log('-----------获取数据 end--------------');
// 基础的目录
const uplaodBasePath = '../public/uploads';
// 生成文件名
const filename = `${Date.now()}${Number.parseInt(
Math.random() * 1000,
)}${path.extname(file.filename).toLocaleLowerCase()}`;