Share attachment picker across approval and student forms

This commit is contained in:
biss committed 2026-12-03 16:01:52 +08:00
1 parent 52776feb2b
commit b6c01ce0c0
5 files changed
+37 -38

No files matched your search

+1 -1
View File
@@ -12,10 +12,10 @@ export {}
declare module 'vue' {
export interface GlobalComponents {
AcademicCalendarReminder: typeof import('./components/AcademicCalendarReminder.vue')['default']
ApprovalAttachmentPicker: typeof import('./components/ApprovalAttachmentPicker.vue')['default']
AppUpdateManagementPanel: typeof import('./components/AppUpdateManagementPanel.vue')['default']
ArchiveDialog: typeof import('./components/ArchiveDialog.vue')['default']
AttachmentFileList: typeof import('./components/AttachmentFileList.vue')['default']
AttachmentPicker: typeof import('./components/AttachmentPicker.vue')['default']
ChinesePostalCodeInput: typeof import('./components/ChinesePostalCodeInput.vue')['default']
CourseDiscussionPanel: typeof import('./components/CourseDiscussionPanel.vue')['default']
CustomExportDialog: typeof import('./components/CustomExportDialog.vue')['default']
@@ -1,19 +1,26 @@
<script setup lang="ts">
import { onMounted, ref, useId } from 'vue'
import { useI18n } from 'vue-i18n'
import { ElMessage } from 'element-plus'
import http, { apiErrorMessage } from '../api/http'
const policyHintId = useId()
const props = defineProps<{ type: string }>()
const props = defineProps<{ policyEndpoint: string }>()
const emit = defineEmits<{ change: [files: File[]] }>()
const { t } = useI18n()
const policy = ref<{ allowedExtensions: string; maxFileBytes: number; maxFileCount: number }>()
const loadError = ref('')
const invalidSelection = ref(false)
const fileInput = ref<HTMLInputElement | null>(null)
function reset() {
if (fileInput.value) fileInput.value.value = ''
invalidSelection.value = false
}
defineExpose({ reset })
async function loadPolicy() {
loadError.value = ''
try {
policy.value = (await http.get(`/approval-attachments/policy/${props.type}`)).data
policy.value = (await http.get(props.policyEndpoint)).data
} catch (error) {
loadError.value = apiErrorMessage(error)
}
@@ -25,17 +32,18 @@ function change(event: Event) {
if (!limits) return
const input = event.target as HTMLInputElement
const files = Array.from(input.files ?? [])
const allowed = limits.allowedExtensions.split(',').map(x => x.trim().toLowerCase())
if (files.length > limits.maxFileCount || files.reduce((sum, x) => sum + x.size, 0) > 104857600 || files.some(x => !x.size || x.size > limits.maxFileBytes || !allowed.some(ext => x.name.toLowerCase().endsWith(ext)))) {
ElMessage.warning(t('approvalAttachments.invalid'))
input.value = ''; emit('change', []); return
}
emit('change', files)
const allowed = limits.allowedExtensions.split(',').map(ext => ext.trim().toLowerCase())
const totalBytes = files.reduce((total, file) => total + file.size, 0)
const invalidFile = files.some(file => !file.size || file.size > limits.maxFileBytes ||
!allowed.some(ext => file.name.toLowerCase().endsWith(ext)))
invalidSelection.value = files.length > limits.maxFileCount || totalBytes > 104857600 || invalidFile
if (invalidSelection.value) input.value = ''
emit('change', invalidSelection.value ? [] : files)
}
</script>
<template>
<div class="attachment-picker">
<input type="file" :aria-label="t('approvalAttachments.title')" :aria-describedby="policyHintId" multiple :disabled="!policy" :accept="policy?.allowedExtensions" @change="change" />
<input ref="fileInput" type="file" :aria-invalid="invalidSelection || undefined" :aria-label="t('approvalAttachments.title')" :aria-describedby="invalidSelection ? `${policyHintId} ${policyHintId}-error` : policyHintId" multiple :disabled="!policy" :accept="policy?.allowedExtensions" @change="change" />
<p v-if="policy" :id="policyHintId" class="attachment-hint">
{{ policy.allowedExtensions }} · {{ t('approvalAttachments.limits', { size: (policy.maxFileBytes / 1048576).toFixed(1), count: policy.maxFileCount }) }}
</p>
@@ -44,5 +52,8 @@ function change(event: Event) {
<el-button link type="primary" @click="loadPolicy">{{ t('common.reload') }}</el-button>
</div>
<p v-else :id="policyHintId" class="attachment-hint" role="status">{{ t('approvalAttachments.loadingPolicy') }}</p>
<p v-if="invalidSelection" :id="`${policyHintId}-error`" class="attachment-error" role="alert">
{{ t('approvalAttachments.invalid') }}
</p>
</div>
</template>
+11 -23
View File
@@ -1,12 +1,12 @@
<script setup lang="ts">
import { computed, onMounted, reactive, ref, useId } from 'vue'
import { computed, onMounted, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { ElMessage, ElMessageBox } from 'element-plus'
import http, { apiErrorMessage } from '../api/http'
import AttachmentPicker from './AttachmentPicker.vue'
import RecordAttachmentList from './RecordAttachmentList.vue'
import { downloadApiFile } from '../api/excel'
const policyHintId = useId()
const props = defineProps<{ student: boolean; manager: boolean }>()
const emit = defineEmits<{ reviewed: [] }>()
const { locale } = useI18n()
@@ -20,13 +20,9 @@ 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 attachmentPicker = ref<InstanceType<typeof AttachmentPicker> | null>(null)
const attachmentDialog = ref(false)
const attachmentLeaveId = ref('')
function selectFiles(event: Event) {
files.value = Array.from((event.target as HTMLInputElement).files ?? [])
}
function showAttachments(id: string) {
attachmentLeaveId.value = id
attachmentDialog.value = true
@@ -49,17 +45,14 @@ 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 {
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 = ''
files.value = []
attachmentPicker.value?.reset()
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 }
@@ -77,13 +70,7 @@ 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(async () => {
await load()
if (props.student) {
try { filePolicy.value = (await http.get('/student-leave-attachments/policy')).data }
catch (e) { ElMessage.error(apiErrorMessage(e)) }
}
})
onMounted(load)
</script>
<template>
@@ -114,10 +101,11 @@ onMounted(async () => {
<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')">
<div class="attachment-picker">
<input ref="fileInput" :aria-label="tr('附件', 'Attachments')" :aria-describedby="policyHintId" type="file" multiple :accept="filePolicy.allowedExtensions" @change="selectFiles" />
<p :id="policyHintId" class="attachment-hint">{{ 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>
</div>
<AttachmentPicker
ref="attachmentPicker"
policy-endpoint="/student-leave-attachments/policy"
@change="files = $event"
/>
</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>
Loaded 3 of 5 files, more files were not shown because too many files have changed in this diff. Show more