forked from goplus/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
582 additions
and
366 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
interface ImportMetaEnv { | ||
readonly VITE_API_BASE_URL: string | ||
readonly VITE_PUBLISH_BASE_URL:string | ||
readonly VITE_CASDOOR_ENDPOINT: string | ||
readonly VITE_CASDOOR_CLIENT_ID: string | ||
readonly VITE_CASDOOR_ORGANIZATION_NAME: string | ||
readonly VITE_CASDOOR_APP_NAME: string | ||
} | ||
// interface ImportMetaEnv { | ||
// readonly VITE_API_BASE_URL: string | ||
// readonly VITE_PUBLISH_BASE_URL:string | ||
// readonly VITE_CASDOOR_ENDPOINT: string | ||
// readonly VITE_CASDOOR_CLIENT_ID: string | ||
// readonly VITE_CASDOOR_ORGANIZATION_NAME: string | ||
// readonly VITE_CASDOOR_APP_NAME: string | ||
// } | ||
|
||
interface ImportMeta { | ||
readonly env: ImportMetaEnv | ||
} | ||
// interface ImportMeta { | ||
// readonly env: ImportMetaEnv | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,244 +1,74 @@ | ||
/* | ||
* @Author: Yao xinyue | ||
* @Date: 2024-01-22 11:17:08 | ||
* @LastEditors: xuning [email protected] | ||
* @LastEditTime: 2024-03-14 11:22:33 | ||
* @FilePath: \spx-gui\src\api\asset.ts | ||
* @Description: | ||
*/ | ||
import { service } from '@/axios' | ||
import type { Asset, PageAssetResponse } from '@/interface/library.ts' // Adjust the import paths as needed | ||
import type { ResponseData } from '@/axios' | ||
import type { AxiosResponse } from 'axios' | ||
import { PublicStatus } from '@/class/project' | ||
import type { ByPage, PaginationParams, IsPublic, FileCollection } from './common' | ||
import { client } from './common' | ||
|
||
export enum PublishState { | ||
NotPublished = -1, | ||
PrivateLibrary = 0, | ||
PublicAndPrivateLibrary = 1 | ||
} | ||
|
||
/** | ||
* Fetches a list of assets | ||
* | ||
* @param assetLibraryType 'public' / 'private'; | ||
* @param pageIndex The index of the page to retrieve in a paginated list. | ||
* @param pageSize The number of assets to retrieve per page. | ||
* @param assetType The type of the asset. See src/constant/constant.ts for details. | ||
* @param category (Optional) The category of the assets to filter by. | ||
* @param isOrderByTime (Optional) Whether to order assets by time. | ||
* @param isOrderByHot (Optional) Whether to order assets by popularity. | ||
* @returns PageAssetResponse | ||
*/ | ||
export function getAssetList({ | ||
isPublic, | ||
pageIndex, | ||
pageSize, | ||
assetType, | ||
category, | ||
isOrderByTime, | ||
isOrderByHot, | ||
author | ||
}: { | ||
pageIndex: number | ||
pageSize: number | ||
assetType: number | ||
category?: string | ||
isOrderByTime?: boolean | ||
isOrderByHot?: boolean | ||
isPublic?: PublicStatus | ||
author?: string | ||
}): Promise<PageAssetResponse> { | ||
const baseAssetUrl = '/assets/list' | ||
const params = new URLSearchParams() | ||
params.append('pageIndex', pageIndex.toString()) | ||
params.append('pageSize', pageSize.toString()) | ||
params.append('assetType', assetType.toString()) | ||
|
||
if (isPublic != null) { | ||
params.append('isPublic', isPublic.toString()) | ||
} | ||
if (category) { | ||
params.append('category', category) | ||
} | ||
if (isOrderByTime) { | ||
params.append('isOrderByTime', '1') | ||
} | ||
if (isOrderByHot) { | ||
params.append('isOrderByHot', '1') | ||
} | ||
if (author) { | ||
params.append('author', author) | ||
} | ||
|
||
const url = `${baseAssetUrl}?${params.toString()}` | ||
export type { IsPublic } | ||
|
||
return service({ | ||
url: url, | ||
method: 'get' | ||
}) | ||
export enum AssetType { | ||
Sprite = 0, | ||
Backdrop = 1, | ||
Sound = 2 | ||
} | ||
|
||
/** | ||
* Fetches a single asset | ||
* | ||
* @param id | ||
* @returns Asset | ||
*/ | ||
export function getAsset(id: number): Promise<Asset> { | ||
const url = `/asset/${id}` | ||
return service({ | ||
url: url, | ||
method: 'get' | ||
}) | ||
export type AssetData = { | ||
// Globally unique ID | ||
id: string | ||
// Name to display | ||
displayName: string | ||
// Name of asset owner | ||
owner: string | ||
// Asset Category | ||
category: string | ||
// Public status | ||
isPublic: IsPublic | ||
// Files the asset contains | ||
files: FileCollection | ||
// Preview URL for the asset, e.g., a gif for a sprite | ||
preview: string | ||
// Asset Type | ||
assetType: AssetType | ||
// Click count of the asset | ||
clickCount: number | ||
} | ||
|
||
/** | ||
* @description: Search Asset by name. | ||
* @param {string} search | ||
* @param {number} assetType | ||
* @return { SearchAssetResponse } | ||
*/ | ||
export function searchAssetByName( | ||
pageIndex: number, | ||
pageSize: number, | ||
search: string, | ||
assetType: number | ||
): Promise<PageAssetResponse> { | ||
const baseAssetUrl = `/assets/search` | ||
export type AddAssetParams = Pick<AssetData, 'displayName' | 'category' | 'isPublic' | 'files' | 'preview' | 'assetType'> | ||
|
||
const params = new URLSearchParams() | ||
params.append('pageIndex', pageIndex.toString()) | ||
params.append('pageSize', pageSize.toString()) | ||
params.append('search', search) | ||
params.append('assetType', assetType.toString()) | ||
export function addAsset(params: AddAssetParams) { | ||
return client.post('/asset', params) as Promise<AssetData> | ||
} | ||
|
||
const url = `${baseAssetUrl}?${params.toString()}` | ||
export type UpdateAssetParams = AddAssetParams | ||
|
||
return service({ | ||
url: url, | ||
method: 'get' | ||
}) | ||
export function updateAsset(id: string, params: UpdateAssetParams) { | ||
return client.put(`/asset/${encodeURIComponent(id)}`, params) as Promise<AssetData> | ||
} | ||
|
||
/** | ||
* Save asset | ||
* | ||
* @param id | ||
* @param name | ||
* @param uid | ||
* @param category | ||
* @param isPublic | ||
* @param assetType The type of the asset. See src/constant/constant.ts for details. | ||
* @param file | ||
*/ | ||
export async function saveAsset( | ||
id: number, | ||
name: string, | ||
uid: number, | ||
category: string, | ||
isPublic: number, | ||
assetType: number, | ||
file: File | ||
): Promise<Asset> { | ||
const url = '/asset/save' | ||
const formData = new FormData() | ||
formData.append('id', id.toString()) | ||
formData.append('name', name) | ||
formData.append('uid', uid.toString()) | ||
formData.append('category', category) | ||
formData.append('isPublic', isPublic ? '1' : '0') | ||
formData.append('assetType', assetType.toString()) | ||
formData.append('file', file) | ||
|
||
return service({ | ||
url: url, | ||
method: 'post', | ||
data: formData, | ||
headers: { | ||
'Content-Type': 'multipart/form-data' | ||
} | ||
}) | ||
export function deleteAsset(id: string) { | ||
return client.delete(`/asset/${encodeURIComponent(id)}`) as Promise<void> | ||
} | ||
|
||
/** | ||
* @description: Add asset click count. | ||
* @param id | ||
* @param assetType The type of the asset. See src/constant/constant.ts for details. | ||
* @return {Promise<AxiosResponse<ResponseData<string>>>} | ||
*/ | ||
export function addAssetClickCount(id: number): Promise<AxiosResponse<ResponseData<string>>> { | ||
const url = `/asset/${id}/click-count` | ||
return service({ | ||
url: url, | ||
method: 'post' | ||
}) | ||
export enum ListAssetParamOrderBy { | ||
Default = 'default', | ||
TimeDesc = 'time', | ||
ClickCountDesc = 'clickCount' | ||
} | ||
|
||
/** | ||
* @description: Publish asset to library. | ||
* @param { string } name - sprite name named by user. | ||
* @param { File[] } files - sprite costumes files, saved to show in lib. | ||
* @param { string } assetType - sprite assetType, 0: sprite, 1: backdrop, 2: sound | ||
* @param { PublishState } publishState - The publishing state of the asset. -1: not publish, 0: private lib, 1: public lib. | ||
* @param { string|undefined } [gif] - Optional. The address of the sprite's GIF. | ||
* Only provide this parameter if there is more than one file. | ||
* It is used to display in the library when the sprite is hovering. | ||
* @param { string|undefined } category - the category of the sprite(used to classify in library). | ||
* @return { Promise<string> } - The result of the publishing operation. | ||
*/ | ||
export function publishAsset( | ||
name: string, | ||
files: File[], | ||
assetType: number, | ||
publishState: PublishState, | ||
previewAddress?: string, | ||
export type ListAssetParams = PaginationParams & { | ||
keyword?: string | ||
assetType?: AssetType | ||
category?: string | ||
): Promise<string> { | ||
const url = `/asset` | ||
const formData = new FormData() | ||
formData.append('name', name) | ||
formData.append('assetType', assetType.toString()) | ||
files.forEach((file) => { | ||
formData.append('files', file) | ||
}) | ||
if (previewAddress) { | ||
formData.append('previewAddress', previewAddress) | ||
} | ||
if (category) { | ||
formData.append('category', category) | ||
} | ||
formData.append('publishState', publishState.toString()) | ||
isPublic?: IsPublic | ||
owner?: string | ||
orderBy?: ListAssetParamOrderBy | ||
} | ||
|
||
// Assume `service` is a predefined function for handling HTTP requests. | ||
return service({ | ||
url: url, | ||
method: 'post', | ||
data: formData, | ||
headers: { | ||
'Content-Type': 'multipart/form-data' | ||
} | ||
}) | ||
export function listAsset(params?: ListAssetParams) { | ||
return client.get('/assets/list', params) as Promise<ByPage<AssetData>> | ||
} | ||
|
||
/** | ||
* @description: generate gif by costumes files | ||
* @param {File} files - sprite costumes files | ||
* @return {string} get sprites gif address. | ||
*/ | ||
export function generateGifByCostumes(files: File[]): Promise<AxiosResponse<ResponseData<string>>> { | ||
const url = `/util/to-gif` | ||
const formData = new FormData() | ||
files.forEach((file) => { | ||
formData.append('files', file) | ||
}) | ||
export function getAsset(id: string) { | ||
return client.get(`/asset/${encodeURIComponent(id)}`) as Promise<AssetData> | ||
} | ||
|
||
return service({ | ||
url: url, | ||
method: 'post', | ||
data: formData, | ||
headers: { | ||
'Content-Type': 'multipart/form-data' | ||
} | ||
}) | ||
export function increaseAssetClickCount(id: string) { | ||
return client.post(`/asset/${encodeURIComponent(id)}/click`) as Promise<void> | ||
} |
Oops, something went wrong.