低代码引擎实战
基于 Low-Code Engine 快速打造高生产力的低代码研发平台
官网
https://lowcode-engine.cn/
实践
1. 把低代码部署到服务端
1 2 3 4 5 6 7 8 9
|
app.get('/lowcode', async function (req, res) { res.render('index.ejs'); })
|
2. 保存schema到服务端
1 2 3 4 5 6 7 8 9 10 11 12
|
export const saveSchema = async () => { let currentPage = 'home'; if (location.search) { currentPage = new URLSearchParams(location.search.slice(1)).get('page') || 'home' } const schema = project.exportSchema(); const url = 'http://localhost:3000/api/v1/schemas'; const response = await request2.post(url, { page: currentPage, schema}) Message.success('成功保存'); };
|
1 2 3 4 5 6 7 8 9 10 11
|
app.post('/api/v1/schemas', async function (req, res) { const { page, schema } = req.body; const response = await fs.writeFileSync(`./schemas/${page}.json`, JSON.stringify(schema)) res.send({ code: 0, data: response }); });
|
3. 从服务器获取资产包
1 2 3 4 5 6 7 8 9 10 11
|
- import assets from './assets.json' ...... + const assets = getAssets();
export const getAssets = async ()=> { return request2.get('http://localhost:3000/assets.json'); }
|
1 2 3 4 5 6 7 8 9
|
app.get('/assets.json', async function (req, res) { res.send({ code: 0, data: assets, }); })
|
4. 页面预览
1 2 3 4 5
| app.get('/preview.html', async function (req, res) { res.render('preview.ejs'); })
|
1 2 3 4 5 6 7 8 9 10
|
async function init() { const { data: { packages } } = await getAssets(); const { data: projectSchema } = await getCurrentPageSchema(); ... const { componentsMap: componentsMapArray, componentsTree } = projectSchema; ... }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
app.get('/assets.json', async function (req, res) { res.send({ code: 0, data: assets, }); })
app.get('/get-page-schema', async function (req, res) { console.log('req---', req.query) const { page } = req.query; const data = require(`./schemas/${page}.json`); res.send({ code: 0, data }); })
|
5. 新增面板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import * as React from 'react'; import { Nav } from '@alifd/next'; const { Item } = Nav; export default () => { const queryString = location.search || ''; const defaultCurrentPage = queryString.includes('home') ? 'home' : 'login'; const onSelect = (keys : string[]) => { const key: string = keys[0]; location.href = `${location.pathname}?page=${key}`; }; return <Nav type='line' selectedKeys={[defaultCurrentPage]} onSelect={onSelect}> <Item key='home' >首页</Item> <Item key='login' >登录页</Item> </Nav> }
import navPage from 'src/plugins/nav/index'; ...
skeleton.add({ index: -1, area: 'leftArea', type: 'PanelDock', name: 'navPage', content: navPage, contentProps: {}, props: { align: 'top', icon: 'kaiwenjianjia', description: '页面导航', }, });
|
6. 自定义物料
初始化物料仓库
npm init @alilc/element lowcode-material-example
1 2 3 4 5 6 7
| { "lowcode:build": "build-scripts build --config ./build.lowcode.js", } # npm run lowcode:build # npm login
|
发布完成后,查看资产包:
https://unpkg.com/browse/lowcode-material-example@1.1.3/build/lowcode/assets-prod.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
{ "packages": [ ... { "package": "lowcode-material-example", "version": "1.1.3", "library": "BizComp", "urls": [ "https://unpkg.com/lowcode-material-example@1.1.3/build/lowcode/render/default/view.js", "https://unpkg.com/lowcode-material-example@1.1.3/build/lowcode/render/default/view.css" ], "editUrls": [ "https://unpkg.com/lowcode-material-example@1.1.3/build/lowcode/view.js", "https://unpkg.com/lowcode-material-example@1.1.3/build/lowcode/view.css" ], "advancedUrls": { "default": [ "https://unpkg.com/lowcode-material-example@1.1.3/build/lowcode/render/default/view.js", "https://unpkg.com/lowcode-material-example@1.1.3/build/lowcode/render/default/view.css" ] }, "advancedEditUrls": {} } ], "components": [ ... { "exportName": "LowcodeMaterialExampleMeta", "npm": { "package": "lowcode-material-example", "version": "1.1.3" }, "url": "https://unpkg.com/lowcode-material-example@1.1.3/build/lowcode/meta.js", "urls": { "default": "https://unpkg.com/lowcode-material-example@1.1.3/build/lowcode/meta.js" }, "advancedUrls": { "default": [ "https://unpkg.com/lowcode-material-example@1.1.3/build/lowcode/meta.js" ] } } ] }
|
7. 获取远程数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| class LowcodeComponent extends Component { state = { brands: [] } componentDidMount() { fetch('http://127.0.0.1:3000/api/brands') .then(response => response.json()) .then(response => { console.log('data__', response) const { data: { list = [] } } = response; this.setState({ brands: list }) }); } componentWillUnmount() { console.log('will unmount'); } testFunc() { console.log('test func'); } onClick() { this.setState({ isShowDialog: true }) } closeDialog() { this.setState({ isShowDialog: false }) } }
|