| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- import { computed, ref } from 'vue';
- import { message, Modal } from 'ant-design-vue';
- import { ObjectUtils } from '@imengyu/imengyu-utils';
- import type { IHomeCommonCategoryDefine } from '../../CommonCategoryDefine';
- import DefaultEditorJson from '../../DefaultCategory.json';
- import CommonCategoryApi, { type ICommonCategoryConfigItem } from '../../api/CommonCategoryApi';
- /** 历史版本列表项(接口返回的 items 元素) */
- export interface IHistoryListItem {
- id?: number;
- data?: IHomeCommonCategoryDefine;
- createTime?: string;
- name?: string;
- [key: string]: any;
- }
- export function useEditorConfig() {
- const currentEditorJson = ref<IHomeCommonCategoryDefine>(DefaultEditorJson as IHomeCommonCategoryDefine);
- const selectedPage = ref<(IHomeCommonCategoryDefine['page'][0]) | null>(null);
- const historyList = ref<IHistoryListItem[]>([]);
- const currentConfig = ref<ICommonCategoryConfigItem>();
- const currentHistoryId = ref<number>(0);
- const currentShowConfigName = computed(() => {
- if (currentHistoryId.value === 0) return '默认配置';
- return historyList.value.find(item => item.id === currentHistoryId.value)?.name ?? '未知';
- });
- // 另存为弹窗
- const saveAsModalVisible = ref(false);
- const saveAsVersionName = ref('');
- const saveAsLoading = ref(false);
- async function loadEditorJson(selectDefault = false) {
- try {
- currentConfig.value = await CommonCategoryApi.getConfigWithoutCache();
- if (!currentConfig.value) throw new Error('加载基础配置失败');
- if (selectDefault) currentHistoryId.value = currentConfig.value.activeHistoryId;
- if (currentHistoryId.value > 0) {
- const activeHistory = historyList.value.find(item => item.id === currentHistoryId.value);
- if (activeHistory) {
- currentEditorJson.value = ObjectUtils.clone(activeHistory.data!) as IHomeCommonCategoryDefine;
- } else {
- throw new Error('当前激活的历史版本不存在');
- }
- } else {
- currentEditorJson.value = ObjectUtils.clone(currentConfig.value.data) as IHomeCommonCategoryDefine;
- }
- message.success('加载分类成功');
- } catch (error) {
- Modal.error({
- title: '加载分类失败',
- content: '' + error,
- });
- }
- }
- async function loadEditorJsonHistorys() {
- try {
- const res = await CommonCategoryApi.getConfigHistoryList(1, 10);
- const items = res?.items ?? [];
- historyList.value = Array.isArray(items) ? items : [];
- } catch (error) {
- historyList.value = [];
- Modal.error({
- title: '加载历史版本列表失败',
- content: '' + error,
- });
- }
- }
- function onSelectDefault() {
- currentHistoryId.value = 0;
- loadEditorJson(true);
- }
- function onSelectHistory(id: number) {
- currentHistoryId.value = id;
- loadEditorJson(false);
- }
- async function saveEditorJson() {
- try {
- const saveToHistoryId = currentHistoryId.value === 0 ? undefined : currentHistoryId.value;
- await CommonCategoryApi.editConfig(
- currentEditorJson.value,
- undefined,
- saveToHistoryId
- );
- message.success('保存成功');
- } catch (error) {
- Modal.error({
- title: '保存失败',
- content: '' + error,
- });
- }
- }
- function openSaveAsModal() {
- saveAsVersionName.value = '';
- saveAsModalVisible.value = true;
- }
- async function confirmSaveAs() {
- const name = saveAsVersionName.value?.trim();
- if (!name) {
- message.warning('请输入版本名称');
- return;
- }
- saveAsLoading.value = true;
- try {
- await CommonCategoryApi.editConfig(currentEditorJson.value, name, 0);
- message.success('已另存为历史版本');
- saveAsModalVisible.value = false;
- await loadEditorJsonHistorys();
- } catch (error) {
- Modal.error({
- title: '另存为失败',
- content: '' + error,
- });
- } finally {
- saveAsLoading.value = false;
- }
- }
- async function setActiveHistory() {
- await CommonCategoryApi.setActiveConfigHistory(currentHistoryId.value);
- message.success('设置为激活版本成功');
- }
- function deleteHistory() {
- Modal.confirm({
- title: '删除历史版本',
- content: '确定要删除该历史版本吗?',
- onOk: async () => {
- await CommonCategoryApi.deleteConfigHistory(currentHistoryId.value);
- message.success('删除历史版本成功');
- await loadEditorJsonHistorys();
- await loadEditorJson(true);
- },
- });
- }
- function exportToJsonFile() {
- const json = JSON.stringify(currentEditorJson.value);
- const blob = new Blob([json], { type: 'application/json' });
- const url = URL.createObjectURL(blob);
- const a = document.createElement('a');
- a.href = url;
- a.download = 'editor.json';
- a.click();
- }
- async function init() {
- await loadEditorJsonHistorys();
- await loadEditorJson(true);
- }
- return {
- currentEditorJson,
- selectedPage,
- historyList,
- currentConfig,
- currentHistoryId,
- currentShowConfigName,
- saveAsModalVisible,
- saveAsVersionName,
- saveAsLoading,
- loadEditorJson,
- loadEditorJsonHistorys,
- onSelectDefault,
- onSelectHistory,
- saveEditorJson,
- openSaveAsModal,
- confirmSaveAs,
- setActiveHistory,
- deleteHistory,
- exportToJsonFile,
- init,
- };
- }
|