Initial commit: TradeMate 外贸小助手 MVP

项目结构:
- backend/     Python FastAPI 后端
- uni-app/     uni-app跨端前端
- docs/        设计文档
- docker-compose.yml  Docker编排
- nginx/scripts/systemd 运维配置

已完成功能:
- 用户认证 (JWT)
- 智能翻译 + 回复建议
- 营销素材生成
- 客户管理 + 沉默检测
- 报价单管理
- 产品库管理
- 汇率换算
- 推送通知 (uni-push)
- WhatsApp Webhook框架
- Celery定时任务
This commit is contained in:
TradeMate Dev
2026-05-08 18:17:12 +08:00
commit c6206787da
121 changed files with 11743 additions and 0 deletions
View File
+204
View File
@@ -0,0 +1,204 @@
from typing import Dict, Any, Optional, List
from datetime import datetime, timedelta
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, func, and_
from app.models.customer import Customer, Conversation, Message
import logging
logger = logging.getLogger(__name__)
class CustomerService:
def __init__(self, db: AsyncSession):
self.db = db
async def list_customers(self, user_id: str, status: Optional[str] = None, page: int = 1, size: int = 20) -> Dict[str, Any]:
query = select(Customer).where(Customer.user_id == user_id)
count_query = select(func.count()).select_from(Customer).where(Customer.user_id == user_id)
if status:
query = query.where(Customer.status == status)
count_query = count_query.where(Customer.status == status)
query = query.order_by(Customer.updated_at.desc()).offset((page - 1) * size).limit(size)
total = await self.db.execute(count_query)
result = await self.db.execute(query)
customers = result.scalars().all()
return {
"items": [self._to_dict(c) for c in customers],
"total": total.scalar(),
"page": page,
"size": size,
}
async def get_customer(self, user_id: str, customer_id: str) -> Optional[Dict]:
result = await self.db.execute(
select(Customer).where(
and_(Customer.id == customer_id, Customer.user_id == user_id)
)
)
c = result.scalar_one_or_none()
return self._to_dict(c) if c else None
async def create_customer(self, user_id: str, data: Dict[str, Any]) -> Dict:
c = Customer(user_id=user_id, **data)
self.db.add(c)
await self.db.flush()
return self._to_dict(c)
async def update_customer(self, user_id: str, customer_id: str, data: Dict[str, Any]) -> Optional[Dict]:
result = await self.db.execute(
select(Customer).where(
and_(Customer.id == customer_id, Customer.user_id == user_id)
)
)
c = result.scalar_one_or_none()
if not c:
return None
for k, v in data.items():
if hasattr(c, k):
setattr(c, k, v)
await self.db.flush()
return self._to_dict(c)
async def delete_customer(self, user_id: str, customer_id: str) -> bool:
result = await self.db.execute(
select(Customer).where(
and_(Customer.id == customer_id, Customer.user_id == user_id)
)
)
c = result.scalar_one_or_none()
if not c:
return False
await self.db.delete(c)
return True
async def get_silent_customers(self, user_id: str, days: int = 3) -> List[Dict]:
cutoff = datetime.utcnow() - timedelta(days=days)
result = await self.db.execute(
select(Customer)
.where(
and_(
Customer.user_id == user_id,
Customer.status.in_(["lead", "negotiating"]),
Customer.last_contact_at.isnot(None),
Customer.last_contact_at < cutoff,
)
)
.order_by(Customer.last_contact_at.asc())
)
return [self._to_dict(c) for c in result.scalars().all()]
async def record_contact(self, user_id: str, customer_id: str):
now = datetime.utcnow()
result = await self.db.execute(
select(Customer).where(
and_(Customer.id == customer_id, Customer.user_id == user_id)
)
)
c = result.scalar_one_or_none()
if c:
c.last_contact_at = now
c.silence_started_at = None
c.next_followup_at = now + timedelta(days=3)
await self.db.flush()
async def get_conversation(self, user_id: str, customer_id: str, page: int = 1, size: int = 50) -> Dict[str, Any]:
conv_query = select(Conversation).where(
and_(Conversation.user_id == user_id, Conversation.customer_id == customer_id)
).order_by(Conversation.created_at.desc()).limit(1)
conv_result = await self.db.execute(conv_query)
conv = conv_result.scalar_one_or_none()
if not conv:
return {"messages": [], "total": 0, "conversation_id": None}
msg_query = (
select(Message)
.where(Message.conversation_id == conv.id)
.order_by(Message.created_at.asc())
.offset((page - 1) * size)
.limit(size)
)
msg_result = await self.db.execute(msg_query)
messages = msg_result.scalars().all()
return {
"conversation_id": str(conv.id),
"messages": [
{
"id": str(m.id),
"direction": m.direction,
"content": m.content,
"content_translated": m.content_translated,
"ai_suggestions": m.ai_suggestions,
"selected_suggestion": m.selected_suggestion,
"created_at": m.created_at.isoformat() if m.created_at else None,
}
for m in messages
],
"total": conv.message_count,
}
async def save_message(
self, user_id: str, customer_id: str, direction: str, content: str,
translation: Optional[str] = None, suggestions: Optional[List] = None,
) -> Dict:
conv_result = await self.db.execute(
select(Conversation).where(
and_(Conversation.user_id == user_id, Conversation.customer_id == customer_id)
).order_by(Conversation.created_at.desc()).limit(1)
)
conv = conv_result.scalar_one_or_none()
if not conv:
conv = Conversation(
user_id=user_id,
customer_id=customer_id,
channel="whatsapp",
status="active",
)
self.db.add(conv)
await self.db.flush()
msg = Message(
conversation_id=conv.id,
direction=direction,
content=content,
content_translated=translation,
ai_suggestions=suggestions,
)
self.db.add(msg)
conv.message_count = (conv.message_count or 0) + 1
conv.last_message_at = datetime.utcnow()
await self.db.flush()
await self.record_contact(user_id, customer_id)
return {
"message_id": str(msg.id),
"conversation_id": str(conv.id),
"direction": direction,
"content": content,
}
def _to_dict(self, c: Customer) -> Dict:
if not c:
return {}
return {
"id": str(c.id),
"name": c.name,
"company": c.company,
"country": c.country,
"phone": c.phone,
"email": c.email,
"whatsapp_id": c.whatsapp_id,
"source": c.source,
"tags": c.tags,
"status": c.status,
"last_contact_at": c.last_contact_at.isoformat() if c.last_contact_at else None,
"silence_days": (datetime.utcnow() - c.last_contact_at).days if c.last_contact_at else 0,
"created_at": c.created_at.isoformat() if c.created_at else None,
}
+84
View File
@@ -0,0 +1,84 @@
from typing import Dict, Any, Optional, List
from app.ai.router import get_ai_router
import logging
logger = logging.getLogger(__name__)
class MarketingService:
def __init__(self):
self.ai = get_ai_router()
async def generate(
self,
product_info: Dict[str, Any],
target: str,
style: str = "professional",
language: str = "en",
count: int = 3,
) -> List[Dict[str, Any]]:
results = []
styles = self._get_style_variants(style, count)
for s in styles:
try:
result = await self.ai.marketing(product_info, target, s, language)
results.append({
"content": result.get("content", ""),
"style": s,
"provider": result.get("provider_used", "unknown"),
})
except Exception as e:
logger.warning(f"Marketing generation failed for style '{s}': {e}")
results.append({"content": "", "style": s, "error": str(e)})
return results
async def generate_keywords(
self, product_info: Dict[str, Any], language: str = "en", count: int = 10
) -> List[str]:
try:
schema = {
"type": "object",
"properties": {
"keywords": {
"type": "array",
"items": {"type": "string"},
}
},
}
text = f"Product: {product_info.get('name', '')}. {product_info.get('description', '')}"
result = await self.ai.extract(text, schema)
keywords = result.get("data", {}).get("keywords", [])
return keywords[:count]
except Exception as e:
logger.warning(f"Keyword generation failed: {e}")
return []
def _get_style_variants(self, base_style: str, count: int) -> List[str]:
all_styles = ["professional", "friendly", "urgent", "benefit_focused", "storytelling"]
if base_style in all_styles:
all_styles.remove(base_style)
all_styles.insert(0, base_style)
return all_styles[:count]
async def analyze_competitors(
self, product_info: Dict[str, Any], market: str = "US"
) -> Dict[str, Any]:
try:
text = f"Product: {product_info.get('name', '')} in {market} market. Category: {product_info.get('category', '')}. Description: {product_info.get('description', '')}"
schema = {
"type": "object",
"properties": {
"price_range": {"type": "string"},
"key_selling_points": {"type": "array", "items": {"type": "string"}},
"common_keywords": {"type": "array", "items": {"type": "string"}},
"market_trends": {"type": "string"},
"suggestions": {"type": "array", "items": {"type": "string"}},
},
}
result = await self.ai.extract(text, schema)
return result.get("data", {})
except Exception as e:
logger.warning(f"Competitor analysis failed: {e}")
return {}
+100
View File
@@ -0,0 +1,100 @@
from typing import Dict, Any, Optional
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, func, and_
from app.models.user import Product
from datetime import datetime
import logging
logger = logging.getLogger(__name__)
class ProductService:
def __init__(self, db: AsyncSession):
self.db = db
async def list_products(self, user_id: str, category: Optional[str] = None, page: int = 1, size: int = 20) -> Dict[str, Any]:
query = select(Product).where(Product.user_id == user_id, Product.is_active == True)
count_query = select(func.count()).select_from(Product).where(Product.user_id == user_id, Product.is_active == True)
if category:
query = query.where(Product.category == category)
count_query = count_query.where(Product.category == category)
query = query.order_by(Product.updated_at.desc()).offset((page - 1) * size).limit(size)
total = await self.db.execute(count_query)
result = await self.db.execute(query)
products = result.scalars().all()
return {
"items": [self._to_dict(p) for p in products],
"total": total.scalar(),
"page": page,
"size": size,
}
async def get_product(self, user_id: str, product_id: str) -> Optional[Dict]:
result = await self.db.execute(
select(Product).where(
and_(Product.id == product_id, Product.user_id == user_id)
)
)
p = result.scalar_one_or_none()
return self._to_dict(p) if p else None
async def create_product(self, user_id: str, data: Dict[str, Any]) -> Dict:
p = Product(user_id=user_id, **data)
self.db.add(p)
await self.db.flush()
return self._to_dict(p)
async def update_product(self, user_id: str, product_id: str, data: Dict[str, Any]) -> Optional[Dict]:
result = await self.db.execute(
select(Product).where(
and_(Product.id == product_id, Product.user_id == user_id)
)
)
p = result.scalar_one_or_none()
if not p:
return None
for k, v in data.items():
if v is not None and hasattr(p, k):
setattr(p, k, v)
await self.db.flush()
return self._to_dict(p)
async def delete_product(self, user_id: str, product_id: str) -> bool:
result = await self.db.execute(
select(Product).where(
and_(Product.id == product_id, Product.user_id == user_id)
)
)
p = result.scalar_one_or_none()
if not p:
return False
p.is_active = False
await self.db.flush()
return True
def _to_dict(self, p: Product) -> Dict:
if not p:
return {}
return {
"id": str(p.id),
"name": p.name,
"name_en": p.name_en,
"description": p.description,
"description_en": p.description_en,
"category": p.category,
"price": p.price,
"price_unit": p.price_unit,
"moq": p.moq,
"keywords": p.keywords or [],
"specifications": p.specifications or {},
"images": p.images or [],
"is_active": p.is_active,
"created_at": p.created_at.isoformat() if p.created_at else None,
"updated_at": p.updated_at.isoformat() if p.updated_at else None,
}
+166
View File
@@ -0,0 +1,166 @@
from typing import Dict, Any, Optional, List
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, and_
from app.models.quotation import Quotation, QuotationItem
from app.models.customer import Customer
from app.models.user import Product
from datetime import datetime
import logging
logger = logging.getLogger(__name__)
class QuotationService:
def __init__(self, db: AsyncSession):
self.db = db
async def create_quotation(self, user_id: str, data: Dict[str, Any]) -> Dict:
items_data = data.pop("items", [])
if data.get("customer_id"):
cust_result = await self.db.execute(
select(Customer).where(
and_(Customer.id == data["customer_id"], Customer.user_id == user_id)
)
)
if not cust_result.scalar_one_or_none():
raise ValueError("Customer not found")
q = Quotation(user_id=user_id, **data)
self.db.add(q)
await self.db.flush()
total = 0
for item_data in items_data:
item_total = item_data.get("quantity", 0) * item_data.get("unit_price", 0)
item = QuotationItem(
quotation_id=q.id,
product_name=item_data["product_name"],
description=item_data.get("description"),
quantity=item_data["quantity"],
unit_price=item_data["unit_price"],
total_price=item_total,
unit=item_data.get("unit", "pcs"),
)
self.db.add(item)
total += item_total
q.subtotal = total
q.total = total - (data.get("discount", 0)) + (data.get("shipping", 0))
await self.db.flush()
return await self._to_dict(q)
async def get_quotation(self, user_id: str, quotation_id: str) -> Optional[Dict]:
result = await self.db.execute(
select(Quotation).where(
and_(Quotation.id == quotation_id, Quotation.user_id == user_id)
)
)
q = result.scalar_one_or_none()
return await self._to_dict(q) if q else None
async def list_quotations(self, user_id: str, page: int = 1, size: int = 20) -> Dict[str, Any]:
from sqlalchemy import func
query = select(Quotation).where(Quotation.user_id == user_id).order_by(Quotation.created_at.desc()).offset((page - 1) * size).limit(size)
count_query = select(func.count()).select_from(Quotation).where(Quotation.user_id == user_id)
total = await self.db.execute(count_query)
result = await self.db.execute(query)
quotations = result.scalars().all()
items = []
for q in quotations:
items.append(await self._to_dict(q))
return {"items": items, "total": total.scalar(), "page": page, "size": size}
async def update_status(self, user_id: str, quotation_id: str, status: str) -> Optional[Dict]:
result = await self.db.execute(
select(Quotation).where(
and_(Quotation.id == quotation_id, Quotation.user_id == user_id)
)
)
q = result.scalar_one_or_none()
if not q:
return None
q.status = status
if status == "sent":
q.sent_at = datetime.utcnow()
await self.db.flush()
return await self._to_dict(q)
async def generate_quotation_text(self, q: Quotation) -> str:
items_result = await self.db.execute(
select(QuotationItem).where(QuotationItem.quotation_id == q.id)
)
items = items_result.scalars().all()
lines = [f"QUOTATION", f"", f"Date: {datetime.utcnow().strftime('%Y-%m-%d')}"]
if q.valid_until:
lines.append(f"Valid until: {q.valid_until}")
lines.append(f"")
lines.append(f"{'Item':<30} {'Qty':<10} {'Unit Price':<15} {'Total':<15}")
lines.append("-" * 70)
for item in items:
lines.append(f"{item.product_name:<30} {item.quantity:<10} ${item.unit_price:<12.2f} ${item.total_price:<10.2f}")
lines.append("-" * 70)
if q.subtotal:
lines.append(f"{'Subtotal':>55} ${q.subtotal:<10.2f}")
if q.discount:
lines.append(f"{'Discount':>55} -${q.discount:<9.2f}")
if q.shipping:
lines.append(f"{'Shipping':>55} ${q.shipping:<10.2f}")
lines.append(f"{'TOTAL':>55} ${q.total or q.subtotal or 0:<10.2f}")
lines.append(f"")
if q.payment_terms:
lines.append(f"Payment: {q.payment_terms}")
if q.delivery_terms:
lines.append(f"Delivery: {q.delivery_terms}")
if q.lead_time:
lines.append(f"Lead time: {q.lead_time}")
if q.notes:
lines.append(f"")
lines.append(f"Notes: {q.notes}")
return "\n".join(lines)
async def _to_dict(self, q: Quotation) -> Dict:
items_result = await self.db.execute(
select(QuotationItem).where(QuotationItem.quotation_id == q.id)
)
items = items_result.scalars().all()
return {
"id": str(q.id),
"customer_id": str(q.customer_id) if q.customer_id else None,
"title": q.title,
"status": q.status,
"currency": q.currency,
"exchange_rate": q.exchange_rate,
"payment_terms": q.payment_terms,
"delivery_terms": q.delivery_terms,
"lead_time": q.lead_time,
"valid_until": q.valid_until,
"subtotal": q.subtotal,
"discount": q.discount,
"shipping": q.shipping,
"total": q.total,
"notes": q.notes,
"items": [
{
"product_name": i.product_name,
"description": i.description,
"quantity": i.quantity,
"unit_price": i.unit_price,
"total_price": i.total_price,
"unit": i.unit,
}
for i in items
],
"text": await self.generate_quotation_text(q),
"sent_at": q.sent_at.isoformat() if q.sent_at else None,
"created_at": q.created_at.isoformat() if q.created_at else None,
}
+115
View File
@@ -0,0 +1,115 @@
from typing import Dict, Any, Optional, List
from app.ai.router import get_ai_router
from app.ai.trade_corpus import TradeCorpus
import logging
logger = logging.getLogger(__name__)
class TranslationService:
def __init__(self):
self.ai = get_ai_router()
self.corpus = TradeCorpus()
async def translate(
self, text: str, target_lang: str, source_lang: Optional[str] = None,
context: Optional[str] = None, user_id: Optional[str] = None,
) -> Dict[str, Any]:
similar = await self.corpus.find_similar(text, "translate")
if similar:
best = similar[0]
if len(best["source"]) > 20 and self._similarity_ratio(text, best["source"]) > 0.85:
return {
"translated_text": best["target"],
"source_lang": source_lang or "auto",
"provider_used": "corpus_cache",
"from_cache": True,
}
result = await self.ai.translate(text, target_lang, source_lang, context)
translated = result.get("translated_text", "")
provider = result.get("provider_used", "unknown")
await self.corpus.record(
source_text=text,
target_text=translated,
task_type="translate",
provider=provider,
source_lang=source_lang,
target_lang=target_lang,
metadata={"user_id": user_id} if user_id else None,
)
result["source_lang"] = result.get("detected_source_lang", source_lang or "auto")
result["from_cache"] = False
return result
async def generate_reply(
self, inquiry: str, context: Optional[Dict[str, Any]] = None,
tone: str = "professional", count: int = 3,
) -> List[Dict[str, Any]]:
similar = await self.corpus.find_similar(inquiry, "reply")
if similar and count > 1:
pass
results = []
tones = self._get_tones(tone, count)
for t in tones:
try:
result = await self.ai.reply(inquiry, context, t)
results.append({
"reply": result.get("reply", ""),
"tone": t,
"provider": result.get("provider_used", "unknown"),
})
except Exception as e:
logger.warning(f"Reply generation failed for tone '{t}': {e}")
results.append({"reply": "", "tone": t, "error": str(e)})
return results
async def extract_info(self, text: str, extract_type: str = "auto") -> Dict[str, Any]:
schemas = {
"product": {
"type": "object",
"properties": {
"product_name": {"type": "string"},
"quantity": {"type": "string"},
"price": {"type": "string"},
"currency": {"type": "string"},
"delivery_terms": {"type": "string"},
"target_country": {"type": "string"},
},
},
"inquiry": {
"type": "object",
"properties": {
"intent": {"type": "string"},
"product_interest": {"type": "string"},
"quantity": {"type": "string"},
"budget": {"type": "string"},
"urgency": {"type": "string"},
"contact_info": {"type": "string"},
},
},
}
schema = schemas.get(extract_type, schemas["inquiry"])
result = await self.ai.extract(text, schema)
return result.get("data", {})
def _get_tones(self, base: str, count: int) -> List[str]:
tones = ["professional", "friendly", "formal"]
if base in tones:
tones.remove(base)
tones.insert(0, base)
return tones[:count]
def _similarity_ratio(self, a: str, b: str) -> float:
if not a or not b:
return 0.0
set_a, set_b = set(a.lower().split()), set(b.lower().split())
if not set_a or not set_b:
return 0.0
return len(set_a & set_b) / len(set_a | set_b)
+109
View File
@@ -0,0 +1,109 @@
from typing import Dict, Any, Optional
import httpx
import hashlib
import hmac
import logging
from app.config import settings
logger = logging.getLogger(__name__)
class WhatsAppService:
def __init__(self):
self.api_token = settings.WHATSAPP_API_TOKEN
self.phone_number_id = settings.WHATSAPP_PHONE_NUMBER_ID
self.api_base = f"https://graph.facebook.com/v18.0/{self.phone_number_id}"
def verify_webhook(self, mode: str, token: str, challenge: str) -> Optional[str]:
if mode == "subscribe" and token == settings.WHATSAPP_WEBHOOK_VERIFY_TOKEN:
return challenge
return None
def verify_signature(self, body: bytes, signature: str) -> bool:
if not signature:
return False
expected = hmac.new(
settings.WHATSAPP_API_TOKEN.encode(),
body,
hashlib.sha256,
).hexdigest()
return hmac.compare_digest(f"sha256={expected}", signature)
async def send_text(self, to: str, text: str) -> bool:
if not self.api_token or not self.phone_number_id:
logger.warning("WhatsApp not configured")
return False
async with httpx.AsyncClient() as client:
resp = await client.post(
f"{self.api_base}/messages",
headers={
"Authorization": f"Bearer {self.api_token}",
"Content-Type": "application/json",
},
json={
"messaging_product": "whatsapp",
"to": to,
"type": "text",
"text": {"body": text},
},
timeout=15,
)
if resp.status_code != 200:
logger.error(f"WhatsApp send failed: {resp.text}")
return False
return True
async def send_template(self, to: str, template_name: str, params: Dict[str, str]) -> bool:
if not self.api_token or not self.phone_number_id:
return False
components = [
{
"type": "body",
"parameters": [
{"type": "text", "text": v} for v in params.values()
],
}
]
async with httpx.AsyncClient() as client:
resp = await client.post(
f"{self.api_base}/messages",
headers={"Authorization": f"Bearer {self.api_token}", "Content-Type": "application/json"},
json={
"messaging_product": "whatsapp",
"to": to,
"type": "template",
"template": {
"name": template_name,
"language": {"code": "en"},
"components": components,
},
},
timeout=15,
)
return resp.status_code == 200
def parse_webhook(self, body: Dict) -> Optional[Dict]:
try:
entry = body.get("entry", [{}])[0]
change = entry.get("changes", [{}])[0]
value = change.get("value", {})
messages = value.get("messages", [])
if not messages:
return None
msg = messages[0]
return {
"from": msg.get("from"),
"text": msg.get("text", {}).get("body", ""),
"msg_id": msg.get("id"),
"timestamp": msg.get("timestamp"),
"type": msg.get("type", "text"),
"profile_name": value.get("contacts", [{}])[0].get("profile", {}).get("name"),
}
except Exception as e:
logger.warning(f"Failed to parse WhatsApp webhook: {e}")
return None