setting.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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[data-target="#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. $('#unit_list tr').click(function () {
  218. const id = parseInt($(this).data('id'));
  219. const one = _.find(unitList, { id });
  220. if (one) {
  221. $(this).siblings('tr').removeClass('table-warning');
  222. $(this).addClass('table-warning');
  223. $('#unit_name').val(one.name);
  224. $('#unit_corporation').val(one.corporation);
  225. $('#unit_credit_code').val(one.credit_code);
  226. $('#unit_tel').val(one.tel);
  227. $('#unit_website').val(one.website);
  228. $('#unit_region').val(one.region);
  229. $('#unit_address').val(one.address);
  230. $('#unit_basic').val(one.basic);
  231. $('#unit_type').val(one.type);
  232. if(one.sign_path) {
  233. $('#sign-show').html('<img src="' + fujianOssPath + one.sign_path + '" width="120">');
  234. $('#delete-sign').show();
  235. $('#upload-sign').hide();
  236. } else {
  237. $('#sign-show').html('');
  238. $('#delete-sign').hide();
  239. $('#upload-sign').show();
  240. }
  241. oneUnit = one;
  242. }
  243. });
  244. // 参建单位编辑
  245. // 回车提交
  246. $('#one_unit input').on('keypress', function () {
  247. if(window.event.keyCode === 13) {
  248. $(this).blur();
  249. }
  250. });
  251. $('#one_unit input').blur(function () {
  252. console.log('hello');
  253. const val_name = $(this).data('name');
  254. let val = _.trim($(this).val()) !== '' ? _.trim($(this).val()) : null;
  255. if (!oneUnit) {
  256. toastr.error('所选单位有误,请重新选择');
  257. return false;
  258. }
  259. switch(val_name) {
  260. case 'name':
  261. if(val && val.length > 100) {
  262. toastr.error('单位名称超过100个字,请缩减名称');
  263. $(this).val(oneUnit[val_name]);
  264. return false;
  265. }
  266. const tongming = _.find(unitList, { name: val });
  267. if (tongming && tongming.id !== oneUnit.id) {
  268. toastr.error('单位名称不能重复');
  269. $(this).val(oneUnit[val_name]);
  270. return false;
  271. }
  272. break;
  273. default:
  274. if(val && val.length > 255) {
  275. toastr.error('超出字段范围,请缩减');
  276. $(this).val(oneUnit[val_name]);
  277. return false;
  278. }
  279. break;
  280. }
  281. if(oneUnit[val_name] !== val) {
  282. const _self = $(this);
  283. postData('/setting/user/unit/save', { type: 'update', id: oneUnit.id, val_name, val}, function (result) {
  284. oneUnit[val_name] = val;
  285. _self.val(oneUnit[val_name]);
  286. if (val_name === 'name') {
  287. oneUnit.account_num = result.account_num;
  288. $('#unit_list tr[class="table-warning"]').find('a').text(oneUnit[val_name]);
  289. $('#unit_list tr[class="table-warning"]').children('td').eq(2).text(result.account_num);
  290. }
  291. }, function () {
  292. _self.val(oneUnit[val_name]);
  293. })
  294. } else {
  295. $(this).val(oneUnit[val_name]);
  296. }
  297. });
  298. $('#one_unit textarea').blur(function () {
  299. const val_name = $(this).data('name');
  300. let val = _.trim($(this).val()) !== '' ? _.trim($(this).val()) : null;
  301. if (!oneUnit) {
  302. toastr.error('所选单位有误,请重新选择');
  303. return false;
  304. }
  305. if(oneUnit[val_name] !== val) {
  306. const _self = $(this);
  307. postData('/setting/user/unit/save', { type: 'update', id: oneUnit.id, val_name, val}, function (result) {
  308. oneUnit[val_name] = val;
  309. _self.val(oneUnit[val_name]);
  310. $('#unit_list tr[class="table-warning"]').children('td').eq(4).text(oneUnit[val_name]);
  311. }, function () {
  312. _self.val(oneUnit[val_name]);
  313. })
  314. } else {
  315. $(this).val(oneUnit[val_name]);
  316. }
  317. });
  318. $('#one_unit select').change(function () {
  319. const val_name = $(this).attr('data-name');
  320. let val = _.trim($(this).val()) !== '' ? _.trim($(this).val()) : null;
  321. if (!oneUnit) {
  322. toastr.error('所选单位有误,请重新选择');
  323. return false;
  324. }
  325. if(oneUnit[val_name] !== val) {
  326. const _self = $(this);
  327. postData('/setting/user/unit/save', { type: 'update', id: oneUnit.id, name: oneUnit.name, val_name, val}, function (result) {
  328. oneUnit[val_name] = val;
  329. _self.val(oneUnit[val_name]);
  330. $('#unit_list tr[class="table-warning"]').children('td').eq(3).text(accountGroup[parseInt(oneUnit[val_name])]);
  331. }, function () {
  332. _self.val(oneUnit[val_name]);
  333. })
  334. } else {
  335. $(this).val(oneUnit[val_name]);
  336. }
  337. });
  338. // 删除单位弹窗
  339. $('#del-modal-btn').click(function () {
  340. if (!oneUnit) {
  341. toastr.error('所选单位有误,请重新选择');
  342. return false;
  343. }
  344. if (oneUnit.account_num === 0) {
  345. $('.del-btn').show();
  346. $('.not-del-btn').hide();
  347. } else {
  348. $('.del-btn').hide();
  349. $('.not-del-btn').show();
  350. }
  351. });
  352. // 删除单位
  353. $('#delete-unit').click(function () {
  354. if (!oneUnit) {
  355. toastr.error('所选单位有误,请重新选择');
  356. return false;
  357. }
  358. postData('/setting/user/unit/save', { type: 'delete', name: oneUnit.name, id: oneUnit.id }, function (result) {
  359. window.location.href = '/setting/user/unit';
  360. })
  361. });
  362. // 上传签章
  363. $('#sign-upload').change(function () {
  364. if (!oneUnit) {
  365. toastr.error('所选单位有误,请重新选择');
  366. return false;
  367. }
  368. const file = this.files[0];
  369. const ext = file.name.toLowerCase().split('.').splice(-1)[0];
  370. const imgStr = /(jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG)$/;
  371. if (!imgStr.test(ext)) {
  372. toastr.error('请上传正确的图片格式文件');
  373. return
  374. }
  375. if ($(this).val()) {
  376. const formData = new FormData();
  377. formData.append('file', this.files[0]);
  378. formData.append('id', oneUnit.id);
  379. postDataWithFile('/setting/user/unit/upload', formData, function (result) {
  380. const html = '<img src="'+ fujianOssPath + result.sign_path +'" width="120">';
  381. $('#sign-show').html(html);
  382. $('#sign-upload').val('');
  383. oneUnit.sign_path = result.sign_path;
  384. $('#upload-sign').hide();
  385. $('#delete-sign').show();
  386. toastr.success('上传成功');
  387. });
  388. }
  389. });
  390. // 移除签章
  391. $('#delete-sign').click(function () {
  392. if (!oneUnit) {
  393. toastr.error('所选单位有误,请重新选择');
  394. return false;
  395. }
  396. postData('/setting/user/unit/save', { type: 'del-sign', id: oneUnit.id }, function (result) {
  397. $('#sign-show').html('');
  398. toastr.warning('已移除');
  399. oneUnit.sign_path = null;
  400. $('#upload-sign').show();
  401. $('#delete-sign').hide();
  402. })
  403. });
  404. });
  405. function checkPasswordForm() {
  406. try {
  407. if ($('#edit-password input[name="account"]').val() == '' || $('#edit-password input[name="account"]').hasClass('is-invalid')) {
  408. throw '账号不能为空或已存在';
  409. }
  410. const resetPassword = $('#edit-password input[name="reset_password"]').val();
  411. if (resetPassword.length < 6) {
  412. throw '密码长度不能小于6';
  413. }
  414. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test(resetPassword)) {
  415. throw '密码只支持英文数字及符号';
  416. }
  417. // 判断新密码的强度
  418. const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
  419. if (!reg.test(resetPassword)) {
  420. throw '请设置至少包含数字和字母的密码';
  421. }
  422. } catch (err) {
  423. toastr.error(err);
  424. return false;
  425. }
  426. }
  427. /**
  428. * 表单检测
  429. */
  430. function checkUserForm(status) {
  431. try {
  432. if (status === 'add') {
  433. if ($('#add-user input[name="account_group"]').val() == 0) {
  434. throw '请选择账号组';
  435. }
  436. if ($('#add-user input[name="account"]').val() == '' || $('#add-user input[name="account"]').hasClass('is-invalid')) {
  437. throw '账号不能为空或已存在';
  438. }
  439. if ($('#add-user input[name="password"]').val() == '' || $('#add-user input[name="password"]').val().length < 6) {
  440. throw '密码不能为空或不能小于6位';
  441. }
  442. if (!/^[0-9a-zA-Z*~!@&%$^\\(\\)#_\[\]\-\+={}|?'":,<>.`]+$/.test($('#add-user input[name="password"]').val())) {
  443. throw '密码只支持英文数字及符号';
  444. }
  445. // 判断新密码的强度
  446. const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
  447. if (!reg.test($('#add-user input[name="password"]').val())) {
  448. throw '请设置至少包含数字和字母的密码';
  449. }
  450. if ($('#add-user input[name="name"]').val() == '') {
  451. throw '姓名不能为空';
  452. }
  453. if (_.findIndex(unitList, { name: $('#add-user select[name="company"]').val() }) === -1) {
  454. throw '请选择单位名称';
  455. }
  456. if ($('#add-user input[name="role"]').val() == '') {
  457. throw '职位名称不能为空';
  458. }
  459. $('#add-user input[name="account"]').val(trimInvalidChar($('#add-user input[name="account"]').val()));
  460. $('#add-user input[name="name"]').val(trimInvalidChar($('#add-user input[name="name"]').val()));
  461. // $('#add-user input[name="company"]').val(trimInvalidChar($('#add-user input[name="company"]').val()));
  462. $('#add-user input[name="role"]').val(trimInvalidChar($('#add-user input[name="role"]').val()));
  463. $('#add-user input[name="telephone"]').val(trimInvalidChar($('#add-user input[name="telephone"]').val()));
  464. } else {
  465. if ($('#edit-user input[name="account_group"]').val() == 0) {
  466. throw '请选择账号组';
  467. }
  468. if ($('#edit-user input[name="account"]').val() == '' || $('#add-user input[name="account"]').hasClass('is-invalid')) {
  469. throw '账号不能为空或已存在';
  470. }
  471. if ($('#edit-user input[name="name"]').val() == '') {
  472. throw '姓名不能为空';
  473. }
  474. if (_.findIndex(unitList, { name: $('#edit-user select[name="company"]').val() }) === -1) {
  475. throw '请选择单位名称';
  476. }
  477. if ($('#edit-user input[name="role"]').val() == '') {
  478. throw '职位名称不能为空';
  479. }
  480. $('#edit-user input[name="account"]').val(trimInvalidChar($('#edit-user input[name="account"]').val()));
  481. $('#edit-user input[name="name"]').val(trimInvalidChar($('#edit-user input[name="name"]').val()));
  482. // $('#edit-user input[name="company"]').val(trimInvalidChar($('#edit-user input[name="company"]').val()));
  483. $('#edit-user input[name="role"]').val(trimInvalidChar($('#edit-user input[name="role"]').val()));
  484. $('#edit-user input[name="telephone"]').val(trimInvalidChar($('#edit-user input[name="telephone"]').val()));
  485. }
  486. } catch (err) {
  487. toastr.error(err);
  488. return false;
  489. }
  490. }
  491. /**
  492. * 表单检测
  493. */
  494. function checkUnitForm() {
  495. try {
  496. if ($('#add-company input[name="name"]').val() == '') {
  497. throw '单位名称不能为空';
  498. }
  499. if ($('#add-company select[name="type"]').val() == 0) {
  500. throw '请选择类型';
  501. }
  502. // 检测同名
  503. if (_.findIndex(unitList, { name: $('#add-company input[name="name"]').val() }) !== -1) {
  504. throw '已存在对应的单位名称';
  505. }
  506. } catch (err) {
  507. toastr.error(err);
  508. return false;
  509. }
  510. }
  511. /**
  512. * 随机密码(必须包含数字和字母)
  513. */
  514. function randPassword() {
  515. const result = [];
  516. // 随机6-10位
  517. const length = Math.ceil(Math.random() * 2 + 6);
  518. let numberSeed = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  519. let stringSeed = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  520. 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  521. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
  522. const numRan = numberSeed[Math.floor((Math.random() * numberSeed.length))];
  523. const strRan = stringSeed[Math.floor((Math.random() * stringSeed.length))];
  524. const randSeed = stringSeed.concat(numberSeed);
  525. const seedLength = randSeed.length - 1;
  526. for (let i = 0; i < length; i++) {
  527. const index = Math.ceil(Math.random() * seedLength);
  528. result.push(randSeed[index]);
  529. }
  530. result.splice(Math.floor((Math.random() * result.length)), 0, numRan);
  531. result.splice(Math.floor((Math.random() * result.length)), 0, strRan);
  532. return result.join('');
  533. }