mirror of
				https://github.com/continew-org/continew-admin-ui.git
				synced 2025-10-31 22:57:15 +08:00 
			
		
		
		
	refactor: 文件管理,重命名,下载,滚动查询
This commit is contained in:
		| @@ -5,7 +5,7 @@ const BASE_URL = '/system/file' | |||||||
|  |  | ||||||
| /** @desc 查询文件列表 */ | /** @desc 查询文件列表 */ | ||||||
| export function listFile(query: System.FileQuery) { | export function listFile(query: System.FileQuery) { | ||||||
|   return http.get<System.FileItem[]>(`${BASE_URL}/list`, query) |   return http.get<PageRes<System.FileItem[]>>(`${BASE_URL}`, query) | ||||||
| } | } | ||||||
|  |  | ||||||
| /** @desc 修改文件 */ | /** @desc 修改文件 */ | ||||||
|   | |||||||
| @@ -22,6 +22,7 @@ export function downloadByUrl({ | |||||||
|   url: string |   url: string | ||||||
|   target?: '_self' | '_blank' |   target?: '_self' | '_blank' | ||||||
|   fileName?: string |   fileName?: string | ||||||
|  |   isSameHost: boolean | ||||||
| }): Promise<boolean> { | }): Promise<boolean> { | ||||||
|   // 是否同源 |   // 是否同源 | ||||||
|   const isSameHost = new URL(url).host == location.host |   const isSameHost = new URL(url).host == location.host | ||||||
| @@ -49,28 +50,36 @@ export function downloadByUrl({ | |||||||
|       window.open(url, target) |       window.open(url, target) | ||||||
|       return resolve(true) |       return resolve(true) | ||||||
|     } else { |     } else { | ||||||
|       const canvas = document.createElement('canvas') |       const elink = document.createElement('a') | ||||||
|       const img = document.createElement('img') |       elink.href = url | ||||||
|       img.setAttribute('crossOrigin', 'Anonymous') |       elink.target = '_self' | ||||||
|       img.src = url |       elink.download = fileName as string | ||||||
|       img.onload = () => { |       elink.style.display = 'none' | ||||||
|         canvas.width = img.width |       document.body.appendChild(elink) | ||||||
|         canvas.height = img.height |       elink.click() | ||||||
|         const context = canvas.getContext('2d')! |       document.body.removeChild(elink) | ||||||
|         context.drawImage(img, 0, 0, img.width, img.height) |       // const canvas = document.createElement('canvas') | ||||||
|         // window.navigator.msSaveBlob(canvas.msToBlob(),'image.jpg'); |       // const img = document.createElement('img') | ||||||
|         // saveAs(imageDataUrl, '附件'); |       // img.setAttribute('crossOrigin', 'Anonymous') | ||||||
|         canvas.toBlob((blob) => { |       // img.src = url | ||||||
|           const link = document.createElement('a') |       // img.onload = () => { | ||||||
|           if (!blob) return |       //   canvas.width = img.width | ||||||
|           link.href = window.URL.createObjectURL(blob) |       //   canvas.height = img.height | ||||||
|           link.download = getFileName(url) |       //   const context = canvas.getContext('2d')! | ||||||
|           link.click() |       //   context.drawImage(img, 0, 0, img.width, img.height) | ||||||
|           URL.revokeObjectURL(link.href) |       //   // window.navigator.msSaveBlob(canvas.msToBlob(),'image.jpg'); | ||||||
|           resolve(true) |       //   // saveAs(imageDataUrl, '附件'); | ||||||
|         }, 'image/jpeg') |       //   canvas.toBlob((blob) => { | ||||||
|       } |       //     const link = document.createElement('a') | ||||||
|       img.onerror = (e) => reject(e) |       //     if (!blob) return | ||||||
|  |       //     link.href = window.URL.createObjectURL(blob) | ||||||
|  |       //     link.download =  fileName || getFileName(url) | ||||||
|  |       //     link.click() | ||||||
|  |       //     URL.revokeObjectURL(link.href) | ||||||
|  |       //     resolve(true) | ||||||
|  |       //   }, 'image/jpeg') | ||||||
|  |       // } | ||||||
|  |       // img.onerror = (e) => reject(e) | ||||||
|     } |     } | ||||||
|   }) |   }) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -18,8 +18,9 @@ export function openFileRenameModal(data: FileItem) { | |||||||
|       }), |       }), | ||||||
|     onBeforeOk: async () => { |     onBeforeOk: async () => { | ||||||
|       const isInvalid = await ModalContentRef.value?.formRef?.validate() |       const isInvalid = await ModalContentRef.value?.formRef?.validate() | ||||||
|  |       const modelParams = ModalContentRef.value?.formRef?.model | ||||||
|       if (isInvalid) return false |       if (isInvalid) return false | ||||||
|       await updateFile({ name: data.name }, data.id) |       await updateFile({ name: modelParams?.name }, data.id) | ||||||
|       Message.success('重命名成功') |       Message.success('重命名成功') | ||||||
|       return true |       return true | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -91,7 +91,7 @@ const handleRightMenuClick = (mode: string, item: FileItem) => { | |||||||
| .file-grid { | .file-grid { | ||||||
|   flex: 1; |   flex: 1; | ||||||
|   margin-top: 12px; |   margin-top: 12px; | ||||||
|   overflow: scroll; |   // overflow: scroll; | ||||||
|   background: var(--color-bg-2); |   background: var(--color-bg-2); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -60,7 +60,7 @@ | |||||||
|     </a-row> |     </a-row> | ||||||
|  |  | ||||||
|     <!-- 文件列表-宫格模式 --> |     <!-- 文件列表-宫格模式 --> | ||||||
|     <a-spin class="file-main__list" :loading="loading"> |     <a-spin class="file-main__list" id="fileMain" @scroll="handleScroll" :loading="loading"> | ||||||
|       <FileGrid |       <FileGrid | ||||||
|         v-show="fileList.length && mode == 'grid'" |         v-show="fileList.length && mode == 'grid'" | ||||||
|         :data="fileList" |         :data="fileList" | ||||||
| @@ -101,9 +101,34 @@ import useFileManage from './useFileManage' | |||||||
| import { ImageTypes } from '@/constant/file' | import { ImageTypes } from '@/constant/file' | ||||||
| import { api as viewerApi } from 'v-viewer' | import { api as viewerApi } from 'v-viewer' | ||||||
| import 'viewerjs/dist/viewer.css' | import 'viewerjs/dist/viewer.css' | ||||||
|  | import { downloadByUrl } from '@/utils/downloadFile' | ||||||
| const FileList = defineAsyncComponent(() => import('./FileList.vue')) | const FileList = defineAsyncComponent(() => import('./FileList.vue')) | ||||||
|  | onMounted(() => { | ||||||
|  |   const fileMainDom = document.getElementById('fileMain') | ||||||
|  |   fileMainDom.addEventListener('scrool', handleScroll) | ||||||
|  |   console.table('fileMainDom', fileMainDom) | ||||||
|  | }) | ||||||
|  | onUnmounted(() => { | ||||||
|  |   // 移除事件监听 | ||||||
|  |   const fileMainDom = document.getElementById('fileMain') | ||||||
|  |   fileMainDom.removeEventListener('scroll', handleScroll) | ||||||
|  | }) | ||||||
|  | const timer = ref<any>() | ||||||
|  | const handleScroll = (event) => { | ||||||
|  |   const { clientHeight, scrollHeight, scrollTop } = event.target | ||||||
|  |   const scrollLinerHeight = (clientHeight / scrollHeight) * scrollHeight | ||||||
|  |   if (!timer.value) { | ||||||
|  |     timer.value = setTimeout(() => { | ||||||
|  |       if ((scrollTop + scrollLinerHeight) / scrollHeight >= 0.9 && !loading.value) { | ||||||
|  |         ++pagination.page | ||||||
|  |         getFileList() | ||||||
|  |       } | ||||||
|  |       timer.value = null | ||||||
|  |     }, 1000) | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   console.log('event', event) | ||||||
|  | } | ||||||
| const route = useRoute() | const route = useRoute() | ||||||
| const { mode, selectedFileIds, toggleMode, addSelectedFileItem } = useFileManage() | const { mode, selectedFileIds, toggleMode, addSelectedFileItem } = useFileManage() | ||||||
|  |  | ||||||
| @@ -112,19 +137,35 @@ const queryForm = reactive({ | |||||||
|   type: route.query.type?.toString() || undefined, |   type: route.query.type?.toString() || undefined, | ||||||
|   sort: ['updateTime,desc'] |   sort: ['updateTime,desc'] | ||||||
| }) | }) | ||||||
|  | const pagination = reactive({ | ||||||
|  |   page: 1, | ||||||
|  |   size: 30 | ||||||
|  | }) | ||||||
| const fileList = ref<FileItem[]>([]) | const fileList = ref<FileItem[]>([]) | ||||||
| const isBatchMode = ref(false) | const isBatchMode = ref(false) | ||||||
| const loading = ref(false) | const loading = ref(false) | ||||||
| // 查询文件列表 | // 查询文件列表 | ||||||
| const getFileList = async (query: FileQuery = { ...queryForm, page: 1, size: 50 }) => { | const getFileList = async (query: FileQuery = { ...queryForm, page: pagination.page, size: pagination.size }) => { | ||||||
|   try { |   try { | ||||||
|     loading.value = true |     loading.value = true | ||||||
|     isBatchMode.value = false |     isBatchMode.value = false | ||||||
|     query.type = query.type === '0' ? undefined : query.type |     query.type = query.type === '0' ? undefined : query.type | ||||||
|     const res = await listFile(query) |     const res = await listFile(query) | ||||||
|     fileList.value = res.data |     if (res.data.list) { | ||||||
|  |       if (query.page === 1) { | ||||||
|  |         fileList.value = res.data.list | ||||||
|  |         timer.value = setTimeout(() => { | ||||||
|  |           clearTimeout(timer.value) | ||||||
|  |           timer.value = null | ||||||
|  |         }, 1000) | ||||||
|  |       } else { | ||||||
|  |         fileList.value = [...fileList.value, ...res.data.list] | ||||||
|  |       } | ||||||
|  |     } else { | ||||||
|  |       --pagination.page | ||||||
|  |     } | ||||||
|   } catch (error) { |   } catch (error) { | ||||||
|  |     --pagination.page | ||||||
|     return error |     return error | ||||||
|   } finally { |   } finally { | ||||||
|     loading.value = false |     loading.value = false | ||||||
| @@ -156,7 +197,7 @@ const handleClickFile = (item: FileItem) => { | |||||||
| } | } | ||||||
|  |  | ||||||
| // 右键菜单 | // 右键菜单 | ||||||
| const handleRightMenuClick = (mode: string, fileInfo: FileItem) => { | const handleRightMenuClick = async (mode: string, fileInfo: FileItem) => { | ||||||
|   if (mode === 'delete') { |   if (mode === 'delete') { | ||||||
|     Modal.warning({ |     Modal.warning({ | ||||||
|       title: '提示', |       title: '提示', | ||||||
| @@ -173,6 +214,15 @@ const handleRightMenuClick = (mode: string, fileInfo: FileItem) => { | |||||||
|     openFileRenameModal(fileInfo) |     openFileRenameModal(fileInfo) | ||||||
|   } else if (mode === 'detail') { |   } else if (mode === 'detail') { | ||||||
|     openFileDetailModal(fileInfo) |     openFileDetailModal(fileInfo) | ||||||
|  |   } else if (mode === 'download') { | ||||||
|  |     console.log('fileInfo', fileInfo) | ||||||
|  |     const res = await downloadByUrl({ | ||||||
|  |       url: fileInfo.url, | ||||||
|  |       target: '_self', | ||||||
|  |       fileName: `${fileInfo.name}.${fileInfo.extension}` | ||||||
|  |     }) | ||||||
|  |     res ? Message.success('下载成功') : Message.error('下载失败') | ||||||
|  |     search() | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -204,7 +254,7 @@ const handleUpload = (options: RequestOption) => { | |||||||
|     const formData = new FormData() |     const formData = new FormData() | ||||||
|     formData.append(name as string, fileItem.file as Blob) |     formData.append(name as string, fileItem.file as Blob) | ||||||
|     try { |     try { | ||||||
|       const res = uploadFile(formData) |       const res = await uploadFile(formData) | ||||||
|       Message.success('上传成功') |       Message.success('上传成功') | ||||||
|       onSuccess(res) |       onSuccess(res) | ||||||
|       search() |       search() | ||||||
| @@ -221,6 +271,7 @@ const handleUpload = (options: RequestOption) => { | |||||||
|  |  | ||||||
| // 查询 | // 查询 | ||||||
| const search = () => { | const search = () => { | ||||||
|  |   pagination.page = 1 | ||||||
|   getFileList() |   getFileList() | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -253,7 +304,8 @@ onMounted(() => { | |||||||
|     flex: 1; |     flex: 1; | ||||||
|     padding: 0 $padding $padding; |     padding: 0 $padding $padding; | ||||||
|     box-sizing: border-box; |     box-sizing: border-box; | ||||||
|     overflow: hidden; |     // overflow: hidden; | ||||||
|  |     overflow-y: auto; | ||||||
|     display: flex; |     display: flex; | ||||||
|     flex-direction: column; |     flex-direction: column; | ||||||
|   } |   } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 秋帆
					秋帆