attachment.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <div class="attachment pl-4 pr-4 pt-3 pb-3 h-full flex flex-col">
  3. <div class="flex justify-between">
  4. <div class="title">附件</div>
  5. <div>
  6. <el-upload ref="uploadRef" :http-request="httpUploadFile" :limit="1" :show-file-list="false"
  7. element-loading-text="正在上传" :loading="allLoading.uploadFileLoading">
  8. <template #trigger>
  9. <el-button type="primary">上传</el-button>
  10. </template>
  11. </el-upload>
  12. </div>
  13. </div>
  14. <div class="flex-1 overflow-auto pt-3">
  15. <el-table :data="attachmenttable" border style="width: 100%;height: 200px;">
  16. <el-table-column prop="documentName" label="附件名称" />
  17. <el-table-column prop="size" label="附件大小" width="120" />
  18. <el-table-column prop="creatorName" label="上传人" width="120">
  19. <template #default="scope">
  20. <TextTranslation translationTypes="userName" :translationValue="scope.row.creatorName">
  21. </TextTranslation>
  22. </template>
  23. </el-table-column>
  24. <el-table-column prop="indate" label="上传时间" width="180" />
  25. <el-table-column label="操作" width="180" fixed="right">
  26. <template #default="scope">
  27. <el-button link type="primary" size="large" @click="fileDownload(scope.row)">下载</el-button>
  28. <el-button link type="primary" size="large" @click="operation(scope.row)">重命名</el-button>
  29. <el-button link type="danger" size="large" @click="fileDetele(scope.row)">删除</el-button>
  30. </template>
  31. </el-table-column>
  32. </el-table>
  33. </div>
  34. <!-- 弹窗 -->
  35. <el-dialog v-model="allVisible.renameDialogVisible" width="800" :show-close="false" top="10vh">
  36. <template #header="{ close, titleId, titleClass }">
  37. <div class="flex justify-between items-center border-b pb-3 dialog-header">
  38. <h4 :id="titleId">{{ '文件重命名' }}</h4>
  39. <div>
  40. <el-button type="primary" @click="saveEditClue()"
  41. :loading="allLoading.saveLoading">保存</el-button>
  42. <el-button @click="allVisible.renameDialogVisible = false">取消</el-button>
  43. </div>
  44. </div>
  45. </template>
  46. <div class="pt-3">
  47. <el-input v-model.trim="fileFormVal.name" style="width: 100%" class="pb-3" clearable />
  48. </div>
  49. </el-dialog>
  50. </div>
  51. </template>
  52. <script lang="ts" setup>
  53. import { post, uploadFile } from '@/utils/request';
  54. import { UploadRequestOptions } from 'element-plus';
  55. import { ref, reactive, onMounted, onUnmounted, inject, watchEffect } from 'vue'
  56. import { URLFILEDETELE, URL_REFNAME, URL_UPLOADFILE } from '@/pages/api';
  57. import { downloadFile } from '@/utils/tools';
  58. const globalPopup = inject<GlobalPopup>('globalPopup')
  59. const emits = defineEmits(['refreshData']);
  60. const props = defineProps<{
  61. data: any
  62. }>()
  63. type fileFormVal = {
  64. id?: string,
  65. name?: string
  66. }
  67. const uploadRef = ref<any>()
  68. const information = ref<any>({})
  69. const attachmenttable = ref([])
  70. const fileTypeStr = ref('') // 文件重命名的类型
  71. const fileFormVal = ref<fileFormVal>({})
  72. const allLoading = reactive({
  73. uploadFileLoading: false,
  74. saveLoading: false
  75. })
  76. const allVisible = reactive({
  77. renameDialogVisible: false
  78. })
  79. function saveEditClue() {
  80. if (!fileFormVal.value.name) {
  81. globalPopup?.showWarning('请输入文件名称')
  82. return
  83. }
  84. allLoading.saveLoading = true
  85. post(URL_REFNAME, {
  86. fileId: fileFormVal.value.id,
  87. newName: fileFormVal.value.name + '.' + fileTypeStr.value
  88. }).then(() => {
  89. allVisible.renameDialogVisible = false
  90. globalPopup?.showSuccess('重命名成功')
  91. emits('refreshData');
  92. }).finally(() => {
  93. allLoading.saveLoading = false
  94. })
  95. }
  96. function operation(item: any) {
  97. fileTypeStr.value = item.documentName.split('.').pop()
  98. fileFormVal.value = {
  99. id: item.id,
  100. name: item.documentName.replace(/\.[^/.]+$/, '')
  101. }
  102. allVisible.renameDialogVisible = true
  103. }
  104. function fileDownload(item: any) {
  105. downloadFile(`${item.url}`, item.documentName)
  106. }
  107. function fileDetele(item: any) {
  108. post(URLFILEDETELE, { fileIds: item.id }).then(() => {
  109. globalPopup?.showSuccess('删除成功')
  110. emits('refreshData');
  111. })
  112. }
  113. // 上传附件
  114. async function httpUploadFile(param: UploadRequestOptions) {
  115. const id = information.value.id
  116. const formData = new FormData();
  117. formData.append('file', param.file)
  118. formData.append('contactsId', id)
  119. allLoading.uploadFileLoading = true
  120. const res = await uploadFile(URL_UPLOADFILE, formData).finally(() => {
  121. allLoading.uploadFileLoading = false
  122. uploadRef.value.clearFiles()
  123. })
  124. if (res.code == 'ok') {
  125. globalPopup?.showSuccess(res.msg || '')
  126. emits('refreshData');
  127. return
  128. }
  129. globalPopup?.showError(res.msg || '')
  130. return res
  131. }
  132. watchEffect(() => {
  133. const { data } = props
  134. information.value = data
  135. attachmenttable.value = data.contactsDocumentList || []
  136. });
  137. </script>
  138. <style scoped lang="scss"></style>