egg(41,42)--rbac之角色curd

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>
相關文章
相關標籤/搜索