feat(project): connect category icon upload to files api
This commit is contained in:
45
apps/web-antd/src/api/files.ts
Normal file
45
apps/web-antd/src/api/files.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { requestClient } from '#/api/request';
|
||||
|
||||
export type UploadFileType =
|
||||
| 'business_license'
|
||||
| 'dish_image'
|
||||
| 'merchant_logo'
|
||||
| 'other'
|
||||
| 'review_image'
|
||||
| 'user_avatar';
|
||||
|
||||
export interface FileUploadResponse {
|
||||
fileName?: null | string;
|
||||
fileSize: number;
|
||||
url?: null | string;
|
||||
}
|
||||
|
||||
const TENANT_STORAGE_KEY = 'sys-tenant-id';
|
||||
const DEV_TENANT_ID = import.meta.env.DEV ? import.meta.env.VITE_TENANT_ID : '';
|
||||
|
||||
function resolveTenantId() {
|
||||
const hostname = window.location.hostname;
|
||||
const hostnameParts = hostname.split('.').filter(Boolean);
|
||||
const isIpAddress = /^\d+\.\d+\.\d+\.\d+$/.test(hostname);
|
||||
const subdomainTenantId =
|
||||
hostnameParts.length > 2 && !isIpAddress ? hostnameParts[0] : '';
|
||||
const storageTenantId = localStorage.getItem(TENANT_STORAGE_KEY) || '';
|
||||
return subdomainTenantId || storageTenantId || DEV_TENANT_ID || '';
|
||||
}
|
||||
|
||||
export async function uploadTenantFileApi(
|
||||
file: File,
|
||||
type: UploadFileType = 'other',
|
||||
) {
|
||||
const tenantId = resolveTenantId();
|
||||
if (!tenantId) {
|
||||
throw new Error('缺少租户标识');
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('File', file);
|
||||
formData.append('TenantId', String(tenantId));
|
||||
formData.append('Type', type);
|
||||
|
||||
return requestClient.post<FileUploadResponse>('/files/upload', formData);
|
||||
}
|
||||
Reference in New Issue
Block a user