39 lines
1.4 KiB
TypeScript
39 lines
1.4 KiB
TypeScript
import { http } from './http';
|
|
|
|
function unwrap<T>(res: any): T {
|
|
if (res && typeof res.code === 'number') {
|
|
if (res.code !== 0) {
|
|
const message: string = res.msg || '请求失败';
|
|
throw new Error(message);
|
|
}
|
|
return (res.data as T) ?? ({} as T);
|
|
}
|
|
return res as T;
|
|
}
|
|
|
|
export const rakutenApi = {
|
|
// 上传 Excel 或按店铺名查询
|
|
getProducts(params: { file?: File; shopName?: string; batchId?: string }) {
|
|
const formData = new FormData();
|
|
if (params.file) formData.append('file', params.file);
|
|
if (params.batchId) formData.append('batchId', params.batchId);
|
|
if (params.shopName) formData.append('shopName', params.shopName);
|
|
return http
|
|
.upload('/api/rakuten/products', formData)
|
|
.then(res => unwrap<{ products: any[]; total?: number; sessionId?: string }>(res));
|
|
},
|
|
search1688(imageUrl: string, sessionId?: string) {
|
|
const payload: Record<string, unknown> = { imageUrl };
|
|
if (sessionId) payload.sessionId = sessionId;
|
|
return http.post('/api/rakuten/search1688', payload).then(res => unwrap<any>(res));
|
|
},
|
|
getLatestProducts() {
|
|
return http.get('/api/rakuten/products/latest').then(res => unwrap<{ products: any[] }>(res));
|
|
},
|
|
exportAndSave(exportData: unknown) {
|
|
return http
|
|
.post('/api/rakuten/export-and-save', exportData)
|
|
.then(res => unwrap<{ filePath: string; fileName?: string; recordCount?: number; hasImages?: boolean }>(res));
|
|
},
|
|
};
|