✨ feat: 人事流程
This commit is contained in:
301
ruoyi-ui/src/components/FileList/index.vue
Normal file
301
ruoyi-ui/src/components/FileList/index.vue
Normal file
@@ -0,0 +1,301 @@
|
||||
<template>
|
||||
<div class="file-manager">
|
||||
<!-- 操作栏 -->
|
||||
<el-row class="mb8" v-if="!readOnly">
|
||||
<el-button type="primary" size="mini" @click="showAddDialog = true">添加文件</el-button>
|
||||
<!-- <el-button type="danger" size="mini" :disabled="selectedFiles.length === 0" @click="handleDelete">删除选中</el-button> -->
|
||||
</el-row>
|
||||
|
||||
<!-- 文件表格 -->
|
||||
<el-table
|
||||
v-loading="loading"
|
||||
:data="fileList"
|
||||
style="width: 100%"
|
||||
@selection-change="handleSelectionChange"
|
||||
>
|
||||
<el-table-column type="selection" width="55" align="center" />
|
||||
<el-table-column prop="fileName" label="文件名" min-width="200">
|
||||
<template #default="{row}">
|
||||
<div class="file-name-cell">
|
||||
<span>{{ row.fileName }}</span>
|
||||
</div>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="fileType" label="类型" width="120">
|
||||
<template #default="{row}">
|
||||
<el-tag effect="plain">{{ row.fileType }}</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="uploadTime" label="上传时间" width="180">
|
||||
<template #default="{row}">{{ formatDate(row.uploadTime) }}</template>
|
||||
</el-table-column>
|
||||
<!-- <el-table-column label="操作" width="150" fixed="right" >
|
||||
<template #default="{row}">
|
||||
<el-button size="mini" type="text" @click="handlePreview(row)">预览</el-button>
|
||||
<el-button size="mini" type="text" @click="handleDownload(row)">下载</el-button>
|
||||
</template>
|
||||
</el-table-column> -->
|
||||
</el-table>
|
||||
|
||||
<!-- 添加文件对话框 -->
|
||||
<el-dialog
|
||||
title="添加文件"
|
||||
:visible.sync="showAddDialog"
|
||||
:modal="false"
|
||||
width="600px"
|
||||
@closed="resetUpload"
|
||||
>
|
||||
<el-upload
|
||||
class="upload-demo"
|
||||
drag
|
||||
multiple
|
||||
:auto-upload="false"
|
||||
:on-change="handleFileChange"
|
||||
:file-list="tempFiles"
|
||||
:before-upload="beforeUpload"
|
||||
>
|
||||
<i class="el-icon-upload"></i>
|
||||
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
|
||||
<div class="el-upload__tip">支持格式:doc/docx/pdf/jpg/png,单个文件不超过50MB</div>
|
||||
</el-upload>
|
||||
|
||||
<el-form
|
||||
ref="fileForm"
|
||||
:model="formData"
|
||||
label-width="80px"
|
||||
class="file-form"
|
||||
>
|
||||
<el-form-item
|
||||
v-for="(file, index) in tempFiles"
|
||||
:key="file.uid"
|
||||
:label="`文件 ${index + 1}`"
|
||||
:prop="'files.' + index + '.fileName'"
|
||||
:rules="{ required: true, message: '文件名不能为空', trigger: 'blur' }"
|
||||
>
|
||||
<div class="file-item">
|
||||
<span class="file-name">{{ file.name }}</span>
|
||||
<el-input
|
||||
v-model="formData.files[index].fileName"
|
||||
placeholder="请输入文件名"
|
||||
style="width: 200px; margin-right: 10px;"
|
||||
/>
|
||||
<el-select
|
||||
v-model="formData.files[index].fileType"
|
||||
placeholder="选择类型"
|
||||
>
|
||||
<el-option
|
||||
v-for="type in fileTypes"
|
||||
:key="type"
|
||||
:label="type.dictLabel"
|
||||
:value="type.dictValue"
|
||||
/>
|
||||
</el-select>
|
||||
</div>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
|
||||
<div slot="footer" class="dialog-footer">
|
||||
<el-button @click="showAddDialog = false">取 消</el-button>
|
||||
<el-button type="primary" @click="handleConfirmUpload">确认上传</el-button>
|
||||
</div>
|
||||
</el-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { listDocument, addFile, uploadFile, getFileTypeList } from '@/api/oa/document'
|
||||
|
||||
export default {
|
||||
name: 'UserFileManager',
|
||||
props: {
|
||||
userId: [Number, String],
|
||||
readOnly: Boolean
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
loading: false,
|
||||
fileList: [],
|
||||
selectedFiles: [],
|
||||
showAddDialog: false,
|
||||
tempFiles: [], // 临时存储选择的文件
|
||||
formData: {
|
||||
files: [] // 存储文件名和类型
|
||||
},
|
||||
fileTypes: []
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
userId: {
|
||||
immediate: true,
|
||||
handler(newVal) {
|
||||
newVal && this.loadFiles()
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
async loadFiles() {
|
||||
try {
|
||||
this.loading = true
|
||||
const res = await listDocument(this.userId)
|
||||
this.fileList = res.data.fileList;
|
||||
console.log(this.fileList)
|
||||
const fileTypes = await getFileTypeList();
|
||||
this.fileTypes = fileTypes.rows;
|
||||
} catch (error) {
|
||||
this.$message.error('文件加载失败')
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
|
||||
handleFileChange(file, fileList) {
|
||||
this.tempFiles = fileList
|
||||
// 初始化表单数据
|
||||
this.formData.files = fileList.map(f => ({
|
||||
fileName: f.name.replace(/\.[^/.]+$/, ""), // 去除扩展名作为默认文件名
|
||||
fileType: '',
|
||||
rawFile: f.raw
|
||||
}))
|
||||
},
|
||||
|
||||
beforeUpload(file) {
|
||||
const isValidType = ['image/jpeg', 'image/png', 'application/pdf'].includes(file.type)
|
||||
const isLt50M = file.size / 1024 / 1024 < 50
|
||||
|
||||
if (!isValidType) {
|
||||
this.$message.error('不支持的文件格式!')
|
||||
return false
|
||||
}
|
||||
if (!isLt50M) {
|
||||
this.$message.error('文件大小不能超过50MB!')
|
||||
return false
|
||||
}
|
||||
return false // 阻止自动上传
|
||||
},
|
||||
|
||||
async handleConfirmUpload() {
|
||||
try {
|
||||
// 验证表单
|
||||
console.log(this.formData.files)
|
||||
|
||||
// 上传所有文件
|
||||
const uploadResults = await Promise.all(
|
||||
this.formData.files.map(async file => {
|
||||
const formData = new FormData()
|
||||
formData.append('file', file.rawFile)
|
||||
|
||||
const res = await uploadFile(formData)
|
||||
|
||||
return {
|
||||
fileName: file.fileName,
|
||||
fileType: parseInt(file.fileType),
|
||||
filepath: res.url // 假设返回结构包含url
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
// 调用添加文件接口
|
||||
await addFile(this.userId, uploadResults)
|
||||
|
||||
this.$message.success('文件上传成功')
|
||||
this.showAddDialog = false
|
||||
this.loadFiles()
|
||||
} catch (error) {
|
||||
this.$message.error('文件上传失败: ' + (error.message || error))
|
||||
}
|
||||
},
|
||||
|
||||
async handleDelete() {
|
||||
try {
|
||||
const ids = this.selectedFiles.map(f => f.id)
|
||||
await this.$confirm(`确定删除选中的 ${ids.length} 个文件吗?`, '提示', {
|
||||
type: 'warning'
|
||||
})
|
||||
|
||||
await this.$axios.delete('/api/files', { data: { ids } })
|
||||
this.$message.success('删除成功')
|
||||
this.loadFiles()
|
||||
} catch (error) {
|
||||
if (error !== 'cancel') {
|
||||
this.$message.error('删除失败')
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
handleSelectionChange(selection) {
|
||||
this.selectedFiles = selection
|
||||
},
|
||||
|
||||
handleAdd() {
|
||||
this.showAddDialog = true
|
||||
},
|
||||
|
||||
handleUploadSuccess(res) {
|
||||
if (res.code === 200) {
|
||||
this.fileList.unshift(res.data)
|
||||
this.$message.success('上传成功')
|
||||
this.showAddDialog = false
|
||||
}
|
||||
},
|
||||
|
||||
formatSize(size) {
|
||||
if (size > 1024 * 1024) {
|
||||
return (size / 1024 / 1024).toFixed(1) + 'MB'
|
||||
} else if (size > 1024) {
|
||||
return (size / 1024).toFixed(1) + 'KB'
|
||||
}
|
||||
return size + 'B'
|
||||
},
|
||||
|
||||
formatDate(timestamp) {
|
||||
return new Date(timestamp).toLocaleString()
|
||||
},
|
||||
|
||||
checkSelectable(row) {
|
||||
return !this.readOnly
|
||||
},
|
||||
|
||||
resetUpload() {
|
||||
this.tempFiles = []
|
||||
this.formData.files = []
|
||||
this.$refs.fileForm?.resetFields()
|
||||
},
|
||||
|
||||
handleSelectionChange(selection) {
|
||||
this.selectedFiles = selection
|
||||
},
|
||||
|
||||
handleDownload(row) {
|
||||
// const a = document.createElement('a')
|
||||
// a.href = row.fileUrl
|
||||
// a.download = row.fileName
|
||||
// a.click()
|
||||
window.open(row.filePath)
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.file-manager {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.file-form {
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.file-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.file-name {
|
||||
flex: none;
|
||||
width: 150px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user