38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { Inject, Provide } from '@midwayjs/core';
|
|
import { ShopyyAdapter } from '../adapter/shopyy.adapter';
|
|
import { WooCommerceAdapter } from '../adapter/woocommerce.adapter';
|
|
import { ISiteAdapter } from '../interface/site-adapter.interface';
|
|
import { ShopyyService } from './shopyy.service';
|
|
import { SiteService } from './site.service';
|
|
import { WPService } from './wp.service';
|
|
|
|
@Provide()
|
|
export class SiteApiService {
|
|
@Inject()
|
|
siteService: SiteService;
|
|
|
|
@Inject()
|
|
wpService: WPService;
|
|
|
|
@Inject()
|
|
shopyyService: ShopyyService;
|
|
|
|
async getAdapter(siteId: number): Promise<ISiteAdapter> {
|
|
const site = await this.siteService.get(siteId, true);
|
|
if (!site) {
|
|
throw new Error(`Site ${siteId} not found`);
|
|
}
|
|
|
|
if (site.type === 'woocommerce') {
|
|
if (!site?.consumerKey || !site.consumerSecret || !site.apiUrl) {
|
|
throw new Error('站点配置缺少 consumerKey/consumerSecret/apiUrl');
|
|
}
|
|
return new WooCommerceAdapter(site, this.wpService);
|
|
} else if (site.type === 'shopyy') {
|
|
return new ShopyyAdapter(site, this.shopyyService);
|
|
}
|
|
|
|
throw new Error(`Unsupported site type: ${site.type}`);
|
|
}
|
|
}
|