80 lines
2.2 KiB
TypeScript
80 lines
2.2 KiB
TypeScript
import { defineStore } from 'pinia';
|
|
import { ref } from 'vue';
|
|
import $cache from '@/plugins/cache';
|
|
|
|
// 草稿数据类型
|
|
export interface ProcurementDraft {
|
|
id: string;
|
|
draftNumber: string;
|
|
planName: string;
|
|
saveTime: string;
|
|
content: any;
|
|
}
|
|
|
|
// 保存草稿到本地存储
|
|
const saveDraftsToStorage = (drafts: ProcurementDraft[]) => {
|
|
$cache.local.setJSON('procurementDrafts', drafts);
|
|
};
|
|
|
|
// 从本地存储获取草稿
|
|
const getDraftsFromStorage = (): ProcurementDraft[] => {
|
|
const stored = $cache.local.getJSON('procurementDrafts');
|
|
return stored && Array.isArray(stored) ? stored : [];
|
|
};
|
|
|
|
export const useProcurementDraftStore = defineStore('procurementDraft', () => {
|
|
const draftList = ref<ProcurementDraft[]>(getDraftsFromStorage());
|
|
|
|
// 保存草稿
|
|
const saveDraft = (planName: string, content: any): ProcurementDraft => {
|
|
const today = new Date();
|
|
const dateStr = today.getFullYear() + '-' +
|
|
String(today.getMonth() + 1).padStart(2, '0') + '-' +
|
|
String(today.getDate()).padStart(2, '0');
|
|
const randomNum = Math.floor(100 + Math.random() * 900);
|
|
const draftNumber = `DRAFT-${dateStr}-${randomNum}`;
|
|
|
|
const newDraft: ProcurementDraft = {
|
|
id: `draft_${Date.now()}_${randomNum}`,
|
|
draftNumber,
|
|
planName,
|
|
saveTime: new Date().toLocaleString(),
|
|
content: JSON.parse(JSON.stringify(content)) // 深拷贝内容
|
|
};
|
|
|
|
// 添加到草稿列表并保存到本地存储
|
|
draftList.value.unshift(newDraft);
|
|
saveDraftsToStorage(draftList.value);
|
|
|
|
return newDraft;
|
|
};
|
|
|
|
// 获取草稿列表
|
|
const getDraftList = (): ProcurementDraft[] => {
|
|
return draftList.value;
|
|
};
|
|
|
|
// 获取单个草稿
|
|
const getDraft = (draftId: string): ProcurementDraft | undefined => {
|
|
return draftList.value.find(draft => draft.id === draftId);
|
|
};
|
|
|
|
// 删除草稿
|
|
const deleteDraft = (draftId: string): boolean => {
|
|
const index = draftList.value.findIndex(draft => draft.id === draftId);
|
|
if (index !== -1) {
|
|
draftList.value.splice(index, 1);
|
|
saveDraftsToStorage(draftList.value);
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
return {
|
|
draftList,
|
|
saveDraft,
|
|
getDraftList,
|
|
getDraft,
|
|
deleteDraft
|
|
};
|
|
}); |