setting.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. 'use strict';
  2. /**
  3. * 项目信息js
  4. *
  5. * @author EllisRan.
  6. * @date 2019/3/19
  7. * @version
  8. */
  9. $(document).ready(() => {
  10. // 启用和停用账号
  11. $('.account-switch-btn').on('click', function () {
  12. const data = {
  13. enable: $(this).hasClass('btn-outline-success') ? 1 : 0,
  14. id: $(this).data('account'),
  15. };
  16. postData('/setting/user/switch', data, function () {
  17. window.location.href = '/setting/user';
  18. });
  19. });
  20. // 编辑账号
  21. $('a[data-target="#edit-user"]').on('click', function () {
  22. const account = $(this).data('account');
  23. $('#edit-user input[name="account"]').val(account.account);
  24. $('#edit-user input[name="name"]').val(account.name);
  25. $('#edit-user input[name="company"]').val(account.company);
  26. $('#edit-user input[name="role"]').val(account.role);
  27. $('#edit-user input[name="mobile"]').val(account.mobile);
  28. $('#edit-user input[name="telephone"]').val(account.telephone);
  29. $('#edit-user input[name="id"]').val(account.id);
  30. $('#edit-user select[name="account_group"]').val(account.account_group);
  31. $('#edit-user input[class="account-check"]').val(account.account);
  32. $('#edit-user input[data-mobile="auth-mobile"]').val(account.auth_mobile);
  33. });
  34. // 分配随机密码
  35. $("#rand-password").click(function() {
  36. const password = randPassword();
  37. $(this).parent().parent().find('input').val(password);
  38. });
  39. // 重置密码
  40. let isChange = false;
  41. $("#reset-password-btn").click(function() {
  42. try {
  43. if (isChange) {
  44. throw '稍后再操作';
  45. }
  46. const resetPassword = $("#reset-password").val();
  47. const id = $("#user-id").val();
  48. if (resetPassword.length < 6) {
  49. throw '密码长度不能小于6';
  50. }
  51. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test(resetPassword)) {
  52. throw '密码只支持英文数字及符号';
  53. }
  54. const btn = $(this);
  55. $.ajax({
  56. url: '/setting/user/reset/password',
  57. type: 'post',
  58. data: { id: id, reset_password: resetPassword },
  59. dataType: 'json',
  60. error: function() {
  61. isChange = false;
  62. btn.html('重置密码');
  63. throw '网络错误!';
  64. },
  65. beforeSend: function(xhr) {
  66. let csrfToken = Cookies.get('csrfToken');
  67. xhr.setRequestHeader('x-csrf-token', csrfToken);
  68. isChange = true;
  69. btn.html('<i class="fa fa-spinner fa-pulse"></i>');
  70. },
  71. success: function(response) {
  72. isChange = false;
  73. btn.html('重置密码');
  74. if (response.err !== 0) {
  75. throw response.msg;
  76. }
  77. $("#reset-password").val('');
  78. toastr.success('重置成功');
  79. }
  80. });
  81. } catch (error) {
  82. toastr.error(error);
  83. console.log(error);
  84. }
  85. });
  86. // 账号查重
  87. $('input[name="account"]').on('blur', function () {
  88. const self = $(this);
  89. if (self.val() !== self.siblings('input').val()) {
  90. const data = {account: $(this).val()};
  91. postData('/setting/user/exist', data, function (data) {
  92. if (data === null) {
  93. self.removeClass('is-invalid');
  94. } else {
  95. self.addClass('is-invalid');
  96. }
  97. })
  98. } else {
  99. self.removeClass('is-invalid');
  100. }
  101. });
  102. // 选中创建标段才可以选择协作办公
  103. $('a[data-target="#edit-user2"]').on('click', function () {
  104. $('#edit-user2 input:radio').prop('checked', false);
  105. $('#edit-user2 input:checkbox').prop('checked', false);
  106. const account = $(this).data('account');
  107. $('#edit-user2 input[name="id"]').val(account.id);
  108. // 权限赋值
  109. if (account.permission !== '') {
  110. const permission = JSON.parse(account.permission);
  111. for (const pm in permission) {
  112. if (pm === 'tender' && permission[pm].indexOf('1') !== -1) {
  113. $('#edit-user2 input[name="cooperation"]').attr('disabled', false);
  114. } else {
  115. $('#edit-user2 input[name="cooperation"]').attr('disabled', true);
  116. }
  117. if (allPermission[pm].type === 'checkbox') {
  118. for (const index of permission[pm]) {
  119. $('#edit-user2 input:checkbox[id="' + pm + '_' + index + '"]').prop('checked', true);
  120. }
  121. } else if (allPermission[pm].type === 'radio') {
  122. $('#edit-user2 input:radio[id="' + pm + '_' + permission[pm] + '"]').prop('checked', true);
  123. }
  124. }
  125. }
  126. // 协作赋值
  127. $('#edit-user2 input:radio[name="cooperation"][value="' + account.cooperation + '"]').prop('checked', true);
  128. });
  129. // 选择创建标段功能后可选协作办公
  130. $('#edit-user2 input:checkbox').click(function () {
  131. if ($(this).attr('id') === 'tender_1') {
  132. if ($(this).is(':checked')) {
  133. $('#edit-user2 input[name="cooperation"]').attr('disabled', false);
  134. } else {
  135. $('#edit-user2 input[name="cooperation"]').attr('disabled', true);
  136. }
  137. }
  138. })
  139. });
  140. /**
  141. * 表单检测
  142. */
  143. function checkUserForm(status) {
  144. try {
  145. if (status === 'add') {
  146. if ($('#add-user select[name="account_group"]').val() == 0) {
  147. throw '请选择账号组';
  148. }
  149. if ($('#add-user input[name="account"]').val() == '' || $('#add-user input[name="account"]').hasClass('is-invalid')) {
  150. throw '账号不能为空或已存在';
  151. }
  152. if ($('#add-user input[name="password"]').val() == '' || $('#add-user input[name="password"]').val().length < 6) {
  153. throw '密码不能为空或不能小于6位';
  154. }
  155. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test($('#add-user input[name="password"]').val())) {
  156. throw '密码只支持英文数字及符号';
  157. }
  158. if ($('#add-user input[name="name"]').val() == '') {
  159. throw '姓名不能为空';
  160. }
  161. if ($('#add-user input[name="company"]').val() == '') {
  162. throw '单位名称不能为空';
  163. }
  164. if ($('#add-user input[name="role"]').val() == '') {
  165. throw '职位名称不能为空';
  166. }
  167. } else {
  168. if ($('#edit-user select[name="account_group"]').val() == 0) {
  169. throw '请选择账号组';
  170. }
  171. if ($('#edit-user input[name="account"]').val() == '' || $('#add-user input[name="account"]').hasClass('is-invalid')) {
  172. throw '账号不能为空或已存在';
  173. }
  174. if ($('#edit-user input[name="name"]').val() == '') {
  175. throw '姓名不能为空';
  176. }
  177. if ($('#edit-user input[name="company"]').val() == '') {
  178. throw '单位名称不能为空';
  179. }
  180. if ($('#edit-user input[name="role"]').val() == '') {
  181. throw '职位名称不能为空';
  182. }
  183. }
  184. } catch (err) {
  185. toastr.error(err);
  186. return false;
  187. }
  188. }
  189. /**
  190. * 随机密码
  191. */
  192. function randPassword() {
  193. let result = '';
  194. // 随机6-10位
  195. const length = Math.ceil(Math.random() * 2 + 8);
  196. let numberSeed = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  197. let stringSeed = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  198. 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  199. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
  200. const randSeed = stringSeed.concat(numberSeed);
  201. const seedLength = randSeed.length - 1;
  202. for (let i = 0; i < length; i++) {
  203. const index = Math.ceil(Math.random() * seedLength);
  204. result += randSeed[index];
  205. }
  206. return result;
  207. }