Files
TakeoutSaaS.TenantUI/apps/web-antd/src/api/files.ts

45 lines
1.2 KiB
TypeScript

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('缺少租户标识');
}
return requestClient.upload<FileUploadResponse>('/files/upload', {
file,
tenantId: String(tenantId),
type,
});
}