Files
makemd/server/src/core/operation/adapters/GenericAdapter.ts
wurenzhi 8de9ea0aaa feat: 实现Operation-Agent核心功能及电商平台适配器
refactor: 重构项目结构,分离server和dashboard代码
style: 统一代码风格,修复lint警告
test: 添加平台适配器工厂测试用例
ci: 更新CI/CD流程,增加语义验证和性能测试
docs: 添加语义中心文档,定义统一数据模型和状态机
2026-03-19 15:23:56 +08:00

168 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Injectable } from '@nestjs/common';
import { IPlatformAdapter } from './IPlatformAdapter';
import { Product } from '../../../types/models/Product';
import { Order } from '../../../types/models/Order';
import { ShopInfo } from '../../../types/models/ShopInfo';
import { Logger } from '@nestjs/common';
@Injectable()
export class GenericAdapter implements IPlatformAdapter {
private readonly logger = new Logger(GenericAdapter.name);
private authToken: string | null = null;
async authorize(authInfo: Record<string, any>): Promise<boolean> {
this.logger.log('通用平台授权开始');
try {
// 模拟通用平台授权过程
const { apiKey, apiSecret } = authInfo;
if (!apiKey || !apiSecret) {
throw new Error('缺少必要的授权信息');
}
// 模拟授权成功
this.authToken = 'generic_auth_token_' + Date.now();
this.logger.log('通用平台授权成功');
return true;
} catch (error) {
this.logger.error('通用平台授权失败', error);
throw error;
}
}
async getShopInfo(): Promise<ShopInfo> {
this.logger.log('获取通用平台店铺信息');
if (!this.authToken) {
throw new Error('未授权');
}
// 模拟获取店铺信息
return {
id: 'generic_shop_123',
name: 'Test Generic Store',
description: 'Test Generic Store Description',
status: 'active',
createdAt: new Date(),
updatedAt: new Date()
};
}
async getProducts(limit: number = 10, offset: number = 0): Promise<Product[]> {
this.logger.log(`获取通用平台商品列表limit: ${limit}, offset: ${offset}`);
if (!this.authToken) {
throw new Error('未授权');
}
// 模拟获取商品列表
const products: Product[] = [];
for (let i = 0; i < limit; i++) {
products.push({
id: `generic_product_${offset + i + 1}`,
name: `Generic Product ${offset + i + 1}`,
sku: `GEN-${offset + i + 1}`,
price: 70 + (offset + i) * 7,
stock: 160,
description: `Generic Product ${offset + i + 1} Description`,
images: [`https://example.com/generic_image${offset + i + 1}.jpg`],
categories: ['General', 'Miscellaneous'],
attributes: {
brand: 'Generic Brand',
model: `Model ${offset + i + 1}`
},
createdAt: new Date(),
updatedAt: new Date()
});
}
return products;
}
async getOrders(limit: number = 10, offset: number = 0): Promise<Order[]> {
this.logger.log(`获取通用平台订单列表limit: ${limit}, offset: ${offset}`);
if (!this.authToken) {
throw new Error('未授权');
}
// 模拟获取订单列表
const orders: Order[] = [];
for (let i = 0; i < limit; i++) {
orders.push({
id: `generic_order_${offset + i + 1}`,
customerId: `customer_${offset + i + 1}`,
totalAmount: 140 + (offset + i) * 35,
status: 'processing',
items: [
{
productId: `generic_product_${offset + i + 1}`,
quantity: 1,
price: 110 + (offset + i) * 7
}
],
shippingAddress: {
name: `Customer ${offset + i + 1}`,
address: `303 Generic St`,
city: 'Generic City',
state: 'GS',
zip: '12345',
country: 'US'
},
paymentMethod: 'credit_card',
createdAt: new Date(),
updatedAt: new Date()
});
}
return orders;
}
async updateProductPrice(productId: string, price: number): Promise<boolean> {
this.logger.log(`更新通用平台商品价格: ${productId}, 价格: ${price}`);
if (!this.authToken) {
throw new Error('未授权');
}
// 模拟更新商品价格
this.logger.log(`通用平台商品价格更新成功: ${productId}, 价格: ${price}`);
return true;
}
async updateProductStock(productId: string, stock: number): Promise<boolean> {
this.logger.log(`更新通用平台商品库存: ${productId}, 库存: ${stock}`);
if (!this.authToken) {
throw new Error('未授权');
}
// 模拟更新商品库存
this.logger.log(`通用平台商品库存更新成功: ${productId}, 库存: ${stock}`);
return true;
}
async listProduct(product: Product): Promise<string> {
this.logger.log(`上架通用平台商品: ${product.name}`);
if (!this.authToken) {
throw new Error('未授权');
}
// 模拟上架商品
const productId = `generic_product_${Date.now()}`;
this.logger.log(`通用平台商品上架成功商品ID: ${productId}`);
return productId;
}
async delistProduct(productId: string): Promise<boolean> {
this.logger.log(`下架通用平台商品: ${productId}`);
if (!this.authToken) {
throw new Error('未授权');
}
// 模拟下架商品
this.logger.log(`通用平台商品下架成功: ${productId}`);
return true;
}
async getApiStatus(): Promise<{ status: string; timestamp: number }> {
this.logger.log('获取通用平台API状态');
return {
status: 'healthy',
timestamp: Date.now()
};
}
}