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