setting.js 22 KB

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