material_file.js 9.8 KB

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