sp_setting_permission.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. $(document).ready(() => {
  2. autoFlashHeight();
  3. // 选择账号
  4. const refreshUnitUsersHtml = function () {
  5. const keyword = $('#sb-keyword').val();
  6. const select = $('#sel-batch-unit').val();
  7. const selectGroup = accountGroup.find(x => { return x.name === select; });
  8. const html = [];
  9. if (selectGroup) {
  10. const filter = keyword ? selectGroup.groupList.filter(x => {
  11. return x.role.indexOf(keyword) >= 0 || x.name.indexOf(keyword) >= 0;
  12. }) : selectGroup.groupList;
  13. for (const u of filter) {
  14. html.push(`<tr class="text-center">`);
  15. html.push(`<td><input type="checkbox" name="sel-batch-check" id="${u.id}" unit="${selectGroup.name}" ${(u.select ? 'checked' : '')} ${u.sp_exist ? 'disabled' : ''}></td>`);
  16. html.push(`<td>${u.name}</td>`);
  17. html.push(`<td>${u.role}</td>`);
  18. html.push(`<td>${u.company}</td>`);
  19. html.push('<tr>');
  20. }
  21. }
  22. const noCompanyAccount = accountList.filter(x => { return !x.company; });
  23. for (const u of noCompanyAccount) {
  24. html.push(`<tr class="text-center table-secondary">`);
  25. html.push(`<td><input type="checkbox" name="sel-batch-check" id="${u.id}" unit="${selectGroup.name}" ${(u.select ? 'checked' : '')} disabled></td>`);
  26. html.push(`<td>${u.name}</td>`);
  27. html.push(`<td>${u.role}</td>`);
  28. html.push(`<td>${u.company}</td>`);
  29. html.push('<tr>');
  30. }
  31. $('#sel-batch-users').html(html.join(''));
  32. };
  33. $('#sel-batch').on('show.bs.modal', function() {
  34. accountGroup.forEach(ag => {
  35. ag.groupList.forEach(u => { u.select = false; });
  36. });
  37. refreshUnitUsersHtml();
  38. });
  39. $('#sel-batch-unit').change(function() {
  40. refreshUnitUsersHtml();
  41. });
  42. $('#sb-search').click(function() {
  43. refreshUnitUsersHtml();
  44. });
  45. $('#sb-keyword').change(function() {
  46. refreshUnitUsersHtml();
  47. });
  48. $('body').on('click', '[name=sel-batch-check]', function() {
  49. const select = $('#sel-batch-unit').val();
  50. const selectGroup = accountGroup.find(x => { return x.name === select; });
  51. const user = selectGroup.groupList.find(x => { return x.id === parseInt(this.getAttribute('id')); });
  52. user.select = this.checked;
  53. });
  54. $('#sel-batch-all').change(function() {
  55. const select = $('#sel-batch-unit').val();
  56. const selectGroup = accountGroup.find(x => { return x.name === select; });
  57. selectGroup.groupList.forEach(x => {
  58. x.select = x.sp_exist ? false : this.checked;
  59. });
  60. refreshUnitUsersHtml();
  61. });
  62. $('#sel-batch-ok').click(() => {
  63. const select = [];
  64. for (const ag of accountGroup) {
  65. for (const u of ag.groupList) {
  66. if (u.select && !u.sp_exist) select.push(u.id);
  67. }
  68. }
  69. postData(`/sp/${spid}/setting/user/permission/update`, { add: select }, function() {
  70. window.location.reload();
  71. });
  72. });
  73. // 移除账号
  74. $('[name=remove-user]').click(function() {
  75. const id = this.getAttribute('data-id');
  76. postData(`/sp/${spid}/setting/user/permission/update`, { del: id }, function() {
  77. window.location.reload();
  78. });
  79. });
  80. $('[name=permission-check]').click(function() {
  81. const ptype = $(this).attr('ptype');
  82. if (ptype === 'contract') {
  83. const pvalue = $(this).attr('pvalue');
  84. if (this.checked) {
  85. 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')) {
  86. $(this).parents('.permission-parent').find('[ptype=contract][pvalue="5"]').prop('checked', true);
  87. }
  88. if (pvalue === '3' || pvalue === '4') {
  89. $(this).parents('.permission-parent').find('[ptype=contract][pvalue="5"]').prop('checked', false);
  90. } else if (pvalue === '5') {
  91. $(this).parents('.permission-parent').find('[ptype=contract][pvalue="3"]').prop('checked', false);
  92. $(this).parents('.permission-parent').find('[ptype=contract][pvalue="4"]').prop('checked', false);
  93. }
  94. } else if (!this.checked) {
  95. if (pvalue === '3' || pvalue === '4') {
  96. if (!$(this).parents('.permission-parent').find('[ptype=contract][pvalue="3"]').prop('checked') && !$(this).parents('.permission-parent').find('[ptype=contract][pvalue="4"]').prop('checked')) {
  97. $(this).parents('.permission-parent').find('[ptype=contract][pvalue="5"]').prop('checked', true);
  98. }
  99. }
  100. }
  101. } else if (ptype === 'payment') {
  102. const pvalue = $(this).attr('pvalue');
  103. if (this.checked && pvalue === '2') {
  104. $(this).parents('.permission-parent').find('[ptype=payment][pvalue="3"]').prop('checked', true);
  105. }
  106. }
  107. });
  108. $('[name=set-permission]').click(function() {
  109. const data = JSON.parse(this.getAttribute('data-account'));
  110. const permissionCheck = $('[name="permission-check"]');
  111. for (const pc of permissionCheck) {
  112. const ptype = pc.getAttribute('ptype');
  113. const pvalue = pc.getAttribute('pvalue');
  114. const typePermission = (data[ptype + '_permission'] || '').split(',');
  115. pc.checked = typePermission.indexOf(pvalue) >= 0;
  116. }
  117. $('#permission-uid').val(data.permission_id);
  118. $('#permission').modal('show');
  119. });
  120. $('#permission-ok').click(function() {
  121. const data = { id: $('#permission-uid').val() };
  122. const updatePermission = [];
  123. const permissionCheck = $('[name="permission-check"]');
  124. for (const pc of permissionCheck) {
  125. const ptype = pc.getAttribute('ptype');
  126. const pvalue = pc.getAttribute('pvalue');
  127. let up = updatePermission.find(x => { return x.key === ptype; });
  128. if (!up) {
  129. up = { key: ptype, value: [] };
  130. updatePermission.push(up);
  131. }
  132. if (pc.checked) up.value.push(pvalue);
  133. }
  134. updatePermission.forEach(x => {
  135. data[x.key + '_permission'] = x.value.join(',');
  136. });
  137. postData(`/sp/${spid}/setting/user/permission/update`, { update: data }, function() {
  138. window.location.reload();
  139. });
  140. });
  141. $('#filter-valid').change(function() {
  142. const filter = this.checked;
  143. const users = $('[name=user-permission]');
  144. for (const user of users) {
  145. if (filter) {
  146. const checked = $(':checked', user);
  147. if (checked.length > 0) {
  148. $(user).show();
  149. } else {
  150. $(user).hide();
  151. }
  152. } else {
  153. $(user).show();
  154. }
  155. }
  156. });
  157. $('#save-permission').click(function() {
  158. const updateData = [];
  159. const users = $('[name=user-permission]');
  160. for (const user of users) {
  161. const data = { id: user.getAttribute('pid') };
  162. const permissionKey = [], permission = {};
  163. const check = $('[type=checkbox]', user);
  164. for (const c of check) {
  165. const ptype = c.getAttribute('ptype');
  166. const pvalue = c.getAttribute('pvalue');
  167. if (permissionKey.indexOf(ptype) < 0) {
  168. permissionKey.push(ptype);
  169. permission[ptype] = [];
  170. }
  171. if (c.checked) permission[ptype].push(pvalue);
  172. }
  173. permissionKey.forEach(x => {
  174. data[x + '_permission'] = permission[x].join(',');
  175. });
  176. updateData.push(data);
  177. }
  178. postData(`/sp/${spid}/setting/user/permission/update`, { update: updateData }, function() {
  179. window.location.reload();
  180. })
  181. });
  182. });