eggjs 项目实践

架构: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()}`;
   
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值