material_file.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.file_path}">${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.file_path}">${fileInfo.file_name}</a></td>
  63. <td>${fileInfo.file_size}</td>
  64. <td>${fileInfo.upload_time}</td>`
  65. if (user_id == fileInfo.user_id && !checked) {
  66. html += `<td>
  67. <a class="btn btn-light btn-sm delete-file" data-attid="${fileInfo.id}" title="删除附件">
  68. <span class="fa fa-trash text-danger"></span>
  69. </a>
  70. </td></tr>`
  71. } else {
  72. html += `<td></td></tr>`
  73. }
  74. })
  75. $('#file-list').empty();
  76. $('#file-list').append(html);
  77. })
  78. })
  79. // 删除附件
  80. $('body').on('click', '.delete-file', function () {
  81. let attid = $(this).data('attid');
  82. let self = $(this);
  83. const data = {id: attid};
  84. postData('/tender/measure/material/file/delete', data, function (result) {
  85. self.parents('tr').remove();
  86. // 重新排序
  87. let newsort = 1;
  88. $('#file-list tr').each(function(){
  89. $(this).children('td').eq(0).text(newsort);
  90. newsort++;
  91. });
  92. });
  93. });
  94. });
  95. /**
  96. * 校验文件大小、格式
  97. * @param {Array} files 文件数组
  98. */
  99. function validateFiles(files) {
  100. const reg = /(doc|docx|excel|xlsx|xls|txt|zip|jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG)$/;
  101. return files.every(file => {
  102. if (file.size > 1024 * 1024 * 10) {
  103. toastr.error('文件大小限制为10MB');
  104. return false
  105. }
  106. if (!reg.test(file.ext)) {
  107. toastr.error('请上传正确的格式文件');
  108. return false
  109. }
  110. return true
  111. })
  112. }