sp_setting_permission.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. $(document).ready(() => {
  2. autoFlashHeight();
  3. // 选择账号
  4. const refreshUnitUsersHtml = function () {
  5. const keyword = $('#sb-keyword').val().trim();
  6. const html = [];
  7. const select = $('#sel-batch-unit').val();
  8. const users = accountList.filter(user => {
  9. if (select && user.company !== select) return false;
  10. return !keyword || (user.role || '').indexOf(keyword) >= 0 || (user.name || '').indexOf(keyword) >= 0;
  11. });
  12. const selectableUsers = users.filter(user => !user.sp_exist);
  13. for (const u of users) {
  14. html.push('<tr class="text-center">');
  15. html.push(`<td><input type="checkbox" name="sel-batch-check" id="${u.id}" ${(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. $('#sel-batch-users').html(html.join(''));
  22. $('#sel-batch-all').prop('checked', selectableUsers.length > 0 && selectableUsers.every(user => user.select));
  23. };
  24. $('#sel-batch').on('show.bs.modal', function() {
  25. accountList.forEach(user => { user.select = false; });
  26. refreshUnitUsersHtml();
  27. });
  28. $('#sel-batch-unit').change(function() {
  29. refreshUnitUsersHtml();
  30. });
  31. $('#sb-search').click(function() {
  32. refreshUnitUsersHtml();
  33. });
  34. $('#sb-keyword').change(function() {
  35. refreshUnitUsersHtml();
  36. });
  37. $('body').on('click', '[name=sel-batch-check]', function() {
  38. const user = accountList.find(user => user.id === parseInt(this.getAttribute('id')));
  39. if (!user || user.sp_exist) return;
  40. user.select = this.checked;
  41. const checks = $('[name=sel-batch-check]:enabled');
  42. $('#sel-batch-all').prop('checked', checks.length > 0 && checks.filter(':checked').length === checks.length);
  43. });
  44. $('#sel-batch-all').change(function() {
  45. for (const check of $('[name=sel-batch-check]:enabled')) {
  46. const user = accountList.find(user => user.id === parseInt(check.getAttribute('id')));
  47. if (user) user.select = this.checked;
  48. }
  49. refreshUnitUsersHtml();
  50. });
  51. $('#sel-batch-ok').click(() => {
  52. const select = accountList.filter(user => user.select && !user.sp_exist).map(user => user.id);
  53. postData(`/sp/${spid}/setting/user/permission/update`, { add: select }, function() {
  54. window.location.reload();
  55. });
  56. });
  57. // 移除账号
  58. $('[name=remove-user]').click(function() {
  59. const id = this.getAttribute('data-id');
  60. postData(`/sp/${spid}/setting/user/permission/update`, { del: id }, function() {
  61. window.location.reload();
  62. });
  63. });
  64. $('[name=permission-check]').click(function() {
  65. const ptype = $(this).attr('ptype');
  66. if (ptype === 'contract') {
  67. const pvalue = $(this).attr('pvalue');
  68. if (this.checked) {
  69. if ((pvalue === '1' || pvalue === '2' || pvalue === '7' || pvalue === '8') && !$(this).parents('.permission-parent').find('[ptype=contract][pvalue="3"]').prop('checked') && !$(this).parents('.permission-parent').find('[ptype=contract][pvalue="4"]').prop('checked')) {
  70. $(this).parents('.permission-parent').find('[ptype=contract][pvalue="5"]').prop('checked', true);
  71. }
  72. if (pvalue === '3' || pvalue === '4') {
  73. $(this).parents('.permission-parent').find('[ptype=contract][pvalue="5"]').prop('checked', false);
  74. } else if (pvalue === '5') {
  75. $(this).parents('.permission-parent').find('[ptype=contract][pvalue="3"]').prop('checked', false);
  76. $(this).parents('.permission-parent').find('[ptype=contract][pvalue="4"]').prop('checked', false);
  77. }
  78. } else if (!this.checked) {
  79. if (pvalue === '3' || pvalue === '4') {
  80. if (!$(this).parents('.permission-parent').find('[ptype=contract][pvalue="3"]').prop('checked') && !$(this).parents('.permission-parent').find('[ptype=contract][pvalue="4"]').prop('checked')) {
  81. $(this).parents('.permission-parent').find('[ptype=contract][pvalue="5"]').prop('checked', true);
  82. }
  83. }
  84. }
  85. } else if (ptype === 'payment') {
  86. const pvalue = $(this).attr('pvalue');
  87. if (this.checked && pvalue === '2') {
  88. $(this).parents('.permission-parent').find('[ptype=payment][pvalue="3"]').prop('checked', true);
  89. }
  90. }
  91. });
  92. $('[name=permission-check-all]').click(function() {
  93. const ptype = $(this).attr('ptype');
  94. const pvalue = $(this).attr('pvalue');
  95. $(`[name=permission-check][ptype=${ptype}][pvalue=${pvalue}]`).attr("checked", this.checked);
  96. $('#save-permission').show();
  97. });
  98. $('[name=set-permission]').click(function() {
  99. const data = JSON.parse(this.getAttribute('data-account'));
  100. const permissionCheck = $('[name="permission-check"]');
  101. for (const pc of permissionCheck) {
  102. const ptype = pc.getAttribute('ptype');
  103. const pvalue = pc.getAttribute('pvalue');
  104. const typePermission = (data[ptype + '_permission'] || '').split(',');
  105. pc.checked = typePermission.indexOf(pvalue) >= 0;
  106. }
  107. $('#permission-uid').val(data.permission_id);
  108. $('#permission').modal('show');
  109. });
  110. $('#permission-ok').click(function() {
  111. const data = { id: $('#permission-uid').val() };
  112. const updatePermission = [];
  113. const permissionCheck = $('[name="permission-check"]');
  114. for (const pc of permissionCheck) {
  115. const ptype = pc.getAttribute('ptype');
  116. const pvalue = pc.getAttribute('pvalue');
  117. let up = updatePermission.find(x => { return x.key === ptype; });
  118. if (!up) {
  119. up = { key: ptype, value: [] };
  120. updatePermission.push(up);
  121. }
  122. if (pc.checked) up.value.push(pvalue);
  123. }
  124. updatePermission.forEach(x => {
  125. data[x.key + '_permission'] = x.value.join(',');
  126. });
  127. postData(`/sp/${spid}/setting/user/permission/update`, { update: data }, function() {
  128. window.location.reload();
  129. });
  130. });
  131. $('#filter-valid').change(function() {
  132. const filter = this.checked;
  133. const users = $('[name=user-permission]');
  134. for (const user of users) {
  135. if (filter) {
  136. const checked = $(':checked', user);
  137. if (checked.length > 0) {
  138. $(user).show();
  139. } else {
  140. $(user).hide();
  141. }
  142. } else {
  143. $(user).show();
  144. }
  145. }
  146. });
  147. $('#save-permission').click(function() {
  148. const updateData = [];
  149. const users = $('[name=user-permission]');
  150. for (const user of users) {
  151. const data = { id: user.getAttribute('pid') };
  152. const permissionKey = [], permission = {};
  153. const check = $('[type=checkbox]', user);
  154. for (const c of check) {
  155. const ptype = c.getAttribute('ptype');
  156. const pvalue = c.getAttribute('pvalue');
  157. if (permissionKey.indexOf(ptype) < 0) {
  158. permissionKey.push(ptype);
  159. permission[ptype] = [];
  160. }
  161. if (c.checked) permission[ptype].push(pvalue);
  162. }
  163. permissionKey.forEach(x => {
  164. data[x + '_permission'] = permission[x].join(',');
  165. });
  166. updateData.push(data);
  167. }
  168. postData(`/sp/${spid}/setting/user/permission/update`, { update: updateData }, function() {
  169. window.location.reload();
  170. })
  171. });
  172. $('[name=copy-permission]').click(function() {
  173. let data = $('[name=set-permission]', $(this).parent()).attr('data-account');
  174. $('#source-permission').val(data);
  175. data = JSON.parse(data);
  176. const html = [];
  177. for (const u of subProjectAllAccountList) {
  178. if (u.id === data.id) continue;
  179. html.push('<tr>');
  180. html.push(`<td class=text-center><input type="checkbox" name="copy-batch-check" id="${u.permission_id}"></td>`);
  181. html.push(`<td>${u.name}</td>`);
  182. html.push(`<td>${u.role}</td>`);
  183. html.push(`<td>${u.company}</td>`);
  184. html.push('</tr>');
  185. }
  186. $('#copy-batch-users').html(html.join(''));
  187. $('#copy-permission').modal('show');
  188. });
  189. $('#copy-batch-all').click(function() {
  190. for (const c of $('[name=copy-batch-check]')) {
  191. c.checked = this.checked;
  192. }
  193. });
  194. $('#copy-batch-ok').click(function() {
  195. const source = JSON.parse($('#source-permission').val());
  196. const updateData = [];
  197. const batchUser = $('[name=copy-batch-check]:checked');
  198. if (batchUser.length === 0) {
  199. toastr.warning('未选择账号');
  200. return;
  201. }
  202. for (const user of batchUser) {
  203. const data = { id: user.getAttribute('id') };
  204. const permissionCheck = $('[name="permission-check"]');
  205. for (const pc of permissionCheck) {
  206. const ptype = pc.getAttribute('ptype');
  207. data[ptype + '_permission'] = source[ptype + '_permission'];
  208. }
  209. updateData.push(data);
  210. }
  211. postData(`/sp/${spid}/setting/user/permission/update`, { update: updateData }, function() {
  212. window.location.reload();
  213. })
  214. });
  215. $('#copy-user-batch').on('show.bs.modal', function() {
  216. $('#cub-sp-all')[0].checked = false;
  217. for (const c of $('[name=cub-sp-check]')) {
  218. c.checked = false;
  219. }
  220. $('#cub-user-all')[0].checked = false;
  221. for (const c of $('[name=cub-user-check]')) {
  222. c.checked = false;
  223. }
  224. });
  225. $('#cub-sp-all').click(function() {
  226. for (const c of $('[name=cub-sp-check]')) {
  227. c.checked = this.checked;
  228. }
  229. });
  230. $('#cub-user-all').click(function() {
  231. for (const c of $('[name=cub-user-check]')) {
  232. c.checked = this.checked;
  233. }
  234. });
  235. $('#copy-user-batch-ok').click(function() {
  236. const spCheck = $('[name=cub-sp-check][tree_is_folder="0"]:checked');
  237. if (spCheck.length === 0) {
  238. toastr.warning('未选择项目');
  239. return;
  240. }
  241. const userCheck = $('[name=cub-user-check]:checked');
  242. if (userCheck.length === 0) {
  243. toastr.warning('未选择账号');
  244. return;
  245. }
  246. const copyData = { spid: [], uid: [] };
  247. for (const sp of spCheck) {
  248. copyData.spid.push(sp.getAttribute('spid'));
  249. }
  250. for (const u of userCheck) {
  251. copyData.uid.push(parseInt(u.getAttribute('uid')));
  252. }
  253. const existType = $('[name=exist-type]:checked').val();
  254. postData(`/sp/${spid}/setting/user/permission/update`, { copy: copyData, existType }, function(result) {
  255. toastr.success('设置成功');
  256. $('#copy-user-batch').modal('hide');
  257. });
  258. });
  259. $('#sel-batch-unit').select2({
  260. placeholder: '全部单位',
  261. allowClear: true,
  262. width: '300px',
  263. dropdownParent: $('#sel-batch'),
  264. minimumResultsForSearch: 0,
  265. });
  266. });