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(){
});
}