sp_setting_permission.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. $(document).ready(() => {
  2. autoFlashHeight();
  3. // 选择账号
  4. const refreshUnitUsersHtml = function () {
  5. const select = $('#sel-batch-unit').val();
  6. const selectGroup = accountGroup.find(x => { return x.name === select; });
  7. const html = [];
  8. if (selectGroup) {
  9. for (const u of selectGroup.groupList) {
  10. html.push(`<tr class="text-center">`);
  11. html.push(`<td><input type="checkbox" name="sel-batch-check" id="${u.id}" unit="${selectGroup.name}" ${(u.select ? 'checked' : '')} ${u.sp_exist ? 'disabled' : ''}></td>`);
  12. html.push(`<td>${u.name}</td>`);
  13. html.push(`<td>${u.role}</td>`);
  14. html.push('<tr>');
  15. }
  16. }
  17. $('#sel-batch-users').html(html.join(''));
  18. };
  19. $('#sel-batch').on('show.bs.modal', function() {
  20. accountGroup.forEach(ag => {
  21. ag.groupList.forEach(u => { u.select = false; });
  22. });
  23. refreshUnitUsersHtml();
  24. });
  25. $('#sel-batch-unit').change(function() {
  26. refreshUnitUsersHtml();
  27. });
  28. $('body').on('click', '[name=sel-batch-check]', function() {
  29. const select = $('#sel-batch-unit').val();
  30. const selectGroup = accountGroup.find(x => { return x.name === select; });
  31. const user = selectGroup.groupList.find(x => { return x.id === parseInt(this.getAttribute('id')); });
  32. user.select = this.checked;
  33. });
  34. $('#sel-batch-all').change(function() {
  35. const select = $('#sel-batch-unit').val();
  36. const selectGroup = accountGroup.find(x => { return x.name === select; });
  37. selectGroup.groupList.forEach(x => {
  38. x.select = x.sp_exist ? false : this.checked;
  39. });
  40. refreshUnitUsersHtml();
  41. });
  42. $('#sel-batch-ok').click(() => {
  43. const select = [];
  44. for (const ag of accountGroup) {
  45. for (const u of ag.groupList) {
  46. if (u.select && !u.sp_exist) select.push(u.id);
  47. }
  48. }
  49. postData(`/sp/${spid}/setting/user/permission/update`, { add: select }, function() {
  50. window.location.reload();
  51. });
  52. });
  53. // 移除账号
  54. $('[name=remove-user]').click(function() {
  55. const id = this.getAttribute('data-id');
  56. postData(`/sp/${spid}/setting/user/permission/update`, { del: id }, function() {
  57. window.location.reload();
  58. });
  59. });
  60. $('[name=set-permission]').click(function() {
  61. const data = JSON.parse(this.getAttribute('data-account'));
  62. const permissionCheck = $('[name="permission-check"]');
  63. for (const pc of permissionCheck) {
  64. const ptype = pc.getAttribute('ptype');
  65. const pvalue = pc.getAttribute('pvalue');
  66. const typePermission = (data[ptype + '_permission'] || '').split(',');
  67. pc.checked = typePermission.indexOf(pvalue) >= 0;
  68. }
  69. $('#permission-uid').val(data.permission_id);
  70. $('#permission').modal('show');
  71. });
  72. $('#permission-ok').click(function() {
  73. const data = { id: $('#permission-uid').val() };
  74. const updatePermission = [];
  75. const permissionCheck = $('[name="permission-check"]');
  76. for (const pc of permissionCheck) {
  77. const ptype = pc.getAttribute('ptype');
  78. const pvalue = pc.getAttribute('pvalue');
  79. let up = updatePermission.find(x => { return x.key === ptype; });
  80. if (!up) {
  81. up = { key: ptype, value: [] };
  82. updatePermission.push(up);
  83. }
  84. if (pc.checked) up.value.push(pvalue);
  85. }
  86. updatePermission.forEach(x => {
  87. data[x.key + '_permission'] = x.value.join(',');
  88. });
  89. postData(`/sp/${spid}/setting/user/permission/update`, { update: data }, function() {
  90. window.location.reload();
  91. });
  92. });
  93. $('#filter-valid').change(function() {
  94. const filter = this.checked;
  95. const users = $('[name=user-permission]');
  96. for (const user of users) {
  97. if (filter) {
  98. const checked = $(':checked', user);
  99. if (checked.length > 0) {
  100. $(user).show();
  101. } else {
  102. $(user).hide();
  103. }
  104. } else {
  105. $(user).show();
  106. }
  107. }
  108. });
  109. $('#save-permission').click(function() {
  110. const updateData = [];
  111. const users = $('[name=user-permission]');
  112. for (const user of users) {
  113. const data = { id: user.getAttribute('pid') };
  114. const permissionKey = [], permission = {};
  115. const check = $('[type=checkbox]', user);
  116. for (const c of check) {
  117. const ptype = c.getAttribute('ptype');
  118. const pvalue = c.getAttribute('pvalue');
  119. if (permissionKey.indexOf(ptype) < 0) {
  120. permissionKey.push(ptype);
  121. permission[ptype] = [];
  122. }
  123. if (c.checked) permission[ptype].push(pvalue);
  124. }
  125. permissionKey.forEach(x => {
  126. data[x + '_permission'] = permission[x].join(',');
  127. });
  128. updateData.push(data);
  129. }
  130. postData(`/sp/${spid}/setting/user/permission/update`, { update: updateData }, function() {
  131. window.location.reload();
  132. })
  133. });
  134. });