sp_setting_permission.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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=permission-check]').click(function() {
  61. const ptype = $(this).attr('ptype');
  62. if (ptype === 'contract') {
  63. const pvalue = $(this).attr('pvalue');
  64. if (this.checked) {
  65. if ((pvalue === '1' || pvalue === '2') && !$('[ptype=contract][pvalue="3"]').prop('checked') && !$('[ptype=contract][pvalue="4"]').prop('checked')) {
  66. $('[ptype=contract][pvalue="5"]').prop('checked', true);
  67. }
  68. if (pvalue === '3' || pvalue === '4') {
  69. $('[ptype=contract][pvalue="5"]').prop('checked', false);
  70. } else if (pvalue === '5') {
  71. $('[ptype=contract][pvalue="3"]').prop('checked', false);
  72. $('[ptype=contract][pvalue="4"]').prop('checked', false);
  73. }
  74. } else if (!this.checked) {
  75. if (pvalue === '3' || pvalue === '4') {
  76. if (!$('[ptype=contract][pvalue="3"]').prop('checked') && !$('[ptype=contract][pvalue="4"]').prop('checked')) {
  77. $('[ptype=contract][pvalue="5"]').prop('checked', true);
  78. }
  79. }
  80. }
  81. }
  82. });
  83. $('[name=set-permission]').click(function() {
  84. const data = JSON.parse(this.getAttribute('data-account'));
  85. const permissionCheck = $('[name="permission-check"]');
  86. for (const pc of permissionCheck) {
  87. const ptype = pc.getAttribute('ptype');
  88. const pvalue = pc.getAttribute('pvalue');
  89. const typePermission = (data[ptype + '_permission'] || '').split(',');
  90. pc.checked = typePermission.indexOf(pvalue) >= 0;
  91. }
  92. $('#permission-uid').val(data.permission_id);
  93. $('#permission').modal('show');
  94. });
  95. $('#permission-ok').click(function() {
  96. const data = { id: $('#permission-uid').val() };
  97. const updatePermission = [];
  98. const permissionCheck = $('[name="permission-check"]');
  99. for (const pc of permissionCheck) {
  100. const ptype = pc.getAttribute('ptype');
  101. const pvalue = pc.getAttribute('pvalue');
  102. let up = updatePermission.find(x => { return x.key === ptype; });
  103. if (!up) {
  104. up = { key: ptype, value: [] };
  105. updatePermission.push(up);
  106. }
  107. if (pc.checked) up.value.push(pvalue);
  108. }
  109. updatePermission.forEach(x => {
  110. data[x.key + '_permission'] = x.value.join(',');
  111. });
  112. postData(`/sp/${spid}/setting/user/permission/update`, { update: data }, function() {
  113. window.location.reload();
  114. });
  115. });
  116. $('#filter-valid').change(function() {
  117. const filter = this.checked;
  118. const users = $('[name=user-permission]');
  119. for (const user of users) {
  120. if (filter) {
  121. const checked = $(':checked', user);
  122. if (checked.length > 0) {
  123. $(user).show();
  124. } else {
  125. $(user).hide();
  126. }
  127. } else {
  128. $(user).show();
  129. }
  130. }
  131. });
  132. $('#save-permission').click(function() {
  133. const updateData = [];
  134. const users = $('[name=user-permission]');
  135. for (const user of users) {
  136. const data = { id: user.getAttribute('pid') };
  137. const permissionKey = [], permission = {};
  138. const check = $('[type=checkbox]', user);
  139. for (const c of check) {
  140. const ptype = c.getAttribute('ptype');
  141. const pvalue = c.getAttribute('pvalue');
  142. if (permissionKey.indexOf(ptype) < 0) {
  143. permissionKey.push(ptype);
  144. permission[ptype] = [];
  145. }
  146. if (c.checked) permission[ptype].push(pvalue);
  147. }
  148. permissionKey.forEach(x => {
  149. data[x + '_permission'] = permission[x].join(',');
  150. });
  151. updateData.push(data);
  152. }
  153. postData(`/sp/${spid}/setting/user/permission/update`, { update: updateData }, function() {
  154. window.location.reload();
  155. })
  156. });
  157. });