81 lines
1.7 KiB
TypeScript
81 lines
1.7 KiB
TypeScript
import { clearCardViewHistory, getCardViewHistory } from '../../utils/history';
|
|
import { formatTime } from '../../utils/util';
|
|
import type { Lawyer } from '../../types/card';
|
|
|
|
interface HistoryCardItem {
|
|
lawyer: Lawyer;
|
|
viewedAt: number;
|
|
timeText: string;
|
|
}
|
|
|
|
function createFallbackLawyer(lawyerId: string): Lawyer {
|
|
return {
|
|
id: lawyerId,
|
|
name: '历史浏览名片',
|
|
title: '',
|
|
office: '',
|
|
phone: '',
|
|
email: '',
|
|
address: '',
|
|
avatar: '',
|
|
coverImage: '',
|
|
specialties: [],
|
|
bio: '',
|
|
wechatQrImage: '',
|
|
};
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
items: [] as HistoryCardItem[],
|
|
},
|
|
|
|
onShow() {
|
|
this.loadHistory();
|
|
},
|
|
|
|
loadHistory() {
|
|
const history = getCardViewHistory();
|
|
if (!history.length) {
|
|
this.setData({ items: [] });
|
|
return;
|
|
}
|
|
|
|
const items = history
|
|
.map((record) => ({
|
|
lawyer: record.lawyer || createFallbackLawyer(record.lawyerId),
|
|
viewedAt: record.viewedAt,
|
|
timeText: formatTime(new Date(record.viewedAt)),
|
|
}));
|
|
|
|
this.setData({ items });
|
|
},
|
|
|
|
handleLawyerSelect(event: WechatMiniprogram.CustomEvent<{ id: string }>) {
|
|
const lawyerId = event.detail.id;
|
|
if (!lawyerId) {
|
|
return;
|
|
}
|
|
wx.navigateTo({
|
|
url: `/pages/lawyer-detail/index?id=${lawyerId}`,
|
|
});
|
|
},
|
|
|
|
clearHistory() {
|
|
if (!this.data.items.length) {
|
|
return;
|
|
}
|
|
wx.showModal({
|
|
title: '清空记录',
|
|
content: '确定清空所有名片查看记录吗?',
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
clearCardViewHistory();
|
|
this.setData({ items: [] });
|
|
wx.showToast({ title: '已清空', icon: 'success' });
|
|
}
|
|
},
|
|
});
|
|
},
|
|
});
|