chore: 渲染端通过 api 获取数据
This commit is contained in:
@@ -1,21 +1,44 @@
|
||||
import instance from './instance';
|
||||
import { type HistorySchema } from '@vtj/core';
|
||||
|
||||
export const getMaterialsList = async () => {
|
||||
export type LowCodeHistorySchema = {
|
||||
project_id: string;
|
||||
id: string;
|
||||
page_id?: string;
|
||||
block_id?: string;
|
||||
dsl?: HistorySchema;
|
||||
};
|
||||
|
||||
function transformHistoryData(data: LowCodeHistorySchema) {
|
||||
return {
|
||||
...data,
|
||||
dsl: JSON.stringify(data.dsl || {})
|
||||
};
|
||||
}
|
||||
export const getHistories = async () => {
|
||||
const response = await instance.get('/api/v1/histories');
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const createMaterials = async (data: any) => {
|
||||
const response = await instance.post('/api/v1/histories', data);
|
||||
export const getHistory = async (id: string) => {
|
||||
const response = await instance.get(`/api/v1/histories/${id}`);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const updateMaterials = async (id: string, data: any) => {
|
||||
export const createHistory = async (data: LowCodeHistorySchema) => {
|
||||
const response = await instance.post(
|
||||
'/api/v1/histories',
|
||||
transformHistoryData(data)
|
||||
);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const updateHistory = async (id: string, data: any) => {
|
||||
const response = await instance.put(`/api/v1/histories/${id}`, data);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const deleteMaterials = async (id: string) => {
|
||||
export const deleteHistory = async (id: string) => {
|
||||
const response = await instance.delete(`/api/v1/histories/${id}`);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
@@ -4,3 +4,4 @@ export * from './file';
|
||||
export * from './materials';
|
||||
export * from './project';
|
||||
export * from './application';
|
||||
export * from './history';
|
||||
|
||||
@@ -19,7 +19,11 @@ import {
|
||||
updateFile as updateLowCodeFile,
|
||||
getFile as getLowCodeFile,
|
||||
deleteFile as deleteLowCodeFile,
|
||||
type LowCodeFileSchema
|
||||
getHistory as getLowCodeHistory,
|
||||
updateHistory as updateLowCodeHistory,
|
||||
deleteHistory as deleteLowCodeHistory,
|
||||
getHistories as getLowCodeHistories,
|
||||
createHistory as createLowCodeHistory
|
||||
} from '@/io';
|
||||
import { isNumeric } from 'licia-es';
|
||||
const storage = new Storage({
|
||||
@@ -39,20 +43,9 @@ const stringifyFields = [
|
||||
|
||||
export class StorageService extends BaseService {
|
||||
public async init(project: ProjectSchema): Promise<ProjectSchema> {
|
||||
// console.log('init-project', project);
|
||||
// const model = new ProjectModel(project);
|
||||
// // console.log('init-project-model', model);
|
||||
// const match = storage.get(`project_${model.id}`);
|
||||
// console.log('init-project-match', match);
|
||||
// const dsl = Object.assign(model.toDsl(), match || {});
|
||||
// console.log('init-project-dsl', dsl);
|
||||
// storage.save(`project_${model.id}`, dsl);
|
||||
// return Promise.resolve(dsl);
|
||||
const remoteProject = await getProject('2');
|
||||
console.log('remoteProject', remoteProject);
|
||||
const model = new ProjectModel(remoteProject);
|
||||
const dsl = model.toDsl();
|
||||
console.log('dsl', dsl);
|
||||
storage.save(`project_${model.id}`, dsl);
|
||||
return Promise.resolve(dsl);
|
||||
}
|
||||
@@ -64,7 +57,6 @@ export class StorageService extends BaseService {
|
||||
}
|
||||
|
||||
public saveProject(project: ProjectSchema): Promise<boolean> {
|
||||
console.log('saveProject', project);
|
||||
const newProject = {
|
||||
...project,
|
||||
...Object.fromEntries(
|
||||
@@ -73,7 +65,6 @@ export class StorageService extends BaseService {
|
||||
.map(([key, value]) => [key, JSON.stringify(value)])
|
||||
)
|
||||
};
|
||||
console.log('newProject', newProject);
|
||||
updateProject('2', newProject);
|
||||
const model = new ProjectModel(project);
|
||||
storage.save(`project_${model.id}`, model.toDsl());
|
||||
@@ -93,51 +84,60 @@ export class StorageService extends BaseService {
|
||||
console.log('saveFile', file);
|
||||
if (file.id) {
|
||||
const existFile = await getLowCodeFile(file.id);
|
||||
console.log('existFile', existFile);
|
||||
if (existFile.file_id) {
|
||||
console.log('updateFile', file, existFile);
|
||||
await updateLowCodeFile(file.id, {
|
||||
return updateLowCodeFile(file.id, {
|
||||
...existFile,
|
||||
dsl: file
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
return Promise.resolve(true);
|
||||
})
|
||||
.catch((err) => {
|
||||
return Promise.reject(err);
|
||||
});
|
||||
} else {
|
||||
console.log('createFile', file);
|
||||
await createFile({
|
||||
return createFile({
|
||||
project_id: 2,
|
||||
publish: false,
|
||||
active: true,
|
||||
dsl: file,
|
||||
file_id: file.id
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
return Promise.resolve(true);
|
||||
})
|
||||
.catch((err) => {
|
||||
return Promise.reject(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
return Promise.resolve(true);
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
|
||||
public async getFile(id: string): Promise<BlockSchema> {
|
||||
const dsl = storage.get(`file_${id}`);
|
||||
const lowCodeFile = await getLowCodeFile(id);
|
||||
console.log('getFile', lowCodeFile);
|
||||
if (lowCodeFile.dsl) {
|
||||
return Promise.resolve(lowCodeFile.dsl as BlockSchema);
|
||||
} else {
|
||||
return Promise.reject(null);
|
||||
}
|
||||
}
|
||||
|
||||
public async removeFile(id: string): Promise<boolean> {
|
||||
storage.remove(`file_${id}`);
|
||||
console.log('removeFile', id);
|
||||
// return Promise.resolve(true);
|
||||
return deleteLowCodeFile(id).then((res) => {
|
||||
console.log('removeFile-res', res);
|
||||
return Promise.resolve(true);
|
||||
console.log('getFile', id);
|
||||
return getLowCodeFile(id).then((lowCodeFile) => {
|
||||
if (lowCodeFile.dsl) {
|
||||
return Promise.resolve(lowCodeFile.dsl as BlockSchema);
|
||||
} else {
|
||||
return Promise.reject(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public saveHistory(history: HistorySchema): Promise<boolean> {
|
||||
public async removeFile(id: string): Promise<boolean> {
|
||||
return deleteLowCodeFile(id).then(() => Promise.resolve(true));
|
||||
}
|
||||
|
||||
public async saveHistory(history: HistorySchema): Promise<boolean> {
|
||||
console.log('saveHistory', history);
|
||||
// const existHistory = await getLowCodeHistory(history.id);
|
||||
// if (existHistory.history_id) {
|
||||
// await updateLowCodeHistory(history.id, history);
|
||||
// } else {
|
||||
// await createLowCodeHistory(history);
|
||||
// }
|
||||
storage.save(`history_${history.id}`, history);
|
||||
// console.log('saveHistory', history);
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
@@ -152,23 +152,18 @@ export class StorageService extends BaseService {
|
||||
};
|
||||
|
||||
public removeHistory(id: string): Promise<boolean> {
|
||||
const history = storage.get(`history_${id}`) as HistorySchema;
|
||||
if (history) {
|
||||
const items = history.items || [];
|
||||
const ids = items.map((item) => item.id);
|
||||
this.removeHistoryItem(id, ids);
|
||||
storage.remove(`history_${id}`);
|
||||
console.log('removeHistory', history);
|
||||
}
|
||||
|
||||
return Promise.resolve(true);
|
||||
return deleteLowCodeHistory(id).then((res) => {
|
||||
console.log('removeHistory', res);
|
||||
return Promise.resolve(true);
|
||||
});
|
||||
}
|
||||
|
||||
public getHistory(id: string): Promise<HistorySchema> {
|
||||
public async getHistory(id: string): Promise<HistorySchema> {
|
||||
const dsl = storage.get(`history_${id}`);
|
||||
console.log('getHistoryDSL', dsl);
|
||||
const history = new HistoryModel(dsl || { id });
|
||||
console.log('getHistory', dsl, id, history);
|
||||
// const histories = await getLowCodeHistories(id);
|
||||
return Promise.resolve(history.toDsl());
|
||||
}
|
||||
|
||||
@@ -178,9 +173,9 @@ export class StorageService extends BaseService {
|
||||
return Promise.resolve(item);
|
||||
}
|
||||
|
||||
public saveHistoryItem(fId: string, item: HistoryItem): Promise<boolean> {
|
||||
console.log('saveHistoryItem', fId, item);
|
||||
storage.save(`history_${fId}_${item.id}`, item);
|
||||
public saveHistoryItem(fileId: string, item: HistoryItem): Promise<boolean> {
|
||||
console.log('saveHistoryItem', fileId, item);
|
||||
storage.save(`history_${fileId}_${item.id}`, item);
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user