前端

view

admin/manager/index.html
                            <td>
                                <%if(list[i].status == 1){%>
                                    <img src="/public/admin/images/yes.gif" onclick="app.changeStatus(this,'Admin','status','<%=list[i]._id%>')" alt="">
                                <%}else{%>
                                    <img src="/public/admin/images/no.gif" onclick="app.changeStatus(this,'Admin','status','<%=list[i]._id%>')" alt="">
                                <%}%>
                            </td>

clipboard.png

前端公共base.js

app/public/admin/js/base.js
$(function () {
    app.init()
})

var app = {
    init: function () {
        this.toggleAside();
    },
    toggleAside: function () {
        $('.aside h4').click(function () {
            $(this).siblings('ul').slideToggle();
        })
    },
    //改变状态
    changeStatus:function(el,model,attr,id){
        $.get('/admin/changeStatus',{model:model,attr:attr,id:id},function(data){
            if(data.success){
                if(el.src.indexOf('yes') != -1){
                    el.src = '/public/admin/images/no.gif'
                }else{
                    el.src = '/public/admin/images/yes.gif'
                }
            }
        })
    }
}

后台

router.js

router.get('/admin/changeStatus', controller.admin.base.changeStatus);

controler

app/controller/admin/base.js
  async changeStatus(){
    var model = this.ctx.request.query.model;
    var attr = this.ctx.request.query.attr;
    var id = this.ctx.request.query.id;

    var result = await this.ctx.model[model].find({"_id":id});
    if(result.length >0){
      if(result[0][attr] == 1){
        var json = {
          [attr]:0
        }
      }else{
        var json = {
          [attr]:1
        }
      }
      var updateResult = await this.ctx.model[model].updateOne({"_id":id},json);
      if(updateResult){
        this.ctx.body = {"message":"更新成功","success":true}
      }else{
        this.ctx.body = {"message":"更新失败","success":false}
      }
    }else{
      this.ctx.body = {"message":"更新失败,参数错误","success":false}
    }
  }

渣渣辉
1.3k 声望147 粉丝

引用和评论

0 条评论