import { defineStore } from 'pinia' import { store } from '@/store' export interface IPatient { demoFlag: number firstAidId: string firstAidStatus: string firstAidTime: string firstAidZlType: number patientAge: null | number patientGender: number patientIdCardNo: string patientName: string patientNation: string sourceId: string sourceType: number recordValDict: any } // 平车信息 export interface ICar { carNo: string online: number // 0 离线 1在线 powerLowAlarm: number // 0 正常 1 报警 sensorAngleV: null | number // 角度 sensorPower: null | string // 电量百分比 sensorVibrate: null | string // 振幅 sensorWeight: null | string // 重量 } export interface ServiceState { currentPatient: null | IPatient currentCar: null | ICar device: null | { deviceNo: string; host: string } } export const useServiceStore = defineStore({ id: 'service', state: (): ServiceState => ({ currentPatient: null, // 当前病友的信息 device: null, // 当前设备的信息 currentCar: null, // 车的信息 }), getters: { deviceNo: ({ device }) => device?.deviceNo || '', host: ({ device }) => device?.host || '', carNo: ({ currentCar }) => currentCar?.carNo || '', // 平车编号 currentFirstAidId: ({ currentPatient }) => currentPatient?.firstAidId, // 获取急救信息 code 对应的answer getCodeValue({ currentPatient }) { return (code: string) => { if (!currentPatient?.recordValDict || !currentPatient.recordValDict[code] || !currentPatient.recordValDict[code][0]) { return '' } return currentPatient?.recordValDict[code][0]?.answer[0] || '' } }, }, actions: { setCurrentPatient(patient: IPatient | null) { this.currentPatient = patient }, setCurrentCar(car: ICar | null) { this.currentCar = car || null }, // 设置设备信息 setDevice(device: null | { deviceNo: string; host: string }) { this.device = device if (device?.deviceNo) { uni.setStorageSync(uni.$u.LOCAL_KEY.DEVICE_NO, device.deviceNo) } if (device?.host) { uni.setStorageSync(uni.$u.LOCAL_KEY.HOST, device.host) } }, // sync local device info 2 store // 应用启动的时候调用一次 loading页调用了 syncDevice() { const localDeviceNo = uni.getStorageSync(uni.$u.LOCAL_KEY.DEVICE_NO) const localDeviceHost = uni.getStorageSync(uni.$u.LOCAL_KEY.HOST) if (localDeviceNo && localDeviceHost) { this.device = { deviceNo: localDeviceNo, host: localDeviceHost } } else { this.device = null } }, }, }) // Need to be used outside the setup export function useServiceStoreWidthOut() { return useServiceStore(store) }