• egg.js学习笔记


    1.controller文件

    'use strict';
    
    const Controller = require('egg').Controller;
    
    class ArticleController extends Controller {
      // 创建新文章
      async createArticle() {
        const queryObj = this.ctx.request.body;
        const result = await this.ctx.model.Article.create({
          title: queryObj.title,
          subtitle: queryObj.subtitle,
          text: queryObj.text,
          name: queryObj.name,
          openid: queryObj.openid,
          status: 2,
        });
        this.ctx.body = { result };
      }
      // 查询文章
      async getArticle() {
        const queryObj = this.ctx.query;
        console.log('queryObj', queryObj);
        const where = {};
        const pagination = { pageSize: 10, current: 1 };
        // 每页显示数量
        if (queryObj.pageSize) {
          pagination.pageSize = Number(queryObj.pageSize);
        }
        // 当前页数
        if (queryObj.current) {
          pagination.current = queryObj.current;
        }
        if (queryObj.id) {
          where.id = queryObj.id;
        }
        if (queryObj.openid) {
          where.openid = queryObj.openid;
        }
        if (queryObj.status) {
          where.status = queryObj.status;
        }
        const count = await this.app.model.Article.count({ where });
        const result = await this.ctx.model.Article.findAll({
          where,
          offset: (pagination.current - 1) * pagination.pageSize,
          limit: pagination.pageSize,
          order: [[ 'createdAt', 'DESC' ]],
        });
        this.ctx.body = {
          result,
          pagination: {
            total: count,
            pageSize: pagination.pageSize,
            current: pagination.current,
          },
          statu: 200,
        };
      }
    //删除文章 async deleteArticle() { const queryObj
    = this.ctx.request.body; const where = {}; if (queryObj.id) { where.id = queryObj.id; } await this.ctx.model.Article.destroy({ where }); this.ctx.body = { state: 'success', msg: '删除成功' }; }
    // 审核文章 async checkArticle() { const queryObj
    = this.ctx.request.body; console.log('queryObj', queryObj); // eslint-disable-next-line eqeqeq if (queryObj.status == 0 || queryObj.status == 1 || queryObj.status == 2) { const where = {}; if (queryObj.id) { where.id = queryObj.id; } const result = await this.ctx.model.Article.findOne({ where }); if (result) { const result = await this.app.model.Article.update( { status: queryObj.status, }, { where: { id: queryObj.id, }, } ); this.ctx.body = { result, state: 'success', msg: '更改完成', }; } } else { this.ctx.body = { state: 'fail', msg: '请传递正确的审核结果' }; } } } module.exports = ArticleController;

    2.model文件

    'use strict';
    module.exports = app => {
      const { STRING, INTEGER, TEXT, DATE } = app.Sequelize; // 获取数据类型
      const Article = app.model.define(
        'article',
        {
          id: { type: INTEGER, primaryKey: true, autoIncrement: true },
          title: { type: STRING, allowNull: false },
          subtitle: { type: STRING, allowNull: false },
          text: { type: TEXT, allowNull: false },
          name: { type: STRING, allowNull: false },
          openid: { type: STRING, allowNull: false },
          status: { type: INTEGER, allowNull: false },
          createdAt: { type: DATE, defaultValue: app.Sequelize.NOW },
        },
        {
          freezeTableName: true, // Model 对应的表名将与model名相同
          timestamps: false,
        }
      );
      return Article;
    };
    
    /*
      defaultValue 设置默认  Boolean
      allowNull 是否允许为空 Boolean
      unique 属性用来创建一个唯一约束. Boolean | string
      primaryKey 用于定义主键.  Boolean
      autoIncrement 可用于创建自增的整数列 Boolean
      comment 注释   string;
      references: {
        // 这是引用另一个模型
        model: Bar,
    
        // 这是引用模型的列名称
        key: 'id',
    
        // 这声明什么时候检查外键约束. 仅限PostgreSQL.
        deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE
      }
    */

    3.router文件

      router.get('/getArticle', controller.article.getArticle);//查询
      router.post('/createArticle', controller.article.createArticle);//创建
      router.post('/checkArticle', controller.article.checkArticle);//审核
      router.post('/deleteArticle', controller.article.deleteArticle);//删除
      
  • 相关阅读:
    模板层
    视图层
    路由层
    图书管理系统
    orm基础
    django目录结构介绍
    django基础
    20145211 《Java程序设计》第1周学习总结——小荷才露尖尖角
    实迷途其未远,觉今是而昨非——问卷调查
    20145203盖泽双问卷调查
  • 原文地址:https://www.cnblogs.com/hllzww/p/12981041.html
Copyright © 2020-2023  润新知