文件
OmniCard/mpuniapp/pages/owner/useLeads.uts
wikiboss 62e0ae4435 feat: 同步管理端、用户端与后端业务,并接通线上 Web 接口
补齐平台订阅、外部登录、CRM 同步与用户端页面;Nginx 转发用户端接口,登录页使用 Google 与 Apple 标志。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-23 18:01:47 +08:00

475 行
11 KiB
Plaintext

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

/**
* OmniCard AI (CRMmp) - 名片主移动潜客列表业务驱动层
* 分页拉线索、过滤非法 status、空态与错误
*/
import {
LeadEntity,
LeadStatus,
OwnerPageData,
OwnerPagePagination,
OwnerPageQuery,
RadarTrack,
} from '../../common/types.uts';
import {
fetchOwnerLeadPage,
fetchOwnerRadarPage,
fetchRadarHotInbox,
} from '../../common/api.uts';
import { getCurrentLocale, getOmniText } from '../../common/locale.uts';
import { formatLocation, formatStay } from './useRadar.uts';
export type LeadsCopyKey =
| 'title'
| 'followUp'
| 'searchPlaceholder'
| 'cta'
| 'statusAll'
| 'statusNew'
| 'statusContacted'
| 'statusHot'
| 'statusWon'
| 'statusArchived'
| 'empty'
| 'error'
| 'loading'
| 'score'
| 'radarMore'
| 'hotBadge'
| 'hotTitle'
| 'highIntent'
| 'activeRadar'
| 'quickContact'
| 'call'
| 'email'
| 'sourceNfc'
| 'sourceQr'
| 'sourceLink'
| 'sourceAi'
| 'stats'
| 'statTotal'
| 'statHot'
| 'statNew'
| 'statWon'
| 'teamBriefing'
| 'liveTracks'
| 'noCompany'
| 'noTracks';
export type LoadLeadsQuery = {
page: number;
size: number;
status?: string;
keyWord?: string;
};
export type LoadLeadsApi = {
fetchOwnerLeadPage: (
query: OwnerPageQuery
) => Promise<OwnerPageData<LeadEntity>>;
};
export type LoadLeadsResult = {
list: LeadEntity[];
pagination: OwnerPagePagination;
empty: boolean;
errorMessage: string;
};
export type LeadsState = {
list: LeadEntity[];
radarTracks: RadarTrack[];
hotUnread: number;
page: number;
size: number;
total: number;
status: string;
keyWord: string;
loading: boolean;
empty: boolean;
errorMessage: string;
};
export type LeadsPageOptions = {
status?: string;
keyWord?: string;
page?: string;
size?: string;
};
export const LEAD_STATUS_VALUES: LeadStatus[] = [
'new',
'contacted',
'hot',
'won',
'archived',
];
function isZhLocale(): boolean {
return getCurrentLocale() === 'zh-CN';
}
export function parseLeadStatus(
raw: string | null | undefined
): LeadStatus | null {
if (
raw === 'new' ||
raw === 'contacted' ||
raw === 'hot' ||
raw === 'won' ||
raw === 'archived'
) {
return raw;
}
return null;
}
/**
* 页面文案:crm.leads / crm.follow_up 与通用词条走词典,状态筛选用本地词典
*/
export function resolveLeadsText(key: LeadsCopyKey): string {
const locale = getCurrentLocale();
if (key === 'title') {
return getOmniText(locale, 'crm.leads');
}
if (key === 'followUp') {
return getOmniText(locale, 'crm.follow_up');
}
if (key === 'empty') {
return getOmniText(locale, 'common.empty');
}
if (key === 'error') {
return getOmniText(locale, 'common.error');
}
if (key === 'loading') {
return getOmniText(locale, 'common.loading');
}
if (key === 'cta') {
return getOmniText(locale, 'common.search');
}
const zh = isZhLocale();
if (key === 'searchPlaceholder') {
return zh ? '搜索姓名或公司' : 'Search name or company';
}
if (key === 'statusAll') {
return zh ? '全部' : 'All';
}
if (key === 'statusNew') {
return zh ? '新线索' : 'New';
}
if (key === 'statusContacted') {
return zh ? '已联系' : 'Contacted';
}
if (key === 'statusHot') {
return zh ? '高意向' : 'Hot';
}
if (key === 'statusWon') {
return zh ? '已成交' : 'Won';
}
if (key === 'statusArchived') {
return zh ? '已归档' : 'Archived';
}
if (key === 'score') {
return zh ? '意向分' : 'Intent';
}
if (key === 'radarMore') {
return zh ? '查看全部雷达' : 'Full radar';
}
if (key === 'hotBadge') {
return zh ? '高意向访客' : 'Hot visitors';
}
if (key === 'hotTitle') {
return zh ? '高意向商机' : 'Hot Leads';
}
if (key === 'highIntent') {
return zh ? '高意向' : 'High Intent';
}
if (key === 'activeRadar') {
return zh ? '活跃雷达' : 'Live Radar';
}
if (key === 'quickContact') {
return zh ? '快捷联系' : 'Quick Contact';
}
if (key === 'call') {
return zh ? '电话呼叫' : 'Call';
}
if (key === 'email') {
return zh ? '发送邮件' : 'Email';
}
if (key === 'sourceNfc') {
return 'NFC';
}
if (key === 'sourceQr') {
return 'QR Code';
}
if (key === 'sourceLink') {
return 'Web Link';
}
if (key === 'sourceAi') {
return 'AI Chat';
}
if (key === 'stats') {
return zh ? '统计' : 'Stats';
}
if (key === 'statTotal') {
return zh ? '总潜客' : 'Total Leads';
}
if (key === 'statHot') {
return zh ? '高意向' : 'Hot';
}
if (key === 'statNew') {
return zh ? '新线索' : 'New';
}
if (key === 'statWon') {
return zh ? '已成交' : 'Won';
}
if (key === 'teamBriefing') {
return zh ? '团队简报' : 'Team Overview';
}
if (key === 'liveTracks') {
return zh ? '实时访客足迹' : 'Live Visitor Track';
}
if (key === 'noCompany') {
return zh ? '个人客户' : 'Individual';
}
if (key === 'noTracks') {
return zh ? '暂无实时轨迹' : 'No recent radar activity';
}
return '';
}
export function formatHotUnread(unread: number): string {
if (unread < 1) {
return '';
}
return `${resolveLeadsText('hotBadge')} ${unread}`;
}
export function statusLabel(status: string): string {
if (status === 'new') {
return resolveLeadsText('statusNew');
}
if (status === 'contacted') {
return resolveLeadsText('statusContacted');
}
if (status === 'hot') {
return resolveLeadsText('statusHot');
}
if (status === 'won') {
return resolveLeadsText('statusWon');
}
if (status === 'archived') {
return resolveLeadsText('statusArchived');
}
return status;
}
export function sourceLabel(source: string): string {
if (source === 'nfc') {
return resolveLeadsText('sourceNfc');
}
if (source === 'qr') {
return resolveLeadsText('sourceQr');
}
if (source === 'link') {
return resolveLeadsText('sourceLink');
}
if (source === 'ai_chat') {
return resolveLeadsText('sourceAi');
}
return source;
}
export function formatLeadInitials(name: string): string {
const trimmed = name.trim();
if (trimmed === '') {
return 'L';
}
const parts = trimmed.split(' ');
if (parts.length >= 2) {
const first = parts[0].length > 0 ? parts[0].charAt(0) : '';
const second = parts[1].length > 0 ? parts[1].charAt(0) : '';
return (first + second).toUpperCase();
}
return trimmed.substring(0, Math.min(2, trimmed.length)).toUpperCase();
}
export function formatLeadCompany(company: string | null | undefined): string {
if (company == null) {
return '';
}
return company;
}
export function sanitizeLeadQuery(query: LoadLeadsQuery): OwnerPageQuery {
const next: OwnerPageQuery = {
page: query.page,
size: query.size,
};
const status = parseLeadStatus(query.status);
if (status != null) {
next.status = status;
}
if (query.keyWord != null) {
const word = query.keyWord.trim();
if (word !== '') {
next.keyWord = word;
}
}
return next;
}
function readErrorMessage(err: unknown): string {
if (err instanceof Error && err.message !== '') {
return err.message;
}
return resolveLeadsText('error');
}
/**
* 拉取潜客分页。可注入 fetchOwnerLeadPage;非法 status 不入请求
*/
export async function loadLeads(
query: LoadLeadsQuery,
api?: LoadLeadsApi
): Promise<LoadLeadsResult> {
const fetchPage =
api != null ? api.fetchOwnerLeadPage : fetchOwnerLeadPage;
try {
const data = await fetchPage(sanitizeLeadQuery(query));
const list = data.list != null ? data.list : [];
const pagination =
data.pagination != null
? data.pagination
: { page: query.page, size: query.size, total: list.length };
return {
list,
pagination,
empty: list.length === 0,
errorMessage: '',
};
} catch (err) {
return {
list: [],
pagination: { page: query.page, size: query.size, total: 0 },
empty: false,
errorMessage: readErrorMessage(err),
};
}
}
export function createLeadsState(): LeadsState {
return {
list: [],
radarTracks: [],
hotUnread: 0,
page: 1,
size: 20,
total: 0,
status: '',
keyWord: '',
loading: false,
empty: false,
errorMessage: '',
};
}
export function applyLeadsOptions(
state: LeadsState,
options: LeadsPageOptions | null
): void {
if (options == null) {
return;
}
const status = parseLeadStatus(options.status);
state.status = status != null ? status : '';
if (options.keyWord != null) {
state.keyWord = options.keyWord;
}
}
export function applyLeadStatusFilter(state: LeadsState, status: string): void {
const parsed = parseLeadStatus(status);
state.status = parsed != null ? parsed : '';
state.page = 1;
}
export function applyLoadResult(state: LeadsState, result: LoadLeadsResult): void {
state.list = result.list;
state.page = result.pagination.page;
state.size = result.pagination.size;
state.total = result.pagination.total;
state.empty = result.empty;
state.errorMessage = result.errorMessage;
}
export async function refreshLeads(
state: LeadsState,
api?: LoadLeadsApi
): Promise<LoadLeadsResult> {
if (state.loading) {
return {
list: state.list,
pagination: {
page: state.page,
size: state.size,
total: state.total,
},
empty: state.empty,
errorMessage: state.errorMessage,
};
}
state.loading = true;
state.errorMessage = '';
const result = await loadLeads(
{
page: state.page,
size: state.size,
status: state.status,
keyWord: state.keyWord,
},
api
);
applyLoadResult(state, result);
try {
const radar = await fetchOwnerRadarPage({ page: 1, size: 3 });
state.radarTracks = radar.list != null ? radar.list : [];
} catch (_err) {
state.radarTracks = [];
}
try {
const inbox = await fetchRadarHotInbox();
state.hotUnread = inbox != null && inbox.unread != null ? inbox.unread : 0;
} catch (_err) {
state.hotUnread = 0;
}
state.loading = false;
return result;
}
export function formatRadarLine(track: RadarTrack): string {
const place = formatLocation(track.country, track.city);
const stay = formatStay(track.stayDurationMs);
if (place !== '') {
return `${place} · ${stay}`;
}
return stay;
}
export function openRadarPage(): void {
uni.navigateTo({
url: '/pages/owner/radar',
});
}
export function buildLeadDetailUrl(id: string | number): string {
return `/pages/owner/leads-detail?id=${id}`;
}
export function openLeadDetail(id: string | number): void {
uni.navigateTo({
url: buildLeadDetailUrl(id),
});
}