fix: onboarding passes product_info dict; marketing service template fallback when no AI; frontend style-switching tabs

This commit is contained in:
TradeMate Dev
2026-05-14 19:23:45 +08:00
parent 93f6ad306a
commit 566f59f0e4
3 changed files with 128 additions and 12 deletions
+53 -2
View File
@@ -7,7 +7,9 @@ logger = logging.getLogger(__name__)
class MarketingService: class MarketingService:
def __init__(self): def __init__(self):
self.ai = get_ai_router() ai_router = get_ai_router()
self.ai = ai_router
self._ai_available = len(ai_router.providers) > 0
async def generate( async def generate(
self, self,
@@ -18,6 +20,12 @@ class MarketingService:
count: int = 3, count: int = 3,
preference_context: Optional[str] = None, preference_context: Optional[str] = None,
) -> List[Dict[str, Any]]: ) -> List[Dict[str, Any]]:
name = product_info.get("name", "")
desc = product_info.get("description", "")
if not self._ai_available:
return self._template_fallback(name, desc, target, style, count, language)
results = [] results = []
styles = self._get_style_variants(style, count) styles = self._get_style_variants(style, count)
@@ -35,9 +43,44 @@ class MarketingService:
return results return results
def _template_fallback(self, name: str, desc: str, target: str, style: str, count: int, language: str) -> List[Dict[str, Any]]:
styles = self._get_style_variants(style, count)
results = []
for s in styles:
if language == "zh":
results.append({
"content": f"{name}】产品介绍\n\n{desc}\n\n我们诚挚向您推荐{name},产品品质优良,价格具有竞争力,欢迎联系我们获取更多信息。",
"style": s,
"provider": "template",
})
else:
results.append({
"content": f"Subject: Introduction of {name}\n\nDear Customer,\n\nWe are pleased to introduce our {name}. {desc}\n\n{self._get_closing(s)}",
"style": s,
"provider": "template",
})
return results
def _get_closing(self, style: str) -> str:
closings = {
"professional": "Looking forward to your favorable reply. Best regards.",
"friendly": "Hope to hear from you soon! Warm regards.",
"urgent": "Please contact us at your earliest convenience. Best regards.",
"benefit_focused": "Don't miss this opportunity to boost your business. Contact us today!",
"storytelling": "Let us help you tell your brand story. Get in touch!",
}
return closings.get(style, closings["professional"])
async def generate_keywords( async def generate_keywords(
self, product_info: Dict[str, Any], language: str = "en", count: int = 10 self, product_info: Dict[str, Any], language: str = "en", count: int = 10
) -> List[str]: ) -> List[str]:
name = product_info.get("name", "")
desc = product_info.get("description", "")
if not self._ai_available:
words = name.split() + [w for w in desc.split() if len(w) > 4]
return list(dict.fromkeys(words))[:count]
try: try:
schema = { schema = {
"type": "object", "type": "object",
@@ -48,7 +91,7 @@ class MarketingService:
} }
}, },
} }
text = f"Product: {product_info.get('name', '')}. {product_info.get('description', '')}" text = f"Product: {name}. {desc}"
result = await self.ai.extract(text, schema) result = await self.ai.extract(text, schema)
keywords = result.get("data", {}).get("keywords", []) keywords = result.get("data", {}).get("keywords", [])
return keywords[:count] return keywords[:count]
@@ -66,6 +109,14 @@ class MarketingService:
async def analyze_competitors( async def analyze_competitors(
self, product_info: Dict[str, Any], market: str = "US" self, product_info: Dict[str, Any], market: str = "US"
) -> Dict[str, Any]: ) -> Dict[str, Any]:
if not self._ai_available:
return {
"price_range": "Contact us for pricing",
"key_selling_points": [product_info.get("name", "")],
"common_keywords": [],
"market_trends": "AI analysis unavailable. Please configure an AI provider in settings.",
"suggestions": ["Set up an AI provider for competitor insights"],
}
try: try:
text = f"Product: {product_info.get('name', '')} in {market} market. Category: {product_info.get('category', '')}. Description: {product_info.get('description', '')}" text = f"Product: {product_info.get('name', '')} in {market} market. Category: {product_info.get('category', '')}. Description: {product_info.get('description', '')}"
schema = { schema = {
+3 -4
View File
@@ -35,11 +35,10 @@ class OnboardingService:
await self.db.flush() await self.db.flush()
mkt = MarketingService() mkt = MarketingService()
product_info = {"name": name, "description": description, "category": category or "general"}
try: try:
content = await mkt.generate( content = await mkt.generate(
product_name=name, product_info=product_info,
description=description,
category=category or "general",
target=target, target=target,
style="professional", style="professional",
count=3, count=3,
@@ -51,7 +50,7 @@ class OnboardingService:
try: try:
keywords_result = await mkt.generate_keywords( keywords_result = await mkt.generate_keywords(
product_name=name, description=description, category=category or "general" product_info=product_info
) )
keywords = keywords_result if isinstance(keywords_result, list) else [] keywords = keywords_result if isinstance(keywords_result, list) else []
except Exception as e: except Exception as e:
+72 -6
View File
@@ -100,12 +100,23 @@
<text class="export-btn" @click="exportCsv" v-if="activeTab !== 'keywords'">导出CSV</text> <text class="export-btn" @click="exportCsv" v-if="activeTab !== 'keywords'">导出CSV</text>
</view> </view>
</view> </view>
<view class="style-tabs" v-if="activeTab !== 'keywords' && availableStyles.length > 1">
<view
class="style-tab"
v-for="s in availableStyles"
:key="s"
:class="{ active: selectedStyle === s }"
@click="selectedStyle = s"
>
{{ styleLabels[s] || s }}
</view>
</view>
<view class="results-list" v-if="activeTab !== 'keywords'"> <view class="results-list" v-if="activeTab !== 'keywords'">
<view class="result-item" v-for="(item, index) in resultsMap[activeTab]" :key="index"> <view class="result-item" v-for="(item, index) in filteredResults" :key="index">
<text class="result-text">{{ item }}</text> <text class="result-text">{{ item.content || item }}</text>
<view class="result-actions"> <view class="result-actions">
<text class="copy-btn" @click="copyText(item)">复制</text> <text class="copy-btn" @click="copyText(item.content || item)">复制</text>
<text class="send-btn" @click="sendToWhatsapp(item)" v-if="activeTab !== 'product'">发送</text> <text class="send-btn" @click="sendToWhatsapp(item.content || item)" v-if="activeTab !== 'product'">发送</text>
<text class="competitor-btn" @click="runCompetitorAnalysis" v-if="activeTab === 'copy'">竞品分析</text> <text class="competitor-btn" @click="runCompetitorAnalysis" v-if="activeTab === 'copy'">竞品分析</text>
</view> </view>
</view> </view>
@@ -134,7 +145,7 @@
</template> </template>
<script setup> <script setup>
import { ref, reactive } from 'vue' import { ref, reactive, computed } from 'vue'
import { marketingApi, interactionApi } from '@/utils/api.js' import { marketingApi, interactionApi } from '@/utils/api.js'
const tabConfig = { const tabConfig = {
@@ -193,6 +204,31 @@ const loading = ref(false)
const resultsMap = reactive({ copy: [], whatsapp: [], product: [], keywords: [] }) const resultsMap = reactive({ copy: [], whatsapp: [], product: [], keywords: [] })
const competitorResult = ref(null) const competitorResult = ref(null)
const stats = ref(null) const stats = ref(null)
const selectedStyle = ref('professional')
const styleLabels = {
professional: '专业正式',
friendly: '亲切友好',
urgent: '紧急催促',
benefit_focused: '利益导向',
storytelling: '故事叙述',
}
const availableStyles = computed(() => {
const items = resultsMap[activeTab.value]
if (!items || items.length === 0 || typeof items[0] === 'string') return []
const styles = [...new Set(items.map(i => i.style).filter(Boolean))]
return styles
})
const filteredResults = computed(() => {
const items = resultsMap[activeTab.value]
if (!items || items.length === 0) return []
if (typeof items[0] === 'string') return items
const style = selectedStyle.value
const filtered = items.filter(i => i.style === style)
return filtered.length > 0 ? filtered : items
})
const formData = ref({ const formData = ref({
product_name: '', product_name: '',
@@ -254,6 +290,9 @@ const generateContent = async () => {
formData.value.style formData.value.style
) )
resultsMap[tab] = res.results || [] resultsMap[tab] = res.results || []
if (res.results && res.results.length > 0) {
selectedStyle.value = res.results[0].style || formData.value.style
}
loadStats() loadStats()
} }
} catch (err) { } catch (err) {
@@ -278,7 +317,10 @@ const exportCsv = () => {
const items = resultsMap[activeTab.value] const items = resultsMap[activeTab.value]
if (!items || items.length === 0) return if (!items || items.length === 0) return
let csv = 'Content\n' let csv = 'Content\n'
items.forEach(r => { csv += `"${r.replace(/"/g, '""')}"\n` }) items.forEach(r => {
const text = typeof r === 'string' ? r : (r.content || '')
csv += `"${text.replace(/"/g, '""')}"\n`
})
const blob = new Blob([csv], { type: 'text/csv' }) const blob = new Blob([csv], { type: 'text/csv' })
const url = URL.createObjectURL(blob) const url = URL.createObjectURL(blob)
uni.downloadFile({ uni.downloadFile({
@@ -439,6 +481,30 @@ const runCompetitorAnalysis = async () => {
border: 2rpx solid #1890ff; border: 2rpx solid #1890ff;
} }
.style-tabs {
display: flex;
gap: 12rpx;
margin-bottom: 20rpx;
overflow-x: auto;
padding-bottom: 4rpx;
}
.style-tab {
flex-shrink: 0;
padding: 12rpx 24rpx;
background: #f5f5f5;
border-radius: 8rpx;
font-size: 24rpx;
color: #666;
white-space: nowrap;
}
.style-tab.active {
background: #e6f7ff;
color: #1890ff;
border: 2rpx solid #1890ff;
}
.generate-btn { .generate-btn {
width: 100%; width: 100%;
height: 88rpx; height: 88rpx;