attachment.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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="正在上传">
  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="name" label="附件名称" width="180" />
  17. <el-table-column prop="size" label="附件大小" width="120" />
  18. <el-table-column prop="userName" label="上传人" width="120" />
  19. <el-table-column prop="newCreateTime" 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="showVisible(scope.row)">重命名</el-button>
  24. <el-button link type="danger" size="large" @click="deteleFile(scope.row)">删除</el-button>
  25. </template>
  26. </el-table-column>
  27. </el-table>
  28. </div>
  29. <!-- 弹窗 -->
  30. <el-dialog v-model="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()">保存</el-button>
  36. <el-button @click="renameDialogVisible = false">取消</el-button>
  37. </div>
  38. </div>
  39. </template>
  40. <div class="pt-3">
  41. <el-input v-model.trim="renameVal" style="width: 100%" class="pb-3" clearable />
  42. </div>
  43. </el-dialog>
  44. </div>
  45. </template>
  46. <script lang="ts" setup>
  47. import { ElMessageBox, UploadRequestOptions } from 'element-plus';
  48. import { post, uploadFile } from '@/utils/request';
  49. import { confirmAction, downloadFile } from '@/utils/tools';
  50. import { ref, reactive, onMounted, onUnmounted, defineEmits, defineExpose, inject, watchEffect } from 'vue'
  51. import { DETELEFILEFILE, REFIENAMEFILE, UPLOADFILEFILE } from '../api';
  52. import { formatDate } from '@/utils/times';
  53. const emits = defineEmits(['refreshData']);
  54. const globalPopup = inject<GlobalPopup>('globalPopup')
  55. const props = defineProps<{
  56. information: any
  57. }>()
  58. const attachmenttable = ref([])
  59. const information = ref<any>({})
  60. const renameDialogVisible = ref(false)
  61. const renameVal = ref('')
  62. const transitionVal = ref<any>({})
  63. const uploadRef = ref<any>()
  64. // 下载文件
  65. function fileDownload(item: any) {
  66. downloadFile(`${item.path}`, item.name)
  67. }
  68. // 删除文件
  69. function deteleFile(item: any) {
  70. const id = item.id
  71. confirmAction(`确定删除【${item.name}】文件吗?`).then(() => {
  72. post(DETELEFILEFILE, { id }).then((_res) => {
  73. globalPopup?.showSuccess('删除成功')
  74. emits('refreshData')
  75. })
  76. })
  77. }
  78. // 保存重命名
  79. function saveEditClue() {
  80. if (!renameVal.value) {
  81. globalPopup?.showWarning('请输入文件名称')
  82. return
  83. }
  84. // const bussinessId = information.value.id
  85. const bussinessId = transitionVal.value.id
  86. post(REFIENAMEFILE, { name: renameVal.value, id: bussinessId }).then((res) => {
  87. renameDialogVisible.value = false
  88. globalPopup?.showSuccess(res.msg || '')
  89. emits('refreshData')
  90. }).catch((_err) => { })
  91. }
  92. // 显示弹窗
  93. function showVisible(item: any) {
  94. renameVal.value = JSON.parse(JSON.stringify(item.name))
  95. transitionVal.value = JSON.parse(JSON.stringify(item))
  96. renameDialogVisible.value = true
  97. }
  98. // 上传附件
  99. async function httpUploadFile(param: UploadRequestOptions) {
  100. const bussinessId = information.value.id
  101. const formData = new FormData();
  102. formData.append('file', param.file)
  103. formData.append('id', bussinessId)
  104. const res = await uploadFile(UPLOADFILEFILE, formData).finally(() => {
  105. uploadRef.value.clearFiles()
  106. })
  107. if (res.code == 'ok') {
  108. globalPopup?.showSuccess(res.msg || '')
  109. emits('refreshData');
  110. return
  111. }
  112. globalPopup?.showError(res.msg || '')
  113. return res
  114. }
  115. watchEffect(() => {
  116. information.value = props.information
  117. attachmenttable.value = (props.information.uploadFilePList || []).map((item: any) => {
  118. return {
  119. ...item,
  120. newCreateTime: formatDate(new Date(item.createTime))
  121. }
  122. })
  123. });
  124. // 生命周期钩子
  125. onMounted(() => { });
  126. </script>
  127. <style scoped lang="scss">
  128. .attachment {
  129. .title {
  130. font-size: 18px;
  131. color: #000
  132. }
  133. }
  134. </style>