import { defineStore } from 'pinia' import { store } from '@/store' export interface IPatient { name: string gender?: number idCard?: string status?: string age?: number sex?: string time?: string } export interface ServiceState { currentPatient: null | IPatient device: null | { deviceNo: string; host: string } } export const useServiceStore = defineStore({ id: 'service', state: (): ServiceState => ({ currentPatient: null, // 当前病友的信息 device: null, // 当前设备的信息 }), getters: { deviceNo: ({ device }) => device?.deviceNo || '', host: ({ device }) => device?.host || '', }, actions: { setCurrentPatient(patient: IPatient) { this.currentPatient = patient }, // 设置设备信息 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) }