Files
ulthon_admin/extend/base/admin/view/system/auth/assign_users.js
2026-07-17 23:44:33 +08:00

69 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

$(function () {
// 本页自有 init不复用 _common.jsindex 页用),指向 assignUsers 数据源与 toggleUser 切换接口
var init = {
tableElem: '#currentTable',
tableRenderId: 'assignUsersRenderId',
indexUrl: 'system.auth/assignUsers', // AJAX 数据源:后端 isAjax 时返回用户列表(每行带 has_role
toggleUserUrl: 'system.auth/toggleUser',
};
ua.table.render({
init: init,
// 把 role_id 带入分页请求,后端 assignUsers($id) 据此计算 FIND_IN_SET(has_role)
where: { id: $('#roleId').val() },
cols: [[
{ field: 'id', width: 80, title: 'ID' },
{ field: 'username', minWidth: 100, title: '登录账户' },
{ field: 'nickname', minWidth: 100, title: '姓名' },
{ field: 'phone', minWidth: 120, title: '手机' },
{
field: 'has_role',
width: 130,
title: '是否拥有此角色',
// 关键Metis M1filter:false 使 renderSwitch 不自动绑定到 modifyUrl
// modifyUrl 未配置亦无妨,显式 false 更稳妥,避免 listenSwitch 发送 field=value 而非 user_id/role_id
filter: false,
// 自定义 templet绝不能用 ua.table.switch它会把 id/field/value 发到 modifyUrl
templet: function (d) {
var checked = (d.has_role > 0) ? ' checked' : '';
return '<input type="checkbox" name="has_role" lay-skin="switch" lay-text="已分配|未分配" lay-filter="hasRoleSwitch" value="' + d.id + '"' + checked + '>';
}
}
]],
});
// 手动 switch 处理(因为 ua.table.switch / listenSwitch 会发 id/field/value 到 modifyUrl
// 而本场景需要发 user_id + role_id 到 toggleUser
layui.form.on('switch(hasRoleSwitch)', function (obj) {
var userId = this.value;
var roleId = $('#roleId').val();
var elem = this;
$.ajax({
// ulthon 风格相对 URL必须经 ua.url() 补 /admin/ 前缀
url: ua.url(init.toggleUserUrl),
type: 'POST',
dataType: 'json',
data: { user_id: userId, role_id: roleId },
success: function (res) {
// 框架 success() 默认 code=0见 JumpTraitBase::successerror() 默认 code=500
if (res.code === 0) {
layer.msg(res.msg || '操作成功', { icon: 1 });
} else {
// 业务失败:回滚开关状态
elem.checked = !elem.checked;
layui.form.render('checkbox');
layer.msg(res.msg || '操作失败', { icon: 2 });
}
},
error: function () {
// 网络错误:回滚开关状态
elem.checked = !elem.checked;
layui.form.render('checkbox');
layer.msg('网络错误', { icon: 2 });
}
});
});
ua.listen();
});