TraceAttachment.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <template>
  2. <div>
  3. <div>
  4. <input ref="fileInput"
  5. type="file"
  6. class="form-control file-input"
  7. @change="onFileChanges" />
  8. <label for="attachment">
  9. <a role="button"
  10. class="btn btn-primary btn-sm"
  11. @click="clickButton">
  12. <i class="fa fa-upload" />
  13. &nbsp;
  14. 上传附件
  15. </a>
  16. </label>
  17. </div>
  18. <div v-if="attachmentList != undefined && attachmentList.length > 0">
  19. <ul class="list-group">
  20. <template v-for="attachment in attachmentList">
  21. <li class="list-group-item"
  22. :key="attachment.id">
  23. <span class="badge"
  24. @click="deleteByTraceAttachmentId(attachment.id)">删除</span>
  25. <span @click="download(attachment)"
  26. class="badge">下载</span>
  27. {{attachment.attachment}}
  28. </li>
  29. </template>
  30. </ul>
  31. </div>
  32. <Loading ref="loading"></Loading>
  33. </div>
  34. </template>
  35. <script>
  36. var Common = require("../common/Common.js");
  37. var Notify = require("pc-client-component").Notify;
  38. var DownloadService =require("pc-client-component").DownloadService;
  39. var Loading = require("pc-client-component").Loading;
  40. export default {
  41. props: ["traceId"],
  42. data: function () {
  43. return {
  44. className: "com.leanwo.prodog.model.trace.TraceAttachment",
  45. attachmentList: [],
  46. traceConfigDto:{}
  47. }
  48. },
  49. components: {
  50. Loading,DownloadService
  51. },
  52. methods: {
  53. /**
  54. * 下载文件
  55. * @param {String} fileName 文件名
  56. * @return {void}
  57. */
  58. download: function (item) {
  59. var _self = this;
  60. DownloadService.fileDownload(_self.className, item.attachment);
  61. },
  62. /**
  63. * 删除任务附件
  64. */
  65. deleteByTraceAttachmentId: function (traceAttachementId) {
  66. var _self = this;
  67. $.ajax({
  68. url: Common.getApiURL('TraceAttachmentResource/deleteByTraceAttachmentId'),
  69. type: 'get',
  70. dataType: 'json',
  71. data: {
  72. 'id': traceAttachementId
  73. },
  74. beforeSend: function (request) {
  75. Common.addTokenToRequest(request);
  76. },
  77. success: function (data) {
  78. if (data == false) {
  79. Notify.error("提示", "您没有权限删除附件,只有任务发起人和附件上传人有权限删除附件!");
  80. _self.listTraceAttachmentByTraceId();
  81. } else {
  82. _self.listTraceAttachmentByTraceId();
  83. }
  84. },
  85. });
  86. },
  87. /* 选择文件发生改变
  88. * @param {[type]} e [description]
  89. * @return {[type]} [description]
  90. */
  91. onFileChanges: function (e) {
  92. var files = e.target.files || e.dataTransfer.files;
  93. if (!files.length)
  94. return;
  95. this.uploadFile(files[0]);
  96. },
  97. /**
  98. * 上传文件
  99. * @param {File} selectedFile 选择的文件
  100. */
  101. uploadFile: function (selectedFile) {
  102. var _self = this;
  103. if (selectedFile == undefined) {
  104. return;
  105. }
  106. var size = undefined;
  107. if(_self.traceConfigDto.traceAttachmentSize != undefined && _self.traceConfigDto.traceAttachmentSize != null){
  108. size = _self.traceConfigDto.traceAttachmentSize;
  109. }
  110. //当系统没有配置附件大小时默认8m
  111. if(size == undefined){
  112. size = 8;
  113. }
  114. if ((selectedFile.size / 1024) <= (1024 * size)) {
  115. var formData = new FormData();
  116. formData.append("files", selectedFile);
  117. formData.append("className", _self.className);
  118. _self.$refs.loading.show();
  119. $.ajax({
  120. url: Common.getApiURL("file/classFileUpload"),
  121. type: "post",
  122. beforeSend: function (request) {
  123. Common.addTokenToRequest(request);
  124. },
  125. data: formData,
  126. contentType: false,
  127. processData: false,
  128. success: function (data) {
  129. _self.$refs.loading.hide();
  130. if (data != "error") {
  131. var fileName = data.substring(data.indexOf(":") + 1);
  132. _self.uploadTraceAttachment(fileName);
  133. }
  134. },
  135. error: function (XMLHttpRequest, textStatus, errorThrown) {
  136. _self.$refs.loading.hide();
  137. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  138. }
  139. });
  140. } else {
  141. Notify.error("提示", "文件大小不能超过"+size+"M,可在任务配置功能中配置!");
  142. }
  143. },
  144. /**
  145. * 获取任务管理配置
  146. */
  147. getTraceConfig: function() {
  148. var _self = this;
  149. $.ajax({
  150. url: Common.getApiURL('TraceConfigResource/queryTraceConfigDto'),
  151. type: "get",
  152. dataType: "json",
  153. beforeSend: function(request) {
  154. Common.addTokenToRequest(request);
  155. },
  156. success: function(data) {
  157. _self.traceConfigDto = data;
  158. },
  159. error: function(XMLHttpRequest, textStatus, errorThrown) {
  160. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  161. }
  162. });
  163. },
  164. /**
  165. * 查询当前追踪单下所有的附件
  166. */
  167. listTraceAttachmentByTraceId: function () {
  168. var _self = this;
  169. $.ajax({
  170. url: Common.getApiURL("TraceAttachmentResource/listByTraceId"),
  171. type: "get",
  172. contentType: "application/json",
  173. beforeSend: function (request) {
  174. Common.addTokenToRequest(request);
  175. },
  176. data: {
  177. 'traceId': _self.traceId,
  178. },
  179. success: function (data) {
  180. console.log(data);
  181. _self.attachmentList = data;
  182. },
  183. });
  184. },
  185. /**
  186. * 上传附件
  187. */
  188. uploadTraceAttachment: function (fileName) {
  189. var _self = this;
  190. var traceAttachement = {
  191. traceId: _self.traceId,
  192. attachment: fileName,
  193. };
  194. $.ajax({
  195. url: Common.getApiURL("TraceAttachmentResource/save"),
  196. type: "post",
  197. contentType: "application/json",
  198. beforeSend: function (request) {
  199. Common.addTokenToRequest(request);
  200. },
  201. data: JSON.stringify(traceAttachement),
  202. success: function (data) {
  203. _self.listTraceAttachmentByTraceId();
  204. },
  205. error: function (XMLHttpRequest, textStatus, errorThrown) {
  206. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  207. Notify.error("提示", "上传失败!");
  208. }
  209. });
  210. },
  211. /**
  212. * 点击上传按钮事件
  213. * @return {[type]} [description]
  214. */
  215. clickButton: function () {
  216. $(this.$refs.fileInput).click();
  217. },
  218. },
  219. mounted: function () {
  220. if (this.traceId != undefined) {
  221. this.listTraceAttachmentByTraceId();
  222. }
  223. this.getTraceConfig();
  224. },
  225. watch: {
  226. "traceId": function (curVal, oldVal) {
  227. if (curVal != undefined) {
  228. this.listTraceAttachmentByTraceId();
  229. }
  230. }
  231. }
  232. }
  233. </script>
  234. <style scoped>
  235. .badge {
  236. cursor: pointer;
  237. background-color: #428bca;
  238. color: white;
  239. }
  240. </style>
  241. <style scoped>
  242. .file-input {
  243. width: 0.1px;
  244. height: 0.1px;
  245. opacity: 0;
  246. overflow: hidden;
  247. position: absolute;
  248. z-index: -1;
  249. }
  250. </style>