├── icons
├── app.ico
├── app.png
├── app.icns
├── icon.icns
├── icon.ico
└── icon.png
├── public
└── index.html
├── .gitignore
├── app.yao
├── scripts
└── file
│ └── image.js
├── studio
├── table.js
├── move.js
├── remote.js
├── menu.js
├── file.js
├── dashboard.js
├── selector.js
├── model.js
├── schema.js
├── relation.js
├── hasmany.js
├── hasone.js
└── colunm.js
├── .github
└── workflows
│ ├── relaese-linux.yml
│ ├── release_get.yml
│ └── docker.yml
├── Dockerfile
├── README.md
├── models
└── admin
│ └── user.mod.yao
├── LICENSE
└── Teach.md
/icons/app.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YaoApp/yao-admin/HEAD/icons/app.ico
--------------------------------------------------------------------------------
/icons/app.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YaoApp/yao-admin/HEAD/icons/app.png
--------------------------------------------------------------------------------
/icons/app.icns:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YaoApp/yao-admin/HEAD/icons/app.icns
--------------------------------------------------------------------------------
/icons/icon.icns:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YaoApp/yao-admin/HEAD/icons/icon.icns
--------------------------------------------------------------------------------
/icons/icon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YaoApp/yao-admin/HEAD/icons/icon.ico
--------------------------------------------------------------------------------
/icons/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YaoApp/yao-admin/HEAD/icons/icon.png
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 | It works! https://yaoapps.com
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /data
2 | /logs
3 | .env
4 | ./logs/application.log
5 | ./logs/*
6 | *.log
7 | logs/
8 |
--------------------------------------------------------------------------------
/app.yao:
--------------------------------------------------------------------------------
1 | {
2 | "xgen": "1.0",
3 | "name": "::Demo Application",
4 | "short": "::Demo",
5 | "description": "::Another yao application",
6 | "version": "yao-0.10.3-dev",
7 | "adminRoot": "admin",
8 | "setup": "studio.model.Create",
9 | "menu": {
10 | "process": "flows.app.menu",
11 | "args": [
12 | "demo"
13 | ]
14 | },
15 | "optional": {
16 | "hideNotification": true,
17 | "hideSetting": false,
18 | "neo": {
19 | "api": "/neo"
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/scripts/file/image.js:
--------------------------------------------------------------------------------
1 | function ImagesView(data) {
2 | if (!data || !data.length) {
3 | return [];
4 | }
5 | if (data.indexOf(`[`) != -1) {
6 | //console.log(data);
7 | data = JSON.parse(data);
8 | } else {
9 | data = data.split(",");
10 | }
11 | return data;
12 | }
13 |
14 | function ImagesEdit(value, type, name, model_name) {
15 | // console.log([value, type, name, model_name]);
16 | var dsl = Process("schemas.default.TableGet", model_name);
17 | for (var i in dsl.columns) {
18 | if (dsl["columns"][i]["name"] == name) {
19 | if (dsl["columns"][i]["type"] == "json") {
20 | return value[name];
21 | } else {
22 | return JSON.stringify(value[name]);
23 | }
24 | }
25 | }
26 | return value[name];
27 | }
28 |
--------------------------------------------------------------------------------
/studio/table.js:
--------------------------------------------------------------------------------
1 | //yao studio run table.Create
2 | /**
3 | * 创建表格
4 | */
5 | function Create(model_dsl) {
6 | var fs = new FS("dsl");
7 | for (var i in model_dsl) {
8 | var table_name = model_dsl[i]["table"]["name"] + ".tab.yao";
9 | //var dsl = toTable(model_dsl[i]);
10 | var dsl = Studio("colunm.toTable", model_dsl[i]);
11 | var table = JSON.stringify(dsl);
12 | Studio("move.Move", "tables", table_name);
13 | fs.WriteFile("/tables/" + table_name, table);
14 |
15 | ///
16 | var form_name = model_dsl[i]["table"]["name"] + ".form.yao";
17 | var form_dsl = Studio("colunm.toForm", model_dsl[i]);
18 | var form = JSON.stringify(form_dsl);
19 | Studio("move.Move", "forms", form_name);
20 | fs.WriteFile("/forms/" + form_name, form);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/studio/move.js:
--------------------------------------------------------------------------------
1 | /**
2 | * 文件复制移动逻辑
3 | */
4 | function Move(dir, name) {
5 | const fs = new FS("dsl");
6 | var base_dir = ".trash";
7 | // 判断文件夹是否存在.不存在就创建
8 | Mkdir(base_dir);
9 | var new_dir = parseInt(Date.now() / 1000);
10 | // models的文件移动到
11 | var target_name = dir + "/" + name;
12 | // 如果表已经存在,则
13 | if (Exists(dir, name)) {
14 | Mkdir(base_dir + "/" + new_dir);
15 | Copy(target_name, base_dir + "/" + new_dir, name);
16 |
17 | // 复制完成后,删除文件
18 | fs.Remove(target_name);
19 | } else {
20 | return false;
21 | }
22 | }
23 | function Mkdir(name) {
24 | const fs = new FS("dsl");
25 | var res = fs.Exists(name);
26 | if (res !== true) {
27 | fs.Mkdir(name);
28 | }
29 | }
30 |
31 | function Copy(from, to, name) {
32 | const fs = new FS("dsl");
33 | fs.Copy(from, to + "/" + name);
34 | }
35 | /**
36 | * 查看模型是否存在
37 | * @param {*} file_name
38 | * @returns
39 | */
40 | function Exists(dir, file_name) {
41 | const fs = new FS("dsl");
42 | file_name = file_name;
43 | var res = fs.Exists(dir + "/" + file_name);
44 | return res;
45 | }
46 |
--------------------------------------------------------------------------------
/.github/workflows/relaese-linux.yml:
--------------------------------------------------------------------------------
1 | name: Release Linux
2 |
3 | on:
4 | workflow_dispatch:
5 | inputs:
6 | tags:
7 | description: "Version tags"
8 | push:
9 | branches: [main]
10 |
11 | jobs:
12 | release:
13 | runs-on: "ubuntu-latest"
14 | steps:
15 | - name: Install coscmd
16 | run: sudo pip3 install coscmd
17 |
18 | - name: Checkout Code
19 | uses: actions/checkout@v2
20 |
21 | - name: pack for Station (@Silicon Valley)
22 | run: |
23 | rm -rf ./.git
24 | rm -rf ./.github
25 | zip -q -r latest.zip *
26 |
27 | - name: Configure COS For Silicon Valley
28 | env:
29 | SECRET_ID: ${{ secrets.COS_ID }}
30 | SECRET_KEY: ${{ secrets.COS_KEY }}
31 | BUCKET: mirrors-1252011659
32 | REGION: ap-beijing
33 | run: |
34 | ls -l .
35 | coscmd config -a $SECRET_ID -s $SECRET_KEY -b $BUCKET -r $REGION
36 | - name: Push To Silicon Valley
37 | run: |
38 | for file in ./latest.zip; do coscmd upload $file /apps/yaoapp/yao-admin/docker/; done;
39 |
--------------------------------------------------------------------------------
/.github/workflows/release_get.yml:
--------------------------------------------------------------------------------
1 | name: Release Get
2 |
3 | on:
4 | workflow_dispatch:
5 | inputs:
6 | tags:
7 | description: "Version tags"
8 | push:
9 | branches: [main]
10 | env:
11 | IMAGE_NAME: ${{ github.event.repository.name }}
12 | jobs:
13 | release:
14 | runs-on: "ubuntu-latest"
15 | steps:
16 | - name: Install coscmd
17 | run: sudo pip3 install coscmd
18 |
19 | - name: Checkout Code
20 | uses: actions/checkout@v2
21 |
22 | - name: pack for Station (@Silicon Valley)
23 | run: |
24 | mkdir ../app
25 | rm -rf ./.git
26 | rm -rf ./.github
27 | cp -r * ../app
28 | cd ..
29 | zip -q -r latest.zip ./app
30 |
31 | - name: Configure COS For Silicon Valley
32 | env:
33 | SECRET_ID: ${{ secrets.COS_ID }}
34 | SECRET_KEY: ${{ secrets.COS_KEY }}
35 | BUCKET: mirrors-1252011659
36 | REGION: ap-beijing
37 | run: |
38 | ls -l .
39 | coscmd config -a $SECRET_ID -s $SECRET_KEY -b $BUCKET -r $REGION
40 | - name: Push To Silicon Valley
41 | run: |
42 | for file in ../latest.zip; do coscmd upload $file /apps/yaoapp/yao-admin/; done;
43 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | #docker build --platform linux/amd64 --tag yao-admin .
2 | #docker run -d --restart unless-stopped --name yao-admin -p 5099:5099 yao-admin
3 | FROM yaoapp/yao:0.10.3-amd64
4 | ARG VERSION
5 | WORKDIR /data
6 |
7 | #COPY yao /usr/local/bin/yao
8 | #RUN apk add git
9 | RUN addgroup -S -g 1000 yao && adduser -S -G yao -u 999 yao
10 | RUN mkdir -p /data/app && curl -fsSL "https://mirrors-1252011659.cos.ap-beijing.myqcloud.com/apps/yaoapp/yao-admin/docker/latest.zip" > /data/app/latest.zip && \
11 | unzip /data/app/latest.zip
12 | RUN rm -rf /data/app/.git && \
13 | rm -rf /data/app/data && \
14 | rm -rf /data/app/db && \
15 | rm -rf /data/app/*.sh && \
16 | rm -rf /data/app/.env && \
17 | mkdir -p /data/logs && \
18 | mkdir -p /data/charts && \
19 | mkdir -p /data/tables && \
20 | mkdir -p /data/models && \
21 | mkdir -p /data/forms && \
22 | mkdir -p /data/db && \
23 | mkdir -p /data/app/data && \
24 | mkdir -p /data/flows && \
25 | mkdir -p /data/flows/app && \
26 | mkdir -p /data/models && \
27 | chown -R yao:yao /data/app && \
28 | chown -R yao:yao /data/app/data && \
29 | chown -R yao:yao /data/logs && \
30 | chown -R yao:yao /data/db && \
31 | chmod +x /usr/local/bin/yao
32 |
33 | USER root
34 | VOLUME [ "/data" ]
35 | WORKDIR /data
36 | EXPOSE 5099
37 | CMD ["/usr/local/bin/yao", "start"]
38 |
--------------------------------------------------------------------------------
/.github/workflows/docker.yml:
--------------------------------------------------------------------------------
1 | name: Build and push docker images
2 | on:
3 | workflow_dispatch:
4 | inputs:
5 | tags:
6 | description: "Comment"
7 | push:
8 | branches: [main]
9 | paths:
10 | - "app.yao"
11 | - ".github/workflows/docker.yml"
12 | env:
13 | IMAGE_ORG: yaoapp
14 | IMAGE_NAME: ${{ github.event.repository.name }}
15 | REPO_DOCKERFILES: ${{ github.repository_owner }}/dockerfiles
16 | VERSION: 1.0.0
17 | jobs:
18 | build:
19 | runs-on: ubuntu-latest
20 | steps:
21 | - name: Checkout Application Source
22 | uses: actions/checkout@v2
23 |
24 | - name: Get Version
25 | run: |
26 | VERSION=$(cat app.yao |grep version |awk -F : '{print $2}' | sed 's/"//g' | sed 's/,//g' | sed 's/ //g')
27 | if [ -z "$VERSION" ]; then
28 | VERSION=1.0.0
29 | fi
30 | echo VERSION=$VERSION >> $GITHUB_ENV
31 |
32 | - name: Check Version
33 | run: echo $VERSION
34 |
35 | - name: Set up QEMU
36 | uses: docker/setup-qemu-action@v1
37 |
38 | - name: Set up Docker Buildx
39 | uses: docker/setup-buildx-action@v1
40 |
41 | - name: Login to DockerHub
42 | uses: docker/login-action@v1
43 | with:
44 | username: ${{ secrets.DOCKER_USER }}
45 | password: ${{ secrets.DOCKER_PASSWORD }}
46 |
47 | - name: Build & Push Docker Image AMD64
48 | uses: docker/build-push-action@v2
49 | with:
50 | context: ./
51 | platforms: linux/amd64
52 | build-args: |
53 | VERSION=${{ env.VERSION }}
54 | push: true
55 | tags: ${{ env.IMAGE_ORG }}/${{ env.IMAGE_NAME }}:${{ env.VERSION }}-amd64
56 |
--------------------------------------------------------------------------------
/studio/remote.js:
--------------------------------------------------------------------------------
1 | function select(relation_name, relation) {
2 | var column = Process("schemas.default.TableGet", relation.model);
3 | var column = column.columns;
4 | var res = Speculation(column);
5 | if (!res) {
6 | var res = Other(column);
7 | }
8 | CreateScripts(relation_name, res, relation);
9 | return res;
10 | }
11 |
12 | /**
13 | * 字段推测
14 | * @param {*} column
15 | * @returns
16 | */
17 | function Speculation(column) {
18 | var target = ["name", "title"];
19 | for (var i in target) {
20 | var res = GetTarget(target[i], column);
21 | if (res) {
22 | return res;
23 | }
24 | }
25 | return false;
26 | }
27 | function GetTarget(target, column) {
28 | for (var i in column) {
29 | if (column[i].name.indexOf(target) != -1) {
30 | return column[i].name;
31 | }
32 | }
33 | return false;
34 | }
35 |
36 | /**
37 | * 没有其他的话,就找个string类型的
38 | * @param {*} column
39 | * @returns
40 | */
41 | function Other(column) {
42 | for (var i in column) {
43 | if (column[i].type == "string") {
44 | return column[i].name;
45 | }
46 | }
47 | return "id";
48 | }
49 |
50 | /**
51 | * 生成查询的js脚本
52 | * @param {*} relation_name
53 | * @param {*} name
54 | */
55 | function CreateScripts(relation_name, name, relation) {
56 | var field_name = relation_name + ".js";
57 | var fs = new FS("script");
58 | var form_dsl = `function GetSelect() {
59 | var query = new Query();
60 | var res = query.Get({
61 | select: ["id as value", "${name} as label"],
62 | from: "${relation.model}",
63 | });
64 | return res;
65 | }
66 | `;
67 | var dir = relation.model + "/" + field_name;
68 | //console.log(form_dsl);
69 |
70 | Studio("move.Move", "scripts", field_name);
71 | fs.WriteFile("/" + dir, form_dsl);
72 | }
73 |
74 | // function GetSelect() {
75 | // var query = new Query();
76 | // var res = query.Get({
77 | // select: ["id as value", "${name} as label"],
78 | // from: "${relation_name}",
79 | // });
80 | // return res;
81 | // }
82 |
--------------------------------------------------------------------------------
/studio/menu.js:
--------------------------------------------------------------------------------
1 | function Create(model_dsl) {
2 | var insert = [];
3 | var child = [];
4 | var total = model_dsl.length;
5 | insert.push({
6 | blocks: 0,
7 | icon: "icon-activity",
8 | id: 1,
9 | name: "数据模型",
10 | parent: null,
11 | path: "/x/Chart/dashboard",
12 | visible_menu: 0,
13 | });
14 |
15 | for (var i in model_dsl) {
16 | var name = model_dsl[i]["table"]["name"];
17 |
18 | var item = {
19 | name: model_dsl[i].name,
20 | path: "/x/Table/" + name,
21 | icon: "",
22 | rank: i + 1,
23 | status: "enabled",
24 | parent: null,
25 | visible_menu: 0,
26 | blocks: 0,
27 | id: (i + 1) * 10,
28 | model: name,
29 | children: [],
30 | };
31 | if (total >= 10) {
32 | item.visible_menu = 1;
33 | // child.push(item);
34 | if (i == 0) {
35 | var icon = "icon-align-justify";
36 | item.icon = icon;
37 | insert[1] = item;
38 | } else {
39 | insert[1]["children"].push(item);
40 | }
41 | } else {
42 | var icon = GetIcon(name);
43 | item.icon = icon;
44 | insert.push(item);
45 | }
46 | }
47 | Studio("move.Mkdir", "flows");
48 | Studio("move.Mkdir", "flows/app");
49 | var fs = new FS("dsl");
50 |
51 | var dsl = {
52 | name: "APP Menu",
53 | nodes: [],
54 | output: { items: insert, setting: [] },
55 | };
56 |
57 | var dsl = JSON.stringify(dsl);
58 | fs.WriteFile("/flows/app/menu.flow.yao", dsl);
59 |
60 | // 创建看板
61 | if (total >= 10) {
62 | Studio("dashboard.Create", insert, 1);
63 | } else {
64 | Studio("dashboard.Create", insert, 2);
65 | }
66 |
67 | //Process("models.xiang.menu.insert", columns, insert);
68 | }
69 |
70 | /**yao studio run menu.icon user
71 | * 获取菜单图标
72 | * @param {*} name
73 | */
74 | function GetIcon(name) {
75 | var url = "https://brain.yaoapps.com/api/icon/search?name=" + name;
76 | let response = http.Get(url);
77 | // let response = Process("xiang.network.Get", url, {}, {});
78 | if (response.status == 200) {
79 | return response.data.data;
80 | }
81 | return "icon-box";
82 | }
83 |
--------------------------------------------------------------------------------
/studio/file.js:
--------------------------------------------------------------------------------
1 | // 图片组件推测
2 | function File(column, component) {
3 | var guard = [
4 | "img",
5 | "image",
6 | "_pic",
7 | "pic_",
8 | "picture",
9 | "oss",
10 | "photo",
11 | "icon",
12 | "avatar",
13 | "Img",
14 | "logo",
15 | "cover",
16 | "url",
17 | "gallery",
18 | "pic",
19 | ];
20 | const name = column.name;
21 | for (var i in guard) {
22 | if (name.indexOf(guard[i]) != -1) {
23 | var component = {
24 | is_image: true,
25 | bind: name,
26 | view: {
27 | type: "Image",
28 | compute: "scripts.file.image.ImagesView",
29 | props: {},
30 | },
31 | edit: {
32 | type: "Upload",
33 | //compute: "scripts.file.image.ImagesEdit",
34 | props: { filetype: "image", disabled: true },
35 | },
36 | };
37 | return component;
38 | }
39 | }
40 |
41 | return component;
42 | }
43 |
44 | function FormFile(column, component, model_dsl) {
45 | var guard = [
46 | "img",
47 | "image",
48 | "_pic",
49 | "pic_",
50 | "picture",
51 | "oss",
52 | "photo",
53 | "icon",
54 | "avatar",
55 | "Img",
56 | "logo",
57 | "cover",
58 | "url",
59 | "gallery",
60 | "pic",
61 | ];
62 | const name = column.name;
63 | for (var i in guard) {
64 | if (name.indexOf(guard[i]) != -1) {
65 | var component = {
66 | is_image: true,
67 | bind: name,
68 | view: {
69 | type: "Image",
70 | compute: "scripts.file.image.ImagesView",
71 | props: {},
72 | },
73 | edit: {
74 | type: "Upload",
75 | compute: {
76 | process: "scripts.file.image.ImagesEdit",
77 | args: ["$C(row)", "$C(type)", name, model_dsl["table"]["name"]],
78 | },
79 | // compute: "scripts.file.image.ImagesEdit",
80 | props: {
81 | filetype: "image",
82 | $api: { process: "fs.system.Upload" },
83 | },
84 | },
85 | };
86 | return component;
87 | }
88 | }
89 |
90 | return component;
91 | }
92 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
Yao Admin
3 |
4 |
5 |
6 | Website
7 |
8 | ·
9 |
10 | Producthunt
11 |
12 | ·
13 |
14 | Twitter
15 |
16 | ·
17 |
18 | Discord
19 |
20 |
21 |
22 | 真零代码,使用 Yao-Admin 连接数据库,你就有了一个管理后台。
23 |
24 | 
25 |
26 | Yao Admin 采用 Yao 应用引擎开发,借助强大的 Yao Studio API 和 Yao Brain 平台,自动生成数据模型、表格、表单和图表界面;实现连接数据库,全自动生成一套功能完整的管理后台。适用于快速搭建应用管理后台和 API 接口,快速制作应用 API 接口等场景。
27 |
28 | ### 为什么使用 Yao Admin ?
29 |
30 | 1.**零代码** 连接数据库直接生成管理后台。
31 |
32 | 2.**可迭代** 可根据业务需求,按需修改管理界面
33 |
34 | 3.**接口生成** 低代码方式制作 API 接口,支持多种鉴权方式
35 |
36 | ### 安装
37 |
38 | **首先准备好一个有数据表的数据库**
39 |
40 | #### 使用 Yao
41 |
42 |
43 |
44 | [安装 YAO](https://yaoapps.com/doc/%E4%BB%8B%E7%BB%8D/%E5%AE%89%E8%A3%85%E8%B0%83%E8%AF%95)
45 |
46 | ```bash
47 | mkdir -p /app/data
48 | cd /app/data
49 | yao get yaoapp/yao-admin
50 | yao start
51 | ```
52 |
53 | 根据命令行提示配置后登录管理后台
54 |
55 | 管理后台地址: `http://:/yao/`
56 |
57 | 示例:` http://127.0.0.1:5099/yao`
58 |
59 | 进入到页面后配置数据库,选择之前准备好的数据库,然后等待配置完成,进入登录页面
60 |
61 | 默认用户名: `xiang@iqka.com`
62 |
63 | 默认密码: `A123456p+`
64 |
65 | #### 使用 Docker
66 |
67 | [安装 Docker](https://docs.docker.com/get-docker/)
68 |
69 | ```
70 | docker pull yaoapp/yao-admin:0.10.3-dev-amd64
71 | ```
72 |
73 | ```bash
74 | docker run -d --restart unless-stopped --name yao-admin -p 5099:5099 yaoapp/yao-admin:0.10.3-dev-amd64
75 | ```
76 |
77 | 根据命令行提示配置后登录管理后台
78 |
79 | 管理后台地址: `http://:/yao/`
80 |
81 | 示例:` http://127.0.0.1:5099/yao`
82 |
83 | 默认用户名: `xiang@iqka.com`
84 |
85 | 默认密码: `A123456p+`
86 |
87 | ## 效果演示
88 |
89 | 我们找了一些优秀的开源项目,测试 Yao-Admin 生成管理后台的实际效果。
90 |
91 | #### mall 商城管理后台
92 |
93 | Github 地址 [github.com/macrozheng/mall](https://github.com/macrozheng/mall)
94 |
95 | 
96 |
97 | #### litemall 小程序商城管理后台
98 |
99 | Github 地址 [github.com/linlinjava/litemall](https://github.com/linlinjava/litemall)
100 |
101 | 
102 |
103 | #### akaunting 管理后台
104 |
105 | Github 地址 [github.com/akaunting/akaunting](https://github.com/akaunting/akaunting)
106 |
107 | 
108 |
--------------------------------------------------------------------------------
/studio/dashboard.js:
--------------------------------------------------------------------------------
1 | function Create(menu_arr, type) {
2 | var fs = new FS("dsl");
3 |
4 | Studio("move.Move", "charts", "dashboard.chart.yao");
5 | var dsl = Dsl(menu_arr, type);
6 | var dsl = JSON.stringify(dsl);
7 | fs.WriteFile("/charts/" + "dashboard.chart.yao", dsl);
8 | }
9 | function Dsl(menu_arr, type) {
10 | var dsl = {
11 | name: "数据图表",
12 | action: {
13 | data: { process: "scripts.dashboard.Data", default: ["2022-09-20"] },
14 | },
15 | layout: {
16 | operation: {
17 | actions: [],
18 | },
19 | filter: {},
20 | chart: {
21 | columns: [],
22 | },
23 | },
24 | fields: {
25 | filter: {},
26 | chart: {},
27 | },
28 | };
29 | var chart = {
30 | 表格数量: {
31 | bind: "table_count",
32 |
33 | view: { type: "Number", props: { unit: "个" } },
34 | },
35 | 模型数量: {
36 | bind: "model_count",
37 | view: { type: "Number", props: { unit: "个" } },
38 | },
39 | };
40 | var columns = [
41 | { name: "表格数量", width: 12 },
42 | { name: "模型数量", width: 12 },
43 | ];
44 | var script = {
45 | table_count: 0,
46 | model_count: 0,
47 | };
48 |
49 | // 说明是二级菜单
50 | if (type == 1) {
51 | var temp = menu_arr[1]["children"];
52 | script.table_count = temp.length;
53 | script.model_count = script.table_count;
54 | temp.forEach((col) => {
55 | if (col.id != 1) {
56 | var title = col.name + "(" + col.model + ")" + "记录数";
57 | script[col.model] = GetCount(col.model);
58 | chart[title] = {
59 | bind: col.model,
60 | link: "/x/Table/" + col.model,
61 | view: { type: "Number", props: { unit: "条" } },
62 | };
63 | columns.push({ name: title, width: 6 });
64 | }
65 | });
66 | } else {
67 | script.table_count = menu_arr.length - 1;
68 | script.model_count = script.table_count;
69 |
70 | menu_arr.forEach((col) => {
71 | if (col.id != 1) {
72 | var title = col.name + "(" + col.model + ")" + "记录数";
73 | script[col.model] = GetCount(col.model);
74 | chart[title] = {
75 | bind: col.model,
76 | link: "/x/Table/" + col.model,
77 | view: { type: "Number", props: { unit: "条" } },
78 | };
79 | columns.push({ name: title, width: 6 });
80 | }
81 | });
82 | }
83 |
84 | dsl.layout.chart.columns = columns;
85 | dsl.fields.chart = chart;
86 | WriteScript(script);
87 | return dsl;
88 | }
89 | function WriteScript(data) {
90 | var data = JSON.stringify(data);
91 | var sc = new FS("script");
92 | var scripts = `function Data() {
93 | return ${data}
94 | }`;
95 | sc.WriteFile("/dashboard.js", scripts);
96 | }
97 |
98 | function GetCount(model) {
99 | try {
100 | var query = new Query();
101 | var res = query.Get({
102 | select: [":COUNT(id) as 数量"],
103 | from: model,
104 | });
105 | if (res && res.length && res[0]["数量"] > 0) {
106 | return res[0]["数量"];
107 | }
108 | } catch (e) {
109 | return 0;
110 | }
111 |
112 | return 0;
113 | }
114 |
--------------------------------------------------------------------------------
/studio/selector.js:
--------------------------------------------------------------------------------
1 | /**
2 | * 把hasOne变成下拉选择
3 | * @param {*} column
4 | * @param {*} model_dsl
5 | * @param {*} component
6 | * @returns
7 | */
8 | function Select(column, model_dsl, component) {
9 | const props = column.props || {};
10 | const title = column.label;
11 | const name = column.name;
12 |
13 | const bind = `${name}`;
14 | var relation = model_dsl.relations;
15 | for (var i in relation) {
16 | if (relation[i].type == "hasOne" && column.name == relation[i]["foreign"]) {
17 | var field = Studio("remote.select", i, relation[i]);
18 | var component = {
19 | is_select: true,
20 | bind: i + "." + field,
21 | view: { props: {}, type: "Text" },
22 | edit: {
23 | type: "Select",
24 | props: {
25 | xProps: {
26 | $remote: {
27 | process:
28 | "scripts." + relation[i].model + "." + i + ".GetSelect",
29 | // process: "models." + relation[i]["model"] + ".Get",
30 | query: {},
31 | },
32 | },
33 | },
34 | },
35 | };
36 | component = Withs(component, i);
37 | return component;
38 | }
39 | }
40 | return component;
41 | }
42 |
43 | function EditSelect(column, model_dsl, component) {
44 | const props = column.props || {};
45 | const title = column.label;
46 | const name = column.name;
47 |
48 | const bind = `${name}`;
49 | var relation = model_dsl.relations;
50 | for (var i in relation) {
51 | if (relation[i].type == "hasOne" && column.name == relation[i]["foreign"]) {
52 | var field = Studio("remote.select", i, relation[i]);
53 | var component = {
54 | bind: bind,
55 | view: { props: {}, type: "Text" },
56 | edit: {
57 | type: "Select",
58 | props: {
59 | xProps: {
60 | $remote: {
61 | process:
62 | "scripts." + relation[i].model + "." + i + ".GetSelect",
63 | // process: "models." + relation[i]["model"] + ".Get",
64 | query: {},
65 | },
66 | },
67 | },
68 | },
69 | };
70 | component = Withs(component, i);
71 | return component;
72 | }
73 | }
74 | return component;
75 | }
76 |
77 | function Withs(component, relation_name) {
78 | // "option": { "withs": { "user": {} } }
79 |
80 | var withs = [];
81 | withs.push({
82 | name: relation_name,
83 | });
84 | component.withs = withs;
85 | return component;
86 | }
87 |
88 | /**
89 | * 把hasMany变成列表
90 | */
91 | function Table(form_dsl, model_dsl) {
92 | var relation = model_dsl.relations;
93 | for (var i in relation) {
94 | var translate = Studio("relation.translate", i);
95 | if (relation[i].type == "hasMany") {
96 | form_dsl.fields.form["表格" + translate] = {
97 | bind: "id",
98 | edit: {
99 | type: "Table",
100 | props: {
101 | model: relation[i]["model"],
102 | query: {},
103 | },
104 | },
105 | };
106 | form_dsl.layout.form.sections.push({
107 | title: "表格" + translate + "信息",
108 | desc: "表格" + translate + "信息",
109 | columns: [{ name: "表格" + translate, width: 24 }],
110 | });
111 | }
112 | }
113 | return form_dsl;
114 | }
115 |
--------------------------------------------------------------------------------
/studio/model.js:
--------------------------------------------------------------------------------
1 | //yao studio run model.Create
2 | /**
3 | * 创建模型
4 | */
5 | function Create() {
6 | console.log(parseInt(Date.now() / 1000));
7 | var model_dsl = Studio("schema.Relation");
8 |
9 | var fs = new FS("dsl");
10 | for (var i in model_dsl) {
11 | var table_name = model_dsl[i]["table"]["name"] + ".mod.yao";
12 | var table = JSON.stringify(model_dsl[i]);
13 | Studio("move.Move", "models", table_name);
14 | fs.WriteFile("/models/" + table_name, table);
15 | }
16 | Process("models.admin.user.migrate");
17 | console.log(parseInt(Date.now() / 1000));
18 | // 创建表格dsl
19 | Studio("table.Create", model_dsl);
20 | version10_0_3();
21 | login();
22 | // 创建菜单
23 | Studio("menu.Create", model_dsl);
24 | console.log(parseInt(Date.now() / 1000));
25 | }
26 |
27 | //创建单个表格的studio
28 | ///yao studio run model.CreateOne address
29 | function CreateOne(model_name) {
30 | // console.log("进入studio");
31 | //console.log(model_name);
32 | var fs = new FS("dsl");
33 | var model_dsl = [];
34 |
35 | model_dsl.push(JSON.parse(fs.ReadFile("models/" + model_name + ".mod.yao")));
36 |
37 | for (var i in model_dsl) {
38 | var table_name = model_dsl[i]["table"]["name"] + ".mod.yao";
39 | var table = JSON.stringify(model_dsl[i]);
40 | Studio("move.Move", "models", table_name);
41 | fs.WriteFile("/models/" + table_name, table);
42 | }
43 | // 创建表格dsl
44 | Studio("table.Create", model_dsl);
45 | //version10_0_2();
46 | //login();
47 | }
48 |
49 | /**
50 | * 写入10.2版本的
51 | */
52 | function version10_0_2() {
53 | var fs = new FS("dsl");
54 |
55 | fs.WriteFile(
56 | "app.json",
57 | JSON.stringify({
58 | xgen: "1.0",
59 | name: "::Demo Application",
60 | short: "::Demo",
61 | description: "::Another yao application",
62 | version: "0.10.2",
63 | menu: {
64 | process: "flows.app.menu",
65 | args: ["demo"],
66 | },
67 | setup: "studio.model.Create",
68 | adminRoot: "yao",
69 | optional: {
70 | hideNotification: true,
71 | hideSetting: false,
72 | },
73 | })
74 | );
75 | }
76 | /**
77 | * 写入10.2版本的
78 | */
79 | function version10_0_3() {
80 | var fs = new FS("dsl");
81 |
82 | fs.WriteFile(
83 | "app.yao",
84 | JSON.stringify({
85 | xgen: "1.0",
86 | name: "::Demo Application",
87 | short: "::Demo",
88 | description: "::Another yao application",
89 | version: "yao-0.10.3-dev",
90 | adminRoot: "admin",
91 | setup: "studio.model.Create",
92 | menu: {
93 | process: "flows.app.menu",
94 | args: ["demo"],
95 | },
96 | optional: {
97 | hideNotification: true,
98 | hideSetting: false,
99 | neo: {
100 | api: "/neo",
101 | },
102 | },
103 | })
104 | );
105 | }
106 | function login() {
107 | var fs = new FS("dsl");
108 | // var menu = Process("models.xiang.menu.get", {
109 | // limit: 1,
110 | // });
111 | var table_name = "admin.login.yao";
112 | var table = JSON.stringify({
113 | name: "::Admin Login",
114 | action: {
115 | process: "yao.login.Admin",
116 | args: [":payload"],
117 | },
118 | layout: {
119 | entry: "/x/Chart/dashboard",
120 | // captcha: "yao.utils.Captcha",
121 | //cover: "/assets/images/login/cover.svg",
122 | slogan: "::Make Your Dream With Yao App Engine",
123 | site: "https://yaoapps.com?from=yao-admin",
124 | },
125 | });
126 | Studio("move.Move", "logins", table_name);
127 | fs.WriteFile("/logins/" + table_name, table);
128 | }
129 |
--------------------------------------------------------------------------------
/studio/schema.js:
--------------------------------------------------------------------------------
1 | /**yao studio run schema.GetTable role
2 | * 获取单个表字段
3 | * @param {*} name
4 | * @returns
5 | */
6 | function GetTable(name) {
7 | let res = Process("schemas.default.TableGet", name);
8 | return res;
9 | }
10 | /**
11 | * 获取所有表格名称
12 | */
13 | function GetTableName() {
14 | let res = Process("schemas.default.Tables");
15 | return res;
16 | }
17 |
18 | /**
19 | * 分析关联关系处理器
20 | * @param {*} type
21 | * yao run scripts.schema.Relation
22 | */
23 | function Relation() {
24 | var all_table = GetTableName();
25 | var table_num = all_table.length;
26 | if (table_num > 80) {
27 | log.Error("Data tables cannot exceed 80!");
28 | throw new Exception("Data tables cannot exceed 80!", 500);
29 | }
30 | var table_arr = [];
31 |
32 | // 不需要的表格白名单
33 | var guards = ["xiang_menu", "xiang_user", "xiang_workflow", "pet"];
34 | var prefix = TablePrefix(all_table);
35 |
36 | for (var i in all_table) {
37 | if (guards.indexOf(all_table[i]) != -1) {
38 | continue;
39 | }
40 |
41 | var col = GetTable(all_table[i]);
42 | //col.columns = Studio("relation.BatchTranslate", col.columns);
43 | // console.log(col.columns);
44 | // return;
45 |
46 | var id_flag = false;
47 | for (var j in col.columns) {
48 | if (col.columns[j]["name"] == "id" || col.columns[j]["name"] === "ID") {
49 | id_flag = true;
50 | }
51 | // col.columns[j]["label"] = FieldHandle(col.columns[j]["label"]);
52 | if (col.columns[j]["type"] == "dateTime") {
53 | col.columns[j]["type"] = "datetime";
54 | }
55 | if (col.columns[j]["type"] == "BIT" || col.columns[j]["type"] == "bit") {
56 | col.columns[j]["type"] = "boolean";
57 | }
58 | if (
59 | col.columns[j]["type"] == "MEDIUMINT" ||
60 | col.columns[j]["type"] == "mediumint"
61 | ) {
62 | col.columns[j]["type"] = "tinyInteger";
63 | }
64 | }
65 | // 如果没有id的表就不要显示了
66 | if (!id_flag) {
67 | all_table.splice(i, 1);
68 | continue;
69 | }
70 |
71 | // 去除表前缀
72 | var trans = ReplacePrefix(prefix, all_table[i]);
73 |
74 | col.name = trans;
75 | //col.name = Studio("relation.translate", trans);
76 | col.decription = col.name;
77 | col.table = {};
78 | col.table.name = all_table[i];
79 | col.table.comment = col.name;
80 | col.relations = {};
81 | var parent = Studio("relation.parent", all_table[i], col.columns, col);
82 | var parent = Studio("relation.child", all_table[i], col.columns, parent);
83 |
84 | table_arr.push(parent);
85 | }
86 |
87 | table_arr = Studio("relation.other", table_arr);
88 |
89 | table_arr = Studio("relation.BatchModel", table_arr);
90 |
91 | return table_arr;
92 | }
93 |
94 | function FieldHandle(label) {
95 | if (label.length >= 8) {
96 | var label = label.split(";")[0];
97 | var label = label.split(";")[0];
98 | var label = label.split(",")[0];
99 | var label = label.split(";")[0];
100 | var label = label.split("。")[0];
101 | var label = label.split(":")[0];
102 | var label = label.split(":")[0];
103 | var label = label.split(",")[0];
104 | var label = label.split(",")[0];
105 | }
106 |
107 | return label;
108 | }
109 | //yao studio run schema.TablePrefix
110 | function TablePrefix(all_table_name) {
111 | if (!all_table_name || !all_table_name.length) {
112 | var all_table_name = GetTableName();
113 | }
114 | var prefix = [];
115 | for (var i in all_table_name) {
116 | var temp = all_table_name[i].split("_");
117 | // 如果表格下划线有3个以上,有可能有表前缀
118 | if (temp.length >= 3 && prefix.indexOf(temp[0]) == -1) {
119 | prefix.push(temp[0]);
120 | }
121 | }
122 | return prefix;
123 | }
124 |
125 | // 把表前缀替换掉
126 | function ReplacePrefix(prefix, target) {
127 | if (prefix.length) {
128 | for (var i in prefix) {
129 | target = target.replace(prefix[i] + "_", "");
130 | }
131 | }
132 | return target;
133 | }
134 |
--------------------------------------------------------------------------------
/studio/relation.js:
--------------------------------------------------------------------------------
1 | const parents = ["parent", "parent_id", "pid"];
2 | const children = ["children", "children_id", "child", "child_id"];
3 | /**
4 | * 关联关系分析同一个表中关联关系
5 | * @param {*} model_name
6 | * @param {*} columns
7 | * @param {*} table_struct
8 | */
9 | function child(model_name, columns, table_struct) {
10 | for (var i in columns) {
11 | if (columns[i]["type"] != "integer") {
12 | continue;
13 | }
14 | if (children.indexOf(columns[i]["name"]) != -1) {
15 | table_struct.relations.children = {
16 | type: "hasMany",
17 | model: model_name,
18 | key: columns[i]["name"],
19 | foreign: "id",
20 | query: {},
21 | };
22 | return table_struct;
23 | }
24 | }
25 | return table_struct;
26 | }
27 |
28 | /**
29 | * 分析子集
30 | * @param {*} model_name
31 | * @param {*} columns
32 | * @param {*} table_struct
33 | * @returns
34 | */
35 | function parent(model_name, columns, table_struct) {
36 | for (var i in columns) {
37 | if (columns[i]["type"] != "integer") {
38 | continue;
39 | }
40 | if (parents.indexOf(columns[i]["name"]) != -1) {
41 | table_struct.relations.parent = {
42 | type: "hasOne",
43 | model: model_name,
44 | key: "id",
45 | foreign: columns[i]["name"],
46 | query: {},
47 | };
48 | return table_struct;
49 | }
50 | }
51 | return table_struct;
52 | }
53 |
54 | function other(all_table_struct) {
55 | for (var i in all_table_struct) {
56 | var temp = all_table_struct[i]["columns"];
57 | all_table_struct = Studio(
58 | "hasone.hasOne",
59 | all_table_struct[i]["table"]["name"],
60 | all_table_struct
61 | );
62 | for (var j in temp) {
63 | all_table_struct = Studio(
64 | "hasmany.hasMany",
65 | all_table_struct[i]["table"]["name"],
66 | temp[j].name,
67 | all_table_struct
68 | );
69 | }
70 | }
71 | return all_table_struct;
72 | }
73 |
74 | // yao studio run relation.translate icon
75 | function translate(keywords) {
76 | if (keywords == "id" || keywords == "ID") {
77 | return "id";
78 | }
79 | var keywords = keywords.split("_");
80 | //console.log(keywords);
81 | var url = "https://brain.yaoapps.com/api/keyword/column";
82 | let response = http.Post(url, {
83 | keyword: keywords,
84 | });
85 | // let response = Process(
86 | // "xiang.network.PostJSON",
87 | // url,
88 | // {
89 | // keyword: keywords,
90 | // },
91 | // {}
92 | // );
93 | var res = keywords;
94 | if (response.status == 200) {
95 | if (response.data.data) {
96 | var res = "";
97 | for (var i in response.data.data) {
98 | var res = res + response.data.data[i]["label"];
99 | }
100 | }
101 | }
102 | return res;
103 | }
104 |
105 | /**
106 | * 批量翻译
107 | * @param {*} keywords
108 | * @returns
109 | */
110 | function BatchTranslate(keywords) {
111 | var url = "https://brain.yaoapps.com/api/keyword/batch_column";
112 | let response = http.Post(url, {
113 | keyword: keywords,
114 | });
115 | // let response = Process(
116 | // "xiang.network.PostJSON",
117 | // url,
118 | // {
119 | // keyword: keywords,
120 | // },
121 | // {}
122 | // );
123 | var res = keywords;
124 | if (response.status == 200) {
125 | if (response.data.data) {
126 | // console.log(response.data.data);
127 | return response.data.data;
128 | }
129 | }
130 | return res;
131 | }
132 | /**
133 | * Model dsl全部翻译翻译
134 | * @param {*} keywords
135 | * @returns
136 | */
137 | function BatchModel(keywords) {
138 | var url = "https://brain.yaoapps.com/api/keyword/batch_model";
139 |
140 | let response = http.Post(url, {
141 | keyword: keywords,
142 | });
143 | // let response = Process(
144 | // "xiang.network.PostJSON",
145 | // url,
146 | // {
147 | // keyword: keywords,
148 | // },
149 | // {}
150 | // );
151 |
152 | var res = keywords;
153 | if (response.status == 200) {
154 | if (response.data.data) {
155 | // console.log(response.data.data);
156 | return response.data.data;
157 | }
158 | }
159 | return res;
160 | }
161 |
--------------------------------------------------------------------------------
/studio/hasmany.js:
--------------------------------------------------------------------------------
1 | function hasMany(table_name, field_name, all_table) {
2 | var relation = [
3 | "hasMany_id",
4 | "hasMany_ID",
5 | "hasMany_Id",
6 | "PerfixhasMany_id",
7 | "PerfixhasMany_ID",
8 | "PerfixhasMany_Id",
9 | ];
10 | for (var i in relation) {
11 | all_table = Studio(
12 | "hasmany." + relation[i],
13 | table_name,
14 | field_name,
15 | all_table
16 | );
17 | }
18 | return all_table;
19 | }
20 | function hasMany_id(table_name, field_name, all_table) {
21 | // 判断hasMany
22 | // 如果包含下划线+id,说明他有可能是别的表的外键
23 | if (field_name.indexOf("_id") != -1) {
24 | for (var i in all_table) {
25 | var target = field_name.replace("_id", "");
26 |
27 | if (target == all_table[i]["table"]["name"]) {
28 | all_table[i]["relations"][table_name] = {
29 | type: "hasMany",
30 | model: table_name,
31 | key: field_name,
32 | foreign: "id",
33 | query: {},
34 | };
35 | }
36 | }
37 | }
38 | return all_table;
39 | }
40 | function hasMany_ID(table_name, field_name, all_table) {
41 | // 判断hasMany
42 | // 如果包含下划线+id,说明他有可能是别的表的外键
43 | if (field_name.indexOf("ID") != -1) {
44 | for (var i in all_table) {
45 | var target = field_name.replace("ID", "");
46 |
47 | if (target == all_table[i]["table"]["name"]) {
48 | all_table[i]["relations"][table_name] = {
49 | type: "hasMany",
50 | model: table_name,
51 | key: field_name,
52 | foreign: "id",
53 | query: {},
54 | };
55 | }
56 | }
57 | }
58 | return all_table;
59 | }
60 | function hasMany_Id(table_name, field_name, all_table) {
61 | // 判断hasMany
62 | // 如果包含下划线+id,说明他有可能是别的表的外键
63 | if (field_name.indexOf("Id") != -1) {
64 | for (var i in all_table) {
65 | var target = field_name.replace("Id", "");
66 |
67 | if (target == all_table[i]["table"]["name"]) {
68 | all_table[i]["relations"][table_name] = {
69 | type: "hasMany",
70 | model: table_name,
71 | key: field_name,
72 | foreign: "id",
73 | query: {},
74 | };
75 | }
76 | }
77 | }
78 | return all_table;
79 | }
80 | function PerfixhasMany_id(table_name, field_name, all_table) {
81 | // 判断hasMany
82 | // 如果包含下划线+id,说明他有可能是别的表的外键
83 |
84 | if (field_name.indexOf("_id") != -1) {
85 | var prefix = Studio("schema.TablePrefix");
86 |
87 | for (var i in all_table) {
88 | for (var j in prefix) {
89 | var target = prefix[j] + "_" + field_name.replace("_id", "");
90 | if (target == all_table[i]["table"]["name"]) {
91 | all_table[i]["relations"][table_name] = {
92 | type: "hasMany",
93 | model: table_name,
94 | key: field_name,
95 | foreign: "id",
96 | query: {},
97 | };
98 | }
99 | }
100 | }
101 | }
102 | return all_table;
103 | }
104 |
105 | function PerfixhasMany_ID(table_name, field_name, all_table) {
106 | // 判断hasMany
107 | // 如果包含下划线+id,说明他有可能是别的表的外键
108 |
109 | if (field_name.indexOf("ID") != -1) {
110 | var prefix = Studio("schema.TablePrefix");
111 |
112 | for (var i in all_table) {
113 | for (var j in prefix) {
114 | var target = prefix[j] + "_" + field_name.replace("ID", "");
115 | if (target == all_table[i]["table"]["name"]) {
116 | all_table[i]["relations"][table_name] = {
117 | type: "hasMany",
118 | model: table_name,
119 | key: field_name,
120 | foreign: "id",
121 | query: {},
122 | };
123 | }
124 | }
125 | }
126 | }
127 | return all_table;
128 | }
129 | function PerfixhasMany_Id(table_name, field_name, all_table) {
130 | // 判断hasMany
131 | // 如果包含下划线+id,说明他有可能是别的表的外键
132 |
133 | if (field_name.indexOf("Id") != -1) {
134 | var prefix = Studio("schema.TablePrefix");
135 |
136 | for (var i in all_table) {
137 | for (var j in prefix) {
138 | var target = prefix[j] + "_" + field_name.replace("Id", "");
139 | if (target == all_table[i]["table"]["name"]) {
140 | all_table[i]["relations"][table_name] = {
141 | type: "hasMany",
142 | model: table_name,
143 | key: field_name,
144 | foreign: "id",
145 | query: {},
146 | };
147 | }
148 | }
149 | }
150 | }
151 | return all_table;
152 | }
153 |
--------------------------------------------------------------------------------
/studio/hasone.js:
--------------------------------------------------------------------------------
1 | function hasOne(table_name, all_table) {
2 | const relation = [
3 | "hasOne_id",
4 | "hasOneID",
5 | "hasOneId",
6 | "PrefixHasOne_id",
7 | "PrefixhasOneID",
8 | "PrefixhasOneId",
9 | ];
10 | for (var i in relation) {
11 | all_table = Studio("hasone." + relation[i], table_name, all_table);
12 | }
13 | return all_table;
14 | }
15 |
16 | function hasOne_id(table_name, all_table) {
17 | // 先判断hasOne
18 | var foreign_id = table_name + "_id";
19 | for (var i in all_table) {
20 | var temp_column = all_table[i]["columns"];
21 | for (var j in temp_column) {
22 | if (temp_column[j]["name"] == foreign_id) {
23 | all_table[i]["relations"][table_name] = {
24 | type: "hasOne",
25 | model: table_name,
26 | key: "id",
27 | foreign: foreign_id,
28 | query: {},
29 | };
30 | }
31 | }
32 | }
33 | return all_table;
34 | }
35 | function hasOneID(table_name, all_table) {
36 | // 先判断hasOne
37 | var foreign_id = table_name + "ID";
38 | for (var i in all_table) {
39 | var temp_column = all_table[i]["columns"];
40 | for (var j in temp_column) {
41 | if (temp_column[j]["name"] == foreign_id) {
42 | all_table[i]["relations"][table_name] = {
43 | type: "hasOne",
44 | model: table_name,
45 | key: "id",
46 | foreign: foreign_id,
47 | query: {},
48 | };
49 | }
50 | }
51 | }
52 | return all_table;
53 | }
54 | function hasOneId(table_name, all_table) {
55 | // 先判断hasOne
56 | var foreign_id = table_name + "Id";
57 | for (var i in all_table) {
58 | var temp_column = all_table[i]["columns"];
59 | for (var j in temp_column) {
60 | if (temp_column[j]["name"] == foreign_id) {
61 | all_table[i]["relations"][table_name] = {
62 | type: "hasOne",
63 | model: table_name,
64 | key: "id",
65 | foreign: foreign_id,
66 | query: {},
67 | };
68 | }
69 | }
70 | }
71 | return all_table;
72 | }
73 | /**
74 | * 有表前缀的hasone
75 | * @param {*} table_name
76 | * @param {*} all_table
77 | * @param {*} prefix
78 | * @returns
79 | */
80 | function PrefixHasOne_id(table_name, all_table) {
81 | var prefix = Studio("schema.TablePrefix");
82 | if (prefix.length) {
83 | // 获取表前缀
84 | var target = Studio("schema.ReplacePrefix", prefix, table_name);
85 | var foreign_id = target + "_id";
86 |
87 | for (var i in all_table) {
88 | var temp_column = all_table[i]["columns"];
89 | for (var j in temp_column) {
90 | if (temp_column[j]["name"] == foreign_id) {
91 | all_table[i]["relations"][table_name] = {
92 | type: "hasOne",
93 | model: table_name,
94 | key: "id",
95 | foreign: foreign_id,
96 | query: {},
97 | };
98 | }
99 | }
100 | }
101 | }
102 | return all_table;
103 | }
104 | /**
105 | * 有表前缀的hasone
106 | * @param {*} table_name
107 | * @param {*} all_table
108 | * @param {*} prefix
109 | * @returns
110 | */
111 | function PrefixhasOneID(table_name, all_table) {
112 | var prefix = Studio("schema.TablePrefix");
113 | if (prefix.length) {
114 | // 获取表前缀
115 | var target = Studio("schema.ReplacePrefix", prefix, table_name);
116 | var foreign_id = target + "ID";
117 |
118 | for (var i in all_table) {
119 | var temp_column = all_table[i]["columns"];
120 | for (var j in temp_column) {
121 | if (temp_column[j]["name"] == foreign_id) {
122 | all_table[i]["relations"][table_name] = {
123 | type: "hasOne",
124 | model: table_name,
125 | key: "id",
126 | foreign: foreign_id,
127 | query: {},
128 | };
129 | }
130 | }
131 | }
132 | }
133 | return all_table;
134 | }
135 | /**
136 | * 有表前缀的hasone
137 | * @param {*} table_name
138 | * @param {*} all_table
139 | * @param {*} prefix
140 | * @returns
141 | */
142 | function PrefixhasOneId(table_name, all_table) {
143 | var prefix = Studio("schema.TablePrefix");
144 | if (prefix.length) {
145 | // 获取表前缀
146 | var target = Studio("schema.ReplacePrefix", prefix, table_name);
147 | var foreign_id = target + "Id";
148 |
149 | for (var i in all_table) {
150 | var temp_column = all_table[i]["columns"];
151 | for (var j in temp_column) {
152 | if (temp_column[j]["name"] == foreign_id) {
153 | all_table[i]["relations"][table_name] = {
154 | type: "hasOne",
155 | model: table_name,
156 | key: "id",
157 | foreign: foreign_id,
158 | query: {},
159 | };
160 | }
161 | }
162 | }
163 | }
164 | return all_table;
165 | }
166 |
--------------------------------------------------------------------------------
/models/admin/user.mod.yao:
--------------------------------------------------------------------------------
1 | {
2 | "name": "管理员",
3 | "table": { "name": "admin_user", "comment": "the administraor table" },
4 | "columns": [
5 | { "label": "ID", "name": "id", "type": "ID" },
6 | {
7 | "label": "类型",
8 | "name": "type",
9 | "type": "enum",
10 | "option": ["admin", "staff", "user", "robot"],
11 | "comment": "账号类型 admin 管理员, staff 员工, user 用户, robot 机器人",
12 | "default": "user",
13 | "index": true,
14 | "validations": [
15 | {
16 | "method": "typeof",
17 | "args": ["string"],
18 | "message": "{{input}}类型错误, {{label}}应该为字符串"
19 | },
20 | {
21 | "method": "enum",
22 | "args": ["admin", "staff", "user", "robot"],
23 | "message": "{{input}}不在许可范围, {{label}}应该为 admin/staff/user/robot"
24 | }
25 | ]
26 | },
27 | {
28 | "label": "邮箱",
29 | "name": "email",
30 | "type": "string",
31 | "length": 50,
32 | "comment": "邮箱",
33 | "index": true,
34 | "nullable": true,
35 | "validations": [
36 | {
37 | "method": "typeof",
38 | "args": ["string"],
39 | "message": "{{input}}类型错误, {{label}}应该为字符串"
40 | },
41 | {
42 | "method": "email",
43 | "args": [],
44 | "message": "{{input}}不是邮箱地址"
45 | }
46 | ]
47 | },
48 | {
49 | "label": "手机号",
50 | "name": "mobile",
51 | "type": "string",
52 | "length": 50,
53 | "comment": "手机号",
54 | "index": true,
55 | "nullable": true,
56 | "crypt": "AES",
57 | "validations": [
58 | {
59 | "method": "mobile",
60 | "args": [],
61 | "message": "{{input}}格式错误"
62 | }
63 | ]
64 | },
65 | {
66 | "label": "登录密码",
67 | "name": "password",
68 | "type": "string",
69 | "length": 256,
70 | "comment": "登录密码",
71 | "crypt": "PASSWORD",
72 | "index": true,
73 | "nullable": true,
74 | "validations": [
75 | {
76 | "method": "typeof",
77 | "args": ["string"],
78 | "message": "{{input}}类型错误, {{label}}应该为字符串"
79 | },
80 | {
81 | "method": "minLength",
82 | "args": [6],
83 | "message": "{{label}}应该由6-18位,大小写字母、数字和符号构成"
84 | },
85 | {
86 | "method": "maxLength",
87 | "args": [18],
88 | "message": "{{label}}应该由6-18位,大小写字母、数字和符号构成"
89 | },
90 | {
91 | "method": "pattern",
92 | "args": ["[0-9]+"],
93 | "message": "{{label}}应该至少包含一个数字"
94 | },
95 | {
96 | "method": "pattern",
97 | "args": ["[A-Z]+"],
98 | "message": "{{label}}应该至少包含一个大写字母"
99 | },
100 | {
101 | "method": "pattern",
102 | "args": ["[a-z]+"],
103 | "message": "{{label}}应该至少包含一个小写字母"
104 | },
105 | {
106 | "method": "pattern",
107 | "args": ["[@#$&*\\+]+"],
108 | "message": "{{label}}应该至少包含一个符号"
109 | }
110 | ]
111 | },
112 | {
113 | "label": "操作密码",
114 | "name": "password2nd",
115 | "type": "string",
116 | "length": 256,
117 | "comment": "操作密码",
118 | "crypt": "PASSWORD",
119 | "index": true,
120 | "nullable": true,
121 | "validations": [
122 | {
123 | "method": "typeof",
124 | "args": ["string"],
125 | "message": "{{input}}类型错误, {{label}}应该为字符串"
126 | },
127 | {
128 | "method": "minLength",
129 | "args": [6],
130 | "message": "{{label}}应该由6-18位,大小写字母、数字和符号构成"
131 | },
132 | {
133 | "method": "maxLength",
134 | "args": [18],
135 | "message": "{{label}}应该由6-18位,大小写字母、数字和符号构成"
136 | },
137 | {
138 | "method": "pattern",
139 | "args": ["[0-9]+"],
140 | "message": "{{label}}应该至少包含一个数字"
141 | },
142 | {
143 | "method": "pattern",
144 | "args": ["[A-Z]+"],
145 | "message": "{{label}}应该至少包含一个大写字母"
146 | },
147 | {
148 | "method": "pattern",
149 | "args": ["[a-z]+"],
150 | "message": "{{label}}应该至少包含一个小写字母"
151 | },
152 | {
153 | "method": "pattern",
154 | "args": ["[@#$&*\\+]+"],
155 | "message": "{{label}}应该至少包含一个符号"
156 | }
157 | ]
158 | },
159 | {
160 | "label": "姓名",
161 | "name": "name",
162 | "type": "string",
163 | "length": 80,
164 | "comment": "姓名",
165 | "index": true,
166 | "nullable": true,
167 | "validations": [
168 | {
169 | "method": "typeof",
170 | "args": ["string"],
171 | "message": "{{input}}类型错误, {{label}}应该为字符串"
172 | },
173 | {
174 | "method": "minLength",
175 | "args": [2],
176 | "message": "{{label}}至少需要2个字"
177 | },
178 | {
179 | "method": "maxLength",
180 | "args": [40],
181 | "message": "{{label}}不能超过20个字"
182 | }
183 | ]
184 | },
185 | {
186 | "label": "身份证号码",
187 | "name": "idcard",
188 | "type": "string",
189 | "length": 256,
190 | "comment": "身份证号码",
191 | "crypt": "AES",
192 | "nullable": true,
193 | "index": true,
194 | "validations": [
195 | {
196 | "method": "typeof",
197 | "args": ["string"],
198 | "message": "{{input}}类型错误, {{label}}应该为字符串"
199 | },
200 | {
201 | "method": "pattern",
202 | "args": ["^(\\d{18})|(\\d{14}X)$"],
203 | "message": "{{label}}格式错误"
204 | }
205 | ]
206 | },
207 | {
208 | "label": "API Key",
209 | "name": "key",
210 | "type": "string",
211 | "length": 256,
212 | "comment": "API Key",
213 | "nullable": true,
214 | "unique": true,
215 | "validations": [
216 | {
217 | "method": "typeof",
218 | "args": ["integer"],
219 | "message": "{{input}}类型错误, {{label}}应该为数字"
220 | },
221 | {
222 | "method": "pattern",
223 | "args": ["^[0-9]{10}$"],
224 | "message": " {{label}}应该由10位数字构成"
225 | }
226 | ]
227 | },
228 | {
229 | "label": "API 密钥",
230 | "name": "secret",
231 | "type": "string",
232 | "length": 256,
233 | "nullable": true,
234 | "crypt": "AES",
235 | "comment": "API 密钥",
236 | "index": true,
237 | "validations": [
238 | {
239 | "method": "typeof",
240 | "args": ["string"],
241 | "message": "{{input}}类型错误, {{label}}应该为字符串"
242 | },
243 | {
244 | "method": "pattern",
245 | "args": ["^[0-9A-Za-z@#$&*]{32}$"],
246 | "message": "{{label}}应该由32位,大小写字母、数字和符号构成"
247 | }
248 | ]
249 | },
250 | {
251 | "label": "扩展信息",
252 | "name": "extra",
253 | "type": "json",
254 | "comment": "扩展信息",
255 | "nullable": true
256 | },
257 | {
258 | "label": "状态",
259 | "comment": "用户状态 enabled 有效, disabled 无效",
260 | "name": "status",
261 | "type": "enum",
262 | "default": "enabled",
263 | "option": ["enabled", "disabled"],
264 | "index": true,
265 | "validations": [
266 | {
267 | "method": "typeof",
268 | "args": ["string"],
269 | "message": "{{input}}类型错误, {{label}}应该为字符串"
270 | },
271 | {
272 | "method": "enum",
273 | "args": ["enabled", "disabled"],
274 | "message": "{{input}}不在许可范围, {{label}}应该为 enabled/disabled"
275 | }
276 | ]
277 | }
278 | ],
279 | "relations": {},
280 | "values": [
281 | {
282 | "name": "管理员",
283 | "type": "admin",
284 | "email": "xiang@iqka.com",
285 | "mobile": null,
286 | "password": "A123456p+",
287 | "key": "8304925176",
288 | "secret": "XMTdNRVigbgUiAPdiJCfaWgWcz2PaQXw",
289 | "status": "enabled",
290 | "extra": { "sex": "男" }
291 | }
292 | ],
293 | "indexes": [
294 | {
295 | "comment": "类型邮箱唯一",
296 | "name": "type_email_unique",
297 | "columns": ["type", "email"],
298 | "type": "unique"
299 | },
300 | {
301 | "comment": "类型手机号唯一",
302 | "name": "type_mobile_unique",
303 | "columns": ["type", "mobile"],
304 | "type": "unique"
305 | }
306 | ],
307 | "option": { "timestamps": true, "soft_deletes": true }
308 | }
309 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
10 |
11 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
12 |
13 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
14 |
15 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
16 |
17 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
18 |
19 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
20 |
21 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
22 |
23 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
24 |
25 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
26 |
27 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
28 |
29 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
30 |
31 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
32 |
33 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
34 |
35 | You must give any other recipients of the Work or Derivative Works a copy of this License; and
36 | You must cause any modified files to carry prominent notices stating that You changed the files; and
37 | You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
38 | If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
39 |
40 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
41 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
42 |
43 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
44 |
45 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
46 |
47 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
48 |
49 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
50 |
51 | END OF TERMS AND CONDITIONS
--------------------------------------------------------------------------------
/studio/colunm.js:
--------------------------------------------------------------------------------
1 | function getType() {
2 | return {
3 | string: "Input",
4 | char: "Input",
5 | text: "TextArea",
6 | mediumText: "TextArea",
7 | longText: "TextArea",
8 | date: "DatePicker",
9 | datetime: "DatePicker",
10 | datetimeTz: "DatePicker",
11 | time: "DatePicker",
12 | timeTz: "DatePicker",
13 | timestamp: "DatePicker",
14 | timestampTz: "DatePicker",
15 | tinyInteger: "Input",
16 | tinyIncrements: "Input",
17 | unsignedTinyInteger: "Input",
18 | smallInteger: "Input",
19 | unsignedSmallInteger: "Input",
20 | integer: "Input",
21 | bigInteger: "Input",
22 | decimal: "Input",
23 | unsignedDecimal: "Input",
24 | float: "Input",
25 | boolean: "Input",
26 | enum: "Select",
27 | };
28 | }
29 | function Hidden(type) {
30 | if (type == 1) {
31 | // 不展示的名单列表
32 | var hidden = [
33 | "secret",
34 | "password",
35 | "del",
36 | "delete",
37 | "deleted",
38 | "deleted_at",
39 | "pwd",
40 | "deleted",
41 | ];
42 | } else {
43 | var hidden = [
44 | "del",
45 | "delete",
46 | "deleted",
47 | "deleted_at",
48 | "created_at",
49 | "updated_at",
50 | "id",
51 | "ID",
52 | "update_time",
53 | "password",
54 | "pwd",
55 | ];
56 | }
57 |
58 | return hidden;
59 | }
60 | function filter() {
61 | return ["name", "title", "_sn"];
62 | }
63 |
64 | function toTable(model_dsl) {
65 | const columns = model_dsl.columns || [];
66 | var tableTemplate = {
67 | name: model_dsl.name || "表格",
68 | action: {
69 | bind: {
70 | model: model_dsl.table.name,
71 | option: { withs: {} },
72 | },
73 | },
74 | layout: {
75 | primary: "id",
76 | header: { preset: {}, actions: [] },
77 | filter: {
78 | columns: [],
79 | actions: [
80 | {
81 | title: "添加",
82 | icon: "icon-plus",
83 | width: 3,
84 | action: {
85 | "Common.openModal": {
86 | width: 640,
87 | Form: { type: "edit", model: model_dsl.table.name },
88 | },
89 | },
90 | },
91 | ],
92 | },
93 | table: {
94 | columns: [],
95 | operation: {
96 | fold: false,
97 | actions: [
98 | {
99 | title: "查看",
100 | icon: "icon-eye",
101 | action: {
102 | "Common.openModal": {
103 | width: 640,
104 | Form: { type: "view", model: model_dsl.table.name },
105 | },
106 | },
107 | },
108 | {
109 | title: "编辑",
110 | icon: "icon-edit-2",
111 | action: {
112 | "Common.openModal": {
113 | Form: { type: "edit", model: model_dsl.table.name },
114 | },
115 | },
116 | },
117 | {
118 | title: "删除",
119 | icon: "icon-trash-2",
120 | action: { "Table.delete": {} },
121 | style: "danger",
122 | confirm: {
123 | title: "提示",
124 | desc: "确认删除,删除后数据无法恢复?",
125 | },
126 | },
127 | ],
128 | },
129 | },
130 | },
131 | fields: {
132 | filter: {},
133 | table: {},
134 | },
135 | };
136 | columns.forEach((column) => {
137 | var col = castTableColumn(column, model_dsl);
138 | if (col) {
139 | // col.layout.filter.columns.forEach((fc) => {});
140 | col.layout.table.columns.forEach((tc) => {
141 | tableTemplate.layout.table.columns.push(tc);
142 | });
143 | col.fields.table.forEach((c) => {
144 | var cop = c.component.withs || [];
145 | cop.forEach((fct) => {
146 | tableTemplate.action.bind.option.withs[fct.name] = {};
147 | });
148 | delete c.component.withs;
149 | tableTemplate.fields.table[c.name] = c.component;
150 | });
151 |
152 | col.fields.filter.forEach((ff) => {
153 | tableTemplate.layout.filter.columns.push({ name: ff.name, width: 4 });
154 | tableTemplate.fields.filter[ff.name] = ff.component;
155 | });
156 |
157 | // col.fields.filter.forEach((ff) => {});
158 | }
159 |
160 | // col.fields.table.forEach((ft) => {
161 | // tableTemplate.fields.table[ft.name] = ft.component;
162 | // });
163 | });
164 |
165 | return tableTemplate;
166 | }
167 |
168 | function castTableColumn(column, model_dsl) {
169 | column = column || {};
170 | const props = column.props || {};
171 | const title = column.label;
172 | const name = column.name;
173 |
174 | // 不展示隐藏列
175 | var hidden = Hidden(1);
176 | if (hidden.indexOf(name) != -1) {
177 | return false;
178 | }
179 | var types = getType();
180 |
181 | const bind = `${name}`;
182 | if (!name) {
183 | // log.Error("castTableColumn: missing name");
184 | return false;
185 | }
186 |
187 | if (!title) {
188 | //log.Error("castTableColumn: missing title");
189 | return false;
190 | }
191 |
192 | var res = {
193 | layout: { filter: { columns: [] }, table: { columns: [] } },
194 | fields: { filter: [], table: [] },
195 | };
196 |
197 | var component = {
198 | bind: name,
199 | view: { type: "Text", props: {} },
200 | edit: {
201 | type: "Input",
202 | bind: bind,
203 | props: {},
204 | },
205 | };
206 | if (title.length > 5) {
207 | var width = 250;
208 | } else {
209 | var width = 200;
210 | }
211 |
212 | // 如果是json的,去看看是不是图片文件
213 | if (column["type"] == "json") {
214 | var component = Studio("file.File", column, false);
215 | if (!component) {
216 | return false;
217 | }
218 | // log.Error("castTableColumn: Type %s does not support", column.type);
219 | } else if (column["type"] == "enum") {
220 | var component = {
221 | bind: bind,
222 | edit: {
223 | props: {
224 | options: Enum(column["option"]),
225 | placeholder: "请选择" + title,
226 | },
227 | type: "Select",
228 | },
229 | view: { props: {}, type: "Text" },
230 | };
231 |
232 | res.layout.table.columns.push({
233 | name: title,
234 | width: width,
235 | });
236 | } else {
237 | if (column["type"] in types) {
238 | component.edit.type = types[column["type"]];
239 | res.layout.table.columns.push({
240 | name: title,
241 | width: width,
242 | });
243 | }
244 | }
245 |
246 | component = Studio("selector.Select", column, model_dsl, component);
247 | // 如果是下拉的,则增加查询条件
248 | if (component.is_select) {
249 | var where_bind = "where." + name + ".in";
250 | res.fields.filter.push({
251 | name: title,
252 | component: {
253 | bind: where_bind,
254 | edit: component.edit,
255 | },
256 | });
257 | } else {
258 | var filter_target = filter();
259 | for (var f in filter_target) {
260 | if (name.indexOf(filter_target[f]) != -1) {
261 | res.fields.filter.push({
262 | name: title,
263 | component: {
264 | bind: "where." + name + ".match",
265 | edit: {
266 | type: "Input",
267 | compute: "Trim",
268 | props: { placeholder: "请输入" + title },
269 | },
270 | },
271 | });
272 | }
273 | }
274 | }
275 | component = Studio("file.File", column, component);
276 |
277 | // component.edit = { type: "input", props: { value: bind } };
278 | // res.list.columns.push({ name: title });
279 | // res.edit.push({ name: title, width: 24 });
280 | // break;
281 |
282 | res.fields.table.push({
283 | name: title,
284 | component: component,
285 | });
286 |
287 | return res;
288 | }
289 |
290 | function Enum(option) {
291 | var res = [];
292 | for (var i in option) {
293 | res.push({ label: "::" + option[i], value: option[i] });
294 | }
295 | return res;
296 | }
297 |
298 | function toForm(model_dsl) {
299 | const columns = model_dsl.columns || [];
300 | var tableTemplate = {
301 | name: model_dsl.name || "表单",
302 | action: {
303 | bind: {
304 | model: model_dsl.table.name,
305 | option: { withs: {} },
306 | },
307 | },
308 | layout: {
309 | actions: [
310 | {
311 | title: "保存",
312 | icon: "icon-check",
313 |
314 | showWhenAdd: true,
315 |
316 | style: "primary",
317 | action: [
318 | {
319 | name: "Submit",
320 | type: "Form.submit",
321 | payload: {},
322 | },
323 | {
324 | name: "Back",
325 | type: "Common.closeModal",
326 | payload: {},
327 | },
328 | ],
329 | },
330 | {
331 | title: "返回",
332 | divideLine: true,
333 | showWhenAdd: true,
334 | showWhenView: true,
335 | icon: "icon-arrow-left",
336 | action: [
337 | {
338 | name: "closeModal",
339 | type: "Common.closeModal",
340 | payload: {},
341 | },
342 | ],
343 | },
344 | ],
345 | primary: "id",
346 | operation: {
347 | preset: { back: {}, save: { back: true } },
348 | },
349 | form: {
350 | props: {},
351 | sections: [
352 | {
353 | columns: [],
354 | },
355 | ],
356 | },
357 | },
358 | fields: {
359 | form: {},
360 | },
361 | };
362 | /**
363 | * var res = {
364 | layout: [],
365 | fields: {},
366 | };
367 | */
368 | columns.forEach((column) => {
369 | var col = castFormColumn(column, model_dsl);
370 | if (col) {
371 | // col.layout.filter.columns.forEach((fc) => {});
372 | col.layout.forEach((tc) => {
373 | tableTemplate.layout.form.sections[0].columns.push(tc);
374 | });
375 | col.fields.forEach((ft) => {
376 | var cop = ft.component.withs || [];
377 | cop.forEach((fct) => {
378 | tableTemplate.action.bind.option.withs[fct.name] = {};
379 | });
380 | delete ft.component.withs;
381 | tableTemplate.fields.form[ft.name] = ft.component;
382 | });
383 |
384 | // col.fields.filter.forEach((ff) => {});
385 | }
386 | });
387 | tableTemplate = Studio("selector.Table", tableTemplate, model_dsl);
388 | return tableTemplate;
389 | }
390 | function castFormColumn(column, model_dsl) {
391 | var types = getType();
392 | column = column || {};
393 | const title = column.label;
394 | const name = column.name;
395 |
396 | const bind = `${name}`;
397 | if (!name) {
398 | //log.Error("castTableColumn: missing name");
399 | return false;
400 | }
401 |
402 | if (!title) {
403 | // log.Error("castTableColumn: missing title");
404 | return false;
405 | }
406 |
407 | // 不展示隐藏列
408 | var hidden = Hidden(2);
409 | if (hidden.indexOf(name) != -1) {
410 | return false;
411 | }
412 |
413 | var res = {
414 | layout: [],
415 | fields: [],
416 | };
417 |
418 | var component = {
419 | bind: name,
420 | edit: {
421 | type: "Input",
422 | props: {},
423 | },
424 | };
425 | if (column["type"] == "json") {
426 | var component = Studio("file.FormFile", column, false, model_dsl);
427 | if (!component) {
428 | // log.Error("castTableColumn: Type %s does not support", column.type);
429 | return false;
430 | }
431 | } else if (column["type"] == "enum") {
432 | var component = {
433 | bind: bind,
434 | edit: {
435 | props: {
436 | options: Enum(column["option"]),
437 | placeholder: "请选择" + title,
438 | },
439 | type: "Select",
440 | },
441 | };
442 | } else {
443 | if (column["type"] in types) {
444 | component.edit.type = types[column["type"]];
445 | }
446 | }
447 | var width = 8;
448 | component = Studio("selector.EditSelect", column, model_dsl, component);
449 | component = Studio("file.FormFile", column, component, model_dsl);
450 | if (component["is_image"]) {
451 | var width = 24;
452 | }
453 | res.layout.push({
454 | name: title,
455 | width: width,
456 | });
457 | // component.edit = { type: "input", props: { value: bind } };
458 | // res.list.columns.push({ name: title });
459 | // res.edit.push({ name: title, width: 24 });
460 | // break;
461 |
462 | res.fields.push({
463 | name: title,
464 | component: component,
465 | });
466 |
467 | return res;
468 | }
469 |
--------------------------------------------------------------------------------
/Teach.md:
--------------------------------------------------------------------------------
1 | # 使用方法:
2 |
3 | 克隆项目后,执行 `yao start`,打开配置界面`http://127.0.0.1:5099`配置好数据库配置,注意:数据库里面要有数据表,否则不会生成页面
4 |
5 | 
6 |
7 | 
8 |
9 | 等待数据表生成以后,就可以打开登录界面 `http://127.0.0.1:5099/yao/login/admin`输入默认用户名: `xiang@iqka.com`, 密码: `A123456p+`
10 |
11 |
12 |
13 | # 教程:使用 Yao Studio 来构建 Admin 后台
14 |
15 | Yao Studio 是[0.10.2 版本](https://release-sv-1252011659.cos.na-siliconvalley.myqcloud.com/archives/yao-0.10.2-linux-amd64)新增的一个功能,该功能主要分为三个部分:模型构造器,表格构造器,组件菜单构造器。可以让你连接任意数据库后,一键生成数据表格和模型菜单,减少 99%的工作量,实现真正的零代码。
16 | 源码地址:https://github.com/YaoApp/yao-admin
17 |
18 | ## 效果预览图
19 |
20 | 
21 |
22 | 
23 | 
24 |
25 | studio 文件结构说明
26 |
27 | ```
28 | ├─studio
29 | │ ├─colunm.js
30 | │ ├─dashboard.js
31 | │ ├─hasmany.js
32 | │ ├─hasone.js
33 | │ ├─menu.js
34 | │ ├─model.js
35 | │ ├─move.js
36 | │ ├─relation.js
37 | │ ├─remote.js
38 | │ ├─schema.js
39 | │ ├─selector.js
40 | │ └─table.js
41 | ```
42 |
43 | studio 文件说明
44 |
45 | | 文件 | 类型 | 说明 |
46 | | ------------ | ---------- | ------------------------------------------ |
47 | | colunm.js | javascript | 用于 tables 和 forms 文件字段生成的脚本 |
48 | | dashboard.js | javascript | 登录进去后首页展示的看板图脚本 |
49 | | hasmany.js | javascript | 用于一对多关联关系的推测与生成的规则的脚本 |
50 | | hasone.js | javascript | 用于一对一关联关系的推测脚本 |
51 | | menu.js | javascript | 生成登录菜单和图标的脚本 |
52 | | model.js | javascript | 生成模型 DSL 脚本 |
53 | | move.js | javascript | 文件操作,目录操作,移动删除脚本 |
54 | | relation.js | javascript | 其他关联关系规则生成脚本 |
55 | | remote.js | javascript | 生成 hasOne 下拉列表接口的脚本 |
56 | | schema.js | javascript | 模型 dsl 字段处理,表名称处理脚本 |
57 | | selector.js | javascript | 下拉 select 选择框规则处理 |
58 | | table.js | javascript | 表格生成脚本调用 |
59 |
60 | ## 第一步:配置数据库连接
61 |
62 | ### 在`yao start`命令执行过后文件中配置好数据库连接,`.env`文件就会出现如下配置
63 |
64 | ```bash
65 | YAO_DB_AESKEY="KBPdcRn44LzykphsVM\*y"
66 | YAO_DB_DRIVER=mysql
67 | YAO_DB_PRIMARY="root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local" # 主库连接
68 | YAO_DB_SECONDARY="root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local" # 主库连接
69 | ```
70 |
71 | ## 第二步:增加模型构造器,新建文件`/studio/model.js`,有关 Studio 的功能操作要全部写在 studio 文件夹下
72 |
73 | ```javascript
74 | //yao studio run model.Create
75 | /**
76 | * 创建模型
77 | */
78 | function Create() {
79 | var model_dsl = Studio("schema.Relation");
80 |
81 | var fs = new FS("dsl");
82 | for (var i in model_dsl) {
83 | var table_name = model_dsl[i]["table"]["name"] + ".mod.json";
84 | var table = JSON.stringify(model_dsl[i]);
85 | Studio("move.Move", "models", table_name);
86 | fs.WriteFile("/models/" + table_name, table);
87 | }
88 | // 创建表格dsl
89 | Studio("table.Create", model_dsl);
90 | version10_0_2();
91 | login();
92 | // 创建菜单
93 | Studio("menu.Create", model_dsl);
94 | }
95 |
96 | //创建单个表格的studio
97 | ///yao studio run model.CreateOne address
98 | function CreateOne(model_name) {
99 | var fs = new FS("dsl");
100 | var model_dsl = [];
101 |
102 | model_dsl.push(JSON.parse(fs.ReadFile("models/" + model_name + ".mod.json")));
103 |
104 | for (var i in model_dsl) {
105 | var table_name = model_dsl[i]["table"]["name"] + ".mod.json";
106 | var table = JSON.stringify(model_dsl[i]);
107 | Studio("move.Move", "models", table_name);
108 | fs.WriteFile("/models/" + table_name, table);
109 | }
110 | // 创建表格dsl
111 | Studio("table.Create", model_dsl);
112 | //version10_0_2();
113 | //login();
114 | }
115 |
116 | /**
117 | * 写入10.2版本的
118 | */
119 | function version10_0_2() {
120 | var fs = new FS("dsl");
121 |
122 | fs.WriteFile(
123 | "app.json",
124 | JSON.stringify({
125 | xgen: "1.0",
126 | name: "::Demo Application",
127 | short: "::Demo",
128 | description: "::Another yao application",
129 | version: "0.10.2",
130 | menu: {
131 | process: "flows.app.menu",
132 | args: ["demo"],
133 | },
134 | adminRoot: "yao",
135 | optional: {
136 | hideNotification: true,
137 | hideSetting: false,
138 | },
139 | })
140 | );
141 | }
142 | function login() {
143 | var fs = new FS("dsl");
144 | var table_name = "admin.login.json";
145 | var table = JSON.stringify({
146 | name: "::Admin Login",
147 | action: {
148 | process: "yao.login.Admin",
149 | args: [":payload"],
150 | },
151 | layout: {
152 | entry: "/x/Chart/dashboard",
153 | captcha: "yao.utils.Captcha",
154 | cover: "/assets/images/login/cover.svg",
155 | slogan: "::Make Your Dream With Yao App Engine",
156 | site: "https://yaoapps.com",
157 | },
158 | });
159 | Studio("move.Move", "logins", table_name);
160 | fs.WriteFile("/logins/" + table_name, table);
161 | }
162 | ```
163 |
164 | | **命令** | **说明** |
165 | | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
166 | | Studio("schema.Relation"); | Studio 函数,[详情](https://github.com/YaoApp/yao/blob/main/studio/studio.go),调用的是`/studio/schema.Relation`方法 |
167 | | FS("dsl"); | 文件操作函数[详情](https://github.com/YaoApp/gou/blob/main/fs/fs_test.go),dsl 是指文件的根目录,可以对文件进行移动删除,复制,新建等操作 |
168 |
169 |
170 |
171 | `Studio`函数是专门用来调用`/studio`目录下的 js 脚本的,运行该方法,我们可以使用专门的命令:`yao studio run model.Create`,注意:该命令只允许在`YAO_ENV=development`模式下运行
172 |
173 | 分别新建`/studio/schema.js`和`/studio/relation.js`文件,其中`schema.js`的作用是获取数据库表名称,和表的所有字段,`relation.js`是根据现有的表名称和字段来生成对应的关联关系
174 |
175 |
176 |
177 | | 方法 | 说明 |
178 | | --------------- | ------------------------------------------- |
179 | | Create() | 生成模型 DSL |
180 | | version10_0_2() | 生成 yao-0.10.2 版本的启动配置文件 app.json |
181 | | login() | 生成 yao-0.10.2 版本登录脚本文件 |
182 |
183 |
184 | schema.js文件内容
185 |
186 | ```javascript
187 | /**yao studio run schema.GetTable role
188 | * 获取单个表字段
189 | * @param {*} name
190 | * @returns
191 | */
192 | function GetTable(name) {
193 | let res = Process("schemas.default.TableGet", name);
194 | return res;
195 | }
196 | /**
197 | * 获取所有表格名称
198 | */
199 | function GetTableName() {
200 | let res = Process("schemas.default.Tables");
201 | return res;
202 | }
203 |
204 | /**
205 | * 分析关联关系处理器
206 | * @param {*} type
207 | * yao run scripts.schema.Relation
208 | */
209 | function Relation() {
210 | var all_table = GetTableName();
211 | var table_arr = [];
212 |
213 | // 不需要的表格白名单
214 | var guards = ["xiang_menu", "xiang_user", "xiang_workflow", "pet"];
215 | var prefix = TablePrefix(all_table);
216 |
217 | for (var i in all_table) {
218 | if (guards.indexOf(all_table[i]) != -1) {
219 | continue;
220 | }
221 |
222 | var col = GetTable(all_table[i]);
223 |
224 | for (var j in col.columns) {
225 | if (!col.columns[j]["label"]) {
226 | col.columns[j]["label"] = Studio(
227 | "relation.translate",
228 | col.columns[j]["name"]
229 | );
230 | }
231 | col.columns[j]["label"] = FieldHandle(col.columns[j]["label"]);
232 | if (col.columns[j]["type"] == "dateTime") {
233 | col.columns[j]["type"] = "datetime";
234 | }
235 | if (col.columns[j]["type"] == "BIT" || col.columns[j]["type"] == "bit") {
236 | col.columns[j]["type"] = "boolean";
237 | }
238 | if (
239 | col.columns[j]["type"] == "MEDIUMINT" ||
240 | col.columns[j]["type"] == "mediumint"
241 | ) {
242 | col.columns[j]["type"] = "tinyInteger";
243 | }
244 | }
245 |
246 | // 去除表前缀
247 | var trans = ReplacePrefix(prefix, all_table[i]);
248 |
249 | col.name = Studio("relation.translate", trans);
250 | col.decription = col.name;
251 | col.table = {};
252 | col.table.name = all_table[i];
253 | col.table.comment = col.name;
254 | col.relations = {};
255 | var parent = Studio("relation.parent", all_table[i], col.columns, col);
256 | var parent = Studio("relation.child", all_table[i], col.columns, parent);
257 |
258 | table_arr.push(parent);
259 | }
260 |
261 | table_arr = Studio("relation.other", table_arr);
262 |
263 | return table_arr;
264 | }
265 |
266 | function FieldHandle(label) {
267 | if (label.length >= 8) {
268 | var label = label.split(";")[0];
269 | var label = label.split(";")[0];
270 | var label = label.split(",")[0];
271 | var label = label.split(";")[0];
272 | var label = label.split("。")[0];
273 | var label = label.split(":")[0];
274 | var label = label.split(":")[0];
275 | var label = label.split(",")[0];
276 | var label = label.split(",")[0];
277 | }
278 |
279 | return label;
280 | }
281 | //yao studio run schema.TablePrefix
282 | function TablePrefix(all_table_name) {
283 | if (!all_table_name.length) {
284 | var all_table_name = GetTableName();
285 | }
286 | var prefix = [];
287 | for (var i in all_table_name) {
288 | var temp = all_table_name[i].split("_");
289 | // 如果表格下划线有3个以上,有可能有表前缀
290 | if (temp.length >= 4 && prefix.indexOf(temp[0]) == -1) {
291 | prefix.push(temp[0]);
292 | }
293 | }
294 | return prefix;
295 | }
296 |
297 | function ReplacePrefix(prefix, target) {
298 | if (prefix.length) {
299 | for (var i in prefix) {
300 | target = target.replace(prefix[i] + "_", "");
301 | return target;
302 | }
303 | }
304 | return target;
305 | }
306 | ```
307 |
308 |
309 |
310 | | 方法 | 说明 |
311 | | -------------------------------- | ------------------------------------ |
312 | | GetTable() | 主要是通过表格名称用来获取表格的字段 |
313 | | GetTableName() | 获取当前数据库连接的表格名称 |
314 | | Relation() | 关联关系推测脚本 |
315 | | FieldHandle() | 防止字段过长,截取字段的 |
316 | | TablePrefix() 和 ReplacePrefix() | 去除表前缀 |
317 |
318 |
319 |
320 | relation.js 文件内容
321 |
322 | ```javascript
323 | const parents = ["parent", "parent_id", "pid"];
324 | const children = ["children", "children_id", "child", "child_id"];
325 | /**
326 | * 关联关系分析同一个表中关联关系
327 | * @param {*} model_name
328 | * @param {*} columns
329 | * @param {*} table_struct
330 | */
331 | function parent(model_name, columns, table_struct) {
332 | for (var i in columns) {
333 | if (columns[i]["type"] != "integer") {
334 | continue;
335 | }
336 | if (parents.indexOf(columns[i]["name"]) != -1) {
337 | table_struct.relations.children = {
338 | type: "hasMany",
339 | model: model_name,
340 | key: columns[i]["name"],
341 | foreign: "id",
342 | query: {},
343 | };
344 | return table_struct;
345 | }
346 | }
347 | return table_struct;
348 | }
349 |
350 | /**
351 | * 分析子集
352 | * @param {*} model_name
353 | * @param {*} columns
354 | * @param {*} table_struct
355 | * @returns
356 | */
357 | function child(model_name, columns, table_struct) {
358 | for (var i in columns) {
359 | if (columns[i]["type"] != "integer") {
360 | continue;
361 | }
362 | if (children.indexOf(columns[i]["name"]) != -1) {
363 | table_struct.relations.parent = {
364 | type: "hasOne",
365 | model: model_name,
366 | key: "id",
367 | foreign: columns[i]["name"],
368 | query: {},
369 | };
370 | return table_struct;
371 | }
372 | }
373 | return table_struct;
374 | }
375 |
376 | function other(all_table_struct) {
377 | for (var i in all_table_struct) {
378 | var temp = all_table_struct[i]["columns"];
379 | all_table_struct = Studio(
380 | "hasone.hasOne",
381 | all_table_struct[i]["table"]["name"],
382 | all_table_struct
383 | );
384 | for (var j in temp) {
385 | all_table_struct = Studio(
386 | "hasmany.hasMany",
387 | all_table_struct[i]["table"]["name"],
388 | temp[j].name,
389 | all_table_struct
390 | );
391 | }
392 | }
393 | return all_table_struct;
394 | }
395 |
396 | // yao studio run relation.translate icon
397 | function translate(keywords) {
398 | var url = "https://brain.yaoapps.com/api/keyword/column";
399 | let response = Process(
400 | "xiang.network.PostJSON",
401 | url,
402 | {
403 | keyword: [keywords],
404 | },
405 | {}
406 | );
407 | var res = keywords;
408 | if (response.status == 200) {
409 | if (response.data.data) {
410 | var res = response.data.data[0]["label"];
411 | }
412 | }
413 | return res;
414 | }
415 | ```
416 |
417 |
418 |
419 | | 方法 | 说明 |
420 | | ----------- | ---------------------------------------- |
421 | | parent() | 父级 id 的关联关系推测 |
422 | | child() | 子集 id 的关联关系推测 |
423 | | other() | 其他关联关系推测 |
424 | | translate() | 调用 yao-brain 的翻译接口,对字段进行翻译 |
425 |
426 |
427 |
428 | 新建`/studio/move.js`该文件主要用于文件移动,在新建模型文件和表格文件的时候,如果发现同名的文件会把文件移动到`.trash`文件夹下面,然后新建一个文件
429 |
430 | ```javascript
431 | /**
432 | * 文件复制移动逻辑
433 | */
434 | function Move(dir, name) {
435 | const fs = new FS("dsl");
436 | var base_dir = ".trash";
437 | // 判断文件夹是否存在.不存在就创建
438 | Mkdir(base_dir);
439 | var new_dir = parseInt(Date.now() / 1000);
440 | // models的文件移动到
441 | var target_name = dir + "/" + name;
442 | // 如果表已经存在,则
443 | if (Exists(dir, name)) {
444 | Mkdir(base_dir + "/" + new_dir);
445 | Copy(target_name, base_dir + "/" + new_dir, name);
446 |
447 | // 复制完成后,删除文件
448 | fs.Remove(target_name);
449 | } else {
450 | return false;
451 | }
452 | }
453 | function Mkdir(name) {
454 | const fs = new FS("dsl");
455 | var res = fs.Exists(name);
456 | if (res !== true) {
457 | fs.Mkdir(name);
458 | }
459 | }
460 |
461 | function Copy(from, to, name) {
462 | const fs = new FS("dsl");
463 | fs.Copy(from, to + "/" + name);
464 | }
465 | /**
466 | * 查看模型是否存在
467 | * @param {*} file_name
468 | * @returns
469 | */
470 | function Exists(dir, file_name) {
471 | const fs = new FS("dsl");
472 | file_name = file_name;
473 | var res = fs.Exists(dir + "/" + file_name);
474 | return res;
475 | }
476 | ```
477 |
478 |
479 |
480 | | 方法 | 说明 |
481 | | -------- | ----------------------------------------------- |
482 | | Move() | 文件移动,如果文件已经存在,复制到.trash 文件夹内 |
483 | | Mkdir() | 生成目录 |
484 | | Copy() | 复制文件 |
485 | | Exists() | 判断文件是否存在 |
486 |
487 |
488 |
489 | ## 第三步:增加表格构造器,新建文件`/studio/table.js`
490 |
491 | ```javascript
492 | //yao studio run table.Create
493 | /**
494 | * 创建表格
495 | */
496 | function Create(model_dsl) {
497 | var fs = new FS("dsl");
498 | for (var i in model_dsl) {
499 | var table_name = model_dsl[i]["table"]["name"] + ".tab.json";
500 | //var dsl = toTable(model_dsl[i]);
501 | var dsl = Studio("colunm.toTable", model_dsl[i]);
502 | var table = JSON.stringify(dsl);
503 | Studio("move.Move", "tables", table_name);
504 | fs.WriteFile("/tables/" + table_name, table);
505 |
506 | ///
507 | var form_name = model_dsl[i]["table"]["name"] + ".form.json";
508 | var form_dsl = Studio("colunm.toForm", model_dsl[i]);
509 | var form = JSON.stringify(form_dsl);
510 | Studio("move.Move", "forms", form_name);
511 | fs.WriteFile("/forms/" + form_name, form);
512 | }
513 | }
514 | ```
515 |
516 | 新增菜单图标脚本 `/studio/menu.js`
517 |
518 | ```javascript
519 | function Create(model_dsl) {
520 | var insert = [];
521 | insert.push({
522 | blocks: 0,
523 | icon: "icon-activity",
524 | id: 1,
525 | name: "图表",
526 | parent: null,
527 | path: "/x/Chart/dashboard",
528 | visible_menu: 0,
529 | });
530 | for (var i in model_dsl) {
531 | var name = model_dsl[i]["table"]["name"];
532 | var icon = GetIcon(name);
533 | insert.push({
534 | name: model_dsl[i].name,
535 | path: "/x/Table/" + name,
536 | icon: icon,
537 | rank: i + 1,
538 | status: "enabled",
539 | parent: null,
540 | visible_menu: 0,
541 | blocks: 0,
542 | id: (i + 1) * 10,
543 | model: name,
544 | });
545 | }
546 | Studio("move.Mkdir", "flows/app");
547 | var fs = new FS("dsl");
548 | var dsl = {
549 | name: "APP Menu",
550 | nodes: [],
551 | output: insert,
552 | };
553 | var dsl = JSON.stringify(dsl);
554 | fs.WriteFile("/flows/app/menu.flow.json", dsl);
555 |
556 | // 创建看板
557 | Studio("dashboard.Create", insert);
558 |
559 | //Process("models.xiang.menu.insert", columns, insert);
560 | }
561 |
562 | /**yao studio run menu.icon user
563 | * 获取菜单图标
564 | * @param {*} name
565 | */
566 | function GetIcon(name) {
567 | var url = "https://brain.yaoapps.com/api/icon/search?name=" + name;
568 | let response = Process("xiang.network.Get", url, {}, {});
569 | if (response.status == 200) {
570 | return response.data.data;
571 | }
572 | return "icon-box";
573 | }
574 | ```
575 |
576 | 新增 `dashboard.js`,用于生成登录后面板的看板数据统计
577 |
578 | ```javascript
579 | function Create(menu_arr) {
580 | var fs = new FS("dsl");
581 |
582 | Studio("move.Move", "charts", "dashboard.chart.json");
583 | var dsl = Dsl(menu_arr);
584 | var dsl = JSON.stringify(dsl);
585 | fs.WriteFile("/charts/" + "dashboard.chart.json", dsl);
586 | WriteScript();
587 | }
588 | function Dsl(menu_arr) {
589 | var dsl = {
590 | name: "数据图表",
591 | action: {
592 | data: { process: "scripts.dashboard.Data", default: ["2022-09-20"] },
593 | },
594 | layout: {
595 | operation: {
596 | actions: [],
597 | },
598 | filter: {},
599 | chart: {
600 | columns: [],
601 | },
602 | },
603 | fields: {
604 | filter: {},
605 | chart: {},
606 | },
607 | };
608 | var chart = {
609 | 表格数量: {
610 | bind: "table_count",
611 |
612 | view: { type: "Number", props: { unit: "个" } },
613 | },
614 | 模型数量: {
615 | bind: "model_count",
616 | view: { type: "Number", props: { unit: "个" } },
617 | },
618 | };
619 | var columns = [
620 | { name: "表格数量", width: 12 },
621 | { name: "模型数量", width: 12 },
622 | ];
623 |
624 | menu_arr.forEach((col) => {
625 | if (col.id != 1) {
626 | var title = col.name + "(" + col.model + ")" + "记录数";
627 | chart[title] = {
628 | bind: col.model,
629 | link: "/x/Table/" + col.model,
630 | view: { type: "Number", props: { unit: "条" } },
631 | };
632 | columns.push({ name: title, width: 6 });
633 | }
634 | });
635 | dsl.layout.chart.columns = columns;
636 | dsl.fields.chart = chart;
637 | return dsl;
638 | }
639 | function WriteScript() {
640 | var sc = new FS("script");
641 | var scripts = `function Data() {
642 | let res = Process("schemas.default.Tables");
643 |
644 | var script = {
645 | table_count: res.length,
646 | model_count: res.length,
647 | };
648 | for (var i in res) {
649 | script[res[i]] = GetCount(res[i]);
650 | }
651 | return script;
652 | }
653 | function GetCount(model) {
654 | var query = new Query();
655 | var res = query.Get({
656 | select: [":COUNT(id) as 数量"],
657 | from: model,
658 | });
659 | if (res && res.length && res[0]["数量"] > 0) {
660 | return res[0]["数量"];
661 | }
662 | return 0;
663 | }`;
664 | sc.WriteFile("/dashboard.js", scripts);
665 | }
666 | ```
667 |
668 | 最后运行调试命令`yao studio run model.Create`,我们可以看到创建了不少的数据模型和表格,我们运行`yao start`访问一下`http://127.0.0.1:5099/yao/login/admin`,输入默认用户名: `xiang@iqka.com`, 密码: `A123456p+`
669 |
--------------------------------------------------------------------------------