@yukihong/schema-admin-x 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +3 -0
- package/.eslintrc +55 -0
- package/README.md +208 -0
- package/app/controller/base.js +39 -0
- package/app/controller/project.js +80 -0
- package/app/controller/view.js +20 -0
- package/app/extend/logger.js +39 -0
- package/app/middleware/api-params-verify.js +79 -0
- package/app/middleware/api-sign-verify.js +34 -0
- package/app/middleware/error-handler.js +34 -0
- package/app/middleware/project-handler.js +27 -0
- package/app/middleware.js +40 -0
- package/app/pages/asserts/custom.css +13 -0
- package/app/pages/boot.js +53 -0
- package/app/pages/common/curl.js +91 -0
- package/app/pages/common/utils.js +3 -0
- package/app/pages/dashboard/complex-view/header-view/complex-view/sub-menu/sub-menu.vue +21 -0
- package/app/pages/dashboard/complex-view/header-view/header-view.vue +127 -0
- package/app/pages/dashboard/complex-view/iframe-view/iframe-view.vue +44 -0
- package/app/pages/dashboard/complex-view/schema-view/complex-view/search-panel/search-panel.vue +41 -0
- package/app/pages/dashboard/complex-view/schema-view/complex-view/table-panel/table-panel.vue +131 -0
- package/app/pages/dashboard/complex-view/schema-view/components/component-config.js +23 -0
- package/app/pages/dashboard/complex-view/schema-view/components/create-form/create-form.vue +100 -0
- package/app/pages/dashboard/complex-view/schema-view/components/detail-panel/detail-panel.vue +106 -0
- package/app/pages/dashboard/complex-view/schema-view/components/edit-form/edit-form.vue +131 -0
- package/app/pages/dashboard/complex-view/schema-view/hook/schema.js +126 -0
- package/app/pages/dashboard/complex-view/schema-view/schema-view.vue +102 -0
- package/app/pages/dashboard/complex-view/sider-view/complex-view/sub-menu/sub-menu.vue +26 -0
- package/app/pages/dashboard/complex-view/sider-view/sider-view.vue +128 -0
- package/app/pages/dashboard/dashboard.vue +99 -0
- package/app/pages/dashboard/entry.dashboard.js +44 -0
- package/app/pages/store/index.js +4 -0
- package/app/pages/store/menu.js +61 -0
- package/app/pages/store/project.js +17 -0
- package/app/pages/widgets/header-container/asserts/avatar.png +0 -0
- package/app/pages/widgets/header-container/asserts/logo.png +0 -0
- package/app/pages/widgets/header-container/header-container.vue +109 -0
- package/app/pages/widgets/schema-form/complex-view/input/input.vue +146 -0
- package/app/pages/widgets/schema-form/complex-view/input-number/input-number.vue +140 -0
- package/app/pages/widgets/schema-form/complex-view/select/select.vue +121 -0
- package/app/pages/widgets/schema-form/form-item-config.js +23 -0
- package/app/pages/widgets/schema-form/schema-form.vue +131 -0
- package/app/pages/widgets/schema-search-bar/complex-view/date-range/date-range.vue +50 -0
- package/app/pages/widgets/schema-search-bar/complex-view/dynamic-select/dynamic-select.vue +65 -0
- package/app/pages/widgets/schema-search-bar/complex-view/input/input.vue +44 -0
- package/app/pages/widgets/schema-search-bar/complex-view/select/select.vue +51 -0
- package/app/pages/widgets/schema-search-bar/schema-search-bar.vue +127 -0
- package/app/pages/widgets/schema-search-bar/search-item-config.js +27 -0
- package/app/pages/widgets/schema-table/schema-table.vue +248 -0
- package/app/pages/widgets/sider-container/sider-container.vue +28 -0
- package/app/public/static/logo.png +0 -0
- package/app/public/static/normalize.css +239 -0
- package/app/router/project.js +7 -0
- package/app/router/view.js +10 -0
- package/app/router-schema/project.js +30 -0
- package/app/service/base.js +13 -0
- package/app/service/project.js +50 -0
- package/app/view/entry.tpl +25 -0
- package/app/webpack/config/webpack.base.js +213 -0
- package/app/webpack/config/webpack.dev.js +61 -0
- package/app/webpack/config/webpack.prod.js +124 -0
- package/app/webpack/dev.js +63 -0
- package/app/webpack/libs/blank.js +1 -0
- package/app/webpack/prod.js +19 -0
- package/config/config.beta.js +3 -0
- package/config/config.default.js +3 -0
- package/config/config.prod.js +3 -0
- package/elpis-core/env.js +20 -0
- package/elpis-core/index.js +99 -0
- package/elpis-core/loader/config.js +52 -0
- package/elpis-core/loader/controller.js +75 -0
- package/elpis-core/loader/extend.js +55 -0
- package/elpis-core/loader/middleware.js +64 -0
- package/elpis-core/loader/router-schema.js +47 -0
- package/elpis-core/loader/router.js +59 -0
- package/elpis-core/loader/service.js +75 -0
- package/index.js +40 -0
- package/model/index.js +102 -0
- package/package.json +93 -0
- package/test/controller/project.test.js +217 -0
package/.eslintignore
ADDED
package/.eslintrc
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": [
|
|
3
|
+
"plugin:vue/base",
|
|
4
|
+
"plugin:vue/recommended",
|
|
5
|
+
],
|
|
6
|
+
"plugins": ["vue"],
|
|
7
|
+
"env": {
|
|
8
|
+
"browser": true,
|
|
9
|
+
"node": true
|
|
10
|
+
},
|
|
11
|
+
"parser": "vue-eslint-parser",
|
|
12
|
+
"parserOptions": {
|
|
13
|
+
"parser": "babel-eslint",
|
|
14
|
+
"ecmaVersion": 2017,
|
|
15
|
+
"sourceType": "module"
|
|
16
|
+
},
|
|
17
|
+
"rules": {
|
|
18
|
+
"no-unused-vars": [2, {"args": "none"}],
|
|
19
|
+
"strict": "off",
|
|
20
|
+
"valid-jsdoc": "off",
|
|
21
|
+
"jsdoc/require-param-description": "off",
|
|
22
|
+
"jsdoc/require-param-type": "off",
|
|
23
|
+
"jsdoc/check-param-names": "off",
|
|
24
|
+
"jsdoc/require-param": "off",
|
|
25
|
+
"jsdoc/check-tag-names": "off",
|
|
26
|
+
"linebreak-style": "off",
|
|
27
|
+
"array-bracket-spacing": "off",
|
|
28
|
+
"prefer-promise-reject-errors": "off",
|
|
29
|
+
"comma-dangle": "off",
|
|
30
|
+
"newline-per-chained-call": "off",
|
|
31
|
+
"no-loop-func": "off",
|
|
32
|
+
"no-empty": "off",
|
|
33
|
+
"no-else-return": "off",
|
|
34
|
+
"no-unneeded-ternary": "off",
|
|
35
|
+
"no-eval": "off",
|
|
36
|
+
"prefer-destructuring": "off",
|
|
37
|
+
"no-param-reassign": "off",
|
|
38
|
+
"max-len": "off",
|
|
39
|
+
"no-restricted-syntax": "off",
|
|
40
|
+
"no-plusplus": "off",
|
|
41
|
+
"no-useless-escape": "off",
|
|
42
|
+
"no-nested-ternary": "off",
|
|
43
|
+
"radix": "off",
|
|
44
|
+
"arrow-body-style": "off",
|
|
45
|
+
"arrow-parens": "off",
|
|
46
|
+
"vue/multi-word-component-names": "off",
|
|
47
|
+
"vue/valid-v-for": "off",
|
|
48
|
+
"vue/no-multiple-template-root": "off"
|
|
49
|
+
},
|
|
50
|
+
"globals": {
|
|
51
|
+
"$": true,
|
|
52
|
+
"axios": true,
|
|
53
|
+
"Vue": true
|
|
54
|
+
}
|
|
55
|
+
}
|
package/README.md
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# Elpis
|
|
2
|
+
## 一个面向后台管理系统的前后端一体化框架,内置 Koa 服务端和 Vue3 + Element Plus 前端模板,通过配置化 Schema 快速生成列表、表单、搜索等常见业务页面,并提供可插拔的自定义路由、组件和构建配置扩展能力,以 npm 包形式在多个项目中复用。
|
|
3
|
+
|
|
4
|
+
### model配置
|
|
5
|
+
```javascript
|
|
6
|
+
{
|
|
7
|
+
mode: 'dashboard', // 模板类型,不同模板类型对应不一样的模板数据结构
|
|
8
|
+
name: '', // 名称
|
|
9
|
+
desc: '', // 描述
|
|
10
|
+
icon: '', // icon
|
|
11
|
+
homePage: '', // 首页(项目配置)
|
|
12
|
+
// 头部菜单
|
|
13
|
+
menu: [{
|
|
14
|
+
key: '', // 菜单唯一描述
|
|
15
|
+
name: '', // 菜单名称
|
|
16
|
+
menuType: '', // 枚举值 group/ module
|
|
17
|
+
|
|
18
|
+
// 当 menuType == group 时,可填
|
|
19
|
+
subMenu: [{
|
|
20
|
+
// 可递归 menuItem
|
|
21
|
+
}, ...],
|
|
22
|
+
|
|
23
|
+
// 当 menuType == module 时,可填
|
|
24
|
+
moduleType: '', // 枚举值 sider/iframe/custom/schema
|
|
25
|
+
|
|
26
|
+
// 当 moduleType == sider 时 (侧边菜单)
|
|
27
|
+
siderConfig: {
|
|
28
|
+
menu: [{
|
|
29
|
+
// 可递归 menuItem(除 moduleType == sider )
|
|
30
|
+
}, ...]
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
// 当 moduleType == iframe 时
|
|
34
|
+
iframeConfig: {
|
|
35
|
+
path: '', // iframe 路径
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
// 当 moduleType == custom 时
|
|
39
|
+
customConfig: {
|
|
40
|
+
path: '', // 自定义路由路径
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
// 当 moduleType == schema 时
|
|
44
|
+
schemaConfig: {
|
|
45
|
+
api: '', // 数据源API(遵循 RESTFUL 规范)
|
|
46
|
+
schema: { // 板块数据结构
|
|
47
|
+
type: 'object',
|
|
48
|
+
properties: {
|
|
49
|
+
key: {
|
|
50
|
+
...schema, // 标准 schema 配置
|
|
51
|
+
type: '', // 字段类型
|
|
52
|
+
label: '', // 字段的中文名
|
|
53
|
+
// 字段在 table 中的相关配置
|
|
54
|
+
tableOption: {
|
|
55
|
+
...elTableColumnConfig, // 标准的 el-table-column 配置
|
|
56
|
+
toFixed: 0, // 保留小数点后几位
|
|
57
|
+
visible: true, // 默认为 true (false 时,表示不在表单中显示)
|
|
58
|
+
},
|
|
59
|
+
// 字段在 search-bar 中的相关配置
|
|
60
|
+
searchOption: {
|
|
61
|
+
...elComponentConfig, // 标准 el-component-column 配置
|
|
62
|
+
|
|
63
|
+
// 配置组件类型
|
|
64
|
+
// -- input/select/dynamicSelect/dateRange...
|
|
65
|
+
comType: '',
|
|
66
|
+
default: '', // 默认值
|
|
67
|
+
|
|
68
|
+
// comType === 'select'
|
|
69
|
+
enumList: [], // 下拉框可选项
|
|
70
|
+
|
|
71
|
+
// comType === 'dynamicSelect'
|
|
72
|
+
api: '',
|
|
73
|
+
},
|
|
74
|
+
// 字段在不同动态 component 中的相关配置,前缀对应 componentConfig 中的键值
|
|
75
|
+
// 如:componentConfig.createForm,这里对应 createFormOption
|
|
76
|
+
// 字段在 createForm 中相关配置
|
|
77
|
+
createFormOption: {
|
|
78
|
+
...elComponentConfig, // 标准 el-component-column 配置
|
|
79
|
+
comType: '', // 控件类型 input/select/input-number
|
|
80
|
+
visible: true, // 是否展示(true/false),默认为 true
|
|
81
|
+
disabled: false, // 是否禁用(true/false),默认为 false
|
|
82
|
+
default: '', // 默认值
|
|
83
|
+
|
|
84
|
+
// comType === 'select'
|
|
85
|
+
enumList: [], // 下拉框可选项
|
|
86
|
+
},
|
|
87
|
+
// 字段在 editForm 中相关配置
|
|
88
|
+
editFormOption: {
|
|
89
|
+
...elComponentConfig, // 标准 el-component-column 配置
|
|
90
|
+
comType: '', // 控件类型 input/select/input-number
|
|
91
|
+
visible: true, // 是否展示(true/false),默认为 true
|
|
92
|
+
disabled: false, // 是否禁用(true/false),默认为 false
|
|
93
|
+
default: '', // 默认值
|
|
94
|
+
},
|
|
95
|
+
// 字段在 detail-panel 中相关配置
|
|
96
|
+
detailPanelOption: {
|
|
97
|
+
...elComponentConfig, // 标准 el-component-column 配置
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
...
|
|
101
|
+
},
|
|
102
|
+
required: [], // 标记哪些字段是必填项
|
|
103
|
+
},
|
|
104
|
+
// table 相关配置
|
|
105
|
+
tableConfig: {
|
|
106
|
+
headerButtons: [{
|
|
107
|
+
label: '', // 按钮中文名
|
|
108
|
+
eventKey: '', // 按钮事件名
|
|
109
|
+
// 按钮事件具体配置
|
|
110
|
+
eventOption: {
|
|
111
|
+
// 当 eventKey === 'showComponent'
|
|
112
|
+
comName: '', // 组件名称
|
|
113
|
+
},
|
|
114
|
+
...elButtonConfig // 标准 el-button 配置
|
|
115
|
+
}, ...],
|
|
116
|
+
rowButtons: [{
|
|
117
|
+
label: '', // 按钮中文名
|
|
118
|
+
eventKey: '', // 按钮事件名
|
|
119
|
+
eventOption: { // 按钮事件具体配置
|
|
120
|
+
// 当 eventKey === 'showComponent'
|
|
121
|
+
comName: '', // 组件名称
|
|
122
|
+
|
|
123
|
+
// 当 eventKey === 'remove'
|
|
124
|
+
params: {
|
|
125
|
+
// paramKey = 参数的键值
|
|
126
|
+
// rowValueKey = 参数值,格式为 schema::tableKey 的时候,到 table 中找相应的字段)
|
|
127
|
+
// 例如:user_id:schema::user_id
|
|
128
|
+
paramKey: rowValueKey
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
...elButtonConfig // 标准 el-button 配置
|
|
132
|
+
}, ...]
|
|
133
|
+
},
|
|
134
|
+
searchConfig: {}, // search-bar 相关配置
|
|
135
|
+
|
|
136
|
+
// 动态组件 相关配置
|
|
137
|
+
componentConfig: {
|
|
138
|
+
// create-from 表单相关配置
|
|
139
|
+
createForm: {
|
|
140
|
+
title: '', // 表单标题
|
|
141
|
+
saveBtnText: '', // 保存按钮文案
|
|
142
|
+
},
|
|
143
|
+
// edit-form 表单相关配置
|
|
144
|
+
editForm: {
|
|
145
|
+
mainKey: '', // 表单主键,用于唯一标识要修改的数据对象
|
|
146
|
+
title: '', // 表单标题
|
|
147
|
+
saveBtnText: '', // 保存按钮文案
|
|
148
|
+
},
|
|
149
|
+
// detail-panel 相关配置
|
|
150
|
+
detailPanel: {
|
|
151
|
+
mainKey: '', // 表单主键,用于唯一标识要修改的数据对象
|
|
152
|
+
title: '', // 表单标题
|
|
153
|
+
saveBtnText: '', // 保存按钮文案
|
|
154
|
+
}
|
|
155
|
+
// ... 支持用户动态扩展
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}, ...]
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### 服务端启动
|
|
163
|
+
```javascript
|
|
164
|
+
const {
|
|
165
|
+
serverStart
|
|
166
|
+
} = require('@yukihong/schema-admin-x');
|
|
167
|
+
|
|
168
|
+
// 启动 elpis 服务
|
|
169
|
+
const app = serverStart({});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### 自定义服务端
|
|
173
|
+
- router-schema
|
|
174
|
+
- router
|
|
175
|
+
- controller
|
|
176
|
+
- service
|
|
177
|
+
- extend
|
|
178
|
+
- config
|
|
179
|
+
|
|
180
|
+
### 前端构建
|
|
181
|
+
```javascript
|
|
182
|
+
const { frontendBuild } = require('@yukihong/schema-admin-x');
|
|
183
|
+
|
|
184
|
+
// 编译构建前端工程
|
|
185
|
+
frontendBuild(process.env._ENV);
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### 自定义页面扩展
|
|
189
|
+
* 在 `app/pages/` 目录下写入入口 entry.xxx.js
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
### dashboard /custom-view 自定义页面扩展
|
|
193
|
+
* 在 `app/pages/dashboard/xxxx` 下写页面
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
### dashboard / schema-view / components 动态组件扩展
|
|
197
|
+
1. 在 `app/pages/dashboard/complex-view/schema-view/components` 下写组件
|
|
198
|
+
2. 配置到 `app/pages/dashboard/complex-view/schema-view/components/component-config.js`
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
### schema-form 控件扩展
|
|
202
|
+
1. 在 `app/widgets/schema-form/complex-view` 下写控件
|
|
203
|
+
2. 配置到 `app/widgets/schema-form/form-item-config.js`
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
### schema-search-bar 控件扩展
|
|
207
|
+
1. 在 `app/widgets/schema-search-bar/complex-view` 下写控件
|
|
208
|
+
2. 配置到 `app/widgets/schema-search-bar/search-item-config.js`
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module.exports = (app) => class BaseController {
|
|
2
|
+
/**
|
|
3
|
+
* controller 基类
|
|
4
|
+
* 统一收拢 controller 相关的公共方法
|
|
5
|
+
*/
|
|
6
|
+
constructor() {
|
|
7
|
+
this.app = app;
|
|
8
|
+
this.config = app.config;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* API 处理成功时统一返回结构
|
|
13
|
+
* @param {object} ctx 上下文
|
|
14
|
+
* @param {object} data 核心数据
|
|
15
|
+
* @param {object} metadata 附加数据
|
|
16
|
+
*/
|
|
17
|
+
success(ctx, data = {}, metadata = {}) {
|
|
18
|
+
ctx.status = 200;
|
|
19
|
+
ctx.body = {
|
|
20
|
+
success: true,
|
|
21
|
+
data,
|
|
22
|
+
metadata
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* API 处理失败时统一返回结构
|
|
28
|
+
* @param {object} ctx 上下文
|
|
29
|
+
* @param {string} message 错误新信息
|
|
30
|
+
* @param {number} code 错误码
|
|
31
|
+
*/
|
|
32
|
+
fail(ctx, message, code) {
|
|
33
|
+
ctx.body = {
|
|
34
|
+
success: false,
|
|
35
|
+
message,
|
|
36
|
+
code
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
module.exports = (app) => {
|
|
2
|
+
|
|
3
|
+
const BaseController = require('./base.js')(app);
|
|
4
|
+
|
|
5
|
+
return class ProjectController extends BaseController {
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 根据 proj_key 获取项目配置
|
|
9
|
+
*/
|
|
10
|
+
get(ctx) {
|
|
11
|
+
const {
|
|
12
|
+
proj_key: projKey
|
|
13
|
+
} = ctx.request.query;
|
|
14
|
+
|
|
15
|
+
const { project: projectService } = app.service;
|
|
16
|
+
const projConfig = projectService.get(projKey);
|
|
17
|
+
|
|
18
|
+
if(!projConfig) {
|
|
19
|
+
this.fail(ctx, '获取项目异常', 50000);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
this.success(ctx, projConfig);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 获取当前 projectKey 对应模型下的项目列表(如果无 projectKey, 全量获取)
|
|
28
|
+
*/
|
|
29
|
+
async getList(ctx) {
|
|
30
|
+
const {
|
|
31
|
+
proj_key: projKey
|
|
32
|
+
} = ctx.request.query;
|
|
33
|
+
|
|
34
|
+
const { project: projectService } = app.service;
|
|
35
|
+
const projectList = projectService.getList({projKey});
|
|
36
|
+
|
|
37
|
+
// 构造关键数据 list
|
|
38
|
+
const dtoProjectList = projectList.map(item => {
|
|
39
|
+
const { modelKey, key, name, desc, homePage } = item;
|
|
40
|
+
return { modelKey, key, name, desc, homePage };
|
|
41
|
+
})
|
|
42
|
+
this.success(ctx, dtoProjectList);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 获取所有模型与项目的结构化数据
|
|
47
|
+
* @param {Object} ctx 上下文 koa 框架自动传的
|
|
48
|
+
*/
|
|
49
|
+
async getModelList(ctx) {
|
|
50
|
+
const { project: projectService } = app.service;
|
|
51
|
+
const modelList = await projectService.getModelList();
|
|
52
|
+
|
|
53
|
+
// 构造返回结果,只返回关键数据
|
|
54
|
+
const dtoModelList = modelList.reduce((preList, item) => {
|
|
55
|
+
const { model, project } = item;
|
|
56
|
+
|
|
57
|
+
// 构造 model 关键数据
|
|
58
|
+
const { key, name, desc } = model;
|
|
59
|
+
const dtoModel = { key, name, desc };
|
|
60
|
+
|
|
61
|
+
// 构造 project 关键数据
|
|
62
|
+
const dtoProject = Object.keys(project).reduce((preObj, projKey) => {
|
|
63
|
+
const { key, name, desc, homePage } = project[projKey];
|
|
64
|
+
preObj[projKey] = { key, name, desc, homePage };
|
|
65
|
+
return preObj;
|
|
66
|
+
}, {})
|
|
67
|
+
|
|
68
|
+
// 整合返回结构
|
|
69
|
+
preList.push({
|
|
70
|
+
model: dtoModel,
|
|
71
|
+
project: dtoProject
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
return preList;
|
|
75
|
+
}, [])
|
|
76
|
+
|
|
77
|
+
this.success(ctx, dtoModelList)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module.exports = (app) => {
|
|
2
|
+
return class ViewController {
|
|
3
|
+
/**
|
|
4
|
+
* 渲染页面
|
|
5
|
+
* @param {Object} ctx 上下文 koa 框架自动传的
|
|
6
|
+
*/
|
|
7
|
+
async renderPage(ctx) {
|
|
8
|
+
const { query, params } = ctx.request;
|
|
9
|
+
app.logger.info(`[ViewController] query:${JSON.stringify(query)}`);
|
|
10
|
+
app.logger.info(`[ViewController] params:${JSON.stringify(params)}`);
|
|
11
|
+
|
|
12
|
+
await ctx.render(`dist/entry.${ctx.params.page}`, {
|
|
13
|
+
projKey:ctx.query?.proj_key,
|
|
14
|
+
name: app.options?.name,
|
|
15
|
+
env: app.env.get(),
|
|
16
|
+
options: JSON.stringify(app.options)
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const log4js = require('log4js');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 日志工具
|
|
5
|
+
* 外部调用 app.logger.log app.logger.error
|
|
6
|
+
*/
|
|
7
|
+
module.exports = (app) => {
|
|
8
|
+
let logger;
|
|
9
|
+
|
|
10
|
+
if(app.env.isLocal()) {
|
|
11
|
+
// 打印在控制台即可
|
|
12
|
+
logger = console;
|
|
13
|
+
} else {
|
|
14
|
+
// 把日志输出并落地到磁盘(日志落盘)
|
|
15
|
+
log4js.configure({
|
|
16
|
+
appenders: {
|
|
17
|
+
console: {
|
|
18
|
+
type: 'console'
|
|
19
|
+
},
|
|
20
|
+
// 日志文件切分
|
|
21
|
+
dateFile: {
|
|
22
|
+
type: 'dateFile',
|
|
23
|
+
filename: './logs/application.log',
|
|
24
|
+
pattern: '.yyy-MM-dd'
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
categories: {
|
|
28
|
+
default: {
|
|
29
|
+
appenders: [ 'console', 'dateFile' ],
|
|
30
|
+
level: 'trace'
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
logger = log4js.getLogger();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return logger
|
|
39
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const Ajv = require('ajv');
|
|
2
|
+
const ajv = new Ajv();
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* API 参数校验
|
|
6
|
+
*/
|
|
7
|
+
module.exports = (app) => {
|
|
8
|
+
const $schema = "http://json-schema.org/draft-07/schema#";
|
|
9
|
+
|
|
10
|
+
return async (ctx,next) => {
|
|
11
|
+
|
|
12
|
+
// 只对 API 参数做校验
|
|
13
|
+
if(ctx.path.indexOf('/api/') < 0) {
|
|
14
|
+
return await next();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 获取请求参数
|
|
18
|
+
const { body, query, headers } = ctx.request;
|
|
19
|
+
const { params, path, method } = ctx;
|
|
20
|
+
|
|
21
|
+
app.logger.info(`[${method}] ${path} body:${JSON.stringify(body)}`);
|
|
22
|
+
app.logger.info(`[${method}] ${path} query:${JSON.stringify(query)}`);
|
|
23
|
+
app.logger.info(`[${method}] ${path} params:${JSON.stringify(params)}`);
|
|
24
|
+
app.logger.info(`[${method}] ${path} headers:${JSON.stringify(headers)}`);
|
|
25
|
+
|
|
26
|
+
// app.logger.info('当前 roterSchema 结构:', JSON.stringify(app.routerSchema, null, 2));
|
|
27
|
+
const schema = app.routerSchema[path]?.[method.toLowerCase()];
|
|
28
|
+
app.logger.info('schema:', JSON.stringify(schema, null, 2));
|
|
29
|
+
|
|
30
|
+
if(!schema) {
|
|
31
|
+
return await next();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let valid = true;
|
|
35
|
+
|
|
36
|
+
// ajv 校验器
|
|
37
|
+
let validate;
|
|
38
|
+
|
|
39
|
+
// 校验 headers
|
|
40
|
+
if(valid && headers && schema.headers) {
|
|
41
|
+
schema.headers.$schema = $schema;
|
|
42
|
+
validate = ajv.compile(schema.headers);
|
|
43
|
+
valid = validate(headers);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 校验 body
|
|
47
|
+
if(valid && headers && schema.body) {
|
|
48
|
+
schema.body.$schema = $schema;
|
|
49
|
+
validate = ajv.compile(schema.body);
|
|
50
|
+
valid = validate(body);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 校验 query
|
|
54
|
+
if(valid && query && schema.query) {
|
|
55
|
+
schema.query.$schema = $schema;
|
|
56
|
+
validate = ajv.compile(schema.query);
|
|
57
|
+
valid = validate(query);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 校验 params
|
|
61
|
+
if(valid && params && schema.params) {
|
|
62
|
+
schema.params.$schema = $schema;
|
|
63
|
+
validate = ajv.compile(schema.params);
|
|
64
|
+
valid = validate(params);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if(!valid) {
|
|
68
|
+
ctx.status = 200;
|
|
69
|
+
ctx.body = {
|
|
70
|
+
success: false,
|
|
71
|
+
message: `request validate fail:${ajv.errorsText(validate.errors)}`,
|
|
72
|
+
code: 442
|
|
73
|
+
}
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
await next();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const md5 = require("md5");
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* API 签名合法性校验
|
|
5
|
+
*/
|
|
6
|
+
module.exports = (app) => {
|
|
7
|
+
return async (ctx,next) => {
|
|
8
|
+
|
|
9
|
+
// 只对 API 请求做校验
|
|
10
|
+
if(ctx.path.indexOf('api') < 0) {
|
|
11
|
+
return await next();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const { path, method } = ctx;
|
|
15
|
+
const { headers } = ctx.request;
|
|
16
|
+
const { s_sign: sSign, s_t: st } = headers;
|
|
17
|
+
|
|
18
|
+
const signKey = 'asd23fhakjs25dhaksdh5kjashcjk';
|
|
19
|
+
const signature = md5(`${signKey}_${st}`);
|
|
20
|
+
app.logger.info(`[${method} ${path}] signature: ${signature}`);
|
|
21
|
+
|
|
22
|
+
if(!sSign || !st || signature !== sSign.toLowerCase() || Date.now() - st > 600000 ){
|
|
23
|
+
ctx.status = 200;
|
|
24
|
+
ctx.body = {
|
|
25
|
+
success: false,
|
|
26
|
+
message: 'signature not correct or api timeout!!!',
|
|
27
|
+
code: 445
|
|
28
|
+
}
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
await next();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* 运行时异常错误处理,兜底所有异常
|
|
4
|
+
* @param {[object]} app koa 实例
|
|
5
|
+
*/
|
|
6
|
+
module.exports = (app) => {
|
|
7
|
+
return async (ctx, next) => {
|
|
8
|
+
try {
|
|
9
|
+
await next();
|
|
10
|
+
} catch(err) {
|
|
11
|
+
// 异常处理
|
|
12
|
+
const { status, message, detail } = err;
|
|
13
|
+
|
|
14
|
+
app.logger.info(JSON.stringify(err));
|
|
15
|
+
app.logger.error('[-- exception --]:', status, message, detail);
|
|
16
|
+
|
|
17
|
+
if( message && message.indexOf('template not found') > -1) {
|
|
18
|
+
// 页面重定向
|
|
19
|
+
ctx.status = 302; // 临时重定向
|
|
20
|
+
ctx.redirect(`${app.options?.homePage}`)
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const resBody = {
|
|
25
|
+
success: false,
|
|
26
|
+
code: 50000,
|
|
27
|
+
message: '网络异常,请稍后重试'
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
ctx.status = 200;
|
|
31
|
+
ctx.body = resBody;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* projectHandler 处理项目相关内容
|
|
3
|
+
*/
|
|
4
|
+
module.exports = (app) => {
|
|
5
|
+
return async (ctx, next) => {
|
|
6
|
+
// 只对 业务 API 进行 proj_key 处理
|
|
7
|
+
if(ctx.path.indexOf('/api/proj/') < 0) {
|
|
8
|
+
return await next();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// 获取 projKey
|
|
12
|
+
const { proj_key: projKey } = ctx.request.headers;
|
|
13
|
+
if(!projKey) {
|
|
14
|
+
ctx.status = 200;
|
|
15
|
+
ctx.body = {
|
|
16
|
+
success: false,
|
|
17
|
+
message: 'proj_key not found',
|
|
18
|
+
code: 446
|
|
19
|
+
}
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
ctx.projKey = projKey;
|
|
24
|
+
|
|
25
|
+
await next();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
// 当有 HTTP 请求到达时,Koa 会按照中间件注册的顺序,
|
|
4
|
+
// 自动调用队列里的每一个中间件函数,并传入 ctx(上下文)和 next(下一个中间件的引用)。
|
|
5
|
+
module.exports = (app) => {
|
|
6
|
+
|
|
7
|
+
// 配置静态根目录
|
|
8
|
+
const koaStatic = require('koa-static');
|
|
9
|
+
app.use( koaStatic( path.resolve(process.cwd(), './app/public' )) );
|
|
10
|
+
|
|
11
|
+
// 模板渲染引擎
|
|
12
|
+
const koaNunjucks = require('koa-nunjucks-2');
|
|
13
|
+
app.use(koaNunjucks({
|
|
14
|
+
ext: 'tpl',
|
|
15
|
+
path: path.join(process.cwd(), './app/public'),
|
|
16
|
+
nunjucksConfig: {
|
|
17
|
+
noCache: false,
|
|
18
|
+
trimBlocks: true
|
|
19
|
+
}
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
// 引入 ctx.body 解析中间件
|
|
23
|
+
const bdyParser = require('koa-bodyparser');
|
|
24
|
+
app.use(bdyParser({
|
|
25
|
+
formList: '1000ms',
|
|
26
|
+
enableTypes: ['form', 'json', 'text']
|
|
27
|
+
}))
|
|
28
|
+
|
|
29
|
+
// 引入异常捕获中间件
|
|
30
|
+
app.use(app.middlewares.errorHandler);
|
|
31
|
+
|
|
32
|
+
// 签名合法性校验
|
|
33
|
+
app.use(app.middlewares.apiSignVerify);
|
|
34
|
+
|
|
35
|
+
// 引入 API 参数校验
|
|
36
|
+
app.use(app.middlewares.apiParamsVerify);
|
|
37
|
+
|
|
38
|
+
// 引入 项目处理中间件
|
|
39
|
+
app.use(app.middlewares.projectHandler);
|
|
40
|
+
}
|