import { getLawyerDetail, recordCardShare } from '../../api/open'; import type { FirmInfo } from '../../types/card'; import type { Lawyer } from '../../types/card'; import { appendCardViewRecord } from '../../utils/history'; function createEmptyFirm(): FirmInfo { return { id: '', name: '', logo: '', intro: '', hotlinePhone: '', hqAddress: '', hqLatitude: 0, hqLongitude: 0, officeCount: 0, lawyerCount: 0, heroImage: '', officeList: [], practiceAreas: [], }; } function buildDetailBgStyle(coverImage?: string): string { const cover = typeof coverImage === 'string' ? coverImage.trim() : ''; if (!cover) { return ''; } return `background-image: url("${cover}"); background-size: cover; background-position: center;`; } function shouldUseBioScroll(bio?: string): boolean { const text = typeof bio === 'string' ? bio.trim() : ''; return text.length > 180; } Page({ data: { firm: createEmptyFirm(), lawyer: null as Lawyer | null, notFound: false, specialtiesText: '', detailBgStyle: '', bioScrollable: false, loading: false, }, async onLoad(options: Record) { const lawyerId = typeof options.id === 'string' ? options.id : ''; const sourceType = typeof options.sourceType === 'string' ? options.sourceType : 'DIRECT'; const shareFromCardId = typeof options.shareFromCardId === 'string' ? options.shareFromCardId : ''; if (!lawyerId) { this.setData({ notFound: true, specialtiesText: '', detailBgStyle: '', bioScrollable: false }); return; } this.setData({ loading: true }); try { const payload = await getLawyerDetail(lawyerId, sourceType, shareFromCardId); this.setData({ firm: payload.firm, lawyer: payload.lawyer, notFound: false, specialtiesText: payload.lawyer.specialties.join(';'), detailBgStyle: buildDetailBgStyle(payload.lawyer.coverImage), bioScrollable: shouldUseBioScroll(payload.lawyer.bio), }); appendCardViewRecord(payload.lawyer); wx.showShareMenu({ menus: ['shareAppMessage'] }); } catch (error) { const message = error instanceof Error ? error.message : '律师信息不存在'; this.setData({ notFound: true, specialtiesText: '', detailBgStyle: '', bioScrollable: false }); wx.showToast({ title: message, icon: 'none', }); } finally { this.setData({ loading: false }); } }, handleDockAction(event: WechatMiniprogram.CustomEvent<{ action: string }>) { const action = event.detail.action; switch (action) { case 'save': this.saveContact(); break; case 'location': this.navigateOffice(); break; default: break; } }, saveContact() { const lawyer = this.data.lawyer; if (!lawyer) { return; } wx.addPhoneContact({ firstName: lawyer.name, mobilePhoneNumber: lawyer.phone, email: lawyer.email, organization: this.data.firm.name, title: lawyer.title, workAddressStreet: lawyer.address, remark: lawyer.specialties.join('、'), success: () => { wx.showToast({ title: '已添加到通讯录', icon: 'success' }); }, fail: () => { wx.showToast({ title: '添加失败', icon: 'none' }); }, }); }, callPhone() { const phone = this.data.lawyer ? this.data.lawyer.phone : ''; if (!phone) { wx.showToast({ title: '暂无联系电话', icon: 'none' }); return; } wx.makePhoneCall({ phoneNumber: phone, fail: () => { wx.showToast({ title: '拨号失败', icon: 'none' }); }, }); }, navigateOffice() { if (!this.data.firm.hqLatitude || !this.data.firm.hqLongitude) { wx.showToast({ title: '暂未配置地图位置', icon: 'none' }); return; } wx.openLocation({ latitude: this.data.firm.hqLatitude, longitude: this.data.firm.hqLongitude, name: this.data.firm.name, address: this.data.firm.hqAddress, scale: 18, fail: () => { wx.showToast({ title: '打开地图失败', icon: 'none' }); }, }); }, previewWechatQr() { const url = this.data.lawyer ? this.data.lawyer.wechatQrImage : ''; if (!url) { wx.showToast({ title: '二维码未配置', icon: 'none' }); return; } wx.previewImage({ current: url, urls: [url], }); }, goLawyerList() { wx.navigateTo({ url: '/pages/lawyer-list/index' }); }, onShareAppMessage() { const lawyer = this.data.lawyer; if (!lawyer) { return { title: `${this.data.firm.name || '电子名片'}电子名片`, path: '/pages/firm/index', }; } const sharePath = `/pages/lawyer-detail/index?id=${lawyer.id}&sourceType=SHARE&shareFromCardId=${lawyer.id}`; recordCardShare(lawyer.id, sharePath).catch(() => { // 分享埋点失败不阻断分享动作 }); return { title: `${lawyer.name}律师电子名片`, path: sharePath, imageUrl: lawyer.avatar, }; }, });