386 lines
9.9 KiB
TypeScript
386 lines
9.9 KiB
TypeScript
import { ref } from 'vue'
|
|
import { defineStore } from 'pinia'
|
|
import { useLoader } from '@/stores/loader'
|
|
import axiosClient from '@/services/HttpService'
|
|
|
|
const apiEndpoint: string = import.meta.env.VITE_API_ENDPOINT
|
|
|
|
export interface EhrFolder {
|
|
pathname: string
|
|
name: string
|
|
createdAt: string
|
|
createdBy: string
|
|
}
|
|
|
|
export interface EhrFile {
|
|
pathname: string
|
|
|
|
fileName: string
|
|
fileSize: string
|
|
fileType: string
|
|
|
|
title: string
|
|
description: string
|
|
category: string[]
|
|
keyword: string[]
|
|
|
|
updatedAt: string
|
|
updatedBy: string
|
|
createdAt: string
|
|
createdBy: string
|
|
}
|
|
|
|
export interface TreeDataFolder {
|
|
pathname: string
|
|
name: string
|
|
status: boolean
|
|
folder: TreeDataFolder[]
|
|
file?: EhrFile[]
|
|
}
|
|
|
|
export const useTreeDataStore = defineStore('changeCabinet', () => {
|
|
const loader = useLoader()
|
|
const data = ref<TreeDataFolder[]>([])
|
|
const currentFolder = ref<TreeDataFolder[]>([])
|
|
const currentFile = ref<EhrFile[]>([])
|
|
const currentPath = ref<string>('')
|
|
const currentDept = ref<number>(0)
|
|
const listDataFolder = ref<TreeDataFolder[]>()
|
|
const listDataFile = ref<EhrFile[]>()
|
|
async function getCabinet() {
|
|
const res = await axiosClient.get<EhrFolder[]>(`${apiEndpoint}cabinet`)
|
|
|
|
data.value = currentFolder.value = res.data.map((v) => ({
|
|
...v,
|
|
status: false,
|
|
folder: [],
|
|
}))
|
|
|
|
listDataFolder.value = data.value
|
|
}
|
|
|
|
async function getFolder(pathname: string, updateStatus = true) {
|
|
loader.show()
|
|
|
|
const pathArray: string[] = pathname.split('/').filter(Boolean)
|
|
|
|
currentDept.value = pathArray.length
|
|
if (pathArray.length >= 4) {
|
|
currentFolder.value = []
|
|
|
|
await getFile(pathname)
|
|
|
|
let current: (typeof data.value)[number] | undefined
|
|
current = data.value.find((v) => v.name === pathArray[0])
|
|
|
|
if (current && updateStatus) current.status = true
|
|
current = current?.folder.find((v) => v.name === pathArray[1])
|
|
if (current && updateStatus) current.status = true
|
|
current = current?.folder.find((v) => v.name === pathArray[2])
|
|
if (current && updateStatus) current.status = true
|
|
current = current?.folder.find((v) => v.name === pathArray[3])
|
|
if (current && updateStatus) current.status = true
|
|
|
|
currentPath.value = pathname
|
|
return loader.hide()
|
|
}
|
|
|
|
let requestPath = 'cabinet'
|
|
if (pathArray.length >= 1) requestPath += `/${pathArray[0]}/drawer`
|
|
if (pathArray.length >= 2) requestPath += `/${pathArray[1]}/folder`
|
|
if (pathArray.length >= 3) requestPath += `/${pathArray[2]}/subfolder`
|
|
|
|
currentPath.value = pathname
|
|
|
|
const res = await axiosClient.get<EhrFolder[]>(
|
|
`${apiEndpoint}${requestPath}`
|
|
)
|
|
|
|
const list = res.data.map((v) => ({
|
|
...v,
|
|
status: false,
|
|
folder: [],
|
|
}))
|
|
|
|
let current: (typeof data.value)[number] | undefined
|
|
|
|
if (pathArray.length >= 1) {
|
|
current = data.value.find((v) => v.name === pathArray[0])
|
|
if (current && updateStatus) {
|
|
current.status = true
|
|
}
|
|
}
|
|
|
|
if (pathArray.length >= 2) {
|
|
current = current?.folder.find((v) => v.name === pathArray[1])
|
|
if (current && updateStatus) {
|
|
current.status = true
|
|
}
|
|
}
|
|
if (pathArray.length >= 3) {
|
|
current = current?.folder.find((v) => v.name === pathArray[2])
|
|
if (current && updateStatus) {
|
|
current.status = true
|
|
}
|
|
}
|
|
|
|
if (current) current.folder = list
|
|
|
|
currentFolder.value = list
|
|
|
|
await getFile(pathname)
|
|
|
|
listDataFolder.value = currentFolder.value
|
|
|
|
return loader.hide()
|
|
}
|
|
|
|
async function getFile(pathname: string) {
|
|
loader.show()
|
|
|
|
const pathArray: string[] = pathname.split('/').filter(Boolean)
|
|
|
|
if (pathArray.length <= 2) {
|
|
currentFile.value = []
|
|
return loader.hide()
|
|
}
|
|
|
|
let requestPath = `cabinet/${pathArray[0]}/drawer/${pathArray[1]}`
|
|
if (pathArray.length >= 3) requestPath += `/folder/${pathArray[2]}`
|
|
if (pathArray.length >= 4) requestPath += `/subfolder/${pathArray[3]}`
|
|
requestPath += '/file'
|
|
|
|
const res = await axiosClient.get<EhrFile[]>(`${apiEndpoint}${requestPath}`)
|
|
|
|
currentFile.value = res.data
|
|
|
|
listDataFile.value = currentFile.value
|
|
|
|
return loader.hide()
|
|
}
|
|
|
|
async function createFolder(name: string | undefined) {
|
|
loader.show()
|
|
|
|
if (!name) return loader.hide()
|
|
|
|
const pathArray: string[] = currentPath.value.split('/').filter(Boolean)
|
|
let requestPath = 'cabinet'
|
|
if (pathArray.length >= 1) requestPath += `/${pathArray[0]}/drawer`
|
|
if (pathArray.length >= 2) requestPath += `/${pathArray[1]}/folder`
|
|
if (pathArray.length >= 3) requestPath += `/${pathArray[2]}/subfolder`
|
|
|
|
await axiosClient.post(`${apiEndpoint}${requestPath}`, {
|
|
name,
|
|
})
|
|
|
|
if (currentDept.value === 0) await getCabinet()
|
|
else await getFolder(currentPath.value)
|
|
|
|
return loader.hide()
|
|
}
|
|
|
|
async function editFolder(name: string | undefined, pathname: string) {
|
|
loader.show()
|
|
|
|
if (!name) return loader.hide()
|
|
|
|
const pathArray: string[] = pathname.split('/').filter(Boolean)
|
|
|
|
let requestPath = 'cabinet'
|
|
if (pathArray.length >= 1) requestPath += `/${pathArray[0]}`
|
|
if (pathArray.length >= 2) requestPath += `/drawer/${pathArray[1]}`
|
|
if (pathArray.length >= 3) requestPath += `/folder/${pathArray[2]}`
|
|
if (pathArray.length >= 4) requestPath += `/subfolder/${pathArray[3]}`
|
|
|
|
await axiosClient.put(`${apiEndpoint}${requestPath}`, {
|
|
name: name,
|
|
})
|
|
|
|
if (currentDept.value === 0) await getCabinet()
|
|
else await getFolder(currentPath.value)
|
|
|
|
return loader.hide()
|
|
}
|
|
|
|
async function deleteFolder(pathname: string) {
|
|
loader.show()
|
|
|
|
const pathArray: string[] = pathname.split('/').filter(Boolean)
|
|
|
|
let requestPath = 'cabinet'
|
|
if (pathArray.length >= 1) requestPath += `/${pathArray[0]}`
|
|
if (pathArray.length >= 2) requestPath += `/drawer/${pathArray[1]}`
|
|
if (pathArray.length >= 3) requestPath += `/folder/${pathArray[2]}`
|
|
if (pathArray.length >= 4) requestPath += `/subfolder/${pathArray[3]}`
|
|
|
|
await axiosClient.delete(`${apiEndpoint}${requestPath}`)
|
|
|
|
if (currentDept.value === 0) await getCabinet()
|
|
else await getFolder(currentPath.value)
|
|
|
|
return loader.hide()
|
|
}
|
|
|
|
async function gotoParent() {
|
|
const pathname = currentPath.value.split('/').filter(Boolean)
|
|
|
|
pathname.pop()
|
|
|
|
if (pathname.length === 0) {
|
|
const cabinet = currentFolder.value.find((v) => v.status === true)
|
|
if (cabinet) {
|
|
cabinet.status = false
|
|
}
|
|
}
|
|
|
|
await getFolder(pathname.join('/') + '/')
|
|
}
|
|
|
|
async function uploadFile(
|
|
pathname: string,
|
|
file: File,
|
|
metadata: {
|
|
title: string
|
|
description: string
|
|
keyword: string[]
|
|
category: string[]
|
|
}
|
|
) {
|
|
loader.show()
|
|
|
|
const pathArray: string[] = pathname.split('/').filter(Boolean)
|
|
|
|
if (pathArray.length <= 2) return
|
|
|
|
let requestPath = 'cabinet'
|
|
if (pathArray.length >= 1) requestPath += `/${pathArray[0]}`
|
|
if (pathArray.length >= 2) requestPath += `/drawer/${pathArray[1]}`
|
|
if (pathArray.length >= 3) requestPath += `/folder/${pathArray[2]}`
|
|
if (pathArray.length >= 4) requestPath += `/subfolder/${pathArray[3]}`
|
|
requestPath += '/file'
|
|
|
|
const res = await axiosClient.post<EhrFile & { upload: string }>(
|
|
`${apiEndpoint}${requestPath}`,
|
|
{
|
|
file: file.name,
|
|
...metadata,
|
|
}
|
|
)
|
|
|
|
if (res && res.data.upload) {
|
|
await fetch(res.data.upload, {
|
|
method: 'PUT',
|
|
body: file,
|
|
})
|
|
|
|
loader.hide()
|
|
}
|
|
|
|
if (currentDept.value === 0) {
|
|
await getCabinet()
|
|
} else await getFolder(currentPath.value)
|
|
|
|
await getFile(currentPath.value)
|
|
|
|
return loader.hide()
|
|
}
|
|
|
|
async function updateFile(
|
|
pathname: string,
|
|
metadata: {
|
|
title: string
|
|
description: string
|
|
keyword: string[]
|
|
category: string[]
|
|
},
|
|
file?: File
|
|
) {
|
|
loader.show()
|
|
|
|
const pathArray: string[] = pathname.split('/').filter(Boolean)
|
|
|
|
if (pathArray.length < 4) return loader.hide()
|
|
|
|
let requestPath = `cabinet/${pathArray[0]}/drawer/${pathArray[1]}`
|
|
if (pathArray.length >= 4) requestPath += `/folder/${pathArray[2]}`
|
|
if (pathArray.length >= 5) requestPath += `/subfolder/${pathArray[3]}`
|
|
requestPath += `/file/${pathArray.at(-1)}`
|
|
|
|
const res = await axiosClient.patch<{ upload: string }>(
|
|
`${apiEndpoint}${requestPath}`,
|
|
{ file: file?.name, ...metadata }
|
|
)
|
|
|
|
if (res && res.data.upload) {
|
|
await fetch(res.data.upload, {
|
|
method: 'PUT',
|
|
body: file,
|
|
})
|
|
|
|
loader.hide()
|
|
}
|
|
|
|
if (currentDept.value === 0) {
|
|
await getCabinet()
|
|
} else await getFolder(currentPath.value)
|
|
|
|
await getFile(currentPath.value)
|
|
|
|
return loader.hide()
|
|
}
|
|
|
|
async function deleteFile(pathname: string) {
|
|
loader.show()
|
|
|
|
const pathArray: string[] = pathname.split('/').filter(Boolean)
|
|
|
|
if (pathArray.length < 4) return loader.hide()
|
|
|
|
let requestPath = `cabinet/${pathArray[0]}/drawer/${pathArray[1]}`
|
|
if (pathArray.length >= 4) requestPath += `/folder/${pathArray[2]}`
|
|
if (pathArray.length >= 5) requestPath += `/subfolder/${pathArray[3]}`
|
|
requestPath += `/file/${pathArray.at(-1)}`
|
|
|
|
await axiosClient.delete(`${apiEndpoint}${requestPath}`)
|
|
|
|
if (currentDept.value === 0) {
|
|
await getCabinet()
|
|
} else await getFolder(currentPath.value)
|
|
|
|
await getFile(currentPath.value)
|
|
|
|
currentFile.value = currentFile.value.filter((v) => v.pathname !== pathname)
|
|
listDataFile.value = currentFile.value
|
|
return loader.hide()
|
|
}
|
|
|
|
function checkFile(fileName: string) {
|
|
return currentFile.value.some((element) => element.fileName === fileName)
|
|
}
|
|
|
|
function checkFileName(fileName: string) {
|
|
return fileName.length >= 85
|
|
}
|
|
|
|
return {
|
|
data,
|
|
currentFolder,
|
|
currentFile,
|
|
currentDept,
|
|
currentPath,
|
|
listDataFile,
|
|
listDataFolder,
|
|
getCabinet,
|
|
getFolder,
|
|
uploadFile,
|
|
updateFile,
|
|
deleteFile,
|
|
gotoParent,
|
|
createFolder,
|
|
deleteFolder,
|
|
editFolder,
|
|
checkFile,
|
|
checkFileName,
|
|
}
|
|
})
|