attachment.vue 5.1 KB

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