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

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

424 行
9.7 KiB
Plaintext

/**
* OmniCard AI (CRMmp) - 名片主移动名片中心业务驱动层
* 分页拉自己的名片、空态与错误、编辑 / 海报跳转
*/
import {
CardProfile,
CardStatus,
OwnerPageData,
OwnerPagePagination,
OwnerPageQuery,
} from '../../../common/types.uts';
import { fetchOwnerCardPage } from '../../../common/api.uts';
import { getCurrentLocale, getOmniText } from '../../../common/locale.uts';
import { setOwnerCardIds } from '../../portal/usePortal.uts';
export type CardHomeItem = {
id?: string | number;
name: string;
title?: string;
company?: string;
status?: CardStatus;
slug?: string;
};
export type CardHomeCopyKey =
| 'title'
| 'subtitle'
| 'searchPlaceholder'
| 'refresh'
| 'share'
| 'preview'
| 'poster'
| 'empty'
| 'error'
| 'loading'
| 'statusDraft'
| 'statusPublished'
| 'statusArchived'
| 'quotaExceeded'
| 'scanGuide'
| 'linkLabel'
| 'copy'
| 'emptyDesc'
| 'cases'
| 'articles'
| 'linkEmpty'
| 'switchTitle'
| 'quotaBadge'
| 'emptyHeading';
export type LoadCardsQuery = {
page: number;
size: number;
keyWord?: string;
};
export type LoadCardsApi = {
fetchOwnerCardPage: (
query: OwnerPageQuery
) => Promise<OwnerPageData<CardProfile>>;
};
export type LoadCardsResult = {
list: CardHomeItem[];
pagination: OwnerPagePagination;
empty: boolean;
errorMessage: string;
};
export type CardHomeState = {
list: CardHomeItem[];
page: number;
size: number;
total: number;
keyWord: string;
loading: boolean;
empty: boolean;
errorMessage: string;
};
export type CardHomePageOptions = {
keyWord?: string;
page?: string;
size?: string;
};
function isZhLocale(): boolean {
return getCurrentLocale() === 'zh-CN';
}
/**
* 页面文案:分享走词典,标题 / 刷新 / 状态用本地词典
*/
export function resolveCardHomeText(key: CardHomeCopyKey): string {
const locale = getCurrentLocale();
if (key === 'share') {
return getOmniText(locale, 'card.share');
}
if (key === 'preview') {
return getOmniText(locale, 'card.view_profile');
}
if (key === 'empty') {
return getOmniText(locale, 'common.empty');
}
if (key === 'error') {
return getOmniText(locale, 'common.error');
}
if (key === 'loading') {
return getOmniText(locale, 'common.loading');
}
const zh = isZhLocale();
if (key === 'poster') {
return zh ? '出示名片' : 'Show card';
}
if (key === 'title') {
return zh ? '我的名片' : 'My cards';
}
if (key === 'subtitle') {
return zh ? '预览微官网或分享当前名片' : 'Preview the site or share a card';
}
if (key === 'searchPlaceholder') {
return zh ? '搜索姓名、职位或公司' : 'Search name, title or company';
}
if (key === 'refresh') {
return zh ? '刷新' : 'Refresh';
}
if (key === 'statusDraft') {
return zh ? '草稿' : 'Draft';
}
if (key === 'statusPublished') {
return zh ? '已发布' : 'Published';
}
if (key === 'statusArchived') {
return zh ? '已归档' : 'Archived';
}
if (key === 'quotaExceeded') {
return zh ? '名片配额已满,升级后可继续创建' : 'Card quota is full. Upgrade to create more.';
}
if (key === 'scanGuide') {
return zh ? '扫码即可保存这张名片' : 'Scan to save this card';
}
if (key === 'linkLabel') {
return zh ? '名片链接' : 'Card link';
}
if (key === 'copy') {
return zh ? '复制' : 'Copy';
}
if (key === 'emptyDesc') {
return zh ? '创建第一张名片后,就可以在这里出示' : 'Create a card, then show it here';
}
if (key === 'cases') {
return zh ? '相册' : 'Gallery';
}
if (key === 'articles') {
return zh ? '动态' : 'Posts';
}
if (key === 'linkEmpty') {
return zh ? '暂无链接' : 'No link yet';
}
if (key === 'switchTitle') {
return zh ? '选择名片' : 'Choose a card';
}
if (key === 'quotaBadge') {
return zh ? '配额已满' : 'Quota full';
}
if (key === 'emptyHeading') {
return zh ? '还没有名片' : 'No cards yet';
}
return '';
}
export function statusLabel(status: string | null | undefined): string {
if (status === 'draft') {
return resolveCardHomeText('statusDraft');
}
if (status === 'published') {
return resolveCardHomeText('statusPublished');
}
if (status === 'archived') {
return resolveCardHomeText('statusArchived');
}
return status != null ? status : '';
}
export function formatCardField(value: string | null | undefined): string {
if (value == null) {
return '';
}
return value;
}
function readOptionalSlug(card: CardProfile): string {
const extra = card as CardHomeItem;
if (extra.slug == null) {
return '';
}
return extra.slug.trim();
}
export function toCardHomeItem(card: CardProfile): CardHomeItem {
const slug = readOptionalSlug(card);
return {
id: card.id,
name: card.name,
title: card.title,
company: card.company,
status: card.status,
slug: slug !== '' ? slug : undefined,
};
}
export function sanitizeCardQuery(query: LoadCardsQuery): OwnerPageQuery {
const next: OwnerPageQuery = {
page: query.page,
size: query.size,
};
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 resolveCardHomeText('error');
}
/**
* 拉取名片分页。可注入 fetchOwnerCardPage
*/
export async function loadCards(
query: LoadCardsQuery,
api?: LoadCardsApi
): Promise<LoadCardsResult> {
const fetchPage =
api != null ? api.fetchOwnerCardPage : fetchOwnerCardPage;
try {
const data = await fetchPage(sanitizeCardQuery(query));
const rawList = data.list != null ? data.list : [];
const list: CardHomeItem[] = [];
const ownerIds: string[] = [];
for (let i = 0; i < rawList.length; i++) {
const item = toCardHomeItem(rawList[i]);
list.push(item);
if (item.id != null) {
ownerIds.push(String(item.id));
}
if (item.slug != null && item.slug !== '') {
ownerIds.push(item.slug);
}
}
if (ownerIds.length > 0) {
setOwnerCardIds(ownerIds);
}
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 createCardHomeState(): CardHomeState {
return {
list: [],
page: 1,
size: 20,
total: 0,
keyWord: '',
loading: false,
empty: false,
errorMessage: '',
};
}
export function applyCardHomeOptions(
state: CardHomeState,
options: CardHomePageOptions | null
): void {
if (options == null) {
return;
}
if (options.keyWord != null) {
state.keyWord = options.keyWord;
}
}
export function applyLoadResult(
state: CardHomeState,
result: LoadCardsResult
): 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 refreshCards(
state: CardHomeState,
api?: LoadCardsApi
): Promise<LoadCardsResult> {
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 loadCards(
{
page: state.page,
size: state.size,
keyWord: state.keyWord,
},
api
);
applyLoadResult(state, result);
state.loading = false;
return result;
}
export function buildEditUrl(id: string | number): string {
return `/pages/owner/card/edit?id=${id}`;
}
export function buildQrcodeUrl(card: CardHomeItem): string {
if (card.slug != null && card.slug !== '') {
return `/pages/owner/card/qrcode?slug=${card.slug}`;
}
if (card.id != null) {
return `/pages/owner/card/qrcode?id=${card.id}`;
}
return '/pages/owner/card/qrcode?id=';
}
export function openCardEdit(id: string | number): void {
uni.navigateTo({
url: buildEditUrl(id),
});
}
export function openCardQrcode(card: CardHomeItem): void {
uni.navigateTo({
url: buildQrcodeUrl(card),
});
}
export function buildPortalPreviewUrl(card: CardHomeItem): 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: CardHomeItem): void {
uni.navigateTo({
url: buildPortalPreviewUrl(card),
});
}
export function pickShareCard(list: CardHomeItem[]): CardHomeItem | null {
for (let i = 0; i < list.length; i++) {
if (list[i].status === 'published') {
return list[i];
}
}
if (list.length > 0) {
return list[0];
}
return null;
}
export function formatSvgDataUri(svg: string | null | undefined): string {
if (svg == null) {
return '';
}
const trimmed = svg.trim();
if (trimmed === '') {
return '';
}
if (
trimmed.indexOf('http://') === 0 ||
trimmed.indexOf('https://') === 0 ||
trimmed.indexOf('data:') === 0
) {
return trimmed;
}
if (trimmed.indexOf('<svg') === 0) {
return 'data:image/svg+xml;utf8,' + encodeURIComponent(trimmed);
}
return trimmed;
}