Files
陈子默 9605384edc feat: 搭建微信小程序展示端
- 初始化小程序工程配置与类型声明

- 增加首页、律所、律师列表、详情与历史页面

- 补充公共组件、运行时配置与示例素材
2026-03-20 12:44:31 +08:00

66 lines
2.3 KiB
TypeScript

import { CARD_VIEW_HISTORY_KEY } from '../constants/storage';
import type { CardViewRecord, Lawyer } from '../types/card';
function normalizeLawyerSnapshot(input: unknown): Lawyer | undefined {
if (!input || typeof input !== 'object') {
return undefined;
}
const lawyer = input as Partial<Lawyer>;
if (typeof lawyer.id !== 'string' || !lawyer.id) {
return undefined;
}
return {
id: lawyer.id,
name: typeof lawyer.name === 'string' ? lawyer.name : '',
title: typeof lawyer.title === 'string' ? lawyer.title : '',
office: typeof lawyer.office === 'string' ? lawyer.office : '',
phone: typeof lawyer.phone === 'string' ? lawyer.phone : '',
email: typeof lawyer.email === 'string' ? lawyer.email : '',
address: typeof lawyer.address === 'string' ? lawyer.address : '',
avatar: typeof lawyer.avatar === 'string' ? lawyer.avatar : '',
coverImage: typeof lawyer.coverImage === 'string' ? lawyer.coverImage : '',
specialties: Array.isArray(lawyer.specialties)
? lawyer.specialties.filter((item): item is string => typeof item === 'string')
: [],
bio: typeof lawyer.bio === 'string' ? lawyer.bio : '',
wechatQrImage: typeof lawyer.wechatQrImage === 'string' ? lawyer.wechatQrImage : '',
};
}
export function getCardViewHistory(): CardViewRecord[] {
const raw = wx.getStorageSync(CARD_VIEW_HISTORY_KEY);
if (!Array.isArray(raw)) {
return [];
}
return raw
.map((item) => {
const record = item as { lawyerId?: unknown; viewedAt?: unknown; lawyer?: unknown };
const lawyerId = typeof record.lawyerId === 'string' ? record.lawyerId : '';
const viewedAt = typeof record.viewedAt === 'number' ? record.viewedAt : 0;
const lawyer = normalizeLawyerSnapshot(record.lawyer);
return { lawyerId, viewedAt, lawyer };
})
.filter((item) => Boolean(item.lawyerId) && item.viewedAt > 0)
.sort((a, b) => b.viewedAt - a.viewedAt);
}
export function appendCardViewRecord(lawyer: Lawyer): void {
if (!lawyer.id) {
return;
}
const current = getCardViewHistory().filter((item) => item.lawyerId !== lawyer.id);
current.unshift({
lawyerId: lawyer.id,
viewedAt: Date.now(),
lawyer,
});
wx.setStorageSync(CARD_VIEW_HISTORY_KEY, current);
}
export function clearCardViewHistory(): void {
wx.removeStorageSync(CARD_VIEW_HISTORY_KEY);
}