setting.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. $('#sign_permission').hide();
  133. const account = $(this).data('account');
  134. $('#edit-user2 input[name="id"]').val(account.id);
  135. $('#edit-user2 input[name="unit_sign_path"]').val(account.unit_sign_path);
  136. // 权限赋值
  137. if (account.permission !== '') {
  138. const permission = JSON.parse(account.permission);
  139. for (const pm in permission) {
  140. if (pm === 'tender' && permission[pm].indexOf('1') !== -1) {
  141. $('#edit-user2 input[name="cooperation"]').attr('disabled', false);
  142. } else {
  143. $('#edit-user2 input[name="cooperation"]').attr('disabled', true);
  144. }
  145. if (pm === 'tender' && permission[pm].indexOf('5') !== -1) {
  146. $('#edit-user2 input:checkbox[id="change_1"]').prop('checked', true);
  147. }
  148. if (pm === 'tender' && permission[pm].indexOf('4') !== -1) {
  149. $('#edit-user2 input:checkbox[id="material_1"]').prop('checked', true);
  150. }
  151. if (allPermission[pm].type === 'checkbox') {
  152. for (const index of permission[pm]) {
  153. $('#edit-user2 input:checkbox[id="' + pm + '_' + index + '"]').prop('checked', true);
  154. }
  155. } else if (allPermission[pm].type === 'radio') {
  156. $('#edit-user2 input:radio[id="' + pm + '_' + permission[pm] + '"]').prop('checked', true);
  157. }
  158. if (pm === 'other' && permission[pm].indexOf('1') !== -1) {
  159. $('#sign_permission').show();
  160. }
  161. }
  162. }
  163. // 协作赋值
  164. $('#edit-user2 input:radio[name="cooperation"][value="' + account.cooperation + '"]').prop('checked', true);
  165. // 微信通知赋值
  166. if (account.notice_again) {
  167. const noticeAgain = JSON.parse(account.notice_again);
  168. $('#again_all').prop('checked', noticeAgain.checked);
  169. for (const sp in noticeAgainConst.sp) {
  170. $('#again_' + sp).prop('checked', noticeAgain.sp[sp] !== undefined ? noticeAgain.sp[sp] : false);
  171. }
  172. }
  173. });
  174. // 选择创建标段功能后可选协作办公
  175. $('#edit-user2 input:checkbox').click(function () {
  176. if ($(this).attr('id') === 'tender_1') {
  177. if ($(this).is(':checked')) {
  178. $('#edit-user2 input[name="cooperation"]').attr('disabled', false);
  179. } else {
  180. $('#edit-user2 input[name="cooperation"]').attr('disabled', true);
  181. }
  182. }
  183. if ($(this).attr('id') === 'other_1') {
  184. if ($(this).is(':checked')) {
  185. $('#sign_permission').show();
  186. $('#sign_permission').click();
  187. } else {
  188. $('#sign_permission').hide();
  189. }
  190. }
  191. });
  192. $('#sign_permission').on('click', function () {
  193. const uid = $('#edit-user2 input[name="id"]').val();
  194. if (!uid) {
  195. toastr.error('请先选择用户');
  196. return;
  197. }
  198. const user = _.find(accountData, { id: parseInt(uid) });
  199. if (!user) {
  200. toastr.error('用户不存在');
  201. return;
  202. }
  203. let html = '';
  204. if (user.company) {
  205. const unit = _.find(unitList, { name: user.company });
  206. const unit_sign_path = $('#edit-user2 input[name="unit_sign_path"]').val();
  207. if (unit) {
  208. const paths = unit.sign_path ? unit.sign_path.split('&%&') : [];
  209. if (paths.length > 0) {
  210. console.log(paths);
  211. for (const sp of paths) {
  212. const checked = unit_sign_path && unit_sign_path.indexOf(sp) !== -1 ? 'checked' : '';
  213. html += '<div class="col-3 text-center my-2"><img src="'+ fujianOssPath + sp +'" width="120">' +
  214. '<div class="w-100 mt-2"><input type="checkbox" name="signpath[]" '+ (checked ? 'checked' : '') +' data-path="' + sp + '" />' +
  215. '</div></div>';
  216. }
  217. }
  218. }
  219. }
  220. $('#unit-sign').html(html);
  221. $('#sign_permission_modal').modal('show');
  222. });
  223. $('#set_unit_sign').on('click', function () {
  224. $('#sign_permission_modal').modal('hide');
  225. const uid = $('#edit-user2 input[name="id"]').val();
  226. const signPaths = [];
  227. $('#unit-sign input[name="signpath[]"]:checked').each(function () {
  228. signPaths.push($(this).data('path'));
  229. });
  230. $('#edit-user2 input[name="unit_sign_path"]').val(signPaths.join('&%&'));
  231. });
  232. // 解绑第三方平台
  233. $('.unlink-user').on('click', function () {
  234. const id = $(this).data('account');
  235. const accountData = _.find(accountList, { id });
  236. console.log(accountData);
  237. $('#bind_account').text(accountData.name + ' ' + accountData.mobile);
  238. $('#account_id').val(id);
  239. })
  240. // 设置显示默认
  241. $('body').on('click', '#set-default', function () {
  242. const attid = $(this).data('attid');
  243. const data = {id: attid};
  244. postData('/setting/show/update', data, function (result) {
  245. let html = ''
  246. result.forEach((item, idx) => {
  247. html += `<li class="list-group-item">${item.label_name}`
  248. 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>`
  249. })
  250. // <li class="list-group-item">
  251. // 标段列表<a href="#" class="btn btn-primary btn-sm pull-right">设为默认</a>
  252. // </li>
  253. // <li class="list-group-item">金额概况<span class="pull-right">默认</span></li>
  254. // <li class="list-group-item">
  255. // 计量进度<a href="#" class="btn btn-primary btn-sm pull-right">设为默认</a>
  256. // </li>
  257. $('#tenderList').empty()
  258. $('#tenderList').append(html)
  259. const item = result.find((i, idx) => idx=== attid)
  260. $('#nav_tender').attr('href', item.path)
  261. });
  262. });
  263. const $activeItem = $('.list-group-item.active');
  264. if ($activeItem.length) {
  265. const $scrollContainer = $activeItem.closest('.col-3');
  266. const scrollTop = $activeItem.offset().top - $scrollContainer.offset().top + $scrollContainer.scrollTop();
  267. $scrollContainer.animate({ scrollTop: scrollTop }, 100);
  268. }
  269. // 设置页显示数目
  270. $('.nav-tabs .nav-link').each(function () {
  271. const pageSize = getLocalCache('account-pageSize') ? getLocalCache('account-pageSize') : '';
  272. if (getLocalCache('account-pageSize') && $(this).attr('href').indexOf('pageSize') === -1 && $(this).attr('href').indexOf('unit') === -1) {
  273. $(this).attr('href', $(this).attr('href') + '?pageSize=' + getLocalCache('account-pageSize'));
  274. }
  275. });
  276. // 设置页显示数目
  277. $('#user-list .list-group-item').each(function (k,v) {
  278. const pageSize = getLocalCache('account-pageSize') ? getLocalCache('account-pageSize') : '';
  279. if (pageSize) {
  280. $(this).attr('href', $(this).attr('href') + '&pageSize=' + pageSize);
  281. }
  282. })
  283. // 参建单位页切换单位右侧显示
  284. $('body').on('click', '#unit_list tr', function () {
  285. const id = parseInt($(this).data('id'));
  286. $(this).siblings('tr').removeClass('table-warning');
  287. $(this).addClass('table-warning');
  288. setUnitRightHtml(id);
  289. });
  290. // 参建单位编辑
  291. // 回车提交
  292. $('#one_unit input').on('keypress', function () {
  293. if(window.event.keyCode === 13) {
  294. $(this).blur();
  295. }
  296. });
  297. $('#one_unit input').blur(function () {
  298. const val_name = $(this).data('name');
  299. let val = _.trim($(this).val()) !== '' ? _.trim($(this).val()) : null;
  300. if (!oneUnit) {
  301. toastr.error('所选单位有误,请重新选择');
  302. return false;
  303. }
  304. switch(val_name) {
  305. case 'name':
  306. if(val && val.length > 100) {
  307. toastr.error('单位名称超过100个字,请缩减名称');
  308. $(this).val(oneUnit[val_name]);
  309. return false;
  310. }
  311. const tongming = _.find(unitList, { name: val });
  312. if (tongming && tongming.id !== oneUnit.id) {
  313. toastr.error('单位名称不能重复');
  314. $(this).val(oneUnit[val_name]);
  315. return false;
  316. }
  317. break;
  318. default:
  319. if(val && val.length > 255) {
  320. toastr.error('超出字段范围,请缩减');
  321. $(this).val(oneUnit[val_name]);
  322. return false;
  323. }
  324. break;
  325. }
  326. if(oneUnit[val_name] !== val) {
  327. const _self = $(this);
  328. postData('/setting/user/unit/save', { type: 'update', id: oneUnit.id, val_name, val}, function (result) {
  329. oneUnit[val_name] = val;
  330. _self.val(oneUnit[val_name]);
  331. if (val_name === 'name') {
  332. oneUnit.account_num = result.account_num;
  333. $('#unit_list tr[class="table-warning"]').find('a').text(oneUnit[val_name]);
  334. $('#unit_list tr[class="table-warning"]').children('td').eq(2).text(result.account_num);
  335. }
  336. }, function () {
  337. _self.val(oneUnit[val_name]);
  338. })
  339. } else {
  340. $(this).val(oneUnit[val_name]);
  341. }
  342. });
  343. $('#unit_user_permission').change(function () {
  344. const val_name = 'sign_permission';
  345. let val = $(this).is(':checked') ? 1 : 0;
  346. if (!oneUnit) {
  347. toastr.error('所选单位有误,请重新选择');
  348. return false;
  349. }
  350. if(oneUnit[val_name] !== val) {
  351. postData('/setting/user/unit/save', {type: 'update', id: oneUnit.id, val_name, val}, function (result) {
  352. oneUnit[val_name] = val;
  353. });
  354. }
  355. });
  356. $('#one_unit textarea').blur(function () {
  357. const val_name = $(this).data('name');
  358. let val = _.trim($(this).val()) !== '' ? _.trim($(this).val()) : null;
  359. if (!oneUnit) {
  360. toastr.error('所选单位有误,请重新选择');
  361. return false;
  362. }
  363. if(oneUnit[val_name] !== val) {
  364. const _self = $(this);
  365. postData('/setting/user/unit/save', { type: 'update', id: oneUnit.id, val_name, val}, function (result) {
  366. oneUnit[val_name] = val;
  367. _self.val(oneUnit[val_name]);
  368. $('#unit_list tr[class="table-warning"]').children('td').eq(4).text(oneUnit[val_name]);
  369. }, function () {
  370. _self.val(oneUnit[val_name]);
  371. })
  372. } else {
  373. $(this).val(oneUnit[val_name]);
  374. }
  375. });
  376. $('#one_unit select').change(function () {
  377. const val_name = $(this).attr('data-name');
  378. let val = _.trim($(this).val()) !== '' ? _.trim($(this).val()) : null;
  379. if (!oneUnit) {
  380. toastr.error('所选单位有误,请重新选择');
  381. return false;
  382. }
  383. if(oneUnit[val_name] !== val) {
  384. const _self = $(this);
  385. postData('/setting/user/unit/save', { type: 'update', id: oneUnit.id, name: oneUnit.name, val_name, val}, function (result) {
  386. oneUnit[val_name] = val;
  387. _self.val(oneUnit[val_name]);
  388. $('#unit_list tr[class="table-warning"]').children('td').eq(3).text(accountGroup[parseInt(oneUnit[val_name])]);
  389. }, function () {
  390. _self.val(oneUnit[val_name]);
  391. })
  392. } else {
  393. $(this).val(oneUnit[val_name]);
  394. }
  395. });
  396. // 删除单位弹窗
  397. $('#del-modal-btn').click(function () {
  398. if (!oneUnit) {
  399. toastr.error('所选单位有误,请重新选择');
  400. return false;
  401. }
  402. if (oneUnit.account_num === 0) {
  403. $('.del-btn').show();
  404. $('.not-del-btn').hide();
  405. } else {
  406. $('.del-btn').hide();
  407. $('.not-del-btn').show();
  408. }
  409. });
  410. $('#mseal').on('shown.bs.modal', function () {
  411. if (!oneUnit) {
  412. toastr.error('所选单位有误,请重新选择');
  413. return false;
  414. }
  415. makeManageSignHtml(oneUnit.sign_path);
  416. });
  417. // 删除单位
  418. $('#delete-unit').click(function () {
  419. if (!oneUnit) {
  420. toastr.error('所选单位有误,请重新选择');
  421. return false;
  422. }
  423. postData('/setting/user/unit/save', { type: 'delete', name: oneUnit.name, id: oneUnit.id }, function (result) {
  424. window.location.href = '/setting/user/unit';
  425. })
  426. });
  427. // 上传签章
  428. $('#sign-upload').change(function () {
  429. if (!oneUnit) {
  430. toastr.error('所选单位有误,请重新选择');
  431. return false;
  432. }
  433. const files = Array.from(this.files)
  434. const valiData = files.map(v => {
  435. const ext = v.name.substring(v.name.lastIndexOf('.') + 1)
  436. return {
  437. size: v.size,
  438. ext
  439. }
  440. });
  441. if (validateFiles(valiData)) {
  442. if (files.length) {
  443. const formData = new FormData();
  444. formData.append('id', oneUnit.id);
  445. files.forEach(file => {
  446. formData.append('name', file.name);
  447. formData.append('size', file.size);
  448. formData.append('file', file);
  449. });
  450. postDataWithFile('/setting/user/unit/upload', formData, function (result) {
  451. oneUnit.sign_path = result.sign_path;
  452. makeSignHtml(oneUnit.sign_path);
  453. $('#sign-upload').val('');
  454. toastr.success('上传成功');
  455. });
  456. }
  457. }
  458. });
  459. $('#manage-sign-upload').change(function () {
  460. if (!oneUnit) {
  461. toastr.error('所选单位有误,请重新选择');
  462. return false;
  463. }
  464. const files = Array.from(this.files)
  465. const valiData = files.map(v => {
  466. const ext = v.name.substring(v.name.lastIndexOf('.') + 1)
  467. return {
  468. size: v.size,
  469. ext
  470. }
  471. });
  472. if (validateFiles(valiData)) {
  473. if (files.length) {
  474. const formData = new FormData();
  475. formData.append('id', oneUnit.id);
  476. files.forEach(file => {
  477. formData.append('name', file.name);
  478. formData.append('size', file.size);
  479. formData.append('file', file);
  480. });
  481. postDataWithFile('/setting/user/unit/upload', formData, function (result) {
  482. oneUnit.sign_path = result.sign_path;
  483. makeManageSignHtml(oneUnit.sign_path);
  484. makeSignHtml(oneUnit.sign_path);
  485. $('#manage-sign-upload').val('');
  486. toastr.success('上传成功');
  487. });
  488. }
  489. }
  490. });
  491. // 移除签章
  492. $('body').on('click', '#manage-unit-sign .delete-sign', function () {
  493. if (!oneUnit) {
  494. toastr.error('所选单位有误,请重新选择');
  495. return false;
  496. }
  497. const delPath = $(this).attr('data-path');
  498. if (!delPath) {
  499. toastr.error('所选签章有误,请重新选择');
  500. return false;
  501. }
  502. postData('/setting/user/unit/save', { type: 'del-sign', id: oneUnit.id, path: delPath }, function (result) {
  503. oneUnit.sign_path = result;
  504. makeManageSignHtml(oneUnit.sign_path);
  505. makeSignHtml(oneUnit.sign_path);
  506. toastr.warning('已移除');
  507. $('#delete-sign').hide();
  508. })
  509. });
  510. // 导入账号功能
  511. let importFile = null;
  512. $('#upload-xls-file').change(function () {
  513. const file = this.files[0];
  514. importFile = file;
  515. });
  516. $('#import-bills-btn').click(function() {
  517. if (!importFile) {
  518. toastr.error('请选择excel文件再确定');
  519. return;
  520. }
  521. const ext = importFile.name.toLowerCase().split('.').splice(-1)[0];
  522. const imgStr = /(xls|xlsx|XLS|XLSX)$/;
  523. if (!imgStr.test(ext)) {
  524. toastr.error('请导入正确格式的excel文件。');
  525. return
  526. }
  527. const fileReader = new FileReader();
  528. fileReader.onload = async function(ev) {
  529. try{
  530. const data = ev.target.result;
  531. const tree = [];
  532. const includeSpec = $('#xls-spec').is(':checked');
  533. if (/(xls|xlsx|XLS|XLSX)$/.test(ext)) {
  534. const workbook = XLSX.read(data, {type: 'binary'}); // 以二进制流方式读取得到整份excel表格对象
  535. const jsonData = transExcel(XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], { defval: null }));
  536. console.log(jsonData);
  537. // 判断新密码的强度
  538. const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
  539. for (const j of jsonData) {
  540. if (j.account === undefined || j.account === null || j.password === undefined || j.password === null ||
  541. j.name === undefined || j.name === null || j.company === undefined || j.company === null ||
  542. j.role === undefined || j.role === null || j.mobile === undefined || j.telephone === undefined) {
  543. continue;
  544. }
  545. if (!reg.test(j.password)) {
  546. continue;
  547. }
  548. tree.push(j);
  549. // if (_.findIndex(tree, function (o) { return o.account === j.account; }) === -1) {
  550. // tree.push(j);
  551. // }
  552. }
  553. }
  554. console.log(tree);
  555. if (tree.length === 0) {
  556. toastr.error('导入数据为空,请检查excel是否符合模版要求。');
  557. return
  558. }
  559. postData('/setting/user/unit/save', { type: 'import-users', users: tree }, function (result) {
  560. if (result.insertNum > 0) {
  561. toastr.success('成功导入了' + result.insertNum + '个账号');
  562. if (result.overMax) {
  563. toastr.warning('添加账号个数受限,部分账号无法导入,请去后台开放限制。');
  564. }
  565. setTimeout(function () {
  566. window.location.reload();
  567. }, 1500);
  568. } else if (result.insertNum === 0 && !result.overMax) {
  569. toastr.error('未能导入任何账号,请检查账号、公司是否已存在或excel是否符合模版要求。');
  570. } else if (result.insertNum === 0 && result.overMax) {
  571. toastr.warning('添加账号个数受限,账号无法导入,请去后台开放限制。');
  572. }
  573. })
  574. } catch (error) {
  575. console.log(error);
  576. toastr.error(error);
  577. return
  578. }
  579. };
  580. // 以二进制方式打开文件
  581. fileReader.readAsBinaryString(importFile);
  582. $('#upload-xls-file').val('');
  583. importFile = null;
  584. });
  585. function transExcel(results) {
  586. const mapInfo = {
  587. '登录账号': 'account',
  588. '登录密码': 'password',
  589. '姓名': 'name',
  590. '单位名称': 'company',
  591. '职位': 'role',
  592. '手机': 'mobile',
  593. '电话': 'telephone',
  594. };
  595. return results.map(zhObj => {
  596. const enObj = {}
  597. const zhKeys = Object.keys(zhObj);
  598. zhKeys.forEach(zhKey => {
  599. const enKey = mapInfo[zhKey];
  600. enObj[enKey] = zhObj[zhKey] && _.isString(zhObj[zhKey]) ? _.trim(zhObj[zhKey]) : zhObj[zhKey];
  601. });
  602. return enObj
  603. })
  604. }
  605. });
  606. function checkPasswordForm() {
  607. try {
  608. if ($('#edit-password input[name="account"]').val() == '' || $('#edit-password input[name="account"]').hasClass('is-invalid')) {
  609. throw '账号不能为空或已存在';
  610. }
  611. const resetPassword = $('#edit-password input[name="reset_password"]').val();
  612. if (resetPassword.length < 6) {
  613. throw '密码长度不能小于6';
  614. }
  615. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test(resetPassword)) {
  616. throw '密码只支持英文数字及符号';
  617. }
  618. // 判断新密码的强度
  619. const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
  620. if (!reg.test(resetPassword)) {
  621. throw '请设置至少包含数字和字母的密码';
  622. }
  623. } catch (err) {
  624. toastr.error(err);
  625. return false;
  626. }
  627. }
  628. function makeManageSignHtml(signPath) {
  629. const paths = signPath ? signPath.split('&%&') : [];
  630. let html = '';
  631. if (paths.length > 0) {
  632. for (const sp of paths) {
  633. html += '<div class="col-3 text-center my-2"><img src="'+ fujianOssPath + sp +'" width="120"><div class="w-100"><button class="btn btn-sm btn-outline-danger delete-sign" data-path="' + sp + '">移除</button></div></div>';
  634. }
  635. }
  636. $('#manage-unit-sign').html(html);
  637. }
  638. function makeSignHtml(signPath) {
  639. const paths = signPath ? signPath.split('&%&') : [];
  640. let html = '';
  641. if (paths.length > 0) {
  642. for (const sp of paths.slice(0, 5)) {
  643. html += '<img src="' + fujianOssPath + sp + '" width="120" class="mr-1">';
  644. }
  645. }
  646. $('#sign-show').html(html);
  647. }
  648. function setUnitRightHtml(id) {
  649. const one = _.find(unitList, { id });
  650. if (one) {
  651. $('#unit_name').val(one.name);
  652. $('#unit_corporation').val(one.corporation);
  653. $('#unit_credit_code').val(one.credit_code);
  654. $('#unit_tel').val(one.tel);
  655. $('#unit_website').val(one.website);
  656. $('#unit_region').val(one.region);
  657. $('#unit_address').val(one.address);
  658. $('#unit_basic').val(one.basic);
  659. $('#unit_type').val(one.type);
  660. if(one.sign_path) {
  661. makeSignHtml(one.sign_path);
  662. // $('#upload-sign').hide();
  663. } else {
  664. $('#sign-show').html('');
  665. // $('#upload-sign').show();
  666. }
  667. oneUnit = one;
  668. $('#add_user_company').val(one.name);
  669. $('#unit_user_permission').prop('checked', !!oneUnit.sign_permission);
  670. }
  671. }
  672. /**
  673. * 表单检测
  674. */
  675. function checkUserForm(status) {
  676. try {
  677. if (status === 'add') {
  678. if ($('#add-user input[name="account_group"]').val() == 0) {
  679. throw '请选择账号组';
  680. }
  681. if ($('#add-user input[name="account"]').val() == '' || $('#add-user input[name="account"]').hasClass('is-invalid')) {
  682. throw '账号不能为空或已存在';
  683. }
  684. if ($('#add-user input[name="password"]').val() == '' || $('#add-user input[name="password"]').val().length < 6) {
  685. throw '密码不能为空或不能小于6位';
  686. }
  687. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test($('#add-user input[name="password"]').val())) {
  688. throw '密码只支持英文数字及符号';
  689. }
  690. // 判断新密码的强度
  691. const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
  692. if (!reg.test($('#add-user input[name="password"]').val())) {
  693. throw '请设置至少包含数字和字母的密码';
  694. }
  695. if ($('#add-user input[name="name"]').val() == '') {
  696. throw '姓名不能为空';
  697. }
  698. if (_.findIndex(unitList, { name: $('#add-user select[name="company"]').val() }) === -1) {
  699. throw '请选择单位名称';
  700. }
  701. if ($('#add-user input[name="role"]').val() == '') {
  702. throw '职位名称不能为空';
  703. }
  704. $('#add-user input[name="account"]').val(trimInvalidChar($('#add-user input[name="account"]').val()));
  705. $('#add-user input[name="name"]').val(trimInvalidChar($('#add-user input[name="name"]').val()));
  706. // $('#add-user input[name="company"]').val(trimInvalidChar($('#add-user input[name="company"]').val()));
  707. $('#add-user input[name="role"]').val(trimInvalidChar($('#add-user input[name="role"]').val()));
  708. $('#add-user input[name="telephone"]').val(trimInvalidChar($('#add-user input[name="telephone"]').val()));
  709. } else {
  710. if ($('#edit-user input[name="account_group"]').val() == 0) {
  711. throw '请选择账号组';
  712. }
  713. if ($('#edit-user input[name="account"]').val() == '' || $('#add-user input[name="account"]').hasClass('is-invalid')) {
  714. throw '账号不能为空或已存在';
  715. }
  716. if ($('#edit-user input[name="name"]').val() == '') {
  717. throw '姓名不能为空';
  718. }
  719. if (_.findIndex(unitList, { name: $('#edit-user select[name="company"]').val() }) === -1) {
  720. throw '请选择单位名称';
  721. }
  722. if ($('#edit-user input[name="role"]').val() == '') {
  723. throw '职位名称不能为空';
  724. }
  725. $('#edit-user input[name="account"]').val(trimInvalidChar($('#edit-user input[name="account"]').val()));
  726. $('#edit-user input[name="name"]').val(trimInvalidChar($('#edit-user input[name="name"]').val()));
  727. // $('#edit-user input[name="company"]').val(trimInvalidChar($('#edit-user input[name="company"]').val()));
  728. $('#edit-user input[name="role"]').val(trimInvalidChar($('#edit-user input[name="role"]').val()));
  729. $('#edit-user input[name="telephone"]').val(trimInvalidChar($('#edit-user input[name="telephone"]').val()));
  730. }
  731. } catch (err) {
  732. toastr.error(err);
  733. return false;
  734. }
  735. }
  736. function validateFiles(files) {
  737. if (files.length > 10) {
  738. toastr.error('至多同时上传10个文件');
  739. return false
  740. }
  741. return files.every(file => {
  742. if (file.size > 1024 * 1024 * 50) {
  743. toastr.error('文件大小限制为50MB');
  744. return false
  745. }
  746. const imgStr = /(jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG)$/;
  747. if (!imgStr.test(file.ext)) {
  748. toastr.error('请上传正确的图片格式文件');
  749. return false
  750. }
  751. return true
  752. })
  753. }
  754. /**
  755. * 表单检测
  756. */
  757. function checkUnitForm() {
  758. try {
  759. if ($('#add-company input[name="name"]').val() == '') {
  760. throw '单位名称不能为空';
  761. }
  762. if ($('#add-company select[name="type"]').val() == 0) {
  763. throw '请选择类型';
  764. }
  765. // 检测同名
  766. if (_.findIndex(unitList, { name: $('#add-company input[name="name"]').val() }) !== -1) {
  767. throw '已存在对应的单位名称';
  768. }
  769. } catch (err) {
  770. toastr.error(err);
  771. return false;
  772. }
  773. }
  774. /**
  775. * 随机密码(必须包含数字和字母)
  776. */
  777. function randPassword() {
  778. const result = [];
  779. // 随机6-10位
  780. const length = Math.ceil(Math.random() * 2 + 6);
  781. let numberSeed = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  782. let stringSeed = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  783. 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  784. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
  785. const numRan = numberSeed[Math.floor((Math.random() * numberSeed.length))];
  786. const strRan = stringSeed[Math.floor((Math.random() * stringSeed.length))];
  787. const randSeed = stringSeed.concat(numberSeed);
  788. const seedLength = randSeed.length - 1;
  789. for (let i = 0; i < length; i++) {
  790. const index = Math.ceil(Math.random() * seedLength);
  791. result.push(randSeed[index]);
  792. }
  793. result.splice(Math.floor((Math.random() * result.length)), 0, numRan);
  794. result.splice(Math.floor((Math.random() * result.length)), 0, strRan);
  795. return result.join('');
  796. }