setting.js 34 KB

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