setting.js 8.3 KB

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