sp_setting_permission.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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=set-permission]').click(function() {
  114. const data = JSON.parse(this.getAttribute('data-account'));
  115. const permissionCheck = $('[name="permission-check"]');
  116. for (const pc of permissionCheck) {
  117. const ptype = pc.getAttribute('ptype');
  118. const pvalue = pc.getAttribute('pvalue');
  119. const typePermission = (data[ptype + '_permission'] || '').split(',');
  120. pc.checked = typePermission.indexOf(pvalue) >= 0;
  121. }
  122. $('#permission-uid').val(data.permission_id);
  123. $('#permission').modal('show');
  124. });
  125. $('#permission-ok').click(function() {
  126. const data = { id: $('#permission-uid').val() };
  127. const updatePermission = [];
  128. const permissionCheck = $('[name="permission-check"]');
  129. for (const pc of permissionCheck) {
  130. const ptype = pc.getAttribute('ptype');
  131. const pvalue = pc.getAttribute('pvalue');
  132. let up = updatePermission.find(x => { return x.key === ptype; });
  133. if (!up) {
  134. up = { key: ptype, value: [] };
  135. updatePermission.push(up);
  136. }
  137. if (pc.checked) up.value.push(pvalue);
  138. }
  139. updatePermission.forEach(x => {
  140. data[x.key + '_permission'] = x.value.join(',');
  141. });
  142. postData(`/sp/${spid}/setting/user/permission/update`, { update: data }, function() {
  143. window.location.reload();
  144. });
  145. });
  146. $('#filter-valid').change(function() {
  147. const filter = this.checked;
  148. const users = $('[name=user-permission]');
  149. for (const user of users) {
  150. if (filter) {
  151. const checked = $(':checked', user);
  152. if (checked.length > 0) {
  153. $(user).show();
  154. } else {
  155. $(user).hide();
  156. }
  157. } else {
  158. $(user).show();
  159. }
  160. }
  161. });
  162. $('#save-permission').click(function() {
  163. const updateData = [];
  164. const users = $('[name=user-permission]');
  165. for (const user of users) {
  166. const data = { id: user.getAttribute('pid') };
  167. const permissionKey = [], permission = {};
  168. const check = $('[type=checkbox]', user);
  169. for (const c of check) {
  170. const ptype = c.getAttribute('ptype');
  171. const pvalue = c.getAttribute('pvalue');
  172. if (permissionKey.indexOf(ptype) < 0) {
  173. permissionKey.push(ptype);
  174. permission[ptype] = [];
  175. }
  176. if (c.checked) permission[ptype].push(pvalue);
  177. }
  178. permissionKey.forEach(x => {
  179. data[x + '_permission'] = permission[x].join(',');
  180. });
  181. updateData.push(data);
  182. }
  183. postData(`/sp/${spid}/setting/user/permission/update`, { update: updateData }, function() {
  184. window.location.reload();
  185. })
  186. });
  187. });