// components/patient-info/index.js // 患者/健康档案两行信息项组件 // 第一行:姓名 + 性别(左) · 年龄(右) // 第二行:电话(左,可选拨打) · 所在地(右) // // 三种用法: // 1. 直接传展示字段(name/sexLabel/ageText/mobile/locationText),适用于列表批量渲染 // 2. 只传 profile-id,组件内部加载档案:优先读全局缓存(utils/store.js),未命中再请求 // 加载完成后触发 loaded 事件(携带档案原始文档),展示字段以加载结果为准 // 3. 传 snapshot(订单 patient 快照):档案不可读(陪诊员无权限 / 档案已删除)时兜底展示, // 避免患者姓名电话为空 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 }, // 订单患者快照(escort_record.patient):档案加载失败时的兜底 snapshot: { type: Object, value: null, observer(newVal) { if (newVal) this.applySnapshot(newVal) } }, // 是否允许读取完整档案:陪诊员传 false,避免必然的 403 与信息越权 canLoadProfile: { type: Boolean, value: true }, // 健康档案ID:传入后组件内部加载,展示字段以加载结果为准 profileId: { type: String, value: '', observer(newVal) { if (!newVal) return if (this.data.canLoadProfile) this.loadProfile(newVal) else if (this.data.snapshot) this.applySnapshot(this.data.snapshot) } }, // 是否读取缓存:true 缓存优先(默认),false 直接请求最新数据(请求成功后仍回填缓存) useCache: { type: Boolean, value: true } }, lifetimes: { attached() { if (this.data.snapshot) this.applySnapshot(this.data.snapshot) if (this.data.profileId && this.data.canLoadProfile) { this.loadProfile(this.data.profileId) } } }, methods: { onCallTap() { if (!this.data.mobile) return this.triggerEvent('call', { mobile: this.data.mobile }) }, /** * 按档案ID加载:缓存优先,未命中再请求;失败时回落到订单快照 */ loadProfile(id) { // 陪诊员没有档案读取权限:即使 observer 早于属性赋值触发也直接走快照 if (!this.data.canLoadProfile) { this.applySnapshot(this.data.snapshot) return } 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) { this.applySnapshot(this.data.snapshot) return } const profile = res.data.profile store.cacheProfile(profile) this.applyProfile(profile) }) .catch(err => { console.error('获取健康档案失败', err) this.applySnapshot(this.data.snapshot) }) }, /** * 将档案文档映射为展示字段,并抛出 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 }) }, /** * 订单快照兜底:只在档案不可读时使用,因此不触发 loaded 事件 */ applySnapshot(snapshot) { const p = snapshot || {} if (!p.name && !p.mobile) return 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 || '' }) } } })