material_file.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. const curPageNo = $('#file-pagination > li[class="page-item active"] > a').text()
  37. getAllList(parseInt(curPageNo));
  38. }
  39. });
  40. }
  41. }
  42. })
  43. // 选择/未选所有期列表
  44. $('#file-checkbox').click(function() {
  45. getAllList()
  46. })
  47. // 删除附件
  48. $('body').on('click', '.delete-file', function () {
  49. let attid = $(this).data('attid');
  50. const data = {id: attid};
  51. postData('/tender/measure/material/file/delete', data, function () {
  52. const idx = fileData.findIndex(file => file.id === parseInt(attid))
  53. idx !== -1 && fileData.splice(idx, 1)
  54. const curPageNo = parseInt($('#file-pagination > li[class="page-item active"] > a').text())
  55. if ($('#file-list tr').length === 1) {
  56. getAllList(curPageNo - 1);
  57. } else {
  58. getAllList(curPageNo);
  59. }
  60. // self.parents('tr').remove();
  61. // // 重新排序
  62. // let newsort = 1;
  63. // $('#file-list tr').each(function(){
  64. // $(this).children('td').eq(0).text(newsort);
  65. // newsort++;
  66. // });
  67. });
  68. });
  69. // 切换页数
  70. $('#file-pagination').on('click', '.page-item a', function () {
  71. // 找到当前的pageNo
  72. const curPageNo = $('#file-pagination > li[class="page-item active"] > a').text()
  73. const btnType = $(this).attr('aria-label')
  74. const total = calcCount();
  75. // btnType存在,说明点击的是前一个/后一个
  76. // btnType不存在,点击的是页数
  77. if (btnType) {
  78. if (btnType === 'Previous') {
  79. // 只有大于1时才处理
  80. if (parseInt(curPageNo) !== 1) {
  81. getAllList(curPageNo - 1)
  82. }
  83. } else {
  84. if (parseInt(curPageNo) !== Math.ceil(total/15)) {
  85. getAllList(parseInt(curPageNo) + 1)
  86. }
  87. }
  88. } else {
  89. curPageNo !== $(this).text() && getAllList(parseInt($(this).text()))
  90. }
  91. })
  92. // 生成所有附件列表
  93. function getAllList(currPageNum = 1) {
  94. // 每页最多几个附件
  95. const pageCount = 15;
  96. // 未选中checkbox,需要过滤出来当前期的数据
  97. const filterFileData = fileData && fileData.filter(file => file.mid === parseInt(mid) && file.tid === parseInt(tid))
  98. const total = calcCount(filterFileData);
  99. // 总页数
  100. const pageNum = Math.ceil(total/pageCount);
  101. // 当前页附件内容
  102. const currPageAttData = fileData && $('#file-checkbox').is(':checked') ? fileData.slice((currPageNum-1)*pageCount, currPageNum*pageCount) : filterFileData.slice((currPageNum-1)*pageCount, currPageNum*pageCount);
  103. renderHtml(currPageAttData)
  104. // 渲染分页器
  105. renderPagination(currPageNum, pageNum)
  106. }
  107. function renderPagination(pageNo, pageSize) {
  108. $('#file-pagination').empty()
  109. const btnHtml = `<li class="page-item page-back">
  110. <a class="page-link" href="#" aria-label="Previous">
  111. <span aria-hidden="true">&laquo;</span>
  112. </a>
  113. </li>
  114. <li class="page-item page-next">
  115. <a class="page-link" href="#" aria-label="Next">
  116. <span aria-hidden="true">&raquo;</span>
  117. </a>
  118. </li>`
  119. $('#file-pagination').append(btnHtml)
  120. let html = ''
  121. for (let index = 0; index < pageSize; index++) {
  122. if (index + 1 === pageNo) {
  123. // 当前
  124. html+=`<li class="page-item active"><a class="page-link" href="#">${index + 1}</a></li>`
  125. } else if(index === pageNo) {
  126. // 前一页一页
  127. html+=`<li class="page-item"><a class="page-link" href="#">${index + 1}</a></li>`
  128. } else if (index + 2 === pageNo) {
  129. // 后一页
  130. html+=`<li class="page-item"><a class="page-link" href="#">${index + 1}</a></li>`
  131. } else if(index + 1 === pageSize) {
  132. // 末尾
  133. html+=`<li class="page-item"><a class="page-link" href="#">${index + 1}</a></li>`
  134. } else if (index === 0) {
  135. // 开头
  136. html+=`<li class="page-item"><a class="page-link" href="#">${index + 1}</a></li>`
  137. } else {
  138. html+=`<li class="page-item"><span class="page-link">...</span></li>`
  139. }
  140. }
  141. $('.page-next').before(html)
  142. }
  143. function renderHtml(list) {
  144. let html = '';
  145. list.forEach((fileInfo, idx) => {
  146. html += `<tr style="height: 31px;">
  147. <td>${idx + 1}</td>
  148. <td><a href="/${fileInfo.filepath}" target="_blank">${fileInfo.file_name}</a></td>
  149. <td>${fileInfo.file_size}</td>
  150. <td>第${fileInfo.s_order}期</td>
  151. <td>${fileInfo.upload_time}</td>`
  152. if (fileInfo.canDel ) {
  153. html += `<td>
  154. <a class="btn btn-light btn-sm delete-file" data-attid="${fileInfo.id}" title="删除附件">
  155. <span class="fa fa-trash text-danger"></span>
  156. </a>
  157. </td></tr>`
  158. } else {
  159. html += `<td></td></tr>`
  160. }
  161. })
  162. $('#file-list').empty();
  163. $('#file-list').append(html);
  164. }
  165. function handleFileList(fileList) {
  166. fileData = fileList.map(file => {
  167. let showDel = false
  168. // 只判断当前期,因为以往期都是只读的
  169. if (file.mid === parseInt(mid) && file.tid === parseInt(tid) && file.user_id === parseInt(cur_uid)) {
  170. if (!curAuditor) {
  171. material.status === auditConst.status.uncheck && parseInt(cur_uid) === material.user_id && (showDel = true)
  172. material.status === auditConst.status.checkNo && parseInt(cur_uid) === material.user_id && (showDel = true)
  173. } else {
  174. curAuditor.aid === parseInt(cur_uid) && (showDel = true)
  175. }
  176. }
  177. return showDel ? {...file, canDel: true} : file
  178. })
  179. }
  180. function calcCount(filterFileData) {
  181. // 附件总数
  182. let total = fileData && fileData.length;
  183. if(!$('#file-checkbox').is(':checked')) {
  184. total = filterFileData && filterFileData.length
  185. }
  186. return total
  187. }
  188. });
  189. /**
  190. * 校验文件大小、格式
  191. * @param {Array} files 文件数组
  192. */
  193. function validateFiles(files) {
  194. if (files.length > 10) {
  195. toastr.error('至多同时上传10个文件');
  196. return false
  197. }
  198. return files.every(file => {
  199. if (file.size > 1024 * 1024 * 30) {
  200. toastr.error('文件大小限制为30MB');
  201. return false
  202. }
  203. if (whiteList.indexOf('.' + file.ext) === -1) {
  204. toastr.error('请上传正确的格式文件');
  205. return false
  206. }
  207. return true
  208. })
  209. }