ApproveTaskAttachmentEdit.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <div class="form-group">
  3. <div id="uploadAttachmentForm">
  4. <label class="control-label" for="uploadAttachmentForm">{{
  5. $t("lang.approveComment.attachment")
  6. }}</label>
  7. <div class="input-group">
  8. <div>
  9. <a-upload
  10. name="file"
  11. :file-list="fileList"
  12. :before-upload="beforeUpload"
  13. :show-upload-list="{ showDownloadIcon: false, showRemoveIcon: false }"
  14. @change="handleChange"
  15. >
  16. <a-button>
  17. <upload-outlined />
  18. {{ $t("lang.approveComment.chooseFile") }}
  19. </a-button>
  20. </a-upload>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. import Common from '../common/Common.js';
  28. import FileImage from '../widget/FileImage.vue';
  29. import { message } from 'ant-design-vue';
  30. import { UploadOutlined } from '@ant-design/icons-vue';
  31. export default {
  32. components: {
  33. FileImage,
  34. UploadOutlined,
  35. },
  36. props: {
  37. taskId: {
  38. type: String,
  39. default: null,
  40. },
  41. },
  42. data: function () {
  43. return {
  44. fileList: [],
  45. headers: {
  46. token: localStorage.getItem('#token'),
  47. },
  48. };
  49. },
  50. methods: {
  51. beforeUpload: function(file){
  52. console.log(file);
  53. const _self = this;
  54. file.status = 'uploading';
  55. this.fileList = [...this.fileList, file];
  56. this.$nextTick(function(){
  57. _self.uploadFile(file, file.uid);
  58. });
  59. return false;
  60. },
  61. /**
  62. * 设置上传文件的状态
  63. */
  64. setFileStatus : function(uid, status){
  65. const _self = this;
  66. for(var i = 0; i < _self.fileList.length; i++){
  67. if(_self.fileList[i].uid === uid){
  68. _self.fileList[i].status = status;
  69. console.debug(JSON.stringify(_self.fileList[i]));
  70. break;
  71. }
  72. }
  73. this.fileList = [...this.fileList];
  74. },
  75. handleChange: function(info) {
  76. if (info.file.status !== 'uploading') {
  77. console.log(info.file, info.fileList);
  78. }
  79. if (info.file.status === 'done') {
  80. message.success(`${info.file.name} file uploaded successfully`);
  81. } else if (info.file.status === 'error') {
  82. message.error(`${info.file.name} file upload failed.`);
  83. }
  84. },
  85. /**
  86. * 上传文件
  87. * @param file 文件
  88. * @param uid 文件唯一标识
  89. */
  90. uploadFile: function (file, uid) {
  91. var _self = this;
  92. const formData = new FormData();
  93. formData.append('file', file);
  94. formData.append('uid', uid);
  95. formData.append('taskId', _self.taskId);
  96. formData.append('attachmentType', 'FileAttachment');
  97. $.ajax({
  98. url: Common.getApiURL('WorkflowResource/addAttachment'),
  99. type: 'post',
  100. beforeSend: function (request) {
  101. Common.addTokenToRequest(request);
  102. },
  103. data: formData,
  104. contentType: false,
  105. processData: false,
  106. success: function (data) {
  107. if (data.errorCode == 0) {
  108. const fileUploadResponses = data.datas;
  109. if (fileUploadResponses != undefined && fileUploadResponses.length > 0) {
  110. fileUploadResponses.forEach(function (fileUploadResponse) {
  111. if(fileUploadResponse.errorCode == 0){
  112. _self.setFileStatus(fileUploadResponse.uid, 'done');
  113. }else{
  114. _self.setFileStatus(fileUploadResponse.uid, 'error');
  115. }
  116. });
  117. }
  118. } else {
  119. message.error(data.errorMessage);
  120. }
  121. },
  122. error: function (XMLHttpRequest, textStatus, errorThrown) {
  123. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  124. },
  125. });
  126. },
  127. },
  128. };
  129. </script>