sp_setting_permission.js 8.6 KB

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