From 891e2aae6b398a0ef7e2a310069218b274533185 Mon Sep 17 00:00:00 2001 From: DaxPay Date: Tue, 13 Aug 2024 12:31:56 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E7=9B=B8=E5=85=B3=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/common/FileUpload.ts | 31 +-- src/hooks/bootx/useDict.ts | 2 + src/hooks/bootx/useFilePlatform.ts | 44 +++++ src/hooks/bootx/useUpload.ts | 1 + .../header/components/user-dropdown/index.vue | 5 +- src/store/modules/dict.ts | 7 - src/store/modules/filePlatform.ts | 35 ++++ src/store/modules/user.ts | 3 - .../baseapi/file/platform/FilePlatform.api.ts | 54 ++++++ .../file/platform/FilePlatformList.vue | 183 ++++++++++++++++++ .../baseapi/file/upload/FileUpload.api.ts | 113 +++++++++++ .../baseapi/file/upload/FileUploadInfo.vue | 49 +++++ .../baseapi/file/upload/FileUploadList.vue | 180 +++++++++++++++++ types/store.d.ts | 9 + 14 files changed, 675 insertions(+), 41 deletions(-) create mode 100644 src/hooks/bootx/useFilePlatform.ts create mode 100644 src/store/modules/filePlatform.ts create mode 100644 src/views/baseapi/file/platform/FilePlatform.api.ts create mode 100644 src/views/baseapi/file/platform/FilePlatformList.vue create mode 100644 src/views/baseapi/file/upload/FileUpload.api.ts create mode 100644 src/views/baseapi/file/upload/FileUploadInfo.vue create mode 100644 src/views/baseapi/file/upload/FileUploadList.vue diff --git a/src/api/common/FileUpload.ts b/src/api/common/FileUpload.ts index 3a7cc2c4b..217bab876 100644 --- a/src/api/common/FileUpload.ts +++ b/src/api/common/FileUpload.ts @@ -1,40 +1,11 @@ import { defHttp } from '@/utils/http/axios' -import { Result, UploadFileParams } from '#/axios' +import { UploadFileParams } from '#/axios' import { UploadApiResult } from '@/api/sys/model/uploadModel' import { getAppEnvConfig } from '@/utils/env' import { AxiosProgressEvent } from 'axios' const { VITE_GLOB_API_URL, VITE_GLOB_API_URL_PREFIX } = getAppEnvConfig() -/** - * 获取文件预览地址 - */ -export const getFilePreviewUrl = (id) => { - return defHttp.get>({ - url: `/file/getFilePreviewUrl`, - params: { id }, - }) -} - -/** - * 获取文件预览地址前缀 - */ -export const getFilePreviewUrlPrefix = () => { - return defHttp.get>({ - url: `/file/getFilePreviewUrlPrefix`, - }) -} - -/** - * 获取文件下载地址 - */ -export const getFileDownloadUrl = (id) => { - return defHttp.get>({ - url: `/file/getFileDownloadUrl`, - params: { id }, - }) -} - /** * 上传文件 * @param params diff --git a/src/hooks/bootx/useDict.ts b/src/hooks/bootx/useDict.ts index eece20576..02fa2cab9 100644 --- a/src/hooks/bootx/useDict.ts +++ b/src/hooks/bootx/useDict.ts @@ -58,6 +58,8 @@ async function dictDropDownNumber(dictCode: string): Promise { * 字典hooks */ export function useDict() { + // 初始化 + getDict().then() return { dictConvert, dictDropDown, diff --git a/src/hooks/bootx/useFilePlatform.ts b/src/hooks/bootx/useFilePlatform.ts new file mode 100644 index 000000000..78bd80d9f --- /dev/null +++ b/src/hooks/bootx/useFilePlatform.ts @@ -0,0 +1,44 @@ +import { FilePlatform } from '#/store' +import { useFilePlatformStore } from '@/store/modules/filePlatform' + +const platformStore = useFilePlatformStore() + +/** + * 获取存储平台列表 + */ +async function getFilePlatforms(): Promise { + const filePlatform = platformStore.getFilePlatforms + if (filePlatform.length > 0) { + return filePlatform + } else { + return await platformStore.initFilePlatform() + } +} + +/** + * 获取地址 + * @param fileUrl 文件保存的url地址 + * @param platform 存储平台类型编码 + */ +function getFileUrl(fileUrl?: string, platform?: string) { + const platforms = platformStore.getFilePlatforms + const item = platforms.filter((o) => { + return platform ? platform === o.type : o.defaultPlatform + }) + if (item && item.length > 0) { + return item[0].url + fileUrl + } else { + return '' + } +} + +/** + * 存储平台hooks + */ +export function useFilePlatform() { + // 初始化 + getFilePlatforms().then() + return { + getFileUrl, + } +} diff --git a/src/hooks/bootx/useUpload.ts b/src/hooks/bootx/useUpload.ts index 39122260f..45c050e98 100644 --- a/src/hooks/bootx/useUpload.ts +++ b/src/hooks/bootx/useUpload.ts @@ -21,6 +21,7 @@ export function useUpload(uploadUrl: string) { const uploadAction = computed(() => { return VITE_GLOB_API_URL + VITE_GLOB_API_URL_PREFIX + uploadUrl }) + return { tokenHeader, uploadAction, diff --git a/src/layouts/default/header/components/user-dropdown/index.vue b/src/layouts/default/header/components/user-dropdown/index.vue index 0862f1557..8f991ff60 100644 --- a/src/layouts/default/header/components/user-dropdown/index.vue +++ b/src/layouts/default/header/components/user-dropdown/index.vue @@ -30,6 +30,7 @@ import { propTypes } from '@/utils/propTypes' import { openWindow } from '@/utils' import { createAsyncComponent } from '@/utils/factory/createAsyncComponent' + import { useFilePlatform } from '@/hooks/bootx/useFilePlatform' type MenuEvent = 'logout' | 'doc' | 'lock' | 'api' @@ -44,9 +45,11 @@ const { prefixCls } = useDesign('header-user-dropdown') const { getShowDoc } = useHeaderSetting() const userStore = useUserStore() + const { getFileUrl } = useFilePlatform() const getUserInfo = computed(() => { - const { name = '', avatar } = userStore.getUserInfo || {} + let { name = '', avatar } = userStore.getUserInfo || {} + avatar = avatar ? getFileUrl(avatar) : '' return { name, avatar: avatar || headerImg } }) diff --git a/src/store/modules/dict.ts b/src/store/modules/dict.ts index 888da6143..26fadf1e0 100644 --- a/src/store/modules/dict.ts +++ b/src/store/modules/dict.ts @@ -35,10 +35,3 @@ export const useDictStore = defineStore({ }, }, }) -// Need to be used outside the setup -export function useDictStoreWithOut() { - return useDictStore(store) -} - -// 初始化字典 -useDictStoreWithOut().initDict().then() diff --git a/src/store/modules/filePlatform.ts b/src/store/modules/filePlatform.ts new file mode 100644 index 000000000..cb48b2dad --- /dev/null +++ b/src/store/modules/filePlatform.ts @@ -0,0 +1,35 @@ +import { defineStore } from 'pinia' +import { FilePlatform } from '#/store' +import { findAll } from '@/views/baseapi/file/platform/FilePlatform.api' + +interface FilePlatformState { + filePlatform: FilePlatform[] +} +export const useFilePlatformStore = defineStore({ + id: 'app-file-platform', + state: (): FilePlatformState => ({ + filePlatform: [], + }), + getters: { + getFilePlatforms(): FilePlatform[] { + return this.filePlatform + }, + }, + actions: { + /** + * 初始化存储平台, 并列表 + */ + async initFilePlatform() { + const { data } = await findAll() + this.filePlatform = data.map((o) => { + return { + type: o.type, + url: o.url, + defaultPlatform: o.defaultPlatform, + } as FilePlatform + }) + console.log('初始化存储平台') + return this.filePlatform + }, + }, +}) diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index 38276b489..53c33f634 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -8,7 +8,6 @@ import { doLogout, login } from '@/api/sys/login' import { useMessage } from '@/hooks/web/useMessage' import { router } from '@/router' import { h } from 'vue' -import { getFilePreviewUrlPrefix } from '@/api/common/FileUpload' import { getUserInfo } from '@/api/sys/user' import { userLogoutAction } from '@/store/action/userAction' import { TOKEN_KEY, USER_INFO_KEY } from '@/enums/cacheEnum' @@ -81,8 +80,6 @@ export const useUserStore = defineStore({ if (!this.getToken) return null const { data: userInfo } = await getUserInfo() // 设置头像 - const { data: urlPrefix } = await getFilePreviewUrlPrefix() - userInfo.avatar = userInfo.avatar ? urlPrefix + userInfo.avatar : '' this.setUserInfo(userInfo as any) }, /** diff --git a/src/views/baseapi/file/platform/FilePlatform.api.ts b/src/views/baseapi/file/platform/FilePlatform.api.ts new file mode 100644 index 000000000..069bc2a1e --- /dev/null +++ b/src/views/baseapi/file/platform/FilePlatform.api.ts @@ -0,0 +1,54 @@ +import { defHttp } from '@/utils/http/axios' +import { PageResult, Result } from '#/axios' +import { BaseEntity } from '#/web' + +/** + * 查询列表 + */ +export function findAll() { + return defHttp.get>({ + url: '/file/platform/findAll', + }) +} + +/** + * 更新文件存储平台地址 + */ +export function updateUrl(id: string, url: string) { + return defHttp.post>({ + url: '/file/platform/updateUrl', + data: { id, url }, + }) +} + +/** + * 设置默认存储平台地址 + */ +export function setDefault(id: string) { + return defHttp.post>({ + url: '/file/platform/setDefault', + params: { id }, + }) +} + +/** + * 文件存储平台 + */ +export interface FilePlatform extends BaseEntity { + /** + * 文件存储平台名称 + */ + name: string + /** + * 文件存储平台类型 + */ + type: string + /** + * 访问地址 + */ + url?: string + /** + * 默认平台 + */ + defaultPlatform: boolean +} diff --git a/src/views/baseapi/file/platform/FilePlatformList.vue b/src/views/baseapi/file/platform/FilePlatformList.vue new file mode 100644 index 000000000..e09a9006d --- /dev/null +++ b/src/views/baseapi/file/platform/FilePlatformList.vue @@ -0,0 +1,183 @@ + + + + + diff --git a/src/views/baseapi/file/upload/FileUpload.api.ts b/src/views/baseapi/file/upload/FileUpload.api.ts new file mode 100644 index 000000000..b5471d440 --- /dev/null +++ b/src/views/baseapi/file/upload/FileUpload.api.ts @@ -0,0 +1,113 @@ +import { defHttp } from '@/utils/http/axios' +import { PageResult, Result } from '#/axios' + +/** + * 分页 + */ +export const page = (params) => { + return defHttp.get>>({ + url: '/file/page', + params, + }) +} + +/** + * 获取单条 + */ +export const get = (id) => { + return defHttp.get>({ + url: '/file/findById', + params: { id }, + }) +} + +/** + * 添加 + */ +export const add = (obj: UpdateFileInfo) => { + return defHttp.post({ + url: '/file/add', + data: obj, + }) +} + +/** + * 更新 + */ +export const update = (obj: UpdateFileInfo) => { + return defHttp.post({ + url: '/file/update', + data: obj, + }) +} + +/** + * 删除 + */ +export const del = (id) => { + return defHttp.delete({ + url: '/file/delete', + params: { id }, + }) +} + +/** + * 查询全部 + */ +export const findAll = () => { + return defHttp.get>({ + url: '/file/findAll', + }) +} + +/** + * 上传文件信息 + */ +export interface UpdateFileInfo { + // id + id?: number + // 文件访问地址 + url?: string + // 文件大小,单位字节 + size?: string + // 文件名称 + filename?: string + // 原始文件名 + originalFilename?: string + // 基础存储路径 + basePath?: string + // 存储路径 + path?: string + // 文件扩展名 + ext?: string + // MIME类型 + contentType?: string + // 存储平台 + platform?: string + // 缩略图访问路径 + thUrl?: string + // 缩略图名称 + thFilename?: string + // 缩略图大小,单位字节 + thSize?: string + // 缩略图MIME类型 + thContentType?: string + // 文件所属对象id + objectId?: string + // 文件所属对象类型,例如用户头像,评价图片 + objectType?: string + // 文件元数据 + metadata?: string + // 文件用户元数据 + userMetadata?: string + // 缩略图元数据 + thMetadata?: string + // 缩略图用户元数据 + thUserMetadata?: string + // 附加属性 + attr?: string + // 文件ACL + fileAcl?: string + // 缩略图文件ACL + thFileAcl?: string +} diff --git a/src/views/baseapi/file/upload/FileUploadInfo.vue b/src/views/baseapi/file/upload/FileUploadInfo.vue new file mode 100644 index 000000000..e8974801b --- /dev/null +++ b/src/views/baseapi/file/upload/FileUploadInfo.vue @@ -0,0 +1,49 @@ + + + + + diff --git a/src/views/baseapi/file/upload/FileUploadList.vue b/src/views/baseapi/file/upload/FileUploadList.vue new file mode 100644 index 000000000..66652f924 --- /dev/null +++ b/src/views/baseapi/file/upload/FileUploadList.vue @@ -0,0 +1,180 @@ + + + + + diff --git a/types/store.d.ts b/types/store.d.ts index c9d42c83b..9c33504f5 100644 --- a/types/store.d.ts +++ b/types/store.d.ts @@ -60,3 +60,12 @@ export interface Dict { code: string name: string } + +/** + * 存储平台 + */ +export interface FilePlatform { + type: string + url: string + defaultPlatform: boolean +}