Files
ipad/src/utils/examActions.ts
2025-12-25 17:27:01 +08:00

191 lines
4.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 体检操作记录工具函数
* 使用格式yh_exam_actions_${today}_${examId}
* 存储内容:{ idCardSignIn: boolean, consentSign: boolean, printSign: boolean, timestamp: string }
*/
/**
* 获取今天的日期字符串YYYY-MM-DD
*/
export const getTodayString = (): string => {
const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
};
/**
* 获取操作记录的存储 key
*/
export const getExamActionKey = (examId: string | number): string => {
const today = getTodayString();
return `yh_exam_actions_${today}_${examId}`;
};
/**
* 操作记录类型
*/
export interface ExamActionRecord {
/** 身份证拍照与签到 */
idCardSignIn?: boolean;
/** 体检知情同意书的签字 */
consentSign?: boolean;
/** 打印导检单是否签名 */
printSign?: boolean;
/** 记录时间戳 */
timestamp?: string;
}
/**
* 获取操作记录
*/
export const getExamActionRecord = (examId: string | number): ExamActionRecord | null => {
if (typeof window === 'undefined') return null;
const key = getExamActionKey(examId);
const raw = localStorage.getItem(key);
if (!raw) return null;
try {
const parsed = JSON.parse(raw);
return parsed as ExamActionRecord;
} catch (err) {
console.warn('操作记录解析失败', err);
return null;
}
};
/**
* 设置操作记录
*/
export const setExamActionRecord = (
examId: string | number,
action: keyof ExamActionRecord,
value: boolean = true
): void => {
if (typeof window === 'undefined') return;
const key = getExamActionKey(examId);
const existing = getExamActionRecord(examId) || {};
const updated: ExamActionRecord = {
...existing,
[action]: value,
timestamp: new Date().toISOString(),
};
localStorage.setItem(key, JSON.stringify(updated));
};
/**
* 检查操作是否已完成
*/
export const isExamActionDone = (examId: string | number, action: keyof ExamActionRecord): boolean => {
const record = getExamActionRecord(examId);
return record?.[action] === true;
};
/**
* 知情同意书PDF信息
*/
export interface TongyishuPdfInfo {
/** PDF文件名称 */
pdf_name: string;
/** PDF文件地址 */
pdf_url: string;
/** 组合代码 */
combination_code?: number | null;
}
/**
* 获取知情同意书PDF列表的存储 key
*/
export const getTongyishuPdfListKey = (examId: string | number): string => {
const today = getTodayString();
return `yh_tongyishu_pdf_list_${today}_${examId}`;
};
/**
* 存储知情同意书PDF列表
*/
export const setTongyishuPdfList = (
examId: string | number,
pdfList: TongyishuPdfInfo[]
): void => {
if (typeof window === 'undefined') return;
const key = getTongyishuPdfListKey(examId);
localStorage.setItem(key, JSON.stringify(pdfList));
};
/**
* 获取知情同意书PDF列表
*/
export const getTongyishuPdfList = (examId: string | number): TongyishuPdfInfo[] | null => {
if (typeof window === 'undefined') return null;
const key = getTongyishuPdfListKey(examId);
const raw = localStorage.getItem(key);
if (!raw) return null;
try {
const parsed = JSON.parse(raw);
return Array.isArray(parsed) ? parsed as TongyishuPdfInfo[] : null;
} catch (err) {
console.warn('知情同意书PDF列表解析失败', err);
return null;
}
};
/**
* 导检单PDF信息
*/
export interface DaojiandanPdfInfo {
/** PDF文件名称 */
pdf_name: string;
/** PDF文件地址 */
pdf_url: string;
}
/**
* 获取导检单PDF的存储 key
*/
export const getDaojiandanPdfKey = (examId: string | number): string => {
const today = getTodayString();
return `yh_daojiandan_pdf_${today}_${examId}`;
};
/**
* 存储导检单PDF信息
*/
export const setDaojiandanPdf = (
examId: string | number,
pdfInfo: DaojiandanPdfInfo
): void => {
if (typeof window === 'undefined') return;
const key = getDaojiandanPdfKey(examId);
localStorage.setItem(key, JSON.stringify(pdfInfo));
};
/**
* 获取导检单PDF信息
*/
export const getDaojiandanPdf = (examId: string | number): DaojiandanPdfInfo | null => {
if (typeof window === 'undefined') return null;
const key = getDaojiandanPdfKey(examId);
const raw = localStorage.getItem(key);
if (!raw) return null;
try {
const parsed = JSON.parse(raw);
return parsed as DaojiandanPdfInfo;
} catch (err) {
console.warn('导检单PDF信息解析失败', err);
return null;
}
};