user.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. layui.config({
  2. base: '../js/module/'
  3. }).extend({
  4. 'constants': '{/}../js/util/constants',
  5. 'ajax': '{/}../js/util/ajax',
  6. 'customUtil': '{/}../js/util/util',
  7. 'api': '{/}../js/api/user.api'
  8. }).use(['jquery', 'layer', 'form', 'api', 'constants', 'customUtil'], () => {
  9. const $ = layui.jquery
  10. layui.api.init((json) => {
  11. let templet = '<option value="">选择或搜索角色</option>'
  12. for (var i = 0; i < json.result.role.length; i++) {
  13. templet += `<option value="${json.result.role[i].roleId}">${json.result.role[i].roleName}</option>`
  14. }
  15. $('select[name="roleId"]').empty().append(templet)
  16. templet = '<option value="">选择或搜索单位</option>'
  17. for (var i = 0; i < json.result.corp.length; i++) {
  18. templet += `<option value="${json.result.corp[i].deptId}">${json.result.corp[i].deptName}</option>`
  19. }
  20. $('select[name="deptId"]').empty().append(templet)
  21. layui.form.render()
  22. initTable(json.result)
  23. })
  24. initClick()
  25. })
  26. const initTable = (result) => {
  27. let roleObj = {}
  28. for (let i = 0; i < result.role.length; i++) {
  29. roleObj[result.role[i].roleId] = result.role[i].roleName
  30. }
  31. let deptObj = {}
  32. for (let i = 0; i < result.corp.length; i++) {
  33. deptObj[result.corp[i].deptId] = result.corp[i].deptName
  34. }
  35. const templet = `
  36. <div>
  37. <span class="fast-span" lay-event="edit">编辑</span>
  38. <span class="fast-span fast-span-del" lay-event="del">删除</span>
  39. </div>
  40. `
  41. layui.customUtil.table({
  42. elem: '#table',
  43. url: '/user/paging',
  44. cols: [[
  45. {type: 'numbers', title: '序号'},
  46. {field: 'userName', width : 280, title: '姓名'},
  47. {field: 'account', title: '账号'},
  48. {title: '角色', templet: (res) => {
  49. return roleObj[res.roleId] || ''
  50. }},
  51. {title: '单位', width : 280, templet: (res) => {
  52. return deptObj[res.deptId] || ''
  53. }},
  54. {field: 'phone', title: '手机号'},
  55. {field: 'createTime', title: '创建时间'},
  56. {title: '操作', templet}
  57. ]]
  58. })
  59. }
  60. const initClick = () => {
  61. const $ = layui.jquery
  62. $('.brume-main').on('click', '#addBtn', () => {
  63. $('#form-title').text('新增')
  64. layui.form.val('form', {
  65. userId: '',
  66. userName: '',
  67. account: '',
  68. password: '',
  69. repeatPass: '',
  70. phone: '',
  71. roleId: '',
  72. deptId: '',
  73. })
  74. layui.customUtil.autoView('.brume-main', '.brume-form')
  75. })
  76. $('.brume-form').on('click', '#back', () => {
  77. layui.customUtil.autoView('.brume-main', '.brume-form')
  78. })
  79. $('.brume-main').on('click', '.search', () => {
  80. layui.table.reload('table', {
  81. where: {
  82. roleId: $('#RoleSel').val(),
  83. deptId: $('#deptSel').val(),
  84. queryVal: $('input[name="search"]').val()
  85. },
  86. page: { curr: 1 }
  87. })
  88. })
  89. layui.form.on('submit(formBtn)', (data) => {
  90. if (data.field?.userId) {
  91. if (data.field.password && data.field.repeatPass) {
  92. if (data.field.password != data.field.repeatPass) {
  93. layui.layer.msg('两次密码输入不一致', {icon: 5})
  94. return false
  95. }
  96. }
  97. layui.api.update(data.field,
  98. (json) => {
  99. if (json.code == layui.constants.SUCCESS_CODE) {
  100. layui.customUtil.autoView('.brume-main', '.brume-form')
  101. }
  102. layui.customUtil.refush(json.code == layui.constants.SUCCESS_CODE, '编辑成功', json.msg, {
  103. roleId: $('#RoleSel').val(),
  104. deptId: $('#deptSel').val(),
  105. queryVal: $('input[name="search"]').val()
  106. })
  107. }
  108. )
  109. } else {
  110. if (!(data.field.password && data.field.repeatPass)) {
  111. layui.layer.msg('请输入密码', {icon: 5})
  112. return false
  113. }
  114. if (data.field.password != data.field.repeatPass) {
  115. layui.layer.msg('两次密码输入不一致', {icon: 5})
  116. return false
  117. }
  118. layui.api.save(data.field,
  119. (json) => {
  120. if (json.code == layui.constants.SUCCESS_CODE) {
  121. layui.customUtil.autoView('.brume-main', '.brume-form')
  122. }
  123. layui.customUtil.refush(json.code == layui.constants.SUCCESS_CODE, '新增成功', json.msg, {
  124. roleId: $('#RoleSel').val(),
  125. deptId: $('#deptSel').val(),
  126. queryVal: $('input[name="search"]').val()
  127. })
  128. }
  129. )
  130. }
  131. return false
  132. })
  133. layui.table.on('tool(table)', function(obj) {
  134. if (obj.event == 'edit') {
  135. layui.api.one(obj.data.userId,
  136. (json) => {
  137. if (json.result) {
  138. $('#form-title').text('编辑')
  139. layui.form.val('form', json.result)
  140. layui.customUtil.autoView('.brume-main', '.brume-form')
  141. }
  142. })
  143. } else if (obj.event == 'del') {
  144. layui.layer.confirm('确认要删除吗?', {
  145. btn: ['删除', '取消']
  146. }, (index) => {
  147. layui.api.delete(obj.data.userId,
  148. (json) => {
  149. layui.customUtil.refush(json.result, '删除成功', '删除失败', {
  150. roleId: $('#RoleSel').val(),
  151. deptId: $('#deptSel').val(),
  152. queryVal: $('input[name="search"]').val()
  153. })
  154. }
  155. )
  156. })
  157. }
  158. })
  159. }