layui.config({
base: '../js/module/'
}).extend({
'constants': '{/}../js/util/constants',
'ajax': '{/}../js/util/ajax',
'customUtil': '{/}../js/util/util',
'api': '{/}../js/api/user.api'
}).use(['jquery', 'layer', 'form', 'api', 'constants', 'customUtil'], () => {
const $ = layui.jquery
layui.api.init((json) => {
let templet = ''
for (var i = 0; i < json.result.role.length; i++) {
templet += ``
}
$('select[name="roleId"]').empty().append(templet)
templet = ''
for (var i = 0; i < json.result.corp.length; i++) {
templet += ``
}
$('select[name="deptId"]').empty().append(templet)
layui.form.render()
initTable(json.result)
})
initClick()
})
const initTable = (result) => {
let roleObj = {}
for (let i = 0; i < result.role.length; i++) {
roleObj[result.role[i].roleId] = result.role[i].roleName
}
let deptObj = {}
for (let i = 0; i < result.corp.length; i++) {
deptObj[result.corp[i].deptId] = result.corp[i].deptName
}
const templet = `
编辑
删除
`
layui.customUtil.table({
elem: '#table',
url: '/user/paging',
cols: [[
{type: 'numbers', title: '序号'},
{field: 'userName', width : 280, title: '姓名'},
{field: 'account', title: '账号'},
{title: '角色', templet: (res) => {
return roleObj[res.roleId] || ''
}},
{title: '单位', width : 280, templet: (res) => {
return deptObj[res.deptId] || ''
}},
{field: 'phone', title: '手机号'},
{field: 'createTime', title: '创建时间'},
{title: '操作', templet}
]]
})
}
const initClick = () => {
const $ = layui.jquery
$('.brume-main').on('click', '#addBtn', () => {
$('#form-title').text('新增')
layui.form.val('form', {
userId: '',
userName: '',
account: '',
password: '',
repeatPass: '',
phone: '',
roleId: '',
deptId: '',
})
layui.customUtil.autoView('.brume-main', '.brume-form')
})
$('.brume-form').on('click', '#back', () => {
layui.customUtil.autoView('.brume-main', '.brume-form')
})
$('.brume-main').on('click', '.search', () => {
layui.table.reload('table', {
where: {
roleId: $('#RoleSel').val(),
deptId: $('#deptSel').val(),
queryVal: $('input[name="search"]').val()
},
page: { curr: 1 }
})
})
layui.form.on('submit(formBtn)', (data) => {
if (data.field?.userId) {
if (data.field.password && data.field.repeatPass) {
if (data.field.password != data.field.repeatPass) {
layui.layer.msg('两次密码输入不一致', {icon: 5})
return false
}
}
layui.api.update(data.field,
(json) => {
if (json.code == layui.constants.SUCCESS_CODE) {
layui.customUtil.autoView('.brume-main', '.brume-form')
}
layui.customUtil.refush(json.code == layui.constants.SUCCESS_CODE, '编辑成功', json.msg, {
roleId: $('#RoleSel').val(),
deptId: $('#deptSel').val(),
queryVal: $('input[name="search"]').val()
})
}
)
} else {
if (!(data.field.password && data.field.repeatPass)) {
layui.layer.msg('请输入密码', {icon: 5})
return false
}
if (data.field.password != data.field.repeatPass) {
layui.layer.msg('两次密码输入不一致', {icon: 5})
return false
}
layui.api.save(data.field,
(json) => {
if (json.code == layui.constants.SUCCESS_CODE) {
layui.customUtil.autoView('.brume-main', '.brume-form')
}
layui.customUtil.refush(json.code == layui.constants.SUCCESS_CODE, '新增成功', json.msg, {
roleId: $('#RoleSel').val(),
deptId: $('#deptSel').val(),
queryVal: $('input[name="search"]').val()
})
}
)
}
return false
})
layui.table.on('tool(table)', function(obj) {
if (obj.event == 'edit') {
layui.api.one(obj.data.userId,
(json) => {
if (json.result) {
$('#form-title').text('编辑')
layui.form.val('form', json.result)
layui.customUtil.autoView('.brume-main', '.brume-form')
}
})
} else if (obj.event == 'del') {
layui.layer.confirm('确认要删除吗?', {
btn: ['删除', '取消']
}, (index) => {
layui.api.delete(obj.data.userId,
(json) => {
layui.customUtil.refush(json.result, '删除成功', '删除失败', {
roleId: $('#RoleSel').val(),
deptId: $('#deptSel').val(),
queryVal: $('input[name="search"]').val()
})
}
)
})
}
})
}