material_file.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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(window.location.pathname + '/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. $('.dropdown-item').click(function() {
  100. const type = $('#dropdownMenuButton').attr('btn-type')
  101. if (type === 'curr') {
  102. $(this).text('当前期')
  103. $('#dropdownMenuButton').text('所有期')
  104. $('#dropdownMenuButton').attr('btn-type', 'all')
  105. } else {
  106. $(this).text('所有期')
  107. $('#dropdownMenuButton').text('当前期')
  108. $('#dropdownMenuButton').attr('btn-type', 'curr')
  109. }
  110. getAllList()
  111. })
  112. // 生成所有附件列表
  113. function getAllList(currPageNum = 1) {
  114. const isCheckAll = $('#dropdownMenuButton').attr('btn-type') === 'all'
  115. // 未选中checkbox,需要过滤出来当前期的数据
  116. const filterFileData = fileData && isCheckAll ? fileData : fileData.filter(file => file.mid === parseInt(mid) && file.tid === parseInt(tid))
  117. const total = calcCount();
  118. // 总页数
  119. const pageNum = Math.ceil(total/pageCount);
  120. // 当前页附件内容
  121. const currPageAttData = fileData && isCheckAll ? fileData.map((v, index) => {
  122. return {...v, index }
  123. }).slice((currPageNum-1)*pageCount, currPageNum*pageCount)
  124. : filterFileData.map((v, index) => {
  125. return {...v, index }
  126. }).slice((currPageNum-1)*pageCount, currPageNum*pageCount);
  127. renderHtml(currPageAttData)
  128. // 渲染分页器
  129. renderPagination(currPageNum, pageNum)
  130. };
  131. function renderPagination(pageNo, pageSize) {
  132. $('#file-pagination').empty()
  133. if (pageSize < 2) return
  134. const btnHtml = `
  135. <li class="page-item page-begin">
  136. <a class="page-link" href="#" aria-label="Start">
  137. <span aria-hidden="true">&laquo;</span>
  138. </a>
  139. </li>
  140. <li class="page-item page-back">
  141. <a class="page-link" href="#" aria-label="Previous">
  142. <span aria-hidden="true">&lsaquo;</span>
  143. </a>
  144. </li>
  145. <li class="page-item page-next">
  146. <a class="page-link" href="#" aria-label="Next">
  147. <span aria-hidden="true">&rsaquo;</span>
  148. </a>
  149. </li>
  150. <li class="page-item page-end">
  151. <a class="page-link" href="#" aria-label="End">
  152. <span aria-hidden="true">&raquo;</span>
  153. </a>
  154. </li>`
  155. $('#file-pagination').append(btnHtml)
  156. let html = ''
  157. // 考虑极端情况
  158. if (pageNo - 2 <= 0 || pageNo + 2 >= pageSize) {
  159. if (pageNo < 3) {
  160. const max = pageSize > 5 ? 5 : pageSize
  161. for (let i = 0; i < max; i++) {
  162. html += pageNo === i + 1 ?
  163. `<li class="page-item active"><a class="page-link" href="#">${i+1}</a></li>` :
  164. `<li class="page-item"><a class="page-link" href="#">${i+1}</a></li>`
  165. }
  166. } else {
  167. for (let i = (pageSize-4 > 0 ? pageSize-4 : 1 ); i <= pageSize; i++) {
  168. html += pageNo === i ?
  169. `<li class="page-item active"><a class="page-link" href="#">${i}</a></li>` :
  170. `<li class="page-item"><a class="page-link" href="#">${i}</a></li>`
  171. }
  172. }
  173. } else {
  174. for (let i = 0; i < pageSize; i++) {
  175. if (i + 1 === pageNo) {
  176. // 当前
  177. html+=`<li class="page-item active"><a class="page-link" href="#">${i + 1}</a></li>`
  178. } else if(i + 2 === pageNo && i + 2 <= pageSize) {
  179. // 后一页
  180. html+=`<li class="page-item"><a class="page-link" href="#">${i + 1}</a></li>`
  181. } else if (i + 3 === pageNo && i + 3 <= pageSize) {
  182. // 后两页
  183. html+=`<li class="page-item"><a class="page-link" href="#">${i + 1}</a></li>`
  184. } else if(i === pageNo) {
  185. // 前一页
  186. html+=`<li class="page-item"><a class="page-link" href="#">${i + 1}</a></li>`
  187. } else if (i -1 === pageNo && i -1 >= 0) {
  188. // 前两页
  189. html+=`<li class="page-item"><a class="page-link" href="#">${i + 1}</a></li>`
  190. }
  191. }
  192. }
  193. $('.page-next').before(html)
  194. };
  195. function renderHtml(list) {
  196. let html = '';
  197. $('#check-all-file').prop("checked", false)
  198. list.forEach(fileInfo => {
  199. html += `<tr style="height: 31px;">
  200. <td width="25"><input type="checkbox" class="check-file" file-id=${fileInfo.id}></td>
  201. <td>${fileInfo.index + 1}</td>
  202. <td><a href="/${fileInfo.filepath}" target="_blank">${fileInfo.file_name}</a></td>
  203. <td>${fileInfo.file_size}</td>
  204. <td>第${fileInfo.s_order}期</td>
  205. <td>${fileInfo.upload_time}</td>`
  206. if (fileInfo.showDel ) {
  207. html += `<td>
  208. <a href="/${fileInfo.filepath}" class="btn btn-light btn-sm" title="下载"><span class="fa fa-download text-primary"></span></a>
  209. <a href="javascript:void(0);" class="btn btn-light btn-sm delete-file" data-attid="${fileInfo.id}" title="删除附件">
  210. <span class="fa fa-trash text-danger"></span>
  211. </a>
  212. </td></tr>`
  213. } else {
  214. html += `<td><a href="/${fileInfo.filepath}" class="btn btn-light btn-sm" title="下载"><span class="fa fa-download text-primary"></span></a></td></tr>`
  215. }
  216. })
  217. $('#file-list').empty();
  218. $('#file-list').append(html);
  219. };
  220. $('#file-list').on('click', '.check-file', function() {
  221. const checkedList = $('#file-list').find('input:checked')
  222. const childs = $('#file-list').children().length
  223. const checkBox = $('#check-all-file')
  224. if (checkedList.length === childs) {
  225. checkBox.prop("checked", true)
  226. } else {
  227. checkBox.prop("checked", false)
  228. }
  229. })
  230. $('#check-all-file').click(function() {
  231. const isCheck = $(this).is(':checked')
  232. $('#file-list').children().each(function() {
  233. $(this).find('input:checkbox').prop("checked", isCheck)
  234. })
  235. });
  236. $('#bach-download').click(function() {
  237. const fileIds = []
  238. $( '#file-list .check-file:checked').each(function() {
  239. const fileId = $(this).attr('file-id')
  240. fileId && fileIds.push(fileId)
  241. })
  242. // console.log('fileIds', fileIds)
  243. if (fileIds.length) {
  244. // postData( `/tender/${tid}/measure/material/${order}/file/download/compresse-file`, { fileIds })
  245. $('#downloadZip').attr('href', `/tender/${tid}/measure/material/${order}/file/download/compresse-file?fileIds=${JSON.stringify(fileIds)}`);
  246. $('#downloadZip')[0].click();
  247. }
  248. });
  249. function handleFileList(fileList) {
  250. fileData = fileList.map((file, index) => {
  251. let showDel = false
  252. // 只判断当前期,因为以往期都是只读的
  253. if (file.mid === parseInt(mid) && file.tid === parseInt(tid) && file.user_id === parseInt(cur_uid)) {
  254. if (materialStatus === auditConst.status.checked) {
  255. showDel = Boolean(file.extra_upload )
  256. } else {
  257. showDel = true
  258. }
  259. // if (!curAuditor) {
  260. // materialStatus === auditConst.status.uncheck && parseInt(cur_uid) === materialUid && (showDel = true)
  261. // materialStatus === auditConst.status.checkNo && parseInt(cur_uid) === materialUid && (showDel = true)
  262. // } else {
  263. // curAuditor.aid === parseInt(cur_uid) && (showDel = true)
  264. // }
  265. }
  266. return {...file, index, showDel}
  267. })
  268. };
  269. function calcCount() {
  270. // 附件总数
  271. let total = fileData && fileData.length;
  272. if($('#dropdownMenuButton').attr('btn-type') === 'curr') {
  273. total = fileData && fileData.filter(file => file.mid === parseInt(mid) && file.tid === parseInt(tid)).length
  274. }
  275. return total
  276. };
  277. $.subMenu({
  278. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  279. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  280. key: 'menu.1.0.0',
  281. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  282. callback: function (info) {
  283. if (info.mini) {
  284. $('.panel-title').addClass('fluid');
  285. $('#sub-menu').removeClass('panel-sidebar');
  286. } else {
  287. $('.panel-title').removeClass('fluid');
  288. $('#sub-menu').addClass('panel-sidebar');
  289. }
  290. autoFlashHeight();
  291. }
  292. });
  293. });
  294. /**
  295. * 校验文件大小、格式
  296. * @param {Array} files 文件数组
  297. */
  298. function validateFiles(files) {
  299. if (files.length > 10) {
  300. toastr.error('至多同时上传10个文件');
  301. return false
  302. }
  303. return files.every(file => {
  304. if (file.size > 1024 * 1024 * 30) {
  305. toastr.error('文件大小限制为30MB');
  306. return false
  307. }
  308. if (whiteList.indexOf('.' + file.ext) === -1) {
  309. toastr.error('请上传正确的格式文件');
  310. return false
  311. }
  312. return true
  313. })
  314. };