11<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'
62import { toTypedSchema } from ' @vee-validate/zod'
73import dayjs from ' dayjs'
84import { v4 as uuidv4 } from ' uuid'
@@ -32,6 +28,19 @@ definePageMeta({
3228 middleware: [' auth' ],
3329})
3430
31+ const form = useForm ({}) // The form
32+ const inputKey = ref (uuidv4 ()) // To force update the <Input file>
33+ const currentClubData: Ref <Record <string , any > | null > = ref (null ) // Data for the current club's record
34+ const msg = ref (' ' ) // Message (time / status) shown at the bottom of the card
35+
36+ const clubUpdating = ref (false ) // Flag for club data updating
37+ const submitting = ref (false ) // Flag for file submission
38+ const downloading = ref (false ) // Flag for downloading file
39+
40+ const dlink: Ref <HTMLElement | null > = ref (null ) // The <a> element
41+ const downloadLink = ref (' ' ) // The data url to be filled in <a>
42+ const downloadFilename = ref (' ' ) // The filename to be filled in <a>
43+
3544function fileTypesPrompt(fileTypes : string []) {
3645 if (fileTypes .length === 0 || fileTypes .includes (' *' )) {
3746 return ' 无文件类型限制'
@@ -54,7 +63,6 @@ function fileTypesAcceptAttr(fileTypes: string[]) {
5463// file: z.custom(v => v, 'File missing'),
5564// }))
5665
57- // 滚一边去
5866function readFileAsDataURL(file : File ) {
5967 return new Promise ((resolve , reject ) => {
6068 const reader = new FileReader ()
@@ -64,60 +72,51 @@ function readFileAsDataURL(file: File) {
6472 })
6573}
6674
67- const form = useForm ({})
68- const inputKey = ref (uuidv4 ())
69- const submitting = ref (false )
7075const onSubmit = form .handleSubmit (async (values ) => {
7176 submitting .value = true
72- await $fetch (' /api/files/newRecord' , {
77+ const fileName = values .file .name
78+ const status = await $fetch (' /api/files/new-record' , {
7379 method: ' POST' ,
7480 body: {
7581 clubId: Number .parseInt (props .club ),
7682 collectionId: props .collection ,
7783 fileContent: await readFileAsDataURL (values .file ),
78- rawName: values . file . name ,
84+ rawName: fileName ,
7985 },
8086 })
8187 form .resetForm ()
8288 inputKey .value = uuidv4 ()
83- await updateClub ()
89+ msg . value = ( status && status . success ) ? ` ${ fileName } (提交成功) ` : ' 提交失败 '
8490 submitting .value = false
8591})
8692
87- const downloadLink = ref (' ' )
88- const downloadFilename = ref (' ' )
89- const msg = ref (' ' )
90- const currentClubData = ref (null )
91- const clubUpdating = ref (false )
9293async function updateClub() {
9394 downloadLink .value = ' '
9495 downloadFilename .value = ' '
9596 if (! props .club ) {
9697 msg .value = ' 请先选择一个社团'
97- currentClubData .value = undefined
98+ currentClubData .value = null
9899 return
99100 }
100101 clubUpdating .value = true
101- const data = await $fetch (' /api/files/clubRecords ' , {
102+ const data = await $fetch (' /api/files/club-records ' , {
102103 method: ' POST' ,
103104 body: {
104- cludId : Number .parseInt (props .club ),
105+ clubId : Number .parseInt (props .club ),
105106 collection: props .collection ,
106107 },
107108 })
108- if (data && data .length !== 0 ) {
109- msg .value = ` 最后提交于 ${dayjs (data [0 ].createdAt ).fromNow ()}`
109+ if (data [ 0 ] && data .length !== 0 ) {
110+ msg .value = ` ${ data [ 0 ]. file . name } ( ${dayjs (data [0 ].createdAt ).fromNow ()}) `
110111 currentClubData .value = data [0 ]
111112 }
112113 else {
113114 msg .value = ' 尚未提交'
114- currentClubData .value = undefined
115+ currentClubData .value = null
115116 }
116117 clubUpdating .value = false
117118}
118119
119- const dlink: Ref <HTMLElement | null > = ref (null )
120- const downloading = ref (false )
121120async function download() {
122121 if (currentClubData .value ) {
123122 downloading .value = true
@@ -130,12 +129,14 @@ async function download() {
130129 // const blob = new Blob([new Uint8Array(Array.from(atob(data), c => c.charCodeAt(0)))])
131130 // window.open(URL.createObjectURL(blob))
132131 // window.open(data)
133- downloadLink .value = data .url
134- downloadFilename .value = data .name
135- nextTick (() => {
136- dlink .value .click ()
137- })
138- downloading .value = false
132+ if (typeof data .url === ' string' && typeof data .name === ' string' ) {
133+ downloadLink .value = data .url
134+ downloadFilename .value = data .name
135+ nextTick (() => {
136+ dlink .value ! .click ()
137+ })
138+ downloading .value = false
139+ }
139140 }
140141}
141142
@@ -189,7 +190,5 @@ await updateClub()
189190 <a
190191 ref =" dlink" :href =" downloadLink" :download =" downloadFilename" class =" hidden"
191192 >Download</a >
192- <!-- @click="downloadLink = '';
193- downloadFilename = ''" -->
194193 </Card >
195194</template >
0 commit comments