setting.js 9.6 KB

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