material_file.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. 'use strict';
  2. /**
  3. * 材料调差 - 附件
  4. * @author LanJianRong
  5. * @date 2020/06/30
  6. * @version
  7. */
  8. $(document).ready(function () {
  9. // 每页最多几个附件
  10. const pageCount = 15;
  11. // 全局fileData初始化
  12. let fileData = fileList || []
  13. handleFileList(fileData)
  14. getAllList()
  15. $('#upload-file-ok').click(function () {
  16. const files = Array.from($('#upload-fujian-file')[0].files)
  17. const valiData = files.map(v => {
  18. const ext = v.name.substring(v.name.lastIndexOf('.') + 1)
  19. return {
  20. size: v.size,
  21. ext
  22. }
  23. })
  24. if (validateFiles(valiData)) {
  25. if (files.length) {
  26. const formData = new FormData()
  27. files.forEach(file => {
  28. formData.append('name', file.name)
  29. formData.append('size', file.size)
  30. formData.append('file', file)
  31. })
  32. postDataWithFile(window.location.pathname + '/upload', formData, function (result) {
  33. $('#upload-fujian-file').val('');
  34. handleFileList(result)
  35. $('#addfujian').modal('hide');
  36. if (!$('#file-list tr').length) {
  37. getAllList();
  38. } else {
  39. const curPageNo = $('#file-pagination > li[class="page-item active"] > a').text() || 1
  40. getAllList(parseInt(curPageNo));
  41. }
  42. });
  43. }
  44. }
  45. })
  46. // 选择/未选所有期列表
  47. $('#file-checkbox').click(function() {
  48. getAllList()
  49. })
  50. // 删除附件
  51. $('body').on('click', '.delete-file', function () {
  52. let attid = $(this).data('attid');
  53. const data = {id: attid};
  54. postData('/tender/measure/material/file/delete', data, function () {
  55. const idx = fileData.findIndex(file => file.id === parseInt(attid))
  56. idx !== -1 && fileData.splice(idx, 1)
  57. const curPageNo = parseInt($('#file-pagination > li[class="page-item active"] > a').text()) || 1
  58. if ($('#file-list tr').length === 1) {
  59. getAllList(curPageNo - 1);
  60. } else {
  61. getAllList(curPageNo);
  62. }
  63. // self.parents('tr').remove();
  64. // // 重新排序
  65. // let newsort = 1;
  66. // $('#file-list tr').each(function(){
  67. // $(this).children('td').eq(0).text(newsort);
  68. // newsort++;
  69. // });
  70. });
  71. });
  72. // 切换页数
  73. $('#file-pagination').on('click', '.page-item a', function () {
  74. // 找到当前的pageNo
  75. const curPageNo = $('#file-pagination > li[class="page-item active"] > a').text() || 1
  76. const btnType = $(this).attr('aria-label')
  77. const total = calcCount();
  78. // btnType存在,说明点击的是前一个/后一个
  79. // btnType不存在,点击的是页数
  80. if (btnType) {
  81. if (btnType === 'Previous') {
  82. // 只有大于1时才处理
  83. if (parseInt(curPageNo) !== 1) {
  84. getAllList(curPageNo - 1)
  85. }
  86. } else if(btnType === 'Next') {
  87. if (parseInt(curPageNo) !== Math.ceil(total/pageCount)) {
  88. getAllList(parseInt(curPageNo) + 1)
  89. }
  90. } else if(btnType === 'Start') {
  91. getAllList(1)
  92. } else {
  93. getAllList(Math.ceil(total/pageCount))
  94. }
  95. } else {
  96. curPageNo !== $(this).text() && getAllList(parseInt($(this).text()))
  97. }
  98. })
  99. // 生成所有附件列表
  100. function getAllList(currPageNum = 1) {
  101. // 未选中checkbox,需要过滤出来当前期的数据
  102. const filterFileData = fileData && fileData.filter(file => file.mid === parseInt(mid) && file.tid === parseInt(tid))
  103. const total = calcCount(filterFileData);
  104. // 总页数
  105. const pageNum = Math.ceil(total/pageCount);
  106. // 当前页附件内容
  107. const currPageAttData = fileData && $('#file-checkbox').is(':checked') ? fileData.slice((currPageNum-1)*pageCount, currPageNum*pageCount) : filterFileData.map((v, index) => {
  108. return {...v, index }
  109. }).slice((currPageNum-1)*pageCount, currPageNum*pageCount);
  110. renderHtml(currPageAttData)
  111. // 渲染分页器
  112. renderPagination(currPageNum, pageNum)
  113. }
  114. function renderPagination(pageNo, pageSize) {
  115. $('#file-pagination').empty()
  116. if (pageSize < 2) return
  117. const btnHtml = `
  118. <li class="page-item page-begin">
  119. <a class="page-link" href="#" aria-label="Start">
  120. <span aria-hidden="true">&laquo;</span>
  121. </a>
  122. </li>
  123. <li class="page-item page-back">
  124. <a class="page-link" href="#" aria-label="Previous">
  125. <span aria-hidden="true">&lsaquo;</span>
  126. </a>
  127. </li>
  128. <li class="page-item page-next">
  129. <a class="page-link" href="#" aria-label="Next">
  130. <span aria-hidden="true">&rsaquo;</span>
  131. </a>
  132. </li>
  133. <li class="page-item page-end">
  134. <a class="page-link" href="#" aria-label="End">
  135. <span aria-hidden="true">&raquo;</span>
  136. </a>
  137. </li>`
  138. $('#file-pagination').append(btnHtml)
  139. let html = ''
  140. // 考虑极端情况
  141. if (pageNo - 2 <= 0 || pageNo + 2 >= pageSize) {
  142. if (pageNo < 3) {
  143. const max = pageSize > 5 ? 5 : pageSize
  144. for (let i = 0; i < max; i++) {
  145. html += pageNo === i + 1 ?
  146. `<li class="page-item active"><a class="page-link" href="#">${i+1}</a></li>` :
  147. `<li class="page-item"><a class="page-link" href="#">${i+1}</a></li>`
  148. }
  149. } else {
  150. for (let i = (pageSize-4 > 0 ? pageSize-4 : 1 ); i <= pageSize; i++) {
  151. html += pageNo === i ?
  152. `<li class="page-item active"><a class="page-link" href="#">${i}</a></li>` :
  153. `<li class="page-item"><a class="page-link" href="#">${i}</a></li>`
  154. }
  155. }
  156. } else {
  157. for (let i = 0; i < pageSize; i++) {
  158. if (i + 1 === pageNo) {
  159. // 当前
  160. html+=`<li class="page-item active"><a class="page-link" href="#">${i + 1}</a></li>`
  161. } else if(i + 2 === pageNo && i + 2 <= pageSize) {
  162. // 后一页
  163. html+=`<li class="page-item"><a class="page-link" href="#">${i + 1}</a></li>`
  164. } else if (i + 3 === pageNo && i + 3 <= pageSize) {
  165. // 后两页
  166. html+=`<li class="page-item"><a class="page-link" href="#">${i + 1}</a></li>`
  167. } else if(i === pageNo) {
  168. // 前一页
  169. html+=`<li class="page-item"><a class="page-link" href="#">${i + 1}</a></li>`
  170. } else if (i -1 === pageNo && i -1 >= 0) {
  171. // 前两页
  172. html+=`<li class="page-item"><a class="page-link" href="#">${i + 1}</a></li>`
  173. }
  174. }
  175. }
  176. $('.page-next').before(html)
  177. }
  178. function renderHtml(list) {
  179. let html = '';
  180. list.forEach(fileInfo => {
  181. html += `<tr style="height: 31px;">
  182. <td>${fileInfo.index + 1}</td>
  183. <td><a href="/${fileInfo.filepath}" target="_blank">${fileInfo.file_name}</a></td>
  184. <td>${fileInfo.file_size}</td>
  185. <td>第${fileInfo.s_order}期</td>
  186. <td>${fileInfo.upload_time}</td>`
  187. if (fileInfo.canDel ) {
  188. html += `<td>
  189. <a class="btn btn-light btn-sm delete-file" data-attid="${fileInfo.id}" title="删除附件">
  190. <span class="fa fa-trash text-danger"></span>
  191. </a>
  192. </td></tr>`
  193. } else {
  194. html += `<td></td></tr>`
  195. }
  196. })
  197. $('#file-list').empty();
  198. $('#file-list').append(html);
  199. }
  200. function handleFileList(fileList) {
  201. fileData = fileList.map((file, index) => {
  202. let showDel = false
  203. // 只判断当前期,因为以往期都是只读的
  204. if (file.mid === parseInt(mid) && file.tid === parseInt(tid) && file.user_id === parseInt(cur_uid)) {
  205. if (!curAuditor) {
  206. material.status === auditConst.status.uncheck && parseInt(cur_uid) === material.user_id && (showDel = true)
  207. material.status === auditConst.status.checkNo && parseInt(cur_uid) === material.user_id && (showDel = true)
  208. } else {
  209. curAuditor.aid === parseInt(cur_uid) && (showDel = true)
  210. }
  211. }
  212. return showDel ? {...file, canDel: true, index} : {...file, index}
  213. })
  214. }
  215. function calcCount() {
  216. // 附件总数
  217. let total = fileData && fileData.length;
  218. if(!$('#file-checkbox').is(':checked')) {
  219. total = fileData && fileData.filter(file => file.mid === parseInt(mid) && file.tid === parseInt(tid)).length
  220. }
  221. return total
  222. }
  223. });
  224. /**
  225. * 校验文件大小、格式
  226. * @param {Array} files 文件数组
  227. */
  228. function validateFiles(files) {
  229. if (files.length > 10) {
  230. toastr.error('至多同时上传10个文件');
  231. return false
  232. }
  233. return files.every(file => {
  234. if (file.size > 1024 * 1024 * 30) {
  235. toastr.error('文件大小限制为30MB');
  236. return false
  237. }
  238. if (whiteList.indexOf('.' + file.ext) === -1) {
  239. toastr.error('请上传正确的格式文件');
  240. return false
  241. }
  242. return true
  243. })
  244. }