manager.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * 后台管理相关js
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/25
  6. * @version
  7. */
  8. $(document).ready(function() {
  9. let selectId = '';
  10. // 编辑用户
  11. $(".edit").click(function() {
  12. // 获取当前点击的数据
  13. let canLogin = $(this).data('login');
  14. if (canLogin) {
  15. $("input[name='can_login']").attr('checked', 'checked');
  16. } else {
  17. $("input[name='can_login']").removeAttr('checked');
  18. }
  19. // 获取权限
  20. let permission = $(this).data('permission');
  21. permission = permission.split(',');
  22. $("input[name='permission[]']").each(function(index) {
  23. let permissionName = $(this).data('permission');
  24. if (permission.indexOf(permissionName) < 0) {
  25. $(this).removeAttr('checked');
  26. return true;
  27. }
  28. $(this).attr('checked', 'checked');
  29. });
  30. selectId = $(this).data('id');
  31. $('#edit-account').modal('show');
  32. });
  33. // 编辑后保存
  34. let isSaving = false;
  35. $("#save-manager").click(function() {
  36. if (selectId === '' || isSaving) {
  37. return false;
  38. }
  39. // 获取权限
  40. let permission = [];
  41. $("input[name='permission[]']:checked").each(function(index) {
  42. let permissionName = $(this).data('permission');
  43. permission.push(permissionName);
  44. });
  45. permission = permission.join(',');
  46. // 获取是否可登录
  47. let login = $("input[name='can_login']").is(':checked');
  48. $.ajax({
  49. url: '/manager/modify/' + selectId,
  50. type: 'post',
  51. data: {permission: permission, login: login ? 1 : 0},
  52. dataType: 'json',
  53. error: function() {
  54. isSaving = false;
  55. },
  56. beforeSend: function() {
  57. isSaving = true;
  58. },
  59. success: function(response) {
  60. isSaving = false;
  61. if (response.err === 0) {
  62. $('#edit-account').modal('hide');
  63. } else {
  64. let msg = response.msg === undefined ? '未知错误' : response.msg;
  65. alert(msg);
  66. }
  67. }
  68. });
  69. });
  70. });