setting.js 21 KB

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