model

model/role.js
module.exports = app => {
    const mongoose = app.mongoose;
    const Schema = mongoose.Schema;
    var d = new Date();
    const RoleSchema = new Schema({
        title: {
            type: String
        },
        description: {
            type: String
        },
        status: {
            type: Number,
            default: 1
        },
        add_time: {
            type: Number,
            default: d.getTime()
        },
    });

    return mongoose.model('Role', RoleSchema, 'role');
}

router.js

router.js
  router.get('/admin/role', controller.admin.role.index);
  router.get('/admin/role/add', controller.admin.role.add);
  router.get('/admin/role/edit', controller.admin.role.edit);
  router.post('/admin/role/doAdd', controller.admin.role.doAdd);
  router.post('/admin/role/doEdit', controller.admin.role.doEdit);
  router.get('/admin/delete', controller.admin.base.delete);

增加

view

view/admin/role/add.html
<form action="/admin/role/doAdd" method="post">
                            <ul>
                                <input type="hidden" name="_csrf" value="<%= csrf %>">
                                <li> 角色名称: <input type="text" name="title" /></li>
                                <li>
                                    角色描述:
                                    <textarea name="description" id="" cols="60" rows="8"></textarea>
                                </li>
                                <li>
                                    <br />
                                    <button type="submit" class="btn btn-default">提交</button>
                                </li>
                            </ul>
                        </form>

clipboard.png

controller

controller/admin/role.js
  async doAdd(){
    console.log(this.ctx.request.body)
    var role = new this.ctx.model.Role({
      title:this.ctx.request.body.title,
      description:this.ctx.request.body.description,
    })
    await role.save()
    await this.success('/admin/role','增加角色成功')
  }

查找

view

view/admin/role/index.html
                <table class="table table-bordered">
                    <thead>
                        <tr class="th">
                            <th>角色名称</th>
                            <th>描述</th>
                            <th>增加时间</th>
                            <th class="text-center">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <% for(var i = 0;i<list.length;i++){%>
                        <tr>
                            <td>
                                <%= list[i].title %>
                            </td>
                            <td>
                                <%= list[i].description %>
                            </td>
                            <td>
                                <%= list[i].add_time %>
                            </td>
                            <td class="text-center">
                                <a href="/admin/role/edit?id=<%= list[i]._id %>">修改</a>
                                <a href="/admin/delete?model=Role&id=<%= list[i]._id %>">删除</a>
                            </td>
                        </tr>
                        <%}%>
                    </tbody>
                </table>

clipboard.png

controller

controller/admin/role.js
  async index() {
    var result = await this.ctx.model.Role.find({});
    await this.ctx.render('admin/role/index',{
      list:result
    });
  }

编辑

view

view/admin/role/edit.html
                        <form action="/admin/role/doEdit" method="post">
                            <ul>
                                    <input type="hidden" name="_csrf" value="<%= csrf %>">
                                    <input type="hidden" name="_id" value="<%= list._id %>">
                                <li>  角色名称: <input type="text" name="title" value=<%= list.title%>></li>
                                <li>
                                        角色描述:
                                    <textarea name="description" id="" cols="60" rows="8"><%= list.description%></textarea>
                                </li>
                                <li>
                                    <br/>
                                    <button type="submit" class="btn btn-default">提交</button>
                                </li>
                            </ul>
                        </form>

clipboard.png

controller

controller/admin/role.js
  async edit() {
    var id = this.ctx.query.id;
    var result = await this.ctx.model.Role.find({"_id":id});
    await this.ctx.render('admin/role/edit',{
      list:result[0]
    });
  }

  async doEdit(){
    console.log(this.ctx.request.body)
    var _id = this.ctx.request.body._id;
    var title = this.ctx.request.body.title;
    var description = this.ctx.request.body.description;
    await this.ctx.model.Role.updateOne({"_id":_id},{
      title,description
    })
    await this.success('/admin/role','编辑角色成功')
  }

公共删除的方法

controller

controller/admin/base.js
  async delete(){
      // 1.获取要删除的数据库存表 model
  // 2.获取要删除数据的_id
  // 3.执行删除
  // 4.返回到以前的页面
    var model = this.ctx.request.query.model;
    var id = this.ctx.request.query.id;
    await this.ctx.model[model].deleteOne({"_id":id});
    this.ctx.redirect(this.ctx.state.prevPage)
  }

middleware

middleware/adminauth.js
ctx.state.prevPage = ctx.request.headers['referer'];  //获取上一页的url

view

view/admin/role/edit.html
<a href="/admin/delete?model=Role&id=<%= list[i]._id %>">删除</a>

渣渣辉
1.3k 声望147 粉丝

引用和评论

0 条评论