sp_setting_permission.js 9.4 KB

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