material_file.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. /**
  3. * 材料调差 - 附件
  4. * @author LanJianRong
  5. * @date 2020/06/30
  6. * @version
  7. */
  8. $(document).ready(function () {
  9. $('#upload-file-ok').click(function () {
  10. const files = Array.from($('#upload-fujian-file')[0].files)
  11. const valiData = files.map(v => {
  12. const ext = v.name.substring(v.name.lastIndexOf('.') + 1)
  13. return {
  14. size: v.size,
  15. ext
  16. }
  17. })
  18. if (validateFiles(valiData)) {
  19. if (files.length) {
  20. const formData = new FormData()
  21. files.forEach(file => {
  22. formData.append('file', file)
  23. formData.append('name', file.name)
  24. formData.append('size', file.size)
  25. })
  26. postDataWithFile(window.location.pathname + '/upload', formData, function (result) {
  27. const files = result.map(file => {
  28. let showDel = false
  29. // 只判断当前期,因为以往期都是只读的
  30. if (file.mid === parseInt(mid) && file.tid === parseInt(tid) && file.user_id === parseInt(cur_uid)) {
  31. if (!curAuditor) {
  32. material.status === auditConst.status.uncheck && parseInt(cur_uid) === material.user_id && (showDel = true)
  33. material.status === auditConst.status.checkNo && parseInt(cur_uid) === material.user_id && (showDel = true)
  34. } else {
  35. curAuditor.aid === parseInt(cur_uid) && (showDel = true)
  36. }
  37. }
  38. return showDel ? {...file, canDel: true} : file
  39. })
  40. $('#addfujian').modal('hide');
  41. let html = '';
  42. files.forEach((fileInfo, idx) => {
  43. html += `<tr>
  44. <td>${idx + 1}</td>
  45. <td><a href="/${fileInfo.filepath}" target="_blank">${fileInfo.file_name}</a></td>
  46. <td>${fileInfo.file_size}</td>
  47. <td>第${fileInfo.s_order}期</td>
  48. <td>${fileInfo.upload_time}</td>`
  49. if (fileInfo.canDel ) {
  50. html += `<td>
  51. <a class="btn btn-light btn-sm delete-file" data-attid="${fileInfo.id}" title="删除附件">
  52. <span class="fa fa-trash text-danger"></span>
  53. </a>
  54. </td></tr>`
  55. } else {
  56. html += `<td></td></tr>`
  57. }
  58. })
  59. $('#file-list').empty();
  60. $('#file-list').append(html);
  61. });
  62. }
  63. }
  64. })
  65. $('#file-checkbox').click(function() {
  66. let isCheck = false
  67. if($(this).is(':checked')) {
  68. isCheck = true
  69. }
  70. postData(window.location.pathname + '/find', {isCheck}, function(result) {
  71. const files = result.map(file => {
  72. let showDel = false
  73. // 只判断当前期,因为以往期都是只读的
  74. if (file.mid === parseInt(mid) && file.tid === parseInt(tid) && file.user_id === parseInt(cur_uid)) {
  75. if (!curAuditor) {
  76. material.status === auditConst.status.uncheck && parseInt(cur_uid) === material.user_id && (showDel = true)
  77. material.status === auditConst.status.checkNo && parseInt(cur_uid) === material.user_id && (showDel = true)
  78. } else {
  79. curAuditor.aid === parseInt(cur_uid) && (showDel = true)
  80. }
  81. }
  82. return showDel ? {...file, canDel: true} : file
  83. })
  84. let html = '';
  85. files.forEach((fileInfo, idx) => {
  86. html += `<tr>
  87. <td>${idx + 1 }</td>
  88. <td><a href="/${fileInfo.filepath}" target="_blank">${fileInfo.file_name}</a></td>
  89. <td>${fileInfo.file_size}</td>
  90. <td>第${fileInfo.s_order}期</td>
  91. <td>${fileInfo.upload_time}</td>`
  92. if (fileInfo.canDel ) {
  93. html += `<td>
  94. <a class="btn btn-light btn-sm delete-file" data-attid="${fileInfo.id}" title="删除附件">
  95. <span class="fa fa-trash text-danger"></span>
  96. </a>
  97. </td></tr>`
  98. } else {
  99. html += `<td></td></tr>`
  100. }
  101. })
  102. $('#file-list').empty();
  103. $('#file-list').append(html);
  104. })
  105. })
  106. // 删除附件
  107. $('body').on('click', '.delete-file', function () {
  108. let attid = $(this).data('attid');
  109. let self = $(this);
  110. const data = {id: attid};
  111. postData('/tender/measure/material/file/delete', data, function (result) {
  112. self.parents('tr').remove();
  113. // 重新排序
  114. let newsort = 1;
  115. $('#file-list tr').each(function(){
  116. $(this).children('td').eq(0).text(newsort);
  117. newsort++;
  118. });
  119. });
  120. });
  121. });
  122. /**
  123. * 校验文件大小、格式
  124. * @param {Array} files 文件数组
  125. */
  126. function validateFiles(files) {
  127. const reg = /(doc|docx|excel|pdf|xlsx|xls|txt|zip|jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG|gif)$/;
  128. return files.every(file => {
  129. if (file.size > 1024 * 1024 * 10) {
  130. toastr.error('文件大小限制为10MB');
  131. return false
  132. }
  133. if (!reg.test(file.ext)) {
  134. toastr.error('请上传正确的格式文件');
  135. return false
  136. }
  137. return true
  138. })
  139. }