|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { FileCollection } from '@prisma/client' |
| 3 | +import type { AllClubs } from '~~/types/api/user/all_clubs' |
| 4 | +import Toaster from '@/components/ui/toast/Toaster.vue' |
| 5 | +import { useToast } from '@/components/ui/toast/use-toast' |
| 6 | +import { toTypedSchema } from '@vee-validate/zod' |
| 7 | +import dayjs from 'dayjs' |
| 8 | +import { v4 as uuidv4 } from 'uuid' |
| 9 | +import { useForm } from 'vee-validate' |
| 10 | +import * as z from 'zod' |
| 11 | +
|
| 12 | +const props = defineProps({ |
| 13 | + club: { |
| 14 | + type: String, |
| 15 | + required: true, |
| 16 | + }, |
| 17 | + collection: { |
| 18 | + type: String, |
| 19 | + required: true, |
| 20 | + }, |
| 21 | + filetypes: { |
| 22 | + type: Array<string>, |
| 23 | + required: true, |
| 24 | + }, |
| 25 | + title: { |
| 26 | + type: String, |
| 27 | + required: true, |
| 28 | + }, |
| 29 | +}) |
| 30 | +
|
| 31 | +definePageMeta({ |
| 32 | + middleware: ['auth'], |
| 33 | +}) |
| 34 | +
|
| 35 | +function fileTypesPrompt(fileTypes: string[]) { |
| 36 | + if (fileTypes.length === 0 || fileTypes.includes('*')) { |
| 37 | + return '无文件类型限制' |
| 38 | + } |
| 39 | + else { |
| 40 | + return `上传类型为 ${fileTypes.join(', ').toUpperCase()} 的文件` |
| 41 | + } |
| 42 | +} |
| 43 | +function fileTypesAcceptAttr(fileTypes: string[]) { |
| 44 | + if (fileTypes.length === 0 || fileTypes.includes('*')) { |
| 45 | + return '*' |
| 46 | + } |
| 47 | + else { |
| 48 | + return fileTypes.map(type => `.${type}`).join(',') |
| 49 | + } |
| 50 | +} |
| 51 | +
|
| 52 | +// Still seems to be buggy |
| 53 | +// const formSchema = toTypedSchema(z.object({ |
| 54 | +// file: z.custom(v => v, 'File missing'), |
| 55 | +// })) |
| 56 | +
|
| 57 | +// 滚一边去 |
| 58 | +function readFileAsDataURL(file: File) { |
| 59 | + return new Promise((resolve, reject) => { |
| 60 | + const reader = new FileReader() |
| 61 | + reader.onload = () => resolve(reader.result) |
| 62 | + reader.onerror = reject |
| 63 | + reader.readAsDataURL(file) |
| 64 | + }) |
| 65 | +} |
| 66 | +
|
| 67 | +const form = useForm({}) |
| 68 | +const inputKey = ref(uuidv4()) |
| 69 | +const submitting = ref(false) |
| 70 | +const onSubmit = form.handleSubmit(async (values) => { |
| 71 | + submitting.value = true |
| 72 | + await $fetch('/api/files/newRecord', { |
| 73 | + method: 'POST', |
| 74 | + body: { |
| 75 | + clubId: Number.parseInt(props.club), |
| 76 | + collectionId: props.collection, |
| 77 | + fileContent: await readFileAsDataURL(values.file), |
| 78 | + rawName: values.file.name, |
| 79 | + }, |
| 80 | + }) |
| 81 | + form.resetForm() |
| 82 | + inputKey.value = uuidv4() |
| 83 | + await updateClub() |
| 84 | + submitting.value = false |
| 85 | +}) |
| 86 | +
|
| 87 | +const msg = ref('') |
| 88 | +const currentClubData = ref(null) |
| 89 | +const clubUpdating = ref(false) |
| 90 | +async function updateClub() { |
| 91 | + if (!props.club) { |
| 92 | + msg.value = '请先选择一个社团' |
| 93 | + currentClubData.value = undefined |
| 94 | + return |
| 95 | + } |
| 96 | + clubUpdating.value = true |
| 97 | + const data = await $fetch('/api/files/clubRecords', { |
| 98 | + method: 'POST', |
| 99 | + body: { |
| 100 | + cludId: Number.parseInt(props.club), |
| 101 | + collection: props.collection, |
| 102 | + }, |
| 103 | + }) |
| 104 | + if (data && data.length !== 0) { |
| 105 | + msg.value = `最后提交于 ${dayjs(data[0].createdAt).fromNow()}` |
| 106 | + currentClubData.value = data[0] |
| 107 | + } |
| 108 | + else { |
| 109 | + msg.value = '尚未提交' |
| 110 | + currentClubData.value = undefined |
| 111 | + } |
| 112 | + clubUpdating.value = false |
| 113 | +} |
| 114 | +
|
| 115 | +const downloadLink = ref('') |
| 116 | +const downloadFilename = ref('') |
| 117 | +const dlink: Ref<HTMLElement | null> = ref(null) |
| 118 | +const downloading = ref(false) |
| 119 | +async function download() { |
| 120 | + if (currentClubData.value) { |
| 121 | + downloading.value = true |
| 122 | + const data = await $fetch('/api/files/download', { |
| 123 | + method: 'POST', |
| 124 | + body: { |
| 125 | + fileId: currentClubData.value.fileId, |
| 126 | + }, |
| 127 | + }) |
| 128 | + // const blob = new Blob([new Uint8Array(Array.from(atob(data), c => c.charCodeAt(0)))]) |
| 129 | + // window.open(URL.createObjectURL(blob)) |
| 130 | + // window.open(data) |
| 131 | + downloadLink.value = data.url |
| 132 | + downloadFilename.value = data.name |
| 133 | + dlink.value.click() |
| 134 | + downloading.value = false |
| 135 | + } |
| 136 | + downloadLink.value = '' |
| 137 | + downloadFilename.value = '' |
| 138 | +} |
| 139 | +
|
| 140 | +watch( |
| 141 | + () => props.club, |
| 142 | + async () => { |
| 143 | + await updateClub() |
| 144 | + }, |
| 145 | +) |
| 146 | +
|
| 147 | +await updateClub() |
| 148 | +</script> |
| 149 | + |
| 150 | +<template> |
| 151 | + <Card class="px-4 py-4"> |
| 152 | + <div class="mb-5 text-xl font-bold"> |
| 153 | + {{ title }} |
| 154 | + </div> |
| 155 | + <form class="inline-block" @submit="onSubmit"> |
| 156 | + <FormField v-slot="{ componentField }" name="file"> |
| 157 | + <FormItem> |
| 158 | + <FormControl> |
| 159 | + <Input |
| 160 | + v-bind="componentField" |
| 161 | + :key="inputKey" class="text-foreground" |
| 162 | + type="file" |
| 163 | + :accept="fileTypesAcceptAttr(filetypes)" |
| 164 | + /> |
| 165 | + </FormControl> |
| 166 | + <FormDescription> |
| 167 | + {{ fileTypesPrompt(filetypes) }} |
| 168 | + </FormDescription> |
| 169 | + <!-- <FormMessage /> --> |
| 170 | + </FormItem> |
| 171 | + </FormField> |
| 172 | + <div class="mt-2"> |
| 173 | + <Button type="submit" variant="secondary" :disabled="!form.values.file || submitting || clubUpdating"> |
| 174 | + 上传 |
| 175 | + </Button> |
| 176 | + <Button v-if="currentClubData" :disabled="downloading" variant="outline" class="ml-2" type="button" @click="download"> |
| 177 | + 下载 |
| 178 | + </Button> |
| 179 | + </div> |
| 180 | + </form> |
| 181 | + <div v-if="submitting || clubUpdating" class="mt-2"> |
| 182 | + <Skeleton class="h-5 w-full" /> |
| 183 | + </div> |
| 184 | + <div v-else class="mt-2"> |
| 185 | + {{ msg }} |
| 186 | + </div> |
| 187 | + <a ref="dlink" :href="downloadLink" :download="downloadFilename" class="hidden">Download</a> |
| 188 | + </Card> |
| 189 | +</template> |
0 commit comments