AttachmentPanel.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <template>
  2. <div class="container-fluid">
  3. <div>
  4. <h4 class="m-h4">
  5. {{ $t("lang.attachmentPanel.attachment") }}
  6. <a role="button" class="btn btn-primary" @click="addAttachment()">
  7. <i class="fa fa-upload" />&nbsp;{{
  8. $t("lang.attachmentPanel.addAttachment")
  9. }}
  10. </a>
  11. </h4>
  12. </div>
  13. <Modal
  14. v-model:show="attachmentUploadModal"
  15. :show-ok-button="false"
  16. full="true"
  17. @cancel="showAttachmentUpload = false"
  18. >
  19. <template #header>
  20. {{ $t("lang.attachmentPanel.uploadAttachment") }}
  21. </template>
  22. <AttachmentUpload
  23. v-if="showAttachmentUpload"
  24. :class-name="className"
  25. :record-id="recordId"
  26. :window-no="windowNo"
  27. :is-show-edit="isShowEdit"
  28. :file-type="fileType"
  29. @upload-success="refresh"
  30. @get-file="getFile"
  31. />
  32. </Modal>
  33. <div class="row">
  34. <div
  35. v-for="(fileProperty, index) in fileProperties"
  36. :key="'attachment' + index"
  37. style="display: table-cell"
  38. class="col-xs-3 col-sm-3 col-md-2 col-lg-2"
  39. >
  40. <FileImage :file-property="fileProperty" :show-image="true" />
  41. <div style="display: flex">
  42. <a
  43. role="button"
  44. class="btn btn-link btn-sm"
  45. @click="downloadAttachment(fileProperty.fileName)"
  46. >
  47. <i class="fa fa-download" /> &nbsp;{{
  48. $t("lang.attachmentPanel.download")
  49. }}
  50. </a>
  51. <a
  52. v-if="isShowEdit == undefined || isShowEdit"
  53. role="button"
  54. class="btn btn-link btn-sm"
  55. @click="deleteAttachment(fileProperty.fileName)"
  56. >
  57. <i class="fa fa-trash-o" /> &nbsp;{{
  58. $t("lang.attachmentPanel.remove")
  59. }}
  60. </a>
  61. <a
  62. role="button"
  63. class="btn btn-link btn-sm"
  64. @click="previewFile(fileProperty.fileName)"
  65. >
  66. 预览
  67. </a>
  68. </div>
  69. <div class="clearfix" />
  70. <a-modal
  71. v-model:open="visible"
  72. style="text-align:center"
  73. title="预览"
  74. ok-text="确认"
  75. cancel-text="取消"
  76. @ok="handleOk"
  77. >
  78. <a-image :width="200" :src="imgUrl" />
  79. </a-modal>
  80. </div>
  81. </div>
  82. <Loading v-if="loading" />
  83. </div>
  84. </template>
  85. <script>
  86. import Common from '../../common/Common.js';
  87. import AttachmentService from '../../resource/file/AttachmentService.js';
  88. import DownloadService from '../../resource/file/DownloadService.js';
  89. import { getImageSrc } from '../../common/image-src';
  90. import FileImage from '../../widget/FileImage.vue';
  91. import AttachmentUpload from './AttachmentUpload.vue';
  92. import { Notify, Uuid } from 'pc-component-v3';
  93. export default {
  94. components: {
  95. FileImage,
  96. AttachmentUpload,
  97. },
  98. /**
  99. * className: 类名称
  100. * recordId: 记录Id
  101. * tabId: 页签Id
  102. * fileType: 附件类型,eg:".xls,.doc,.txt,.pdf"
  103. * isShowEdit: 显示上传控件(true:显示上传控件,false:不显示上传控件)
  104. */
  105. props: {
  106. className: {
  107. type: String,
  108. default: null,
  109. },
  110. recordId: {
  111. type: Number,
  112. default: null,
  113. },
  114. tabId: {
  115. type: Number,
  116. default: null,
  117. },
  118. fileType: {
  119. type: String,
  120. default: null,
  121. },
  122. isShowEdit: {
  123. type: Boolean,
  124. default: null,
  125. },
  126. windowNo: {
  127. type: String,
  128. default: null,
  129. },
  130. curdWindowFunctionAccess: {
  131. type: Object,
  132. default: function () {
  133. return null;
  134. },
  135. },
  136. },
  137. data: function () {
  138. return {
  139. fileProperties: [],
  140. type: 1,
  141. showAttachmentUpload: false,
  142. loading: false,
  143. attachmentUploadModal: false,
  144. imgUrl: '',
  145. visible: false,
  146. };
  147. },
  148. watch: {
  149. recordId: function () {
  150. this.refresh();
  151. },
  152. className: function () {
  153. this.refresh();
  154. },
  155. tabId: function () {
  156. this.refresh();
  157. },
  158. },
  159. mounted: function () {
  160. this.refresh();
  161. },
  162. methods: {
  163. getImageName: function (name) {
  164. const _self = this;
  165. return getImageSrc(_self.className, name);
  166. },
  167. /**
  168. * 刷新(获取文件属性)
  169. */
  170. refresh: function () {
  171. var _self = this;
  172. this.type = 1;
  173. this.attachmentUploadModal = false;
  174. this.showAttachmentUpload = false;
  175. if (
  176. this.className == undefined ||
  177. this.recordId == undefined ||
  178. this.windowNo == undefined
  179. ) {
  180. this.fileProperties.splice(0, this.fileProperties.length);
  181. return;
  182. }
  183. var data = {
  184. className: _self.className,
  185. recordId: _self.recordId,
  186. };
  187. AttachmentService.getFileProperties(data).then(
  188. successData => {
  189. _self.fileProperties.splice(0, _self.fileProperties.length);
  190. if (successData == undefined || successData.length == 0) {
  191. return;
  192. }
  193. for (var i = 0, len = successData.length; i < len; i++) {
  194. _self.fileProperties.push(successData[i]);
  195. }
  196. },
  197. error => {},
  198. );
  199. },
  200. /**
  201. * 下载附件
  202. */
  203. downloadAttachment: function (fileName) {
  204. var _self = this;
  205. var className = _self.className;
  206. var recordId = _self.recordId;
  207. var windowNo = _self.windowNo;
  208. var url =
  209. Common.getApiURL('file/attachmentDownload') +
  210. '?className=' +
  211. className +
  212. '&windowNo=' +
  213. windowNo +
  214. '&recordId=' +
  215. recordId +
  216. '&fileName=' +
  217. fileName;
  218. DownloadService.downloadFile(url, fileName);
  219. },
  220. /**
  221. * 删除附件
  222. */
  223. deleteAttachment: function (fileName) {
  224. var _self = this;
  225. var data = {
  226. windowNo: _self.windowNo,
  227. className: _self.className,
  228. recordId: _self.recordId,
  229. tabId: _self.tabId,
  230. fileName: fileName,
  231. };
  232. AttachmentService.deleteAttachment(data).then(successData => {
  233. if (data.errorCode != 0) {
  234. // Notify.error(
  235. // _self.$t('lang.Notify.warning'),
  236. // successData.errorMessage,
  237. // false,
  238. // );
  239. }
  240. _self.refresh();
  241. });
  242. },
  243. // 预览文件
  244. previewFile: function (name) {
  245. let flag = this.isPhotoPdf(name);
  246. let pdf = this.isPdf(name);
  247. var _self = this;
  248. var className = _self.className;
  249. var recordId = _self.recordId;
  250. var windowNo = _self.windowNo;
  251. let url =
  252. Common.getApiURL('file/attachmentDownload') +
  253. '?className=' +
  254. className +
  255. '&windowNo=' +
  256. windowNo +
  257. '&recordId=' +
  258. recordId +
  259. '&fileName=' +
  260. name;
  261. const xhr = new XMLHttpRequest();
  262. xhr.responseType = 'blob'; // 返回类型blob
  263. xhr.open('get', url, true);
  264. const token = localStorage.getItem('#token');
  265. xhr.setRequestHeader('token', token);
  266. xhr.onload = function () {
  267. if (this.status === 200) {
  268. if (flag) {
  269. // 图片
  270. _self.visible = true;
  271. _self.imgUrl = window.URL.createObjectURL(this.response);
  272. } else if (pdf) {
  273. // pdf文件
  274. let type = xhr.getResponseHeader('Content-Type');
  275. let blob = new Blob([this.response], {
  276. type: type,
  277. });
  278. let href =window.URL.createObjectURL(blob);
  279. window.open(href,'_blank');
  280. } else {
  281. // 其他文件
  282. _self.$message.error('仅支持预览图片和pdf文件');
  283. }
  284. }
  285. };
  286. xhr.send();
  287. },
  288. handleOk:function(){
  289. this.visible = false;
  290. },
  291. isPdf: function (name) {
  292. if (name.endsWith('.pdf')) {
  293. return true;
  294. } else {
  295. return false;
  296. }
  297. },
  298. isPhotoPdf: function (name) {
  299. var fileName = name;
  300. if (
  301. fileName.endsWith('.jpg') || //图片
  302. fileName.endsWith('.jpeg') ||
  303. fileName.endsWith('.bmp') ||
  304. fileName.endsWith('.gif') ||
  305. fileName.endsWith('.png')
  306. ) {
  307. return true;
  308. } else {
  309. return false;
  310. }
  311. },
  312. /**
  313. * 显示附件面板
  314. */
  315. addAttachment: function () {
  316. this.attachmentUploadModal = true;
  317. this.showAttachmentUpload = true;
  318. },
  319. },
  320. };
  321. </script>
  322. <style scoped>
  323. .m-h4 {
  324. border: 1px solid #eee;
  325. border-top: none;
  326. border-left: none;
  327. border-right: none;
  328. padding-bottom: 10px;
  329. }
  330. </style>