import { get, post } from './http' export const SPECIAL_PRESCRIPTION_STORAGE_KEY = 'special_prescription_register_context' /** * 首页特色方配置 * @param params {{ store_id?: number|string }} */ export async function getSpecialPrescriptionHomeApi(params) { return await get('/special-prescription/home', params, 3) } /** * 特色方列表页配置 * @param params {{ store_id?: number|string }} */ export async function getSpecialPrescriptionListPageApi(params) { return await get('/special-prescription/list-page', params, 3) } /** * 特色方列表 * @param params {{ store_id?: number|string, category_id?: number, name?: string, page?: number, pageSize?: number }} */ export async function getSpecialPrescriptionListApi(params) { return await get('/special-prescription/list', params, 3) } /** * 特色方详情 * @param params {{ id: number|string, store_id?: number|string }} */ export async function getSpecialPrescriptionDetailApi(params) { return await get('/special-prescription/detail', params, 3) } /** * 特色方分类 */ export async function getSpecialPrescriptionCategoriesApi(params = {}) { return await get('/special-prescription/categories', params, 3) } /** * 挂号成功后创建选方记录 * @param params {{ register_id, special_prescription_id, dose_count, store_id, doctor_id }} */ export async function createSpecialPrescriptionPatientRecordApi(params) { return await post('/special-prescription/create-patient-record', params, 3) } /** * 保存特色方挂号上下文(merge 已有值,避免后续页面未传 sku_id 时覆盖为 0) * @param ctx {{ special_prescription_id, dose_count?, sku_id?, doctor_id? }} */ export function saveSpecialPrescriptionRegisterContext(ctx) { if (!ctx || !ctx.special_prescription_id) return const prev = getSpecialPrescriptionRegisterContext() || {} const merged = { special_prescription_id: ctx.special_prescription_id, dose_count: ctx.dose_count != null ? ctx.dose_count : (prev.dose_count || 1), doctor_id: ctx.doctor_id != null ? ctx.doctor_id : (prev.doctor_id || ''), } // 未传 sku_id 字段时保留 storage 已有值,防止中间页覆盖为 0 if (Object.prototype.hasOwnProperty.call(ctx, 'sku_id')) { merged.sku_id = Number(ctx.sku_id) || 0 } else { merged.sku_id = Number(prev.sku_id) || 0 } uni.setStorageSync(SPECIAL_PRESCRIPTION_STORAGE_KEY, merged) } export function getSpecialPrescriptionRegisterContext() { return uni.getStorageSync(SPECIAL_PRESCRIPTION_STORAGE_KEY) || null } export function clearSpecialPrescriptionRegisterContext() { uni.removeStorageSync(SPECIAL_PRESCRIPTION_STORAGE_KEY) } /** * 挂号成功后绑定特色方选方记录(幂等,失败不阻断流程) * @param registerId */ export async function tryCreateSpecialPrescriptionPatientRecord(registerId) { if (!registerId) return const ctx = getSpecialPrescriptionRegisterContext() if (!ctx || !ctx.special_prescription_id) return try { const res = await createSpecialPrescriptionPatientRecordApi({ register_id: registerId, special_prescription_id: ctx.special_prescription_id, dose_count: ctx.dose_count || 1, sku_id: ctx.sku_id || 0, store_id: uni.getStorageSync('store_id') || '11001', doctor_id: ctx.doctor_id, }) if (res.data?.code === 0 || res.data?.code === '0') { clearSpecialPrescriptionRegisterContext() } } catch (e) { console.error('tryCreateSpecialPrescriptionPatientRecord', e) } }