safe_inspection_information.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author lanjianrong
  6. * @date 2020/8/7
  7. * @version
  8. */
  9. $(document).ready(function () {
  10. autoFlashHeight();
  11. $.subMenu({
  12. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  13. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  14. key: 'menu.1.0.0',
  15. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  16. callback: function (info) {
  17. if (info.mini) {
  18. $('.panel-title').addClass('fluid');
  19. $('#sub-menu').removeClass('panel-sidebar');
  20. } else {
  21. $('.panel-title').removeClass('fluid');
  22. $('#sub-menu').addClass('panel-sidebar');
  23. }
  24. autoFlashHeight();
  25. }
  26. });
  27. function debounce(fn, wait) {
  28. var t = null;
  29. return function () {
  30. var args = arguments;
  31. clearTimeout(t);
  32. t = setTimeout(function () { fn.apply(null, args); }, wait);
  33. };
  34. }
  35. $('textarea[id]').each(function () {
  36. var $el = $(this);
  37. var el = $el[0];
  38. var id = $el.attr('id');
  39. var key = 'textarea_height_' + id;
  40. // 1. 页面加载恢复高度
  41. var saved = getLocalCache(key);
  42. if (saved) {
  43. $el.css('height', saved + 'px');
  44. }
  45. // 保存函数(使用 clientHeight)
  46. var save = debounce(function () {
  47. try {
  48. var h = el.getBoundingClientRect().height; // 真实渲染高度
  49. h = Math.round(h); // 防止小数
  50. setLocalCache(key, h);
  51. } catch (e) {
  52. console.warn('保存高度失败', e);
  53. }
  54. }, 150); // 150ms 防抖
  55. // 2. 实时监听拖拽高度
  56. if (window.ResizeObserver) {
  57. var ro = new ResizeObserver(function () {
  58. save();
  59. });
  60. ro.observe(el);
  61. } else {
  62. // 兼容方案
  63. $(window).on('mouseup', function () {
  64. save();
  65. });
  66. // 也监听 blur(防止通过键盘或脚本改变尺寸)
  67. $el.on('blur', function () { save(); });
  68. }
  69. });
  70. // 展开历史审核记录
  71. $('td #fold-btn').click(function () {
  72. const type = $(this).data('target')
  73. const auditCard = $(this).parent().parent()
  74. if (type === 'show') {
  75. $(this).data('target', 'hide')
  76. auditCard.find('.fold-card').slideDown('swing', () => {
  77. auditCard.find('#fold-btn').text('收起历史审核记录')
  78. })
  79. } else {
  80. $(this).data('target', 'show')
  81. auditCard.find('.fold-card').slideUp('swing', () => {
  82. auditCard.find('#fold-btn').text('展开历史审核记录')
  83. })
  84. }
  85. });
  86. // 添加审批流程按钮逻辑
  87. $('.book-list').on('click', 'dt', function () {
  88. const idx = $(this).find('.acc-btn').attr('data-groupid')
  89. const type = $(this).find('.acc-btn').attr('data-type')
  90. if (type === 'hide') {
  91. $(this).parent().find(`div[data-toggleid="${idx}"]`).show(() => {
  92. $(this).children().find('i').removeClass('fa-plus-square').addClass('fa-minus-square-o')
  93. $(this).find('.acc-btn').attr('data-type', 'show')
  94. })
  95. } else {
  96. $(this).parent().find(`div[data-toggleid="${idx}"]`).hide(() => {
  97. $(this).children().find('i').removeClass('fa-minus-square-o').addClass('fa-plus-square')
  98. $(this).find('.acc-btn').attr('data-type', 'hide')
  99. })
  100. }
  101. return false
  102. })
  103. let timer = null
  104. let oldSearchVal = null
  105. $('.gr-search').bind('input propertychange', function (e) {
  106. oldSearchVal = e.target.value
  107. timer && clearTimeout(timer)
  108. timer = setTimeout(() => {
  109. const newVal = $('#gr-search').val()
  110. let html = ''
  111. if (newVal && newVal === oldSearchVal) {
  112. accountList.filter(item => item && inspection.uid !== item.id && (item.name.indexOf(newVal) !== -1 || (item.mobile && item.mobile.indexOf(newVal) !== -1))).forEach(item => {
  113. html += `<dd class="border-bottom p-2 mb-0 " data-id="${item.id}" >
  114. <p class="mb-0 d-flex"><span class="text-primary">${item.name}</span><span
  115. class="ml-auto">${item.mobile || ''}</span></p>
  116. <span class="text-muted">${item.role || ''}</span>
  117. </dd>`
  118. })
  119. $('.book-list').empty()
  120. $('.book-list').append(html)
  121. } else {
  122. if (!$('.acc-btn').length) {
  123. accountGroup.forEach((group, idx) => {
  124. if (!group) return
  125. html += `<dt><a href="javascript: void(0);" class="acc-btn" data-groupid="${idx}" data-type="hide"><i class="fa fa-plus-square"></i>
  126. </a> ${group.groupName}</dt>
  127. <div class="dd-content" data-toggleid="${idx}">`
  128. group.groupList.forEach(item => {
  129. if (item.id !== inspection.uid) {
  130. html += `<dd class="border-bottom p-2 mb-0 " data-id="${item.id}" >
  131. <p class="mb-0 d-flex"><span class="text-primary">${item.name}</span><span
  132. class="ml-auto">${item.mobile || ''}</span></p>
  133. <span class="text-muted">${item.role || ''}</span>
  134. </dd>`
  135. }
  136. });
  137. html += '</div>'
  138. })
  139. $('.book-list').empty()
  140. $('.book-list').append(html)
  141. }
  142. }
  143. }, 400);
  144. })
  145. if (!inspection.readOnly) {
  146. const checkDate = $('#check_date').datepicker({
  147. autoClose: true,
  148. onSelect: function (formattedDate, date, inst) {
  149. if (!date && inspection.check_date) {
  150. toastr.error('检查日期不能为空');
  151. checkDate.selectDate(inspection.check_date ? new Date(inspection.check_date) : new Date());
  152. return;
  153. }
  154. // 判断日期格式
  155. const check_date = moment(date).format('YYYY-MM-DD');
  156. const reg = /^\d{4}-\d{2}-\d{2}$/;
  157. if (!reg.test(check_date)) {
  158. toastr.error('检查日期格式错误,应为YYYY-MM-DD。');
  159. return;
  160. }
  161. if (check_date !== moment(inspection.check_date).format('YYYY-MM-DD')) {
  162. updateInspection('check_date', check_date);
  163. }
  164. }
  165. }).data('datepicker');
  166. checkDate.selectDate(inspection.check_date ? new Date(inspection.check_date) : new Date());
  167. $('#check_table textarea').on('change', function (e) {
  168. const value = $(this).val().trim();
  169. const key = $(this).data('key');
  170. if (value !== inspection[key]) {
  171. updateInspection(key, value);
  172. }
  173. });
  174. $("#check_table input").on('change', function (e) {
  175. const value = $(this).val().trim();
  176. const key = $(this).data('key');
  177. if (key === 'check_date') {
  178. if (!value && inspection.check_date) {
  179. toastr.error('检查日期不能为空');
  180. checkDate.selectDate(inspection.check_date ? new Date(inspection.check_date) : new Date());
  181. return;
  182. }
  183. // 判断日期格式
  184. const reg = /^\d{4}-\d{2}-\d{2}$/;
  185. if (!reg.test(value)) {
  186. toastr.error('检查日期格式错误,应为YYYY-MM-DD。');
  187. checkDate.selectDate(inspection.check_date ? new Date(inspection.check_date) : new Date());
  188. return;
  189. }
  190. }
  191. if (value !== inspection[key]) {
  192. updateInspection(key, value);
  193. }
  194. });
  195. $('#check_table dl').on('click', 'dd', function () {
  196. const id = parseInt($(this).data('id'))
  197. if (id !== 0) {
  198. const user = _.find(accountList, { id });
  199. $('#inspector-set').html(`<span class="badge">
  200. ${user.name}
  201. <span class="dropdown">
  202. <a href="javascript:void(0)" class="btn-sm text-danger px-1" title="移除" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fa fa-remove"></i></a>
  203. <div class="dropdown-menu">
  204. <a class="dropdown-item" href="javascript:void(0);">确认移除检查人?</a>
  205. <div class="dropdown-divider"></div>
  206. <div class="px-2 py-1 text-center">
  207. <button class="btn btn-sm btn-danger remove-btn">移除</button>
  208. <button class="btn btn-sm btn-secondary">取消</button>
  209. </div>
  210. </div>
  211. </span>
  212. </span>`);
  213. $('#inspector-set').siblings('.dropdown').attr('style', 'display:none!important;');
  214. updateInspection('inspector', user.name);
  215. }
  216. });
  217. $('body').on('click', '#check_table .remove-btn', function () {
  218. updateInspection('inspector', '');
  219. $('#inspector-set').html('');
  220. $('#inspector-set').siblings('.dropdown').show();
  221. });
  222. function updateInspection(field, value) {
  223. const data = {
  224. id: inspection.id,
  225. };
  226. data[field] = value;
  227. postData(`${preUrl}/save`, {type: 'update-field', update: data}, function (result) {
  228. inspection[field] = value;
  229. if (field === 'check_date') {
  230. checkDate.selectDate(inspection.check_date ? new Date(inspection.check_date) : new Date());
  231. }
  232. }, function () {
  233. if (field === 'check_date') {
  234. checkDate.selectDate(inspection.check_date ? new Date(inspection.check_date) : new Date());
  235. } else {
  236. $(`#check_table textarea[data-key=${field}]`).val(inspection[field] || '');
  237. }
  238. });
  239. }
  240. // 添加到审批流程中
  241. $('#shenpi_select dl').on('click', 'dd', function () {
  242. const id = parseInt($(this).data('id'))
  243. if (id !== 0) {
  244. postData(preUrl + '/save', {type: 'add-audit', auditorId: id}, (datas) => {
  245. // <p class="m-0 ml-2"><small class="text-muted">中交第一公路工程局有限公司国道311线满别公路施工一分部</small></p>
  246. const html = [];
  247. // 如果是重新上报,添加到重新上报列表中
  248. const auditorshtml = [];
  249. for (const [index, data] of datas.entries()) {
  250. if (index !== 0) {
  251. html.push('<li class="list-group-item d-flex" auditorId="' + data[0].aid + '">');
  252. html.push(`<div class="col-auto">${index}</div>`);
  253. html.push('<div class="col">');
  254. for (const auditor of data) {
  255. html.push(`<div class="d-inline-block mx-1"><i class="fa fa-user text-muted"></i> ${auditor.name} <small class="text-muted">${auditor.role}</small></div>`);
  256. }
  257. html.push('</div>');
  258. html.push('<div class="col-auto">');
  259. if (data[0].audit_type !== auditType.key.common) {
  260. html.push(`<span class="badge badge-pill badge-${auditType.info[data[0].audit_type].class} badge-bg-small"><small>${auditType.info[data[0].audit_type].long}</small></span>`);
  261. }
  262. if (shenpi_status === shenpiConst.sp_status.sqspr || (shenpi_status === shenpiConst.sp_status.gdzs && index + 1 !== datas.length)) {
  263. html.push('<a href="javascript: void(0)" class="text-danger pull-right ml-1">移除</a>');
  264. }
  265. html.push('</div>');
  266. html.push('</li>');
  267. }
  268. // 添加新审批人流程修改
  269. auditorshtml.push('<li class="list-group-item d-flex justify-content-between align-items-center" data-auditorid="' + data[0].aid + '">');
  270. auditorshtml.push('<span class="mr-1"><i class="fa ' + (index === 0 ? 'fa-play-circle fa-rotate-90' : index + 1 === datas.length ? 'fa-stop-circle' : 'fa-chevron-circle-down') + '"></i></span>');
  271. auditorshtml.push('<span class="text-muted">');
  272. for (const auditor of data) {
  273. auditorshtml.push(`<small class="d-inline-block text-dark mx-1" title="${auditor.role}" data-auditorId="${auditor.uid}">${auditor.name}</small>`);
  274. }
  275. auditorshtml.push('</span>');
  276. auditorshtml.push('<div class="d-flex ml-auto">');
  277. if (data[0].audit_type !== auditType.key.common) {
  278. auditorshtml.push(`<span class="badge badge-pill badge-${auditType.info[data[0].audit_type].class} p-1"><small>${auditType.info[data[0].audit_type].short}</small></span>`);
  279. }
  280. if (index === 0) {
  281. auditorshtml.push('<span class="badge badge-light badge-pill ml-auto"><small>原报</small></span>');
  282. } else if (index + 1 === datas.length) {
  283. auditorshtml.push('<span class="badge badge-light badge-pill"><small>终审</small></span>');
  284. } else {
  285. auditorshtml.push('<span class="badge badge-light badge-pill"><small>' + transFormToChinese(index) + '审</small></span>');
  286. }
  287. }
  288. $('#auditors').html(html.join(''));
  289. $('#auditors2').html(auditorshtml.join(''));
  290. });
  291. }
  292. });
  293. // 删除审批人
  294. $('body').on('click', '#auditors li a', function () {
  295. const li = $(this).parents('li');
  296. const data = {
  297. type: 'del-audit',
  298. auditorId: parseInt(li.attr('auditorId')),
  299. };
  300. postData(preUrl + '/save', data, (result) => {
  301. li.remove();
  302. for (const rst of result) {
  303. const aLi = $('li[auditorid=' + rst.aid + ']');
  304. $('div:first', aLi).text(rst.order);
  305. }
  306. // 删除左边审核人
  307. $(`#auditors2 li[data-auditorid='${data.auditorId}']`).remove();
  308. if ($('#auditors2 li').length !== 0 && !$('#auditors-list li i').hasClass('fa-stop-circle')) {
  309. console.log($('#auditors2 li').length - 1, $('#auditors2 li').eq($('#auditors2 li').length - 1).find('i'));
  310. $('#auditors2 li').eq($('#auditors2 li').length - 1).find('i')
  311. .removeClass('fa-chevron-circle-down').addClass('fa-stop-circle');
  312. }
  313. for (let i = 0; i < $('#auditors2 li').length; i++) {
  314. $('#auditors2 li').eq(i).find('.badge-pill').children('small').text(i === 0 ? '原报' : (i + 1 === $('#auditors2 li').length ? '终' : transFormToChinese(i)) + '审');
  315. }
  316. })
  317. });
  318. $('#del-inspection-btn').click(function() {
  319. const text = $('#del-inspection-text').val().trim();
  320. if (text.length === 0 || text !== '确认删除本次巡检') {
  321. toastr.error('请正确输入“确认删除本次巡检”');
  322. return;
  323. }
  324. postData(preUrl + '/save', {type: 'del-inspection' }, function (result) {
  325. let link = `/sp/${spid}/safe/tender/${tender_id}/inspection`;
  326. let orderSetting = getLocalCache('safe-inspection-'+ tender_id +'-list-order');
  327. if (!orderSetting) orderSetting = 'time|desc';
  328. const orders = orderSetting.split('|');
  329. const filterData = [];
  330. filterData.push('sort='+ orders[0]);
  331. filterData.push('order=' + orders[1]);
  332. if (getLocalCache('account-pageSize')) {
  333. filterData.push('pageSize=' + getLocalCache('account-pageSize'));
  334. }
  335. if (filterData.length > 0) {
  336. link += '?' + filterData.join('&');
  337. }
  338. window.location.href = link;
  339. });
  340. });
  341. $('#judge-start-btn').click(function () {
  342. const flag = !(inspection.code && inspection.check_item && inspection.check_date);
  343. if (flag) {
  344. toastr.warning('请完善巡检信息再提交');
  345. return;
  346. }
  347. if ($('#auditors li').length === 0) {
  348. if(shenpi_status === shenpiConst.sp_status.gdspl) {
  349. toastr.error('请联系管理员添加审批人');
  350. } else {
  351. toastr.error('请先选择审批人,再上报数据');
  352. }
  353. return false;
  354. }
  355. $('#sp-done').modal('show');
  356. });
  357. $('#start-btn').click(function () {
  358. $('#start-btn').prop('disabled', true);
  359. postData(preUrl + '/save', { type: 'start-inspection' }, function (result) {
  360. window.location.reload();
  361. });
  362. });
  363. } else if (inspection.shenpiPower) {
  364. // 添加到审批流程中
  365. $('dl').on('click', 'dd', function () {
  366. const id = parseInt($(this).data('id'))
  367. if (id !== 0) {
  368. const user = _.find(accountList, { id });
  369. $('#rectification-user-set').html(`<span class="badge">
  370. ${user.name}
  371. <span class="dropdown">
  372. <a href="javascript:void(0)" class="btn-sm text-danger px-1" title="移除" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fa fa-remove"></i></a>
  373. <div class="dropdown-menu">
  374. <a class="dropdown-item" href="javascript:void(0);">确认移除整改人?</a>
  375. <div class="dropdown-divider"></div>
  376. <div class="px-2 py-1 text-center">
  377. <button class="btn btn-sm btn-danger remove-btn">移除</button>
  378. <button class="btn btn-sm btn-secondary">取消</button>
  379. </div>
  380. </div>
  381. </span>
  382. </span>`);
  383. $('#rectification-uid').val(user.id);
  384. $('#rectification-user-set').siblings('.dropdown').attr('style', 'display:none!important;');
  385. }
  386. });
  387. // 删除审批人
  388. $('body').on('click', '#rectification-user-set .remove-btn', function () {
  389. $('#rectification-user-set').html('');
  390. $('#rectification-uid').val('');
  391. $('#rectification-user-set').siblings('.dropdown').show();
  392. });
  393. $('#approval-success-btn').click(function () {
  394. if (inspection.finalAuditorIds.indexOf(cur_uid) !== -1 && $('#rectification-uid').val() === '') {
  395. toastr.warning('请选择整改人');
  396. return;
  397. }
  398. const opinion = $('#sp-done').find('textarea[name="opinion"]').eq(0).val().replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');
  399. if (opinion.length === 0) {
  400. toastr.warning('请填写审核意见');
  401. return;
  402. }
  403. const data = {
  404. type: 'check',
  405. checkType: auditConst.status.checked,
  406. opinion,
  407. rectification_uid: $('#rectification-uid').val(),
  408. }
  409. postData(preUrl + '/save', data, function (result) {
  410. window.location.reload();
  411. });
  412. });
  413. $('#approval-back-btn').click(function () {
  414. console.log($('#sp-back').find('textarea[name="opinion"]').eq(0).val());
  415. const opinion = $('#sp-back').find('textarea[name="opinion"]').eq(0).val().replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');
  416. if (opinion.length === 0) {
  417. toastr.warning('请填写审核意见');
  418. return;
  419. }
  420. const inlineRadio1 = $('#inlineRadio1:checked').val();
  421. const inlineRadio2 = $('#inlineRadio2:checked').val();
  422. if (!inlineRadio1 && !inlineRadio2) {
  423. if (!$('#warning-text').length) {
  424. $('#reject-process').prepend('<p id="warning-text" style="color: red; margin: 0;">请选择退回流程</p>');
  425. }
  426. return;
  427. }
  428. if ($('#warning-text').length) $('#warning-text').remove()
  429. const data = {
  430. type: 'check',
  431. checkType: parseInt(inlineRadio1 ? inlineRadio1 : inlineRadio2),
  432. opinion,
  433. }
  434. postData(preUrl + '/save', data, function (result) {
  435. window.location.reload();
  436. });
  437. });
  438. $('#approval-stop-btn').click(function () {
  439. const opinion = $('#sp-close').find('textarea[name="opinion"]').eq(0).val().replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');
  440. if (opinion.length === 0) {
  441. toastr.warning('请填写关闭原因');
  442. return;
  443. }
  444. const data = {
  445. type: 'check',
  446. checkType: auditConst.status.checkStop,
  447. opinion,
  448. }
  449. postData(preUrl + '/save', data, function (result) {
  450. window.location.reload();
  451. });
  452. });
  453. } else if (inspection.rectificationPower) {
  454. $('#judge-success-btn').click(function () {
  455. const flag = !(inspection.rectification_item && inspection.rectification_date);
  456. if (flag) {
  457. toastr.warning('请完善整改单再提交');
  458. return;
  459. }
  460. $('#sp-done').modal('show');
  461. });
  462. // 整改完成
  463. $('#rectification-success-btn').click(function () {
  464. const opinion = $('#sp-done').find('textarea[name="opinion"]').eq(0).val().replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');
  465. if (opinion.length === 0) {
  466. toastr.warning('请填写审核意见');
  467. return;
  468. }
  469. const data = {
  470. type: 'rectification',
  471. checkType: auditConst.status.checked,
  472. opinion,
  473. }
  474. postData(preUrl + '/save', data, function (result) {
  475. window.location.reload();
  476. });
  477. });
  478. $('#rectification-back-btn').click(function () {
  479. const opinion = $('#sp-back').find('textarea[name="opinion"]').eq(0).val().replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');
  480. if (opinion.length === 0) {
  481. toastr.warning('请填写审核意见');
  482. return;
  483. }
  484. const data = {
  485. type: 'rectification',
  486. checkType: auditConst.status.checkNoPre,
  487. opinion,
  488. }
  489. postData(preUrl + '/save', data, function (result) {
  490. window.location.reload();
  491. });
  492. });
  493. const rectificationDate = $('#rectification_date').datepicker({
  494. autoClose: true,
  495. onSelect: function (formattedDate, date, inst) {
  496. if (!date && inspection.rectification_date) {
  497. toastr.error('检查日期不能为空');
  498. rectificationDate.selectDate(inspection.rectification_date ? new Date(inspection.rectification_date) : '');
  499. return;
  500. }
  501. // 判断日期格式
  502. const rectification_date = moment(date).format('YYYY-MM-DD');
  503. const reg = /^\d{4}-\d{2}-\d{2}$/;
  504. if (!reg.test(rectification_date)) {
  505. toastr.error('整改日期格式错误,应为YYYY-MM-DD。');
  506. return;
  507. }
  508. if (rectification_date !== moment(inspection.rectification_date).format('YYYY-MM-DD')) {
  509. updateInspection('rectification_date', rectification_date);
  510. }
  511. }
  512. }).data('datepicker');
  513. rectificationDate.selectDate(inspection.rectification_date ? new Date(inspection.rectification_date) : '');
  514. $('#rectification_table textarea').on('change', function (e) {
  515. const value = $(this).val().trim();
  516. const key = $(this).data('key');
  517. if (value !== inspection[key]) {
  518. updateInspection(key, value);
  519. }
  520. });
  521. $("#rectification_table input").on('change', function (e) {
  522. const value = $(this).val().trim();
  523. const key = $(this).data('key');
  524. if (key === 'check_date') {
  525. if (!value && inspection.rectification_date) {
  526. toastr.error('检查日期不能为空');
  527. rectificationDate.selectDate(inspection.rectification_date ? new Date(inspection.rectification_date) : '');
  528. return;
  529. }
  530. // 判断日期格式
  531. const reg = /^\d{4}-\d{2}-\d{2}$/;
  532. if (!reg.test(value)) {
  533. toastr.error('检查日期格式错误,应为YYYY-MM-DD。');
  534. rectificationDate.selectDate(inspection.rectification_date ? new Date(inspection.rectification_date) : '');
  535. return;
  536. }
  537. }
  538. if (value !== inspection[key]) {
  539. updateInspection(key, value);
  540. }
  541. });
  542. function updateInspection(field, value) {
  543. const data = {
  544. id: inspection.id,
  545. };
  546. data[field] = value;
  547. console.log(data);
  548. postData(`${preUrl}/save`, {type: 'update-field', update: data}, function (result) {
  549. inspection[field] = value;
  550. if (field === 'rectification_date') {
  551. rectificationDate.selectDate(inspection.rectification_date ? new Date(inspection.rectification_date) : '');
  552. }
  553. }, function () {
  554. if (field === 'rectification_date') {
  555. rectificationDate.selectDate(inspection.rectification_date ? new Date(inspection.rectification_date) : '');
  556. } else {
  557. $(`#rectification_table textarea[data-key=${field}]`).val(inspection[field] || '');
  558. }
  559. });
  560. }
  561. }
  562. handleFileList(fileList);
  563. $('#file-ok').click(function () {
  564. const files = Array.from($('#file-modal')[0].files)
  565. const valiData = files.map(v => {
  566. const ext = v.name.substring(v.name.lastIndexOf('.') + 1)
  567. return {
  568. size: v.size,
  569. ext
  570. }
  571. });
  572. if (validateFiles(valiData)) {
  573. if (files.length) {
  574. const formData = new FormData();
  575. files.forEach(file => {
  576. formData.append('name', file.name);
  577. formData.append('size', file.size);
  578. formData.append('file', file);
  579. })
  580. postDataWithFile(`${preUrl}/file/upload`, formData, function (result) {
  581. handleFileList(result);
  582. $('#file-modal').val('');
  583. $('#file-cancel').click();
  584. });
  585. }
  586. }
  587. })
  588. function handleFileList(files = []) {
  589. $('#file-content').empty();
  590. const newFiles = files.map(file => {
  591. let showDel = false;
  592. if (file.uid === cur_uid) {
  593. if (inspection.status === auditConst.status.checked) {
  594. showDel = Boolean(file.extra_upload ) || deleteFilePermission
  595. } else {
  596. showDel = true
  597. }
  598. }
  599. return {...file, showDel}
  600. })
  601. let html = inspection.filePermission ? `<tr><td colspan="5"><a href="#addfujian" data-toggle="modal" class="btn btn-sm btn-light text-primary" data-placement="bottom" title="" data-original-title="上传附件"><i class="fa fa-cloud-upload" aria-hidden="true"></i> 上传附件</a></td></tr>` : '';
  602. newFiles.forEach((file, idx) => {
  603. if (file.showDel) {
  604. html += `<tr><td>${idx + 1}</td><td><a href="${file.filepath}" target="_blank">${file.filename}</a></td><td>${file.username}</td><td>${moment(file.upload_time).format('YYYY-MM-DD HH:mm:ss')}</td><td><a href="${preUrl}/file/${file.id}/download" class="mr-2"><i class="fa fa-download"></i></a><a href="javascript: void(0);" class="text-danger file-del" data-id="${file.id}"><i class="fa fa-remove"></i></a></td></tr>`
  605. } else {
  606. html += `<tr><td width="70">${idx + 1}</td><td><a href="${file.filepath}" target="_blank">${file.filename}</a></td><td>${file.username}</td><td>${moment(file.upload_time).format('YYYY-MM-DD HH:mm:ss')}</td><td><a href="${preUrl}/file/${file.id}/download" class="mr-2"><i class="fa fa-download"></i></a></td></tr>`
  607. }
  608. })
  609. $('#file-content').append(html);
  610. }
  611. $('#file-content').on('click', 'a', function () {
  612. if ($(this).hasClass('file-del')) {
  613. const id = $(this).data('id');
  614. postData(`${preUrl}/file/delete`, {id}, (result) => {
  615. handleFileList(result);
  616. })
  617. }
  618. });
  619. })
  620. /**
  621. * 校验文件大小、格式
  622. * @param {Array} files 文件数组
  623. */
  624. function validateFiles(files) {
  625. if (files.length > 10) {
  626. toastr.error('至多同时上传10个文件');
  627. return false
  628. }
  629. return files.every(file => {
  630. if (file.size > 1024 * 1024 * 50) {
  631. toastr.error('文件大小限制为50MB');
  632. return false
  633. }
  634. if (whiteList.indexOf('.' + file.ext) === -1) {
  635. toastr.error('请上传正确的格式文件');
  636. return false
  637. }
  638. return true
  639. })
  640. }