attachment.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 v-permission="['customerEdit']">
  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="createTime" label="上传时间" width="180" sortable />
  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 { post, uploadFile } from '@/utils/request';
  48. import { UploadRequestOptions } from 'element-plus';
  49. import { ref, reactive, onMounted, onUnmounted, defineExpose, inject, watchEffect } from 'vue'
  50. import { URL_DETELEFILE, URL_REFFILENAME, URL_UPLOADFILE } from '../api';
  51. import { confirmAction, downloadFile } from '@/utils/tools';
  52. import { formatDate } from '@/utils/times';
  53. const emits = defineEmits(['refreshData']);
  54. const globalPopup = inject<GlobalPopup>('globalPopup')
  55. const props = defineProps<{
  56. data: any
  57. }>()
  58. const attachmenttable = ref([])
  59. const information = ref<any>({})
  60. const uploadRef = <any>ref()
  61. const renameDialogVisible = ref(false)
  62. const renameVal = ref('')
  63. // 下载文件
  64. function fileDownload(item: any) {
  65. downloadFile(`${item.path}`, item.name)
  66. }
  67. // 删除文件
  68. function deteleFile(item: any) {
  69. const id = item.id
  70. confirmAction(`确定删除【${item.name}】文件吗?`).then(() => {
  71. post(URL_DETELEFILE, { id }).then((_res) => {
  72. globalPopup?.showSuccess('删除成功')
  73. emits('refreshData')
  74. })
  75. })
  76. }
  77. // 保存重命名
  78. function saveEditClue() {
  79. if (!renameVal.value) {
  80. globalPopup?.showWarning('请输入文件名称')
  81. return
  82. }
  83. const id = information.value.id
  84. post(URL_REFFILENAME, { name: renameVal.value, id: id }).then((res) => {
  85. renameDialogVisible.value = false
  86. globalPopup?.showSuccess(res.msg || '')
  87. emits('refreshData')
  88. }).catch((_err) => { })
  89. }
  90. // 上传文件
  91. async function httpUploadFile(param: UploadRequestOptions) {
  92. const id = information.value.id
  93. const formData = new FormData();
  94. formData.append('file', param.file)
  95. formData.append('id', id)
  96. const res = await uploadFile(URL_UPLOADFILE, formData).finally(() => {
  97. uploadRef.value.clearFiles()
  98. })
  99. if (res.code == 'ok') {
  100. globalPopup?.showSuccess(res.msg || '')
  101. emits('refreshData');
  102. return
  103. }
  104. globalPopup?.showError(res.msg || '')
  105. return res
  106. }
  107. // 显示弹窗
  108. function showVisible(item: any) {
  109. renameVal.value = JSON.parse(JSON.stringify(item.name))
  110. renameDialogVisible.value = true
  111. }
  112. watchEffect(() => {
  113. information.value = props.data
  114. attachmenttable.value = (props.data.files || []).map((item: any) => {
  115. return {
  116. ...item,
  117. createTime: formatDate(new Date(item.createTime))
  118. }
  119. })
  120. });
  121. // 生命周期钩子
  122. onMounted(() => {
  123. });
  124. </script>
  125. <style scoped lang="scss">
  126. .attachment {
  127. .title {
  128. font-size: 18px;
  129. color: #000
  130. }
  131. }
  132. </style>