6. 接入文件上传的接口

master
YunaiV 3 years ago
parent 953bfbbeec
commit d8eb096919
  1. 3
      src/api/path/index.ts
  2. 13
      src/api/path/infra.api.ts
  3. 15
      src/api/path/project.api.ts
  4. 15
      src/api/path/system.api.ts
  5. 27
      src/api/path/system.d.ts
  6. 2
      src/enums/httpEnum.ts
  7. 19
      src/hooks/useSystemInit.hook.ts
  8. 12
      src/store/modules/systemStore/systemStore.ts
  9. 6
      src/views/chart/ContentConfigurations/components/CanvasPage/index.vue
  10. 6
      src/views/chart/hooks/useSync.hook.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'

@ -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()
}
}

@ -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()
}
}

@ -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()
}
}

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

@ -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
}

@ -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)
}
}
})
})

@ -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,

@ -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
})
}
}

Loading…
Cancel
Save