sp_setting_permission.js 7.1 KB

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