// components/patient-info/index.js // 患者/健康档案两行信息项组件 // 第一行:姓名 + 性别(左) · 年龄(右) // 第二行:电话(左,可选拨打) · 所在地(右) // // 两种用法: // 1. 直接传展示字段(name/sexLabel/ageText/mobile/locationText),适用于列表批量渲染 // 2. 只传 profile-id,组件内部加载档案:优先读全局缓存(utils/store.js),未命中再请求 // 加载完成后触发 loaded 事件(携带档案原始文档),展示字段以加载结果为准 const store = require('../../utils/store.js') const { calcAge } = require('../../utils/format.js') Component({ properties: { name: { type: String, value: '' }, sexLabel: { type: String, value: '' }, ageText: { type: String, value: '' }, mobile: { type: String, value: '' }, locationText: { type: String, value: '' }, // 是否显示"拨打"并触发 call 事件 dial: { type: Boolean, value: false }, // 健康档案ID:传入后组件内部加载,展示字段以加载结果为准 profileId: { type: String, value: '', observer(newVal) { if (newVal) this.loadProfile(newVal) } }, // 是否读取缓存:true 缓存优先(默认),false 直接请求最新数据(请求成功后仍回填缓存) useCache: { type: Boolean, value: true } }, methods: { onCallTap() { if (!this.data.mobile) return this.triggerEvent('call', { mobile: this.data.mobile }) }, /** * 按档案ID加载:缓存优先,未命中再请求 */ loadProfile(id) { const cached = store.getProfile(id) if (cached) { this.applyProfile(cached) return } const API = require('../../utils/api.js') API.healthProfile.getProfileById(id) .then(res => { if (res.code !== 0 || !res.data || !res.data.profile) return const profile = res.data.profile store.cacheProfile(profile) this.applyProfile(profile) }) .catch(err => { console.error('获取健康档案失败', err) }) }, /** * 将档案文档映射为展示字段,并抛出 loaded 事件 */ applyProfile(profile) { const p = profile.profile || {} const loc = profile.location || {} const age = calcAge(p.birth) const sexLabel = p.sex === 'male' ? '男' : (p.sex === 'female' ? '女' : '') this.setData({ name: p.name || '', sexLabel, ageText: age ? `${age}岁` : '', mobile: p.mobile || '', locationText: [loc.province, loc.city].filter(Boolean).join(' ') }) this.triggerEvent('loaded', { profile }) } } })