material_file.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use strict';
  2. /**
  3. * 材料调差 - 附件
  4. * @author LanJianRong
  5. * @date 2020/06/30
  6. * @version
  7. */
  8. $(document).ready(function () {
  9. // 全局fileData初始化
  10. let fileData = fileList || []
  11. handleFileList(fileData)
  12. getAllList()
  13. $('#upload-file-ok').click(function () {
  14. const files = Array.from($('#upload-fujian-file')[0].files)
  15. const valiData = files.map(v => {
  16. const ext = v.name.substring(v.name.lastIndexOf('.') + 1)
  17. return {
  18. size: v.size,
  19. ext
  20. }
  21. })
  22. if (validateFiles(valiData)) {
  23. if (files.length) {
  24. const formData = new FormData()
  25. files.forEach(file => {
  26. formData.append('file', file)
  27. formData.append('name', file.name)
  28. formData.append('size', file.size)
  29. })
  30. postDataWithFile(window.location.pathname + '/upload', formData, function (result) {
  31. handleFileList(result)
  32. $('#addfujian').modal('hide');
  33. if (!$('#file-list tr').length) {
  34. getAllList();
  35. } else {
  36. getAllList(parseInt($('#currentPage').text()));
  37. }
  38. });
  39. }
  40. }
  41. })
  42. // 选择/未选所有期列表
  43. $('#file-checkbox').click(function() {
  44. getAllList()
  45. })
  46. // 删除附件
  47. $('body').on('click', '.delete-file', function () {
  48. let attid = $(this).data('attid');
  49. const data = {id: attid};
  50. postData('/tender/measure/material/file/delete', data, function () {
  51. const idx = fileData.findIndex(file => file.id === parseInt(attid))
  52. idx !== -1 && fileData.splice(idx, 1)
  53. if ($('#file-list tr').length === 1) {
  54. getAllList(parseInt($('#currentPage').text()) - 1);
  55. } else {
  56. getAllList(parseInt($('#currentPage').text()));
  57. }
  58. // self.parents('tr').remove();
  59. // // 重新排序
  60. // let newsort = 1;
  61. // $('#file-list tr').each(function(){
  62. // $(this).children('td').eq(0).text(newsort);
  63. // newsort++;
  64. // });
  65. });
  66. });
  67. // 切换页数
  68. $('.page-select').on('click', function () {
  69. const totalPageNum = parseInt($('#totalPage').text());
  70. const lastPageNum = parseInt($('#currentPage').text());
  71. const status = $(this).attr('content');
  72. if (status === 'pre' && lastPageNum > 1) {
  73. getAllList(lastPageNum-1);
  74. } else if (status === 'next' && lastPageNum < totalPageNum) {
  75. getAllList(lastPageNum+1);
  76. }
  77. });
  78. // 生成所有附件列表
  79. function getAllList(currPageNum = 1) {
  80. // 每页最多几个附件
  81. const pageCount = 15;
  82. // 附件总数
  83. let total = fileData && fileData.length;
  84. // 未选中checkbox,需要过滤出来当前期的数据
  85. const filterFileData = fileData && fileData.filter(file => file.mid === parseInt(mid) && file.tid === parseInt(tid))
  86. if(!$('#file-checkbox').is(':checked')) {
  87. total = filterFileData.length
  88. }
  89. // 总页数
  90. const pageNum = Math.ceil(total/pageCount);
  91. $('#totalPage').text(pageNum);
  92. // total为0,当前还没上传过附件
  93. $('#currentPage').text(total ? currPageNum : 0);
  94. // 当前页附件内容
  95. const currPageAttData = fileData && $('#file-checkbox').is(':checked') ? fileData.slice((currPageNum-1)*pageCount, currPageNum*pageCount) : filterFileData.slice((currPageNum-1)*pageCount, currPageNum*pageCount);
  96. renderHtml(currPageAttData)
  97. }
  98. function renderHtml(list) {
  99. let html = '';
  100. list.forEach((fileInfo, idx) => {
  101. html += `<tr style="height: 31px;">
  102. <td>${idx + 1}</td>
  103. <td><a href="/${fileInfo.filepath}" target="_blank">${fileInfo.file_name}</a></td>
  104. <td>${fileInfo.file_size}</td>
  105. <td>第${fileInfo.s_order}期</td>
  106. <td>${fileInfo.upload_time}</td>`
  107. if (fileInfo.canDel ) {
  108. html += `<td>
  109. <a class="btn btn-light btn-sm delete-file" data-attid="${fileInfo.id}" title="删除附件">
  110. <span class="fa fa-trash text-danger"></span>
  111. </a>
  112. </td></tr>`
  113. } else {
  114. html += `<td></td></tr>`
  115. }
  116. })
  117. $('#file-list').empty();
  118. $('#file-list').append(html);
  119. }
  120. function handleFileList(fileList) {
  121. fileData = fileList.map(file => {
  122. let showDel = false
  123. // 只判断当前期,因为以往期都是只读的
  124. if (file.mid === parseInt(mid) && file.tid === parseInt(tid) && file.user_id === parseInt(cur_uid)) {
  125. if (!curAuditor) {
  126. material.status === auditConst.status.uncheck && parseInt(cur_uid) === material.user_id && (showDel = true)
  127. material.status === auditConst.status.checkNo && parseInt(cur_uid) === material.user_id && (showDel = true)
  128. } else {
  129. curAuditor.aid === parseInt(cur_uid) && (showDel = true)
  130. }
  131. }
  132. return showDel ? {...file, canDel: true} : file
  133. })
  134. }
  135. });
  136. /**
  137. * 校验文件大小、格式
  138. * @param {Array} files 文件数组
  139. */
  140. function validateFiles(files) {
  141. if (files.length > 10) {
  142. toastr.error('至多同时上传10个文件');
  143. return false
  144. }
  145. return files.every(file => {
  146. if (file.size > 1024 * 1024 * 30) {
  147. toastr.error('文件大小限制为30MB');
  148. return false
  149. }
  150. if (whiteList.indexOf('.' + file.ext) === -1) {
  151. toastr.error('请上传正确的格式文件');
  152. return false
  153. }
  154. return true
  155. })
  156. }