material_file.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. $('#addfujian').modal('hide');
  28. let html = '';
  29. result.forEach((fileInfo, idx) => {
  30. html += `<tr>
  31. <td>${idx + 1}</td>
  32. <td><a href="/${fileInfo.filepath}">${fileInfo.file_name}</a></td>
  33. <td>${fileInfo.file_size}</td>
  34. <td>${fileInfo.upload_time}</td>`
  35. if (user_id == fileInfo.user_id && checked) {
  36. html += `<td>
  37. <a class="btn btn-light btn-sm delete-file" data-attid="${fileInfo.id}" title="删除附件">
  38. <span class="fa fa-trash text-danger"></span>
  39. </a>
  40. </td></tr>`
  41. } else {
  42. html += `<td></td></tr>`
  43. }
  44. })
  45. $('#file-list').empty();
  46. $('#file-list').append(html);
  47. console.log(result)
  48. });
  49. }
  50. }
  51. })
  52. $('#file-checkbox').click(function() {
  53. let isCheck = false
  54. if($(this).is(':checked')) {
  55. isCheck = true
  56. }
  57. postData(window.location.pathname + '/find', {isCheck}, function(result) {
  58. let html = '';
  59. result.forEach((fileInfo, idx) => {
  60. html += `<tr>
  61. <td>${idx + 1 }</td>
  62. <td><a href="/${fileInfo.filepath}">${fileInfo.file_name}</a></td>
  63. <td>${fileInfo.file_size}</td>
  64. <td>${fileInfo.upload_time}</td>`
  65. if (isCheck) {
  66. // 所有期
  67. if (fileInfo.pre_delete && user_id == fileInfo.user_id) {
  68. html += `<td>
  69. <a class="btn btn-light btn-sm delete-file" data-attid="${fileInfo.id}" title="删除附件">
  70. <span class="fa fa-trash text-danger"></span>
  71. </a>
  72. </td></tr>`
  73. } else {
  74. html += `<td></td></tr>`
  75. }
  76. } else {
  77. // 当前期
  78. if (user_id == fileInfo.user_id && checked) {
  79. html += `<td>
  80. <a class="btn btn-light btn-sm delete-file" data-attid="${fileInfo.id}" title="删除附件">
  81. <span class="fa fa-trash text-danger"></span>
  82. </a>
  83. </td></tr>`
  84. } else {
  85. html += `<td></td></tr>`
  86. }
  87. }
  88. })
  89. $('#file-list').empty();
  90. $('#file-list').append(html);
  91. })
  92. })
  93. // 删除附件
  94. $('body').on('click', '.delete-file', function () {
  95. let attid = $(this).data('attid');
  96. let self = $(this);
  97. const data = {id: attid};
  98. postData('/tender/measure/material/file/delete', data, function (result) {
  99. self.parents('tr').remove();
  100. // 重新排序
  101. let newsort = 1;
  102. $('#file-list tr').each(function(){
  103. $(this).children('td').eq(0).text(newsort);
  104. newsort++;
  105. });
  106. });
  107. });
  108. });
  109. /**
  110. * 校验文件大小、格式
  111. * @param {Array} files 文件数组
  112. */
  113. function validateFiles(files) {
  114. const reg = /(doc|docx|excel|xlsx|xls|txt|zip|jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG)$/;
  115. return files.every(file => {
  116. if (file.size > 1024 * 1024 * 10) {
  117. toastr.error('文件大小限制为10MB');
  118. return false
  119. }
  120. if (!reg.test(file.ext)) {
  121. toastr.error('请上传正确的格式文件');
  122. return false
  123. }
  124. return true
  125. })
  126. }