草稿另存为接口

This commit is contained in:
dong 2023-06-09 17:56:19 +08:00
parent b21af0985a
commit d6515cf693
3 changed files with 47 additions and 26 deletions

View File

@ -7,6 +7,7 @@ interface Item {
id: number;
name: string;
proto: string;
type: string;
createdAt: string;
updateAt: string;
creatorId?: number;
@ -76,6 +77,10 @@ export function saveDraft(
* @param data
* @returns
*/
export function saveAsDraft(id: number, data: { name: string }) {
return api.post(`${DraftUriBase}/${id}/saveAs`, data);
export async function saveAsDraft(
id: number,
data: { name: string; proto: string }
): Promise<Item> {
const response = await api.post(`${DraftUriBase}/${id}/saveAs`, data);
return response.data;
}

View File

@ -202,13 +202,29 @@ export function initDrawApp(dom: HTMLElement): JlDrawApp {
global: true,
combinations: [CombinationKey.Ctrl],
onPress: () => {
saveDrawDatas(app);
saveDrawToServer(app);
},
})
);
return drawApp;
}
export function saveDrawToServer(app: JlDrawApp) {
const base64 = saveDrawDatas(app);
const drawStore = useDrawStore();
const id = drawStore.draftId;
if (!id) {
return;
}
saveDraft(id as number, { proto: base64 })
.then(() => {
successNotify('保存数据成功!');
})
.catch((err) => {
errorNotify(err.message, err);
});
}
// const StorageKey = 'graphic-storage';
export function saveDrawDatas(app: JlDrawApp) {
const storage = new graphicData.RtssGraphicStorage();
@ -248,18 +264,7 @@ export function saveDrawDatas(app: JlDrawApp) {
const base64 = fromUint8Array(storage.serialize());
console.log('保存数据', storage);
// localStorage.setItem(StorageKey, base64);
const drawStore = useDrawStore();
const id = drawStore.draftId;
if (!id) {
return;
}
saveDraft(id as number, { proto: base64 })
.then(() => {
successNotify('保存数据成功!');
})
.catch((err) => {
errorNotify('保存数据失败!', err.message);
});
return base64;
}
export async function loadDrawDatas(app: GraphicApp) {

View File

@ -7,11 +7,11 @@
<q-menu>
<q-list style="min-width: 100px">
<q-item clickable v-close-popup @click="saveAllDrawDatas">
<q-item-section>保存并校验</q-item-section>
<q-item-section>保存</q-item-section>
</q-item>
<!-- <q-item clickable v-close-popup @click="saveAsDialog = true">
<q-item clickable v-close-popup @click="saveAsDialog = true">
<q-item-section>另存为</q-item-section>
</q-item> -->
</q-item>
<q-item clickable v-close-popup>
<q-item-section>一键关联</q-item-section>
</q-item>
@ -161,13 +161,19 @@
<script setup lang="ts">
import DrawProperties from 'src/components/draw-app/DrawProperties.vue';
import { getDrawApp, loadDrawDatas, saveDrawDatas } from 'src/drawApp';
import {
getDrawApp,
loadDrawDatas,
saveDrawDatas,
saveDrawToServer,
} from 'src/drawApp';
import { JlDrawApp } from 'src/jl-graphic';
import { useDrawStore } from 'src/stores/draw-store';
import { onMounted, onUnmounted, reactive, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { errorNotify, successNotify } from 'src/utils/CommonNotify';
import { saveAsDraft } from 'src/api/DraftApi';
import { ApiError } from 'src/boot/axios';
const route = useRoute();
const router = useRouter();
@ -273,7 +279,7 @@ function onResize() {
}
function saveAllDrawDatas() {
const drawApp = getDrawApp();
saveDrawDatas(drawApp as JlDrawApp);
saveDrawToServer(drawApp as JlDrawApp);
}
function backConfirm() {
@ -285,15 +291,20 @@ const saveAsName = ref('');
async function saveAs(name: string) {
try {
const record = await saveAsDraft(+route.params.id as number, { name });
console.log('🚀 ~ file: DrawLayout.vue:288 ~ saveAs ~ record:', record);
// if (record) {
// router.replace(`/painting/${record.id}`);
// }
const drawApp = getDrawApp();
const base64 = saveDrawDatas(drawApp as JlDrawApp);
const record = await saveAsDraft(+route.params.id as number, {
name,
proto: base64,
});
if (record) {
router.replace(`/painting/${record.id}/${record.type}`);
}
successNotify('另存为成功');
saveAsDialog.value = false;
} catch (e) {
errorNotify('另存为异常', e);
const error = e as ApiError;
errorNotify(error.title, e);
}
}