feat(project): connect category icon upload to files api

This commit is contained in:
2026-02-20 16:24:00 +08:00
parent df31135940
commit d66cd70b65
2 changed files with 372 additions and 0 deletions

View 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);
}