Support attachments in student leave requests
This commit is contained in:
1 parent
788436ac74
commit
bdfbf49f40
13 files changed
+357
-8
No files matched your search
@@ -15,6 +15,30 @@ const form = reactive({ type: 'Sick', startAt: '', endAt: '', reason: '' })
|
||||
const rows = ref<any[]>([])
|
||||
const dialog = ref(false)
|
||||
const busy = ref(false)
|
||||
const files = ref<File[]>([])
|
||||
const fileInput = ref<HTMLInputElement | null>(null)
|
||||
const filePolicy = ref({ allowedExtensions: '.pdf,.jpg,.jpeg,.png', maxFileBytes: 10485760, maxFileCount: 5 })
|
||||
const attachmentDialog = ref(false)
|
||||
const attachments = ref<{ id: string; fileName: string; length: number }[]>([])
|
||||
const attachmentLeaveId = ref('')
|
||||
function selectFiles(event: Event) {
|
||||
files.value = Array.from((event.target as HTMLInputElement).files ?? [])
|
||||
}
|
||||
async function showAttachments(row: any) {
|
||||
try {
|
||||
attachments.value = (await http.get(`/student-leave-attachments/${row.id}`)).data
|
||||
attachmentLeaveId.value = row.id
|
||||
attachmentDialog.value = true
|
||||
} catch (e) { ElMessage.error(apiErrorMessage(e)) }
|
||||
}
|
||||
async function downloadAttachment(file: { id: string; fileName: string }) {
|
||||
try {
|
||||
const response = await http.get(`/student-leave-attachments/${attachmentLeaveId.value}/${file.id}`, { responseType: 'blob' })
|
||||
const url = URL.createObjectURL(response.data)
|
||||
const link = document.createElement('a'); link.href = url; link.download = file.fileName; link.click()
|
||||
setTimeout(() => URL.revokeObjectURL(url), 1000)
|
||||
} catch (e) { ElMessage.error(apiErrorMessage(e)) }
|
||||
}
|
||||
const formatTime = (value: string) => new Date(value.endsWith('Z') ? value : value + 'Z').toLocaleString()
|
||||
async function load() {
|
||||
try { rows.value = (await http.get(props.student ? '/student-leaves/mine' : '/student-leaves/pending')).data }
|
||||
@@ -24,9 +48,17 @@ async function submit() {
|
||||
if (!form.startAt || !form.endAt || !form.reason.trim() || new Date(form.endAt) <= new Date(form.startAt)) {
|
||||
ElMessage.warning(tr('请填写有效的起止时间和事由', 'Enter a valid time range and reason')); return
|
||||
}
|
||||
const allowed = filePolicy.value.allowedExtensions.split(',').map(x => x.trim().toLowerCase())
|
||||
if (files.value.length > filePolicy.value.maxFileCount || files.value.reduce((total, file) => total + file.size, 0) > 104857600 || files.value.some(file => file.size === 0 || file.size > filePolicy.value.maxFileBytes || !allowed.some(ext => file.name.toLowerCase().endsWith(ext)))) {
|
||||
ElMessage.warning(tr('附件格式、大小或数量不符合要求', 'Attachment type, size or count does not meet the requirements')); return
|
||||
}
|
||||
busy.value = true
|
||||
try {
|
||||
await http.post('/student-leaves', { ...form, startAt: new Date(form.startAt).toISOString(), endAt: new Date(form.endAt).toISOString() })
|
||||
const data = new FormData()
|
||||
data.append('type', form.type); data.append('startAt', new Date(form.startAt).toISOString()); data.append('endAt', new Date(form.endAt).toISOString()); data.append('reason', form.reason)
|
||||
files.value.forEach(file => data.append('files', file))
|
||||
await http.post('/student-leave-attachments', data, { timeout: 120000 })
|
||||
files.value = []; if (fileInput.value) fileInput.value.value = ''
|
||||
dialog.value = false; form.reason = ''; form.startAt = ''; form.endAt = ''; await load()
|
||||
ElMessage.success(tr('申请已提交', 'Request submitted'))
|
||||
} catch (e) { ElMessage.error(apiErrorMessage(e) || tr('提交失败', 'Submission failed')) } finally { busy.value = false }
|
||||
@@ -44,7 +76,13 @@ async function review(row: any, approved: boolean) {
|
||||
ElMessage.success(tr('审核完成', 'Review saved'))
|
||||
} catch (e) { if (e !== 'cancel' && e !== 'close') ElMessage.error(apiErrorMessage(e) || tr('审核失败', 'Review failed')) }
|
||||
}
|
||||
onMounted(load)
|
||||
onMounted(async () => {
|
||||
await load()
|
||||
if (props.student) {
|
||||
try { filePolicy.value = (await http.get('/student-leave-attachments/policy')).data }
|
||||
catch (e) { ElMessage.error(apiErrorMessage(e)) }
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -62,6 +100,7 @@ onMounted(load)
|
||||
<el-table-column v-if="student" :label="tr('状态', 'Status')" min-width="100"><template #default="{ row }">{{ label(statuses, row.status) }}</template></el-table-column>
|
||||
<el-table-column v-if="student" prop="reviewComment" :label="tr('审核意见', 'Review comment')" min-width="120" show-overflow-tooltip />
|
||||
<el-table-column :label="tr('操作', 'Actions')" min-width="150"><template #default="{ row }">
|
||||
<el-button link @click="showAttachments(row)">{{ tr('附件', 'Attachments') }}</el-button>
|
||||
<el-button v-if="student && row.status === 'Submitted'" link @click="withdraw(row)">{{ tr('撤回', 'Withdraw') }}</el-button>
|
||||
<template v-if="manager && !student"><el-button link type="primary" @click="review(row, true)">{{ tr('通过', 'Approve') }}</el-button><el-button link type="danger" @click="review(row, false)">{{ tr('驳回', 'Reject') }}</el-button></template>
|
||||
</template></el-table-column>
|
||||
@@ -72,9 +111,17 @@ onMounted(load)
|
||||
<el-form-item :label="tr('开始时间', 'Start')"><el-date-picker v-model="form.startAt" type="datetime" value-format="YYYY-MM-DDTHH:mm:ss" /></el-form-item>
|
||||
<el-form-item :label="tr('结束时间', 'End')"><el-date-picker v-model="form.endAt" type="datetime" value-format="YYYY-MM-DDTHH:mm:ss" /></el-form-item>
|
||||
<el-form-item :label="tr('请假事由', 'Reason')"><el-input v-model="form.reason" type="textarea" maxlength="500" show-word-limit /></el-form-item>
|
||||
<el-form-item :label="tr('附件', 'Attachments')">
|
||||
<input ref="fileInput" type="file" multiple :accept="filePolicy.allowedExtensions" @change="selectFiles" />
|
||||
<p>{{ tr('允许格式', 'Allowed') }}: {{ filePolicy.allowedExtensions }};{{ tr('单文件最大', 'Maximum per file') }}: {{ (filePolicy.maxFileBytes / 1048576).toFixed(1) }} MiB;{{ tr('最多', 'Maximum') }} {{ filePolicy.maxFileCount }};{{ tr('单次请求总大小不超过约 100 MiB', 'Total request size is limited to about 100 MiB') }}</p>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<template #footer><el-button @click="dialog = false">{{ tr('取消', 'Cancel') }}</el-button><el-button type="primary" :loading="busy" @click="submit">{{ tr('提交', 'Submit') }}</el-button></template>
|
||||
</el-dialog>
|
||||
<el-dialog v-model="attachmentDialog" :title="tr('附件', 'Attachments')" width="min(560px, 95vw)">
|
||||
<el-empty v-if="!attachments.length" :description="tr('暂无附件', 'No attachments')" />
|
||||
<div v-for="file in attachments" :key="file.id"><el-button link type="primary" @click="downloadAttachment(file)">{{ file.fileName }}</el-button> ({{ (file.length / 1024).toFixed(1) }} KiB)</div>
|
||||
</el-dialog>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
|
||||
Reference in new issue
Block a user