Эх сурвалжийг харах

6. 接入文件上传的接口

YunaiV 2 жил өмнө
parent
commit
d8eb096919

+ 2 - 1
src/api/path/index.ts

@@ -1,2 +1,3 @@
 export * from '@/api/path/project.api'
-export * from '@/api/path/system.api'
+export * from '@/api/path/system.api'
+export * from '@/api/path/infra.api'

+ 13 - 0
src/api/path/infra.api.ts

@@ -0,0 +1,13 @@
+import { http } from '@/api/http'
+import { httpErrorHandle } from '@/utils'
+import { RequestHttpEnum, ModuleTypeEnum, ContentTypeEnum } from '@/enums/httpEnum'
+
+// * 上传文件
+export const uploadFile = async (data: object) => {
+  try {
+    const res = await http(RequestHttpEnum.POST)<string>(`${ModuleTypeEnum.INFRA}/file/upload`, data, ContentTypeEnum.FORM_DATA)
+    return res
+  } catch {
+    httpErrorHandle()
+  }
+}

+ 0 - 15
src/api/path/project.api.ts

@@ -81,18 +81,3 @@ export const changeProjectReleaseApi = async (data: object) => {
     httpErrorHandle()
   }
 }
-
-// * 上传文件
-export const uploadFile = async (data: object) => {
-  try {
-    const res = await http(RequestHttpEnum.POST)<{
-      /**
-       * 文件地址
-       */
-      fileName: string
-    }>(`${ModuleTypeEnum.PROJECT}/upload`, data, ContentTypeEnum.FORM_DATA)
-    return res
-  } catch {
-    httpErrorHandle()
-  }
-}

+ 0 - 15
src/api/path/system.api.ts

@@ -23,18 +23,3 @@ export const logoutApi = async () => {
     httpErrorHandle()
   }
 }
-
-// * 获取 oss 上传接口
-export const ossUrlApi = async (data: object) => {
-  try {
-    const res = await http(RequestHttpEnum.GET)<{
-      /**
-       * bucket 地址
-       */
-      bucketURL?: string
-    }>(`${ModuleTypeEnum.SYSTEM}/getOssInfo`, data)
-    return res
-  } catch (err) {
-    httpErrorHandle()
-  }
-}

+ 0 - 27
src/api/path/system.d.ts

@@ -1,30 +1,3 @@
-export interface LoginResult {
-  token: {
-    /**
-     * token 值
-     */
-    tokenValue: string
-    /**
-     * token key
-     */
-    tokenName: string
-  }
-  userinfo: {
-    /**
-     * 昵称
-     */
-    nickname: string
-    /**
-     * 用户名
-     */
-    username: string
-    /**
-     * 用户 id
-     */
-    id: string
-  }
-}
-
 export interface AuthLoginRespVO {
   /**
    * 用户编号

+ 1 - 1
src/enums/httpEnum.ts

@@ -1,7 +1,7 @@
 // 模块 Path 前缀分类
 export enum ModuleTypeEnum {
-  // SYSTEM = 'sys',
   SYSTEM = 'system',
+  INFRA = 'infra',
   PROJECT = 'report/go-view/project',
 }
 

+ 2 - 17
src/hooks/useSystemInit.hook.ts

@@ -1,23 +1,8 @@
 import { useSystemStore } from '@/store/modules/systemStore/systemStore'
 import { SystemStoreEnum } from '@/store/modules/systemStore/systemStore.d'
 import { ResultEnum } from '@/enums/httpEnum'
-import { ossUrlApi } from '@/api/path'
-
 
 // * 初始化
 export const useSystemInit = async () => {
-  const systemStore = useSystemStore()
-
-  // 获取 OSS 信息的 url 地址,用来拼接展示图片的地址
-  const getOssUrl = async () => {
-    const res = await ossUrlApi({})
-    if (res && res.code === ResultEnum.SUCCESS) {
-      systemStore.setItem(SystemStoreEnum.FETCH_INFO, {
-        OSSUrl: res.data?.bucketURL
-      })
-    }
-  }
-
-  // 执行
-  getOssUrl()
-}
+  // TODO 芋艿:看看这里后续要不要拿下 URL 上的 token
+}

+ 3 - 9
src/store/modules/systemStore/systemStore.ts

@@ -1,5 +1,5 @@
 import { defineStore } from 'pinia'
-import { SystemStoreType, UserInfoType, FetchInfoType } from './systemStore.d'
+import { SystemStoreType, UserInfoType } from './systemStore.d'
 import { setLocalStorage, getLocalStorage } from '@/utils'
 import { StorageEnum } from '@/enums/storageEnum'
 
@@ -16,18 +16,12 @@ export const useSystemStore = defineStore({
       userName: undefined,
       userToken: undefined,
       nickName: undefined
-    },
-    fetchInfo: {
-      OSSUrl: undefined
     }
   },
   getters: {
     getUserInfo(): UserInfoType {
       return this.userInfo
-    },
-    getFetchInfo(): FetchInfoType {
-      return this.fetchInfo
-    },
+    }
   },
   actions: {
     setItem<T extends keyof SystemStoreType, K extends SystemStoreType[T]>(key: T, value: K): void {
@@ -37,4 +31,4 @@ export const useSystemStore = defineStore({
       setLocalStorage(GO_SYSTEM_STORE, this.$state)
     }
   }
-})
+})

+ 3 - 3
src/views/chart/ContentConfigurations/components/CanvasPage/index.vue

@@ -277,17 +277,17 @@ const customRequest = (options: UploadCustomRequestOptions) => {
       // 修改名称
       const newNameFile = new File(
         [file.file],
-        `${fetchRouteParamsLocation()}_index_background.png`,
+        `go-view/${fetchRouteParamsLocation()}_index_background.png`,
         { type: file.file.type }
       )
       let uploadParams = new FormData()
-      uploadParams.append('object', newNameFile)
+      uploadParams.append('file', newNameFile)
       const uploadRes = await uploadFile(uploadParams)
 
       if(uploadRes && uploadRes.code === ResultEnum.SUCCESS) {
         chartEditStore.setEditCanvasConfig(
           EditCanvasConfigEnum.BACKGROUND_IMAGE,
-          `${systemStore.getFetchInfo.OSSUrl}${uploadRes.data.fileName}?time=${new Date().getTime()}`
+          uploadRes.data
         )
         chartEditStore.setEditCanvasConfig(
           EditCanvasConfigEnum.SELECT_COLOR,

+ 4 - 2
src/views/chart/hooks/useSync.hook.ts

@@ -289,13 +289,15 @@ export const useSync = () => {
 
         // 上传预览图
         let uploadParams = new FormData()
-        uploadParams.append('object', base64toFile(canvasImage.toDataURL(), `${fetchRouteParamsLocation()}_index_preview.png`))
+        uploadParams.append('file',
+          base64toFile(canvasImage.toDataURL(),
+            `go-view/${fetchRouteParamsLocation()}_index_preview.png`)) // 名字使用 go-view 作为前缀
         const uploadRes = await uploadFile(uploadParams)
         // 保存预览图
         if(uploadRes && uploadRes.code === ResultEnum.SUCCESS) {
           await updateProjectApi({
             id: fetchRouteParamsLocation(),
-            indexImage: `${systemStore.getFetchInfo.OSSUrl}${uploadRes.data.fileName}`
+            picUrl: uploadRes.data
           })
         }
       }