nodejs 点击按钮更换状态

1.controller 里的方法
/**
 * 修改用户状态
 */
async HorselightStatus() {
  try {
    const { id, status } = this.ctx.request.body;
    const user = await this.ctx.service.horselight.findById(id);
    const url = this.app.config.gameServerURl + 'updatePlayerStatus?' + qs.stringify({ account: user.username, status, agent: this.app.config.defualtPlatformId });
    await this.service.tools.sendServerGetRequest(url, 10000); // 更新服务器会员状态
    await this.ctx.service.horselight.changeStatus(id, status); // 更新数据库会员状态
    this.ctx.body = { data: '', msg: '修改成功', count: 1, status: 0 };

  } catch (error) {
    console.log(error);
    this.ctx.body = { data: '', msg: '', status: '',
    };
  }
}
2.service 里的方法
 /*
  * 修改状态
  */
 async changeStatus(id, status) {
   return await this.ctx.model.Advertising.update(
     { status },
     { where: { id } }
   );
 }
3.在路由里面写一个路径
app.router.put('/admin/platformManage/horselightManage/horselightStatus', app.controller.admin.platformManage.horselightManage.horselightStatus);
4.在html页面写一个监听事件
//这是按钮
<button>οnclick='changeStatus("+row.id+",\""+row.title+"\",\""+row.status+"\")'>禁用</button><button type='button' class='layui-btn layui-btn-xs layui-btn-primary' onclick='openAdvertisingPopup(1,"+row.id+")'>编辑</button><button type='button' class='layui-btn layui-btn-xs layui-btn-danger' onclick='deleteShare("+row.id+",\""+row.title+"\")'>删除</button>
//这是监听事件
		 /**
         * 点击禁用启用按钮
        */
        function changeStatus(id, title, status){
            layer.confirm("是否确认切换<span style='color:red'> "+title+" </span>广告状态?", {
                btn: ['确定','取消'] //按钮
            }, function(){
                if(status == 0) {
                    status = 1
                } else {
                    status = 0
                }
                var index = layer.load(1, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                });
                $.ajax({
                    url:`/admin/platformManage/advertising/AdvertisingStatus`,
                    type:"PUT",
                    data:{id:id,status:status,_csrf:$("#_csrf").val()},
                    timeout: 60000, //1分钟
                    success: function(data){
                        layer.close(index);
                        if (data.status == 0) {
                            layer.msg('操作成功');
                            reloadTable();
                            closePopup();
                        } else {
                            layer.msg( data.msg || '操作失败');
                        }
                    },
                    error: function(xhr, status, error){
                        console.log("[ERROR] - ",error);
                        layer.close(index);
                    }
                })
            }, function(){
            });
        }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值