Files
zyt/admin/local-preview/modules/people.mjs
T

69 lines
3.9 KiB
JavaScript

// Synthetic *accounts*, not patients. Only this local adapter knows about the demo cross-links.
const channels = ['微信小程序', '微信公众号', '手机H5', '电脑PC', '苹果APP', '安卓APP']
const aliases = ['青竹', '晴川', '听雨', '小满', '晨光', '山月', '晚舟']
const ok = data => ({ code: 1, msg: '', show: 0, data })
export function memberIdForPatient(patientId) {
if (!['number', 'string'].includes(typeof patientId) || !/^\d+$/.test(String(patientId))) return null
const id = Number(patientId)
return id > 0 && Number.isSafeInteger(70000 + id) ? 70000 + id : null
}
function positiveInt(raw, fallback, max = Number.MAX_SAFE_INTEGER) {
const n = Number(raw)
return Number.isSafeInteger(n) && n > 0 ? Math.min(n, max) : fallback
}
function dateBefore(day, offset) {
const date = new Date(day + 'T00:00:00Z')
date.setUTCDate(date.getUTCDate() - offset)
return date.toISOString().slice(0, 10)
}
function accounts({ patients, orders, appointments = [] }) {
// The order snapshot carries the fixed local demo day; never call out for identity or avatar data.
const anchor = orders[0]?.create_time.slice(0, 10) || '2026-01-31'
const orderCounts = new Map()
for (const order of orders) orderCounts.set(order.patient_id, (orderCounts.get(order.patient_id) || 0) + 1)
const appointmentCounts = new Map()
for (const appt of appointments) appointmentCounts.set(appt.patient_id, (appointmentCounts.get(appt.patient_id) || 0) + 1)
return patients.map((patient, i) => {
const ordinal = String(i + 1).padStart(2, '0')
const id = memberIdForPatient(patient.id)
return {
id, sn: 'U-DEMO-' + patient.id, nickname: aliases[i % aliases.length] + '·示例' + ordinal,
avatar: '', account: 'demo_user_' + ordinal, mobile: patient.phone,
channel: channels[i % channels.length], sex: ['未知', '女', '男'][i % 3],
create_time: dateBefore(anchor, patients.length + 10 - i) + ' 08:30:00',
login_time: dateBefore(anchor, i % 5 + 1) + ' 09:15:00',
real_name: i % 4 ? '模拟姓名' + ordinal : '', user_money: (i % 4 * 15.5).toFixed(2),
// Preview-only navigation context. Account balances are not derived from patient orders.
patient_id: patient.id, patient_name: patient.patient_name,
order_count: orderCounts.get(patient.id) || 0, appointment_count: appointmentCounts.get(patient.id) || 0
}
}).sort((a, b) => b.id - a.id)
}
export function resolvePeople(pathname, params, snapshot) {
if (!['/adminapi/user.user/lists', '/adminapi/user.user/detail'].includes(pathname)) return null
const q = params instanceof URLSearchParams ? Object.fromEntries(params) : params || {}
if (q.export != null) return null
const members = accounts(snapshot)
if (pathname === '/adminapi/user.user/detail') {
const row = members.find(u => u.id === Number(q.id))
return row ? ok(row) : { code: 0, data: null, show: 1, msg: '未找到该演示账号' }
}
const term = String(q.keyword ?? '').trim().toLowerCase()
const end = q.create_time_end?.length === 10 ? q.create_time_end + ' 23:59:59' : q.create_time_end
const rows = members.filter(u =>
(!term || [u.sn, u.account, u.nickname, u.mobile].some(s => s.toLowerCase().includes(term))) &&
(!q.channel || u.channel === channels[Number(q.channel) - 1]) &&
(!q.create_time_start || u.create_time >= q.create_time_start) &&
(!end || u.create_time <= end)
)
const size = positiveInt(q.page_size, 15, 100), page = positiveInt(q.page_no, 1)
const lists = rows.slice((page - 1) * size, page * size).map(({ id, sn, nickname, sex, avatar, account, mobile, channel, create_time }) =>
({ id, sn, nickname, sex, avatar, account, mobile, channel, create_time }))
return ok({ count: rows.length, lists, extend: {} })
}