setting.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 select[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 input[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. $('#edit-password input[name="account"]').val(account.account);
  40. $('#edit-password input[class="account-check"]').val(account.account);
  41. $('#edit-password input[name="id"]').val(account.id);
  42. $('#edit-password input[name="reset_password"]').val('');
  43. });
  44. // 选择单位自动配置账号组
  45. $('select[name="company"]').change(function () {
  46. const company = $(this).val();
  47. const oneUnit = _.find(unitList, { name: company });
  48. $(this).siblings('input[name="account_group"]').val(oneUnit ? oneUnit.type : 7);
  49. });
  50. // 分配随机密码
  51. $("#rand-password").click(function() {
  52. const password = randPassword();
  53. $(this).parent().parent().find('input').val(password);
  54. });
  55. // 分配随机密码
  56. $("#rand-password2").click(function() {
  57. const password = randPassword();
  58. $(this).parent().parent().find('input').val(password);
  59. });
  60. // 重置密码
  61. let isChange = false;
  62. $("#reset-password-btn").click(function() {
  63. try {
  64. if (isChange) {
  65. throw '稍后再操作';
  66. }
  67. const resetPassword = $("#reset-password").val();
  68. const id = $("#user-id").val();
  69. if (resetPassword.length < 6) {
  70. throw '密码长度不能小于6';
  71. }
  72. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test(resetPassword)) {
  73. throw '密码只支持英文数字及符号';
  74. }
  75. // 判断新密码的强度
  76. const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
  77. if (!reg.test(resetPassword)) {
  78. throw '请设置至少包含数字和字母的密码';
  79. }
  80. const btn = $(this);
  81. $.ajax({
  82. url: '/setting/user/reset/password',
  83. type: 'post',
  84. data: { id: id, reset_password: resetPassword },
  85. dataType: 'json',
  86. error: function() {
  87. isChange = false;
  88. btn.html('重置密码');
  89. throw '网络错误!';
  90. },
  91. beforeSend: function(xhr) {
  92. let csrfToken = Cookies.get('csrfToken_j');
  93. xhr.setRequestHeader('x-csrf-token', csrfToken);
  94. isChange = true;
  95. btn.html('<i class="fa fa-spinner fa-pulse"></i>');
  96. },
  97. success: function(response) {
  98. isChange = false;
  99. btn.html('重置密码');
  100. if (response.err !== 0) {
  101. throw response.msg;
  102. }
  103. $("#reset-password").val('');
  104. toastr.success('重置成功');
  105. }
  106. });
  107. } catch (error) {
  108. toastr.error(error);
  109. console.log(error);
  110. }
  111. });
  112. // 账号查重
  113. $('input[name="account"]').on('blur', function () {
  114. const self = $(this);
  115. if (self.val() !== self.siblings('input').val()) {
  116. const data = {account: $(this).val()};
  117. postData('/setting/user/exist', data, function (data) {
  118. if (data === null) {
  119. self.removeClass('is-invalid');
  120. } else {
  121. self.addClass('is-invalid');
  122. }
  123. })
  124. } else {
  125. self.removeClass('is-invalid');
  126. }
  127. });
  128. // 选中创建标段才可以选择协作办公
  129. $('a[aria-controls="edit-user2"]').on('click', function () {
  130. $('#edit-user2 input:radio').prop('checked', false);
  131. $('#edit-user2 input:checkbox').prop('checked', false);
  132. const account = $(this).data('account');
  133. $('#edit-user2 input[name="id"]').val(account.id);
  134. // 权限赋值
  135. if (account.permission !== '') {
  136. const permission = JSON.parse(account.permission);
  137. for (const pm in permission) {
  138. if (pm === 'tender' && permission[pm].indexOf('1') !== -1) {
  139. $('#edit-user2 input[name="cooperation"]').attr('disabled', false);
  140. } else {
  141. $('#edit-user2 input[name="cooperation"]').attr('disabled', true);
  142. }
  143. if (pm === 'tender' && permission[pm].indexOf('5') !== -1) {
  144. $('#edit-user2 input:checkbox[id="change_1"]').prop('checked', true);
  145. }
  146. if (pm === 'tender' && permission[pm].indexOf('4') !== -1) {
  147. $('#edit-user2 input:checkbox[id="material_1"]').prop('checked', true);
  148. }
  149. if (allPermission[pm].type === 'checkbox') {
  150. for (const index of permission[pm]) {
  151. $('#edit-user2 input:checkbox[id="' + pm + '_' + index + '"]').prop('checked', true);
  152. }
  153. } else if (allPermission[pm].type === 'radio') {
  154. $('#edit-user2 input:radio[id="' + pm + '_' + permission[pm] + '"]').prop('checked', true);
  155. }
  156. }
  157. }
  158. // 协作赋值
  159. $('#edit-user2 input:radio[name="cooperation"][value="' + account.cooperation + '"]').prop('checked', true);
  160. });
  161. // 选择创建标段功能后可选协作办公
  162. $('#edit-user2 input:checkbox').click(function () {
  163. if ($(this).attr('id') === 'tender_1') {
  164. if ($(this).is(':checked')) {
  165. $('#edit-user2 input[name="cooperation"]').attr('disabled', false);
  166. } else {
  167. $('#edit-user2 input[name="cooperation"]').attr('disabled', true);
  168. }
  169. }
  170. });
  171. // 解绑第三方平台
  172. $('.unlink-user').on('click', function () {
  173. const id = $(this).data('account');
  174. const accountData = _.find(accountList, { id });
  175. console.log(accountData);
  176. $('#bind_account').text(accountData.name + ' ' + accountData.mobile);
  177. $('#account_id').val(id);
  178. })
  179. // 设置显示默认
  180. $('body').on('click', '#set-default', function () {
  181. const attid = $(this).data('attid');
  182. const data = {id: attid};
  183. postData('/setting/show/update', data, function (result) {
  184. let html = ''
  185. result.forEach((item, idx) => {
  186. html += `<li class="list-group-item">${item.label_name}`
  187. html+= item.is_default ? `<span class="pull-right">默认</span></li>`:`<a href="javascript:void(0)" id="set-default" class="btn btn-primary btn-sm pull-right" data-attid="${idx}">设为默认</a></li>`
  188. })
  189. // <li class="list-group-item">
  190. // 标段列表<a href="#" class="btn btn-primary btn-sm pull-right">设为默认</a>
  191. // </li>
  192. // <li class="list-group-item">金额概况<span class="pull-right">默认</span></li>
  193. // <li class="list-group-item">
  194. // 计量进度<a href="#" class="btn btn-primary btn-sm pull-right">设为默认</a>
  195. // </li>
  196. $('.list-group').empty()
  197. $('.list-group').append(html)
  198. const item = result.find((i, idx) => idx=== attid)
  199. $('#nav_tender').attr('href', item.path)
  200. });
  201. });
  202. // 设置页显示数目
  203. $('.nav-tabs .nav-link').each(function () {
  204. const pageSize = getLocalCache('account-pageSize') ? getLocalCache('account-pageSize') : '';
  205. if (getLocalCache('account-pageSize') && $(this).attr('href').indexOf('pageSize') === -1 && $(this).attr('href').indexOf('unit') === -1) {
  206. $(this).attr('href', $(this).attr('href') + '?pageSize=' + getLocalCache('account-pageSize'));
  207. }
  208. });
  209. // 设置页显示数目
  210. $('#user-list .list-group-item').each(function (k,v) {
  211. const pageSize = getLocalCache('account-pageSize') ? getLocalCache('account-pageSize') : '';
  212. if (pageSize) {
  213. $(this).attr('href', $(this).attr('href') + '&pageSize=' + pageSize);
  214. }
  215. })
  216. // 参建单位页切换单位右侧显示
  217. $('body').on('click', '#unit_list tr', function () {
  218. const id = parseInt($(this).data('id'));
  219. $(this).siblings('tr').removeClass('table-warning');
  220. $(this).addClass('table-warning');
  221. setUnitRightHtml(id);
  222. });
  223. // 参建单位编辑
  224. // 回车提交
  225. $('#one_unit input').on('keypress', function () {
  226. if(window.event.keyCode === 13) {
  227. $(this).blur();
  228. }
  229. });
  230. $('#one_unit input').blur(function () {
  231. console.log('hello');
  232. const val_name = $(this).data('name');
  233. let val = _.trim($(this).val()) !== '' ? _.trim($(this).val()) : null;
  234. if (!oneUnit) {
  235. toastr.error('所选单位有误,请重新选择');
  236. return false;
  237. }
  238. switch(val_name) {
  239. case 'name':
  240. if(val && val.length > 100) {
  241. toastr.error('单位名称超过100个字,请缩减名称');
  242. $(this).val(oneUnit[val_name]);
  243. return false;
  244. }
  245. const tongming = _.find(unitList, { name: val });
  246. if (tongming && tongming.id !== oneUnit.id) {
  247. toastr.error('单位名称不能重复');
  248. $(this).val(oneUnit[val_name]);
  249. return false;
  250. }
  251. break;
  252. default:
  253. if(val && val.length > 255) {
  254. toastr.error('超出字段范围,请缩减');
  255. $(this).val(oneUnit[val_name]);
  256. return false;
  257. }
  258. break;
  259. }
  260. if(oneUnit[val_name] !== val) {
  261. const _self = $(this);
  262. postData('/setting/user/unit/save', { type: 'update', id: oneUnit.id, val_name, val}, function (result) {
  263. oneUnit[val_name] = val;
  264. _self.val(oneUnit[val_name]);
  265. if (val_name === 'name') {
  266. oneUnit.account_num = result.account_num;
  267. $('#unit_list tr[class="table-warning"]').find('a').text(oneUnit[val_name]);
  268. $('#unit_list tr[class="table-warning"]').children('td').eq(2).text(result.account_num);
  269. }
  270. }, function () {
  271. _self.val(oneUnit[val_name]);
  272. })
  273. } else {
  274. $(this).val(oneUnit[val_name]);
  275. }
  276. });
  277. $('#one_unit textarea').blur(function () {
  278. const val_name = $(this).data('name');
  279. let val = _.trim($(this).val()) !== '' ? _.trim($(this).val()) : null;
  280. if (!oneUnit) {
  281. toastr.error('所选单位有误,请重新选择');
  282. return false;
  283. }
  284. if(oneUnit[val_name] !== val) {
  285. const _self = $(this);
  286. postData('/setting/user/unit/save', { type: 'update', id: oneUnit.id, val_name, val}, function (result) {
  287. oneUnit[val_name] = val;
  288. _self.val(oneUnit[val_name]);
  289. $('#unit_list tr[class="table-warning"]').children('td').eq(4).text(oneUnit[val_name]);
  290. }, function () {
  291. _self.val(oneUnit[val_name]);
  292. })
  293. } else {
  294. $(this).val(oneUnit[val_name]);
  295. }
  296. });
  297. $('#one_unit select').change(function () {
  298. const val_name = $(this).attr('data-name');
  299. let val = _.trim($(this).val()) !== '' ? _.trim($(this).val()) : null;
  300. if (!oneUnit) {
  301. toastr.error('所选单位有误,请重新选择');
  302. return false;
  303. }
  304. if(oneUnit[val_name] !== val) {
  305. const _self = $(this);
  306. postData('/setting/user/unit/save', { type: 'update', id: oneUnit.id, name: oneUnit.name, val_name, val}, function (result) {
  307. oneUnit[val_name] = val;
  308. _self.val(oneUnit[val_name]);
  309. $('#unit_list tr[class="table-warning"]').children('td').eq(3).text(accountGroup[parseInt(oneUnit[val_name])]);
  310. }, function () {
  311. _self.val(oneUnit[val_name]);
  312. })
  313. } else {
  314. $(this).val(oneUnit[val_name]);
  315. }
  316. });
  317. // 删除单位弹窗
  318. $('#del-modal-btn').click(function () {
  319. if (!oneUnit) {
  320. toastr.error('所选单位有误,请重新选择');
  321. return false;
  322. }
  323. if (oneUnit.account_num === 0) {
  324. $('.del-btn').show();
  325. $('.not-del-btn').hide();
  326. } else {
  327. $('.del-btn').hide();
  328. $('.not-del-btn').show();
  329. }
  330. });
  331. // 删除单位
  332. $('#delete-unit').click(function () {
  333. if (!oneUnit) {
  334. toastr.error('所选单位有误,请重新选择');
  335. return false;
  336. }
  337. postData('/setting/user/unit/save', { type: 'delete', name: oneUnit.name, id: oneUnit.id }, function (result) {
  338. window.location.href = '/setting/user/unit';
  339. })
  340. });
  341. // 上传签章
  342. $('#sign-upload').change(function () {
  343. if (!oneUnit) {
  344. toastr.error('所选单位有误,请重新选择');
  345. return false;
  346. }
  347. const file = this.files[0];
  348. const ext = file.name.toLowerCase().split('.').splice(-1)[0];
  349. const imgStr = /(jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG)$/;
  350. if (!imgStr.test(ext)) {
  351. toastr.error('请上传正确的图片格式文件');
  352. return
  353. }
  354. if ($(this).val()) {
  355. const formData = new FormData();
  356. formData.append('file', this.files[0]);
  357. formData.append('id', oneUnit.id);
  358. postDataWithFile('/setting/user/unit/upload', formData, function (result) {
  359. const html = '<img src="'+ fujianOssPath + result.sign_path +'" width="120">';
  360. $('#sign-show').html(html);
  361. $('#sign-upload').val('');
  362. oneUnit.sign_path = result.sign_path;
  363. $('#upload-sign').hide();
  364. $('#delete-sign').show();
  365. toastr.success('上传成功');
  366. });
  367. }
  368. });
  369. // 移除签章
  370. $('#delete-sign').click(function () {
  371. if (!oneUnit) {
  372. toastr.error('所选单位有误,请重新选择');
  373. return false;
  374. }
  375. postData('/setting/user/unit/save', { type: 'del-sign', id: oneUnit.id }, function (result) {
  376. $('#sign-show').html('');
  377. toastr.warning('已移除');
  378. oneUnit.sign_path = null;
  379. $('#upload-sign').show();
  380. $('#delete-sign').hide();
  381. })
  382. });
  383. });
  384. function checkPasswordForm() {
  385. try {
  386. if ($('#edit-password input[name="account"]').val() == '' || $('#edit-password input[name="account"]').hasClass('is-invalid')) {
  387. throw '账号不能为空或已存在';
  388. }
  389. const resetPassword = $('#edit-password input[name="reset_password"]').val();
  390. if (resetPassword.length < 6) {
  391. throw '密码长度不能小于6';
  392. }
  393. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test(resetPassword)) {
  394. throw '密码只支持英文数字及符号';
  395. }
  396. // 判断新密码的强度
  397. const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
  398. if (!reg.test(resetPassword)) {
  399. throw '请设置至少包含数字和字母的密码';
  400. }
  401. } catch (err) {
  402. toastr.error(err);
  403. return false;
  404. }
  405. }
  406. function setUnitRightHtml(id) {
  407. const one = _.find(unitList, { id });
  408. if (one) {
  409. $('#unit_name').val(one.name);
  410. $('#unit_corporation').val(one.corporation);
  411. $('#unit_credit_code').val(one.credit_code);
  412. $('#unit_tel').val(one.tel);
  413. $('#unit_website').val(one.website);
  414. $('#unit_region').val(one.region);
  415. $('#unit_address').val(one.address);
  416. $('#unit_basic').val(one.basic);
  417. $('#unit_type').val(one.type);
  418. if(one.sign_path) {
  419. $('#sign-show').html('<img src="' + fujianOssPath + one.sign_path + '" width="120">');
  420. $('#delete-sign').show();
  421. $('#upload-sign').hide();
  422. } else {
  423. $('#sign-show').html('');
  424. $('#delete-sign').hide();
  425. $('#upload-sign').show();
  426. }
  427. oneUnit = one;
  428. }
  429. }
  430. /**
  431. * 表单检测
  432. */
  433. function checkUserForm(status) {
  434. try {
  435. if (status === 'add') {
  436. if ($('#add-user input[name="account_group"]').val() == 0) {
  437. throw '请选择账号组';
  438. }
  439. if ($('#add-user input[name="account"]').val() == '' || $('#add-user input[name="account"]').hasClass('is-invalid')) {
  440. throw '账号不能为空或已存在';
  441. }
  442. if ($('#add-user input[name="password"]').val() == '' || $('#add-user input[name="password"]').val().length < 6) {
  443. throw '密码不能为空或不能小于6位';
  444. }
  445. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test($('#add-user input[name="password"]').val())) {
  446. throw '密码只支持英文数字及符号';
  447. }
  448. // 判断新密码的强度
  449. const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
  450. if (!reg.test($('#add-user input[name="password"]').val())) {
  451. throw '请设置至少包含数字和字母的密码';
  452. }
  453. if ($('#add-user input[name="name"]').val() == '') {
  454. throw '姓名不能为空';
  455. }
  456. if (_.findIndex(unitList, { name: $('#add-user select[name="company"]').val() }) === -1) {
  457. throw '请选择单位名称';
  458. }
  459. if ($('#add-user input[name="role"]').val() == '') {
  460. throw '职位名称不能为空';
  461. }
  462. $('#add-user input[name="account"]').val(trimInvalidChar($('#add-user input[name="account"]').val()));
  463. $('#add-user input[name="name"]').val(trimInvalidChar($('#add-user input[name="name"]').val()));
  464. // $('#add-user input[name="company"]').val(trimInvalidChar($('#add-user input[name="company"]').val()));
  465. $('#add-user input[name="role"]').val(trimInvalidChar($('#add-user input[name="role"]').val()));
  466. $('#add-user input[name="telephone"]').val(trimInvalidChar($('#add-user input[name="telephone"]').val()));
  467. } else {
  468. if ($('#edit-user input[name="account_group"]').val() == 0) {
  469. throw '请选择账号组';
  470. }
  471. if ($('#edit-user input[name="account"]').val() == '' || $('#add-user input[name="account"]').hasClass('is-invalid')) {
  472. throw '账号不能为空或已存在';
  473. }
  474. if ($('#edit-user input[name="name"]').val() == '') {
  475. throw '姓名不能为空';
  476. }
  477. if (_.findIndex(unitList, { name: $('#edit-user select[name="company"]').val() }) === -1) {
  478. throw '请选择单位名称';
  479. }
  480. if ($('#edit-user input[name="role"]').val() == '') {
  481. throw '职位名称不能为空';
  482. }
  483. $('#edit-user input[name="account"]').val(trimInvalidChar($('#edit-user input[name="account"]').val()));
  484. $('#edit-user input[name="name"]').val(trimInvalidChar($('#edit-user input[name="name"]').val()));
  485. // $('#edit-user input[name="company"]').val(trimInvalidChar($('#edit-user input[name="company"]').val()));
  486. $('#edit-user input[name="role"]').val(trimInvalidChar($('#edit-user input[name="role"]').val()));
  487. $('#edit-user input[name="telephone"]').val(trimInvalidChar($('#edit-user input[name="telephone"]').val()));
  488. }
  489. } catch (err) {
  490. toastr.error(err);
  491. return false;
  492. }
  493. }
  494. /**
  495. * 表单检测
  496. */
  497. function checkUnitForm() {
  498. try {
  499. if ($('#add-company input[name="name"]').val() == '') {
  500. throw '单位名称不能为空';
  501. }
  502. if ($('#add-company select[name="type"]').val() == 0) {
  503. throw '请选择类型';
  504. }
  505. // 检测同名
  506. if (_.findIndex(unitList, { name: $('#add-company input[name="name"]').val() }) !== -1) {
  507. throw '已存在对应的单位名称';
  508. }
  509. } catch (err) {
  510. toastr.error(err);
  511. return false;
  512. }
  513. }
  514. /**
  515. * 随机密码(必须包含数字和字母)
  516. */
  517. function randPassword() {
  518. const result = [];
  519. // 随机6-10位
  520. const length = Math.ceil(Math.random() * 2 + 6);
  521. let numberSeed = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  522. let stringSeed = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  523. 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  524. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
  525. const numRan = numberSeed[Math.floor((Math.random() * numberSeed.length))];
  526. const strRan = stringSeed[Math.floor((Math.random() * stringSeed.length))];
  527. const randSeed = stringSeed.concat(numberSeed);
  528. const seedLength = randSeed.length - 1;
  529. for (let i = 0; i < length; i++) {
  530. const index = Math.ceil(Math.random() * seedLength);
  531. result.push(randSeed[index]);
  532. }
  533. result.splice(Math.floor((Math.random() * result.length)), 0, numRan);
  534. result.splice(Math.floor((Math.random() * result.length)), 0, strRan);
  535. return result.join('');
  536. }