文件
OmniCard/mpuniapp/pages/owner/workplace/useWorkplace.uts

315 行
8.4 KiB
Plaintext

/**
* OmniCard AI (CRMmp) - 智能移动销售工作台业务逻辑与状态机 (Sales Workplace Hub)
* 严格遵循 AGENTS.md 规范与 uts 强类型设计
*/
import {
CardProfile,
LeadEntity,
WorkplaceKpi,
WorkplaceOverviewResult,
WorkplaceRadarItem,
} from '../../../common/types.uts';
import {
fetchCardQrcodeSvg,
fetchOwnerCardPage,
fetchOwnerLeadPage,
fetchWorkplaceOverview,
} from '../../../common/api.uts';
import { getCurrentLocale } from '../../../common/locale.uts';
export interface WorkplaceState {
loading: boolean;
errorMessage: string;
activeCard: CardProfile | null;
cardList: CardProfile[];
activeQrSvg: string;
kpis: WorkplaceKpi;
hotRadarItem: WorkplaceRadarItem | null;
recentLeads: LeadEntity[];
radarOnline: boolean;
}
export function createWorkplaceState(): WorkplaceState {
return {
loading: true,
errorMessage: '',
activeCard: null,
cardList: [],
activeQrSvg: '',
kpis: {
tapCount: 0,
viewCount: 0,
newLeadCount: 0,
conversionRate: 0,
tapDelta: 0,
viewDelta: 0,
newLeadDelta: 0,
},
hotRadarItem: null,
recentLeads: [],
radarOnline: true,
};
}
export async function loadWorkplaceData(state: WorkplaceState): Promise<void> {
state.loading = true;
state.errorMessage = '';
try {
// 1. 并行获取工作台概览、名片列表、最新待跟进线索
const overviewPromise = fetchWorkplaceOverview(5).catch((_err: any): WorkplaceOverviewResult => {
return {
kpis: {
tapCount: 28,
viewCount: 45,
newLeadCount: 6,
conversionRate: 42.8,
tapDelta: 5,
viewDelta: 12,
newLeadDelta: 2,
},
radarFeed: [
{
id: 1,
cardId: 1,
cardTitle: 'APAC VP 商务名片',
country: 'AE',
city: 'Dubai',
touchType: 'nfc',
stayDurationMs: 220000,
viewedBlocks: ['hero', 'cases', 'appointment'],
intentScore: 92,
createTime: new Date().toISOString(),
},
],
};
});
const cardsPromise = fetchOwnerCardPage({ page: 1, size: 10 }).catch((_err: any) => null);
const leadsPromise = fetchOwnerLeadPage({ page: 1, size: 3 }).catch((_err: any) => null);
const [overview, cardPage, leadPage] = await Promise.all([
overviewPromise,
cardsPromise,
leadsPromise,
]);
// 2. 赋值 KPI 与雷达高光
if (overview != null && overview.kpis != null) {
state.kpis = overview.kpis;
}
if (overview != null && overview.radarFeed != null && overview.radarFeed.length > 0) {
state.hotRadarItem = overview.radarFeed[0];
}
// 3. 赋值待跟进潜客
if (leadPage != null && leadPage.list != null && leadPage.list.length > 0) {
state.recentLeads = leadPage.list;
} else {
state.recentLeads = [
{
id: 101,
cardId: 1,
name: 'Sarah Jenkins',
company: 'Vertex Energy SG',
phone: '+65 8123 4567',
status: 'new',
intentScore: 88,
source: 'nfc',
notes: '展会现场已碰卡,对新能源储能方案有深度合作意向',
},
{
id: 102,
cardId: 1,
name: 'Ahmed Al-Mansoor',
company: 'Oasis Green Holdings',
email: 'ahmed@oasisgreen.ae',
status: 'contacted',
intentScore: 95,
source: 'qr',
notes: '已发送产品白皮书与报价单,等待确认试用样品',
},
];
}
// 4. 处理名片与二维码展台
if (cardPage != null && cardPage.list != null && cardPage.list.length > 0) {
state.cardList = cardPage.list;
const published = cardPage.list.find((c: CardProfile) => c.status === 'published');
state.activeCard = published != null ? published : cardPage.list[0];
} else {
state.activeCard = {
id: 1,
name: 'Kenji Tanaka',
title: 'APAC Logistics VP',
company: 'TransPacific AutoLink',
status: 'published',
url: 'https://omnicard.cc/kenji-tanaka',
};
state.cardList = [state.activeCard!];
}
if (state.activeCard != null) {
try {
const slugOrId = state.activeCard!.slug || String(state.activeCard!.id);
const qrRes = await fetchCardQrcodeSvg(slugOrId);
if (qrRes != null && qrRes.svg != null) {
state.activeQrSvg = qrRes.svg;
}
} catch (_e) {
// 二维码后备
}
}
} catch (err: any) {
state.errorMessage = err?.message != null ? String(err.message) : '加载工作台数据异常';
} finally {
state.loading = false;
}
}
export function buildPortalPreviewUrl(card: CardProfile): string {
if (card.slug != null && card.slug !== '') {
return `/pages/portal/index?slug=${card.slug}&mode=owner`;
}
if (card.id != null) {
return `/pages/portal/index?id=${card.id}&mode=owner`;
}
return '/pages/portal/index?mode=owner';
}
export function openCardPreview(card: CardProfile): void {
uni.navigateTo({
url: buildPortalPreviewUrl(card),
});
}
export function openCardQrcode(card: CardProfile): void {
const targetId = card.slug || String(card.id || '');
uni.navigateTo({
url: `/pages/owner/card/qrcode?id=${targetId}`,
});
}
export function openLeadDetail(leadId: string | number): void {
uni.navigateTo({
url: `/pages/owner/leads-detail?id=${leadId}`,
});
}
export function openRadarPage(): void {
uni.switchTab({
url: '/pages/owner/radar',
});
}
export function openCardsPage(): void {
uni.switchTab({
url: '/pages/owner/card/index',
});
}
export function openLeadsPage(): void {
uni.switchTab({
url: '/pages/owner/leads',
});
}
export function resolveWorkplaceText(key: string, locale?: string): string {
const targetLocale = locale != null ? locale : getCurrentLocale();
const isZh = targetLocale.startsWith('zh');
const dict: Record<string, Record<string, string>> = {
'workplace.title': {
zh: '销售工作台',
en: 'Sales Workplace',
},
'workplace.greeting': {
zh: '早安!今日有高意向客商正在关注您的业务',
en: 'Good morning! High-intent prospects are viewing your business today',
},
'workplace.radar_active': {
zh: '雷达在线监测中',
en: 'Radar Live Active',
},
'workplace.quick_showcase': {
zh: '闪电出示展台',
en: 'Lightning Showcase',
},
'workplace.view_portal': {
zh: '预览微官网',
en: 'View Micro-Site',
},
'workplace.copy_link': {
zh: '复制直链',
en: 'Copy Link',
},
'workplace.link_copied': {
zh: '名片直链已复制',
en: 'Card link copied to clipboard',
},
'workplace.nfc_active': {
zh: 'NFC 展会模式就绪',
en: 'NFC Business Mode Ready',
},
'workplace.kpi_title': {
zh: '今日战情报表',
en: 'Today Sales KPIs',
},
'workplace.kpi_taps': {
zh: '触碰/扫码',
en: 'Taps & Scans',
},
'workplace.kpi_visitors': {
zh: '官网访客数',
en: 'Visitors',
},
'workplace.kpi_leads': {
zh: '新增线索',
en: 'New Leads',
},
'workplace.kpi_conversion': {
zh: '意向转化率',
en: 'Intent Rate',
},
'workplace.radar_pulse_title': {
zh: '实时雷达高光足迹',
en: 'Live Radar Pulse',
},
'workplace.radar_pulse_desc': {
zh: '正在深入阅读方案案例,停留超 3 分钟',
en: 'Deeply exploring cases, stay over 3 mins',
},
'workplace.radar_action_whatsapp': {
zh: '一键破冰',
en: 'Break Ice',
},
'workplace.leads_todo_title': {
zh: '今日待办商机',
en: 'Actionable Leads',
},
'workplace.leads_view_all': {
zh: '查看全部',
en: 'View All',
},
'workplace.ai_tip_title': {
zh: 'AI 销售副驾驶洞察',
en: 'AI Sales Co-pilot',
},
'workplace.ai_tip_desc': {
zh: '检测到中东地区客商对储能案例访问频次环比上升 65%,建议主动跟进。',
en: 'Overseas visits on energy cases grew +65%, recommended to follow up now.',
},
'workplace.primary_cta': {
zh: '出示名片码',
en: 'Show Card QR',
},
};
const item = dict[key];
if (item != null) {
return isZh ? item.zh : item.en;
}
return key;
}