feat: add publish app
This commit is contained in:
parent
8beb65e637
commit
0fc580aa38
15
package.json
15
package.json
@ -7,7 +7,7 @@
|
||||
"author": "abearxiong",
|
||||
"scripts": {
|
||||
"watch": "cross-env ENV=production webpack --mode=production --watch",
|
||||
"dev": "cross-env NODE_ENV=development nodemon --delay 2.5 -e js,cjs,mjs --exec node dist/app.cjs",
|
||||
"dev": "cross-env NODE_ENV=development nodemon --delay 2.5 -e js,cjs,mjs --watch dist --exec node dist/app.cjs",
|
||||
"test": "tsx test/**/*.ts",
|
||||
"dev:watch": "concurrently -n \"Watch,Dev\" -c \"green,blue\" \"npm run watch\" \"sleep 1 && npm run dev\" ",
|
||||
"build": "cross-env ENV=production webpack --mode=production ",
|
||||
@ -32,11 +32,11 @@
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@abearxiong/auth": "1.0.2",
|
||||
"@abearxiong/router": "0.0.1-alpha.38",
|
||||
"@abearxiong/router": "0.0.1-alpha.40",
|
||||
"@abearxiong/use-config": "^0.0.2",
|
||||
"@babel/core": "^7.25.2",
|
||||
"@babel/preset-env": "^7.25.4",
|
||||
"@babel/preset-typescript": "^7.24.7",
|
||||
"@babel/core": "^7.25.7",
|
||||
"@babel/preset-env": "^7.25.7",
|
||||
"@babel/preset-typescript": "^7.25.7",
|
||||
"@kevisual/ai-graph": "workspace:^",
|
||||
"@kevisual/ai-lang": "workspace:^",
|
||||
"@types/semver": "^7.5.8",
|
||||
@ -55,7 +55,7 @@
|
||||
"ollama": "^0.5.9",
|
||||
"pg": "^8.13.0",
|
||||
"semver": "^7.6.3",
|
||||
"sequelize": "^6.37.3",
|
||||
"sequelize": "^6.37.4",
|
||||
"socket.io": "^4.8.0",
|
||||
"strip-ansi": "^7.1.0",
|
||||
"uuid": "^10.0.0",
|
||||
@ -88,7 +88,8 @@
|
||||
"resolutions": {
|
||||
"glob": "latest",
|
||||
"inflight": "latest",
|
||||
"rimraf": "latest"
|
||||
"rimraf": "latest",
|
||||
"picomatch": "^4.0.2"
|
||||
},
|
||||
"pnpm": {}
|
||||
}
|
1464
pnpm-lock.yaml
generated
1464
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -1,11 +1,11 @@
|
||||
import { useFileStore } from '@abearxiong/use-file-store';
|
||||
import http from 'http';
|
||||
import fs from 'fs';
|
||||
import fs, { rm } from 'fs';
|
||||
import path from 'path';
|
||||
import { IncomingForm } from 'formidable';
|
||||
import { checkToken } from '@abearxiong/auth';
|
||||
import { useConfig } from '@abearxiong/use-config';
|
||||
import { minioClient } from '@/app.ts';
|
||||
import { app, minioClient } from '@/app.ts';
|
||||
import { bucketName } from '@/modules/minio.ts';
|
||||
import { getContentType } from '@/utils/get-content-type.ts';
|
||||
import { User } from '@/models/user.ts';
|
||||
@ -83,6 +83,8 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
|
||||
});
|
||||
}
|
||||
if (req.method === 'POST' && req.url === '/api/app/upload') {
|
||||
if (res.headersSent) return; // 如果响应已发送,不再处理
|
||||
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
const authroization = req.headers?.['authorization'] as string;
|
||||
const error = (msg: string) => {
|
||||
@ -114,8 +116,23 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
|
||||
res.end(error(`Upload error: ${err.message}`));
|
||||
return;
|
||||
}
|
||||
console.log('fields', fields);
|
||||
const clearFiles = () => {
|
||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||
uploadedFiles.forEach((file) => {
|
||||
fs.unlinkSync(file.filepath);
|
||||
});
|
||||
};
|
||||
const { appKey, version } = fields;
|
||||
if (!appKey) {
|
||||
res.end(error('appKey is required'));
|
||||
clearFiles();
|
||||
return;
|
||||
}
|
||||
if (!version) {
|
||||
res.end(error('version is required'));
|
||||
clearFiles();
|
||||
return;
|
||||
}
|
||||
// 逐个处理每个上传的文件
|
||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||
const uploadResults = [];
|
||||
@ -139,12 +156,25 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
|
||||
});
|
||||
fs.unlinkSync(tempPath); // 删除临时文件
|
||||
}
|
||||
// 修改header
|
||||
// res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
const data = {
|
||||
code: 200,
|
||||
data: uploadResults,
|
||||
const r = await app.call({
|
||||
path: 'app',
|
||||
key: 'uploadFiles',
|
||||
payload: {
|
||||
token: token,
|
||||
data: {
|
||||
appKey,
|
||||
version,
|
||||
files: uploadResults,
|
||||
},
|
||||
},
|
||||
});
|
||||
const data: any = {
|
||||
code: r.code,
|
||||
data: r.body,
|
||||
};
|
||||
if (r.message) {
|
||||
data.message = r.message;
|
||||
}
|
||||
res.end(JSON.stringify(data));
|
||||
});
|
||||
}
|
||||
|
@ -1,2 +1,4 @@
|
||||
import './list.ts';
|
||||
import './user-app.ts';
|
||||
|
||||
export * from './module/index.ts';
|
||||
|
@ -1,6 +1,9 @@
|
||||
import { CustomError } from '@abearxiong/router';
|
||||
import { App, CustomError } from '@abearxiong/router';
|
||||
import { AppModel, AppListModel } from './module/index.ts';
|
||||
import { app } from '@/app.ts';
|
||||
import { app, redis } from '@/app.ts';
|
||||
import _ from 'lodash';
|
||||
import { prefixFix } from './util.ts';
|
||||
import { deleteFiles } from '../file/index.ts';
|
||||
|
||||
app
|
||||
.route({
|
||||
@ -21,7 +24,7 @@ app
|
||||
key: data.key,
|
||||
},
|
||||
});
|
||||
ctx.body = list;
|
||||
ctx.body = list.map((item) => prefixFix(item, tokenUser.username));
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
@ -33,6 +36,7 @@ app
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const id = ctx.query.id;
|
||||
if (!id) {
|
||||
throw new CustomError('id is required');
|
||||
@ -41,7 +45,7 @@ app
|
||||
if (!am) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
ctx.body = am;
|
||||
ctx.body = prefixFix(am, tokenUser.username);
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
@ -91,6 +95,17 @@ app
|
||||
if (!app) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
const am = await AppModel.findOne({ where: { key: app.key, uid: app.uid } });
|
||||
if (!am) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
if (am.version === app.version) {
|
||||
throw new CustomError('app is published');
|
||||
}
|
||||
const files = app.data.files || [];
|
||||
if (files.length > 0) {
|
||||
await deleteFiles(files.map((item) => item.path));
|
||||
}
|
||||
await app.destroy({
|
||||
force: true,
|
||||
});
|
||||
@ -98,3 +113,106 @@ app
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'canUploadFiles',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { appKey, version } = ctx.query.data;
|
||||
if (!appKey) {
|
||||
throw new CustomError('appKey is required');
|
||||
}
|
||||
const app = await AppListModel.findOne({ where: { version: version, key: appKey, uid: tokenUser.id } });
|
||||
if (!app) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
ctx.body = app;
|
||||
})
|
||||
.addTo(app);
|
||||
app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'uploadFiles',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { appKey, files, version } = ctx.query.data;
|
||||
if (!appKey) {
|
||||
throw new CustomError('appKey is required');
|
||||
}
|
||||
if (!files || !files.length) {
|
||||
throw new CustomError('files is required');
|
||||
}
|
||||
let app = await AppListModel.findOne({ where: { version: version, key: appKey, uid: tokenUser.id } });
|
||||
if (!app) {
|
||||
// throw new CustomError('app not found');
|
||||
app = await AppListModel.create({
|
||||
key: appKey,
|
||||
version,
|
||||
uid: tokenUser.id,
|
||||
data: {
|
||||
files: [],
|
||||
},
|
||||
});
|
||||
}
|
||||
const dataFiles = app.data.files || [];
|
||||
const newFiles = _.uniqBy([...dataFiles, ...files], 'name');
|
||||
const res = await app.update({ data: { ...app.data, files: newFiles } });
|
||||
|
||||
ctx.body = prefixFix(res, tokenUser.username);
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'publish',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { id } = ctx.query.data;
|
||||
if (!id) {
|
||||
throw new CustomError('id is required');
|
||||
}
|
||||
const app = await AppListModel.findByPk(id);
|
||||
if (!app) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
const files = app.data.files || [];
|
||||
const am = await AppModel.findOne({ where: { key: app.key, uid: tokenUser.id } });
|
||||
if (!am) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
await am.update({ data: { ...am.data, files }, version: app.version });
|
||||
//
|
||||
const keys = await redis.keys('user:app:exist:*');
|
||||
console.log('keys', keys);
|
||||
const expireKey = 'user:app:exist:' + `${app.key}:${am.user}`;
|
||||
console.log('expireKey', expireKey);
|
||||
await redis.set(expireKey, 'v', 'EX', 2);
|
||||
await new Promise((resolve) => setTimeout(resolve, 2100));
|
||||
const keys2 = await redis.keys('user:app:exist:*');
|
||||
console.log('keys2', keys2);
|
||||
ctx.body = 'success';
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'getApp',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { user, key } = ctx.query.data;
|
||||
const app = await AppModel.findOne({ where: { user, key } });
|
||||
if (!app) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
ctx.body = app;
|
||||
})
|
||||
.addTo(app);
|
||||
|
@ -28,15 +28,26 @@ app
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const id = ctx.query.id;
|
||||
if (!id) {
|
||||
const { key } = ctx.query.data || {};
|
||||
if (!id && !key) {
|
||||
throw new CustomError('id is required');
|
||||
}
|
||||
if (id) {
|
||||
const am = await AppModel.findByPk(id);
|
||||
if (!am) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
ctx.body = am;
|
||||
} else {
|
||||
const am = await AppModel.findOne({ where: { key, uid: tokenUser.id } });
|
||||
if (!am) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
ctx.body = am;
|
||||
}
|
||||
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
@ -87,6 +98,7 @@ app
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const id = ctx.query.id;
|
||||
if (!id) {
|
||||
throw new CustomError('id is required');
|
||||
@ -95,7 +107,9 @@ app
|
||||
if (!am) {
|
||||
throw new CustomError('app not found');
|
||||
}
|
||||
await am.destroy();
|
||||
const list = await AppListModel.findAll({ where: { key: am.key, uid: tokenUser.id } });
|
||||
await am.destroy({ force: true });
|
||||
await Promise.all(list.map((item) => item.destroy({ force: true })));
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
15
src/routes/app-manager/util.ts
Normal file
15
src/routes/app-manager/util.ts
Normal file
@ -0,0 +1,15 @@
|
||||
type Opts = {
|
||||
prefix: string;
|
||||
};
|
||||
export const prefixFix = (data: any, prefix: string, opts?: Opts) => {
|
||||
const len = prefix.length || 0;
|
||||
if (data.data.files) {
|
||||
data.data.files = data.data.files.map((item) => {
|
||||
return {
|
||||
...item,
|
||||
path: item.path.slice(len + 1),
|
||||
};
|
||||
});
|
||||
}
|
||||
return data;
|
||||
};
|
@ -1 +1,3 @@
|
||||
import './list.ts';
|
||||
|
||||
export * from './module/get-minio-list.ts';
|
||||
|
@ -53,3 +53,27 @@ export const getFileStat = async (prefix: string): Promise<any> => {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteFile = async (prefix: string): Promise<any> => {
|
||||
try {
|
||||
await minioClient.removeObject(bucketName, prefix, {
|
||||
versionId: 'null',
|
||||
forceDelete: true,
|
||||
});
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error('delete File Error not handle', e);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// 批量删除文件
|
||||
export const deleteFiles = async (prefixs: string[]): Promise<any> => {
|
||||
try {
|
||||
await minioClient.removeObjects(bucketName, prefixs);
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error('delete Files Error not handle', e);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
@ -1 +1,3 @@
|
||||
import './list.ts'
|
||||
|
||||
import './publish.ts'
|
@ -4,6 +4,7 @@ import { PageModel } from './models/index.ts';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { ContainerModel } from '../container/models/index.ts';
|
||||
import { Op } from 'sequelize';
|
||||
import { getDeck } from './module/cache-file.ts';
|
||||
|
||||
app
|
||||
.route({
|
||||
@ -50,18 +51,29 @@ app
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { data, id, title, type, description } = ctx.query.data;
|
||||
const { data, id, publish, ...rest } = ctx.query.data;
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
let needUpdate = { ...rest };
|
||||
if (data) {
|
||||
needUpdate = { ...needUpdate, data };
|
||||
}
|
||||
if (publish) {
|
||||
needUpdate = { ...needUpdate, publish };
|
||||
}
|
||||
|
||||
if (id) {
|
||||
const page = await PageModel.findByPk(id);
|
||||
// if (!page?.publish && publish) {
|
||||
// needUpdate = { ...needUpdate, publish };
|
||||
// }
|
||||
if (page) {
|
||||
const newPage = await page.update({ data: data, title, type, description });
|
||||
const newPage = await page.update({ ...needUpdate });
|
||||
ctx.body = newPage;
|
||||
} else {
|
||||
throw new CustomError('page not found');
|
||||
}
|
||||
} else if (data) {
|
||||
const page = await PageModel.create({ data, title, type, description, uid: tokenUser.id });
|
||||
const page = await PageModel.create({ ...needUpdate, uid: tokenUser.id });
|
||||
ctx.body = page;
|
||||
}
|
||||
})
|
||||
@ -286,26 +298,8 @@ app
|
||||
if (!page) {
|
||||
throw new CustomError(404, 'panel not found');
|
||||
}
|
||||
const { data } = page;
|
||||
const { nodes = [], edges } = data;
|
||||
const containerList = nodes
|
||||
.map((item) => {
|
||||
const { data } = item;
|
||||
return data?.cid;
|
||||
})
|
||||
.filter((item) => item);
|
||||
const quchong = Array.from(new Set(containerList));
|
||||
const containers = await ContainerModel.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: quchong,
|
||||
},
|
||||
},
|
||||
});
|
||||
ctx.body = {
|
||||
page,
|
||||
containerList: containers,
|
||||
};
|
||||
const pageData = await getDeck(page);
|
||||
ctx.body = pageData;
|
||||
} catch (e) {
|
||||
console.log('error', e);
|
||||
throw new CustomError(e.message || 'get error');
|
||||
|
@ -25,6 +25,12 @@ export interface PageData {
|
||||
viewport: any;
|
||||
[key: string]: any;
|
||||
}
|
||||
export type Publish = {
|
||||
id?: string; // resource id
|
||||
description?: string;
|
||||
key?: string;
|
||||
version?: string;
|
||||
};
|
||||
/**
|
||||
* 页面数据
|
||||
*/
|
||||
@ -34,6 +40,7 @@ export class PageModel extends Model {
|
||||
declare description: string;
|
||||
declare type: string;
|
||||
declare data: PageData;
|
||||
declare publish: Publish;
|
||||
declare uid: string;
|
||||
}
|
||||
PageModel.init(
|
||||
@ -60,6 +67,10 @@ PageModel.init(
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: {},
|
||||
},
|
||||
publish: {
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: {},
|
||||
},
|
||||
uid: {
|
||||
type: DataTypes.UUID,
|
||||
allowNull: true,
|
||||
|
74
src/routes/page/module/cache-file.ts
Normal file
74
src/routes/page/module/cache-file.ts
Normal file
@ -0,0 +1,74 @@
|
||||
import { useFileStore } from '@abearxiong/use-file-store';
|
||||
import { PageModel } from '../models/index.ts';
|
||||
import { ContainerModel } from '@/routes/container/models/index.ts';
|
||||
import { Op } from 'sequelize';
|
||||
import { getContainerData } from './get-container.ts';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { getHTML, getDataJs } from './file-template.ts';
|
||||
import { minioClient } from '@/app.ts';
|
||||
import { bucketName } from '@/modules/minio.ts';
|
||||
import { getContentType } from '@/utils/get-content-type.ts';
|
||||
export const cacheFile = useFileStore('cache-file', {
|
||||
needExists: true,
|
||||
});
|
||||
|
||||
export const getDeck = async (page: PageModel) => {
|
||||
const { data } = page;
|
||||
const { nodes = [], edges } = data;
|
||||
const containerList = nodes
|
||||
.map((item) => {
|
||||
const { data } = item;
|
||||
return data?.cid;
|
||||
})
|
||||
.filter((item) => item);
|
||||
const quchong = Array.from(new Set(containerList));
|
||||
const containers = await ContainerModel.findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: quchong,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const pageData = {
|
||||
page,
|
||||
containerList: containers,
|
||||
};
|
||||
return pageData;
|
||||
};
|
||||
|
||||
export const cachePage = async (page: PageModel, opts: { tokenUser: any; key; version }) => {
|
||||
const _result = await getDeck(page);
|
||||
const result = getContainerData(_result);
|
||||
const html = getHTML({ rootId: page.id, title: page?.publish?.key });
|
||||
const dataJs = getDataJs(result);
|
||||
const htmlPath = path.resolve(cacheFile, `${page.id}.html`);
|
||||
const dataJsPath = path.resolve(cacheFile, `${page.id}.js`);
|
||||
fs.writeFileSync(htmlPath, html);
|
||||
fs.writeFileSync(dataJsPath, dataJs);
|
||||
const minioHTML = await uploadMinio({ ...opts, path: `index.html`, filePath: htmlPath });
|
||||
const minioData = await uploadMinio({ ...opts, path: `data.js`, filePath: dataJsPath });
|
||||
return [
|
||||
{
|
||||
name: 'index.html',
|
||||
path: minioHTML,
|
||||
},
|
||||
{
|
||||
name: 'data.js',
|
||||
path: minioData,
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
export const uploadMinio = async ({ tokenUser, key, version, path, filePath }) => {
|
||||
const minioPath = `${tokenUser.username}/${key}/${version}/${path}`;
|
||||
const isHTML = filePath.endsWith('.html');
|
||||
await minioClient.fPutObject(bucketName, minioPath, filePath, {
|
||||
'Content-Type': getContentType(filePath),
|
||||
'app-source': 'user-app',
|
||||
'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
|
||||
});
|
||||
fs.unlinkSync(filePath); // 删除临时文件
|
||||
return minioPath;
|
||||
};
|
46
src/routes/page/module/file-template.ts
Normal file
46
src/routes/page/module/file-template.ts
Normal file
@ -0,0 +1,46 @@
|
||||
type HTMLOptions = {
|
||||
title?: string;
|
||||
rootId: string;
|
||||
};
|
||||
export const getHTML = (opts: HTMLOptions) => {
|
||||
return `<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>${opts.title || 'Kevisual'}</title>
|
||||
<style>
|
||||
html,
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
body {
|
||||
font-size: 16px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module">
|
||||
import { Container } from 'https://kevisual.xiongxiao.me/root/container/index.js'
|
||||
import { data } from './data.js'
|
||||
const container = new Container({
|
||||
root: 'root',
|
||||
data: data
|
||||
});
|
||||
container.render('${opts.rootId}');
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>`;
|
||||
};
|
||||
|
||||
export const getDataJs = (result: any) => {
|
||||
return 'export const data=' + JSON.stringify(result);
|
||||
};
|
143
src/routes/page/module/get-container.ts
Normal file
143
src/routes/page/module/get-container.ts
Normal file
@ -0,0 +1,143 @@
|
||||
// import { RenderData } from '@abearxiong/container';
|
||||
type RenderData = any;
|
||||
|
||||
type Page = {
|
||||
data: {
|
||||
edges: { id: string; source: string; target: string }[];
|
||||
nodes: { id: string; type: string; position: { x: number; y: number }; data: any }[];
|
||||
};
|
||||
id: string;
|
||||
type: string;
|
||||
[key: string]: any;
|
||||
};
|
||||
type Container = {
|
||||
code: string;
|
||||
id: string;
|
||||
[key: string]: any;
|
||||
};
|
||||
type PageEditData = {
|
||||
page: Page;
|
||||
containerList: Container[];
|
||||
};
|
||||
export const getContainerData = (pageEditData: any) => {
|
||||
const { page, containerList } = pageEditData;
|
||||
const containerObj = containerList.reduce((acc, container) => {
|
||||
acc[container.id] = container;
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const { edges, nodes } = page.data;
|
||||
const nodesObj = nodes.reduce((acc, node) => {
|
||||
acc[node.id] = node;
|
||||
return acc;
|
||||
}, {});
|
||||
const treeArray = getTreeFromEdges(edges);
|
||||
const floatNodes = nodes.filter((node) => !treeArray.find((item) => item.id === node.id));
|
||||
const treeNodes = nodes.filter((node) => treeArray.find((item) => item.id === node.id));
|
||||
const renderData: RenderData[] = [];
|
||||
for (let tree of treeArray) {
|
||||
const node = nodesObj[tree.id];
|
||||
const container = containerObj[node.data?.cid];
|
||||
const style = node.data?.style ?? {
|
||||
position: 'absolute',
|
||||
width: 100,
|
||||
height: 100,
|
||||
};
|
||||
const data = {
|
||||
node: { ...node },
|
||||
container: { ...container },
|
||||
};
|
||||
renderData.push({
|
||||
id: node.id,
|
||||
children: tree.children,
|
||||
parents: tree.parents,
|
||||
code: container?.code || '',
|
||||
codeId: container?.id,
|
||||
data: data || {},
|
||||
className: node.data?.className,
|
||||
shadowRoot: node.data?.shadowRoot,
|
||||
showChild: node.data?.showChild,
|
||||
style,
|
||||
});
|
||||
}
|
||||
for (let node of floatNodes) {
|
||||
const container = containerObj[node.data?.cid];
|
||||
const style = node.data?.style ?? {
|
||||
position: 'absolute',
|
||||
width: 100,
|
||||
height: 100,
|
||||
};
|
||||
const data = {
|
||||
node: { ...node },
|
||||
container: { ...container },
|
||||
};
|
||||
renderData.push({
|
||||
id: node.id,
|
||||
children: [],
|
||||
parents: [],
|
||||
code: container?.code || '',
|
||||
codeId: container?.id,
|
||||
data: data || {},
|
||||
className: node.data?.className,
|
||||
shadowRoot: node.data?.shadowRoot,
|
||||
showChild: node.data?.showChild,
|
||||
style,
|
||||
});
|
||||
}
|
||||
return renderData;
|
||||
};
|
||||
const getTreeFromEdges = (
|
||||
edges: { id: string; source: string; target: string }[],
|
||||
): {
|
||||
id: string;
|
||||
parents: string[];
|
||||
children: string[];
|
||||
}[] => {
|
||||
// 构建树形结构
|
||||
function buildNodeTree(edges) {
|
||||
const nodeMap = {};
|
||||
|
||||
// 初始化每个节点的子节点列表和父节点列表
|
||||
edges.forEach((edge) => {
|
||||
if (!nodeMap[edge.source]) {
|
||||
nodeMap[edge.source] = { id: edge.source, parents: [], children: [] };
|
||||
}
|
||||
if (!nodeMap[edge.target]) {
|
||||
nodeMap[edge.target] = { id: edge.target, parents: [], children: [] };
|
||||
}
|
||||
|
||||
// 连接父节点和子节点
|
||||
nodeMap[edge.source].children.push(nodeMap[edge.target]);
|
||||
nodeMap[edge.target].parents.push(nodeMap[edge.source]);
|
||||
});
|
||||
|
||||
return nodeMap;
|
||||
}
|
||||
|
||||
const nodeTree = buildNodeTree(edges);
|
||||
|
||||
// 递归获取所有父节点,按顺序
|
||||
function getAllParents(node) {
|
||||
const parents: string[] = [];
|
||||
function traverseParents(currentNode) {
|
||||
if (currentNode.parents.length > 0) {
|
||||
currentNode.parents.forEach((parent: any) => {
|
||||
parents.push(parent.id);
|
||||
traverseParents(parent);
|
||||
});
|
||||
}
|
||||
}
|
||||
traverseParents(node);
|
||||
return parents.reverse(); // 确保顺序从最顶层到直接父节点
|
||||
}
|
||||
|
||||
function getNodeInfo(nodeMap) {
|
||||
return Object.values(nodeMap).map((node: any) => ({
|
||||
id: node.id,
|
||||
parents: getAllParents(node),
|
||||
children: node.children.map((child) => child.id),
|
||||
}));
|
||||
}
|
||||
const result = getNodeInfo(nodeTree);
|
||||
return result;
|
||||
};
|
69
src/routes/page/publish.ts
Normal file
69
src/routes/page/publish.ts
Normal file
@ -0,0 +1,69 @@
|
||||
import { CustomError } from '@abearxiong/router';
|
||||
import { app } from '../../app.ts';
|
||||
import { PageModel } from './models/index.ts';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { ContainerModel } from '../container/models/index.ts';
|
||||
import { Op } from 'sequelize';
|
||||
import { AppListModel, AppModel } from '../app-manager/index.ts';
|
||||
import { cachePage } from './module/cache-file.ts';
|
||||
import _ from 'lodash';
|
||||
import semver from 'semver';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'page',
|
||||
key: 'publish',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { data, id, publish, ...rest } = ctx.query.data;
|
||||
let needUpdate = { ...rest };
|
||||
if (data) {
|
||||
needUpdate = { ...needUpdate, data };
|
||||
}
|
||||
if (publish) {
|
||||
needUpdate = { ...needUpdate, publish };
|
||||
}
|
||||
if (!id) {
|
||||
throw new CustomError('id is required');
|
||||
}
|
||||
const page = await PageModel.findByPk(id);
|
||||
if (!page) {
|
||||
throw new CustomError('page not found');
|
||||
}
|
||||
await page.update(needUpdate);
|
||||
|
||||
const _publish = page.publish || {};
|
||||
if (!_publish.key) {
|
||||
throw new CustomError('publish key is required');
|
||||
}
|
||||
try {
|
||||
const { key, description } = _publish;
|
||||
const version = _publish.version || '0.0.0';
|
||||
let app = await AppModel.findOne({ where: { key, uid: tokenUser.id } });
|
||||
if (!app) {
|
||||
app = await AppModel.create({ title: key, key, uid: tokenUser.id, description, user: tokenUser.username });
|
||||
}
|
||||
const _version = semver.inc(version, 'patch');
|
||||
let appList = await AppListModel.findOne({ where: { key, version: _version, uid: tokenUser.id } });
|
||||
if (!appList) {
|
||||
appList = await AppListModel.create({ key, version: _version, uid: tokenUser.id, data: {} });
|
||||
}
|
||||
// 上传文件
|
||||
const res = await cachePage(page, { tokenUser, key, version: _version });
|
||||
const appFiles = appList?.data?.files || [];
|
||||
const newFiles = _.uniqBy([...appFiles, ...res], 'name');
|
||||
appList.data = {
|
||||
...appList?.data,
|
||||
files: newFiles,
|
||||
};
|
||||
await appList.save();
|
||||
await page.update({ publish: { ..._publish, id: app.id, version: _version } });
|
||||
ctx.body = page;
|
||||
} catch (e) {
|
||||
console.log('error', e);
|
||||
throw new CustomError(e.message || 'publish error');
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
8
src/scripts/remove-app-list.ts
Normal file
8
src/scripts/remove-app-list.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { AppListModel } from '../routes/app-manager/module/index.ts';
|
||||
|
||||
const main = async () => {
|
||||
const list = await AppListModel.findAll();
|
||||
console.log(list.map((item) => item.key));
|
||||
};
|
||||
|
||||
main();
|
Loading…
x
Reference in New Issue
Block a user