@tushi11/elpis 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 +2 -0
- package/.eslintrc +55 -0
- package/README.md +239 -0
- package/app/controller/base.js +42 -0
- package/app/controller/project.js +78 -0
- package/app/controller/view.js +19 -0
- package/app/extend/logger.js +36 -0
- package/app/middleware/api-params-verify.js +75 -0
- package/app/middleware/api-sign-verify.js +37 -0
- package/app/middleware/error-handler.js +33 -0
- package/app/middleware/project-handler.js +27 -0
- package/app/middleware.js +41 -0
- package/app/pages/asserts/custom.css +13 -0
- package/app/pages/boot.js +49 -0
- package/app/pages/common/curl.js +86 -0
- package/app/pages/common/utils.js +2 -0
- package/app/pages/dashboard/complex-view/header-view/complex-view/sub-menu/sub-menu.vue +20 -0
- package/app/pages/dashboard/complex-view/header-view/header-view.vue +134 -0
- package/app/pages/dashboard/complex-view/iframe-view/iframe-view.vue +48 -0
- package/app/pages/dashboard/complex-view/schema-view/complex-view/search-panel/search-panel.vue +37 -0
- package/app/pages/dashboard/complex-view/schema-view/complex-view/table-panel/table-panel.vue +127 -0
- package/app/pages/dashboard/complex-view/schema-view/components/components-config.js +23 -0
- package/app/pages/dashboard/complex-view/schema-view/components/create-form/create-form.vue +101 -0
- package/app/pages/dashboard/complex-view/schema-view/components/detail-panel/detail-panel.vue +98 -0
- package/app/pages/dashboard/complex-view/schema-view/components/edit-form/edit-form.vue +125 -0
- package/app/pages/dashboard/complex-view/schema-view/hook/schema.js +130 -0
- package/app/pages/dashboard/complex-view/schema-view/schema-view.vue +101 -0
- package/app/pages/dashboard/complex-view/sider-view/complex-view/sub-menu/sub-menu.vue +23 -0
- package/app/pages/dashboard/complex-view/sider-view/sider-view.vue +130 -0
- package/app/pages/dashboard/dashboard.vue +99 -0
- package/app/pages/dashboard/entry.dashboard.js +46 -0
- package/app/pages/store/index.js +4 -0
- package/app/pages/store/menu.js +73 -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 +98 -0
- package/app/pages/widgets/schema-form/complex-view/input/input.vue +135 -0
- package/app/pages/widgets/schema-form/complex-view/input-number/input-number.vue +135 -0
- package/app/pages/widgets/schema-form/complex-view/select/select.vue +116 -0
- package/app/pages/widgets/schema-form/form-item-config.js +23 -0
- package/app/pages/widgets/schema-form/schema-form.vue +144 -0
- package/app/pages/widgets/schema-search-bar/complex-view/date-range/date-range.vue +45 -0
- package/app/pages/widgets/schema-search-bar/complex-view/dynamic-select/dynamic-select.vue +60 -0
- package/app/pages/widgets/schema-search-bar/complex-view/input/input.vue +31 -0
- package/app/pages/widgets/schema-search-bar/complex-view/select/select.vue +40 -0
- package/app/pages/widgets/schema-search-bar/schema-search-bar.vue +120 -0
- package/app/pages/widgets/schema-search-bar/search-item-config.js +27 -0
- package/app/pages/widgets/schema-table/schema-table.vue +255 -0
- package/app/pages/widgets/sider-container/sider-container.vue +28 -0
- package/app/router/project.js +12 -0
- package/app/router/view.js +8 -0
- package/app/router-schema/project.js +31 -0
- package/app/service/base.js +13 -0
- package/app/service/project.js +46 -0
- package/app/view/entry.tpl +25 -0
- package/app/webpack/config/webpack.base.js +267 -0
- package/app/webpack/config/webpack.dev.js +62 -0
- package/app/webpack/config/webpack.prod.js +118 -0
- package/app/webpack/dev.js +56 -0
- package/app/webpack/libs/blank.js +1 -0
- package/app/webpack/prod.js +22 -0
- package/config/config.default.js +12 -0
- package/elpis-core/env.js +20 -0
- package/elpis-core/index.js +98 -0
- package/elpis-core/loader/config.js +57 -0
- package/elpis-core/loader/controller.js +82 -0
- package/elpis-core/loader/extend.js +62 -0
- package/elpis-core/loader/middleware.js +74 -0
- package/elpis-core/loader/router-schema.js +56 -0
- package/elpis-core/loader/router.js +51 -0
- package/elpis-core/loader/service.js +74 -0
- package/index.js +38 -0
- package/model/index.js +111 -0
- package/package.json +92 -0
- package/test/controller/project.test.js +215 -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,239 @@
|
|
|
1
|
+
# elpis
|
|
2
|
+
|
|
3
|
+
## 一个企业级应用框架,通过全栈实现。
|
|
4
|
+
|
|
5
|
+
### model配置
|
|
6
|
+
```javascript
|
|
7
|
+
{
|
|
8
|
+
mode: 'dashboard', // 模板类型,不同模板类型对应不一样的模板数据结构
|
|
9
|
+
name: '', // 名称
|
|
10
|
+
desc: '', // 描述
|
|
11
|
+
icon: '', // 图标
|
|
12
|
+
homePage: '', // 首页(项目配置)
|
|
13
|
+
// 头部菜单
|
|
14
|
+
menu: [
|
|
15
|
+
{
|
|
16
|
+
key: '', // 菜单唯一描述
|
|
17
|
+
name: '', // 菜单名称
|
|
18
|
+
menuType: '', // 枚举值, group / module
|
|
19
|
+
|
|
20
|
+
// 当menuType == group时,可填
|
|
21
|
+
subMenu: [
|
|
22
|
+
{
|
|
23
|
+
// 可递归 menuItem
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
|
|
27
|
+
// 当menuType == module时,可填
|
|
28
|
+
moduleType: '', // 枚举值: sider/iframe/custom/schema
|
|
29
|
+
|
|
30
|
+
// 当moduleType == sider 时
|
|
31
|
+
siderConfig: {
|
|
32
|
+
menu: [
|
|
33
|
+
{
|
|
34
|
+
// 可递归 menuItem(除 moduleType === sider)
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
// 当moduleType == iframe 时
|
|
39
|
+
iframeConfig: {
|
|
40
|
+
path: '', // iframe 路径
|
|
41
|
+
},
|
|
42
|
+
// 当moduleType == custom 时
|
|
43
|
+
customConfig: {
|
|
44
|
+
path: '', // 自定义路由路径
|
|
45
|
+
},
|
|
46
|
+
// 当moduleType == schema 时
|
|
47
|
+
schemaConfig: {
|
|
48
|
+
api: '', // 数据源API(遵循 RESTFUL 规范)
|
|
49
|
+
schema: {
|
|
50
|
+
// 板块数据结构
|
|
51
|
+
type: 'object',
|
|
52
|
+
properties: {
|
|
53
|
+
key: {
|
|
54
|
+
...schema, // 标准 schema 配置
|
|
55
|
+
type: '', // 字段类型
|
|
56
|
+
label: '', // 字段的中文名
|
|
57
|
+
// 字段在 table 中的相关配置
|
|
58
|
+
tableOption: {
|
|
59
|
+
...elTableColumnConfig, // 标准 el-table-column 配置
|
|
60
|
+
toFixed: 0, // 保留小数点后几位
|
|
61
|
+
visible: true, // 默认为 true(为 false 时,表示不在 table 中显示)
|
|
62
|
+
},
|
|
63
|
+
// 字段在 search-bar 中的相关配置
|
|
64
|
+
searchOption: {
|
|
65
|
+
...eleComponentConfig, // 标准 el-component-column 配置
|
|
66
|
+
comType: '', // 配置组件类型(如 input/select/...)
|
|
67
|
+
default: '', // 默认值
|
|
68
|
+
|
|
69
|
+
// 当 comType === select 时
|
|
70
|
+
enumList: [], // 下拉框可选项 // 当 comType === 'dynamicSelect' 时,会和 API 可选项进行合并
|
|
71
|
+
// 当 comType === 'dynamicSelect'
|
|
72
|
+
api: '',
|
|
73
|
+
},
|
|
74
|
+
// 字段在不同动态 component 中的相关配置,前缀对应 componentConfig 中的键值
|
|
75
|
+
// 如:componentConfig.createForm,这里对应 createFormOption
|
|
76
|
+
// 字段在 createForm 中相关配置
|
|
77
|
+
createFormOption: {
|
|
78
|
+
...elComponentConfig, // 标准 el-component 配置
|
|
79
|
+
comType: '', // 控件型(如 input/select/...)
|
|
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 配置
|
|
90
|
+
comType: '', // 控件型(如 input/select/...)
|
|
91
|
+
visible: true, // 是否展示(true/false),默认为 true
|
|
92
|
+
disabled: false, // 是否禁用(true/false),默认为 false
|
|
93
|
+
default: '', // 默认值
|
|
94
|
+
|
|
95
|
+
// 当 comType === 'select' 时生效
|
|
96
|
+
enumList: [], // 枚举列表
|
|
97
|
+
},
|
|
98
|
+
detailPanelOption: {
|
|
99
|
+
...elComponentConfig, // 标准 el-component 配置
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
required: [], // 必填字段列表
|
|
104
|
+
},
|
|
105
|
+
// table 相关配置
|
|
106
|
+
tableConfig: {
|
|
107
|
+
headerButtons: [
|
|
108
|
+
{
|
|
109
|
+
label: '', // 按钮名称
|
|
110
|
+
eventKey: '', // 按钮事件名称
|
|
111
|
+
// 按钮事件具体配置
|
|
112
|
+
eventOption: {
|
|
113
|
+
// 当 eventKey === 'showComponent'
|
|
114
|
+
comName: '', // 组件名称
|
|
115
|
+
},
|
|
116
|
+
...elButtonConfig, // 标准 el-button 配置
|
|
117
|
+
},
|
|
118
|
+
], // 表头按钮
|
|
119
|
+
rowButtons: [
|
|
120
|
+
{
|
|
121
|
+
label: '', // 按钮名称
|
|
122
|
+
eventKey: '', // 按钮事件名称
|
|
123
|
+
eventOption: {
|
|
124
|
+
// 当 eventKey === 'showComponent'
|
|
125
|
+
comName: '', // 组件名称
|
|
126
|
+
|
|
127
|
+
// 当 eventKey === 'remove'时,可填
|
|
128
|
+
params: {
|
|
129
|
+
// paramsKey 等于 参数的键值
|
|
130
|
+
// rowValueKey 等于 参数值,格式为 schema::tableKey,到 table 中找相应的字段
|
|
131
|
+
paramsKey: rowValueKey,
|
|
132
|
+
},
|
|
133
|
+
}, // 按钮事件具体配置
|
|
134
|
+
...elButtonConfig, // 标准 el-button 配置
|
|
135
|
+
},
|
|
136
|
+
], // 行按钮
|
|
137
|
+
},
|
|
138
|
+
// search-bar 相关配置
|
|
139
|
+
searchConfig: {},
|
|
140
|
+
// 动态组件 相关配置
|
|
141
|
+
componentConfig: {
|
|
142
|
+
// create-form 表单相关配置
|
|
143
|
+
createForm: {
|
|
144
|
+
title: '', // 表单标题
|
|
145
|
+
saveBtnText: '', // 保存按钮文案
|
|
146
|
+
},
|
|
147
|
+
// edit-form 表单相关配置
|
|
148
|
+
editForm: {
|
|
149
|
+
mainKey: '', // 表单主键,用于唯一标识要修改的数据对象
|
|
150
|
+
title: '', // 表单标题
|
|
151
|
+
saveBtnText: '', // 保存按钮文案
|
|
152
|
+
},
|
|
153
|
+
// detail-panel 相关配置
|
|
154
|
+
detailPanel: {
|
|
155
|
+
mainKey: '', // 表单主键,用于唯一标识要修改的数据对象
|
|
156
|
+
title: '', // 详情面板标题
|
|
157
|
+
},
|
|
158
|
+
// ...支持用户动态扩展
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
],
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
### 服务端启动
|
|
169
|
+
```javascript
|
|
170
|
+
const { serverStart } = require('@tushi11/elpis');
|
|
171
|
+
|
|
172
|
+
// 启动 elpis 服务
|
|
173
|
+
const app = serverStart({});
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
### 自定义服务端
|
|
179
|
+
- router-schema
|
|
180
|
+
- router
|
|
181
|
+
- controller
|
|
182
|
+
- service
|
|
183
|
+
- extend
|
|
184
|
+
- config
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
### 前端构建
|
|
189
|
+
```javascript
|
|
190
|
+
const { frontendBuild } = require('@tushi11/elpis');
|
|
191
|
+
|
|
192
|
+
// 编译构建前端工程
|
|
193
|
+
frontendBuild(process.env._ENV);
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
### 自定义页面拓展
|
|
199
|
+
* 在 `app/pages/` 目录下写入口 entry.xxx.js
|
|
200
|
+
|
|
201
|
+
### dashboard / custom-view 自定义页面扩展
|
|
202
|
+
* 在 `app/pages/dashboard/xxx` 下写页面
|
|
203
|
+
|
|
204
|
+
### dashboard / schema-view / components 动态组件扩展
|
|
205
|
+
1. 在 `app/pages/dashboard/complex-view/schema-view/components` 下写组件
|
|
206
|
+
2. 配置到 `app/pages/dashboard/complex-view/schema-view/components/components-config.js`
|
|
207
|
+
|
|
208
|
+
### schema-form 控件扩展
|
|
209
|
+
1. 在 `app/widgets/schema-form/complex-view` 下写控件
|
|
210
|
+
2. 配置到 `app/widgets/schema-form/form-item-config.js`
|
|
211
|
+
|
|
212
|
+
### schema-search-bar 控件扩展
|
|
213
|
+
1. 在 `app/widgets/schema-search-bar/complex-view` 下写控件
|
|
214
|
+
2. 配置到 `app/widgets/schema-search-bar/search-item-config.js`
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
## 规范
|
|
219
|
+
|
|
220
|
+
### 引入组件
|
|
221
|
+
1. 在 JS 中,用 大写开头的驼峰
|
|
222
|
+
```
|
|
223
|
+
import HeaderContainer from '$elpisWidgets/header-container/header-container.vue';
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
2. 在template中,用【小写 + '-'】连接(传参数,命名都用这个规则)
|
|
227
|
+
```
|
|
228
|
+
<header-container :title="'项目列表'"></header-container>
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### 组件传参
|
|
232
|
+
1. 组件传参用【'-'】,不用驼峰。
|
|
233
|
+
```
|
|
234
|
+
<sub-menu :menu-item="item"></sub-menu>
|
|
235
|
+
```
|
|
236
|
+
2. 组件接收参数可以用驼峰
|
|
237
|
+
```
|
|
238
|
+
const { menuItem } = defineProps(['menuItem']);
|
|
239
|
+
```
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module.exports = (app) =>
|
|
2
|
+
class BaseController {
|
|
3
|
+
/**
|
|
4
|
+
* controller 基类
|
|
5
|
+
* 统一收拢 controller 相关的公共方法
|
|
6
|
+
*/
|
|
7
|
+
constructor() {
|
|
8
|
+
this.app = app;
|
|
9
|
+
this.config = app.config;
|
|
10
|
+
this.service = app.service;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* API 处理成功时统一返回结构
|
|
15
|
+
* @param {object} ctx 上下文
|
|
16
|
+
* @param {object} data 核心数据
|
|
17
|
+
* @param {object} metadata 附加数据
|
|
18
|
+
*/
|
|
19
|
+
success(ctx, data = {}, metadata = {}) {
|
|
20
|
+
ctx.status = 200;
|
|
21
|
+
ctx.body = {
|
|
22
|
+
success: true,
|
|
23
|
+
data,
|
|
24
|
+
metadata,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* API 处理失败时统一返回结构
|
|
30
|
+
* @param {object} ctx 上下文
|
|
31
|
+
* @param {object} message 错误信息
|
|
32
|
+
* @param {object} code 错误码
|
|
33
|
+
*/
|
|
34
|
+
fail(ctx, message, code) {
|
|
35
|
+
// ctx.status = 200;
|
|
36
|
+
ctx.body = {
|
|
37
|
+
success: false,
|
|
38
|
+
message,
|
|
39
|
+
code,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
module.exports = (app) => {
|
|
2
|
+
const BaseController = require('./base')(app);
|
|
3
|
+
return class ProjectController extends BaseController {
|
|
4
|
+
/**
|
|
5
|
+
* 根据 proj_key 获取项目配置
|
|
6
|
+
*/
|
|
7
|
+
get(ctx) {
|
|
8
|
+
const { proj_key: projKey } = ctx.request.query;
|
|
9
|
+
|
|
10
|
+
const { project: projectService } = app.service;
|
|
11
|
+
const projConfig = projectService.get(projKey);
|
|
12
|
+
|
|
13
|
+
if (!projConfig) {
|
|
14
|
+
this.fail(ctx, '获取项目异常', 50000);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
this.success(ctx, projConfig);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 获取当前 projectKey 对应模型下的项目列表(如果无 projKey, 全量获取)
|
|
23
|
+
*/
|
|
24
|
+
getList(ctx) {
|
|
25
|
+
const { proj_key: projKey } = ctx.request.query;
|
|
26
|
+
|
|
27
|
+
const { project: projectService } = app.service;
|
|
28
|
+
const projectList = projectService.getList({ projKey });
|
|
29
|
+
|
|
30
|
+
// 构造关键数据 list
|
|
31
|
+
const dtoProjectList = projectList.map((item) => {
|
|
32
|
+
const { modelKey, key, name, desc, homePage } = item;
|
|
33
|
+
return { modelKey, key, name, desc, homePage };
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
this.success(ctx, dtoProjectList);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 获取所有模型与项目的结构化数据
|
|
41
|
+
*/
|
|
42
|
+
async getModelList(ctx) {
|
|
43
|
+
const { project: projectService } = app.service;
|
|
44
|
+
const modelList = await projectService.getModelList();
|
|
45
|
+
|
|
46
|
+
// 构造返回结果,只返回关键数据
|
|
47
|
+
const dtoModelList = modelList.reduce((preList, item) => {
|
|
48
|
+
const { model, project } = item;
|
|
49
|
+
|
|
50
|
+
// 构造 model 关键数据
|
|
51
|
+
const { key, name, desc } = model;
|
|
52
|
+
const dtoModel = { key, name, desc };
|
|
53
|
+
|
|
54
|
+
// 构造 project 关键数据
|
|
55
|
+
const dtoProject = Object.keys(project).reduce((preObj, projKey) => {
|
|
56
|
+
const { key, name, desc, homePage } = project[projKey];
|
|
57
|
+
preObj[projKey] = { key, name, desc, homePage };
|
|
58
|
+
return preObj;
|
|
59
|
+
}, {});
|
|
60
|
+
// 另一种写法
|
|
61
|
+
// const dtoProject = {};
|
|
62
|
+
// for (const projKey in project) {
|
|
63
|
+
// const { key, name, desc, homePage } = project[projKey];
|
|
64
|
+
// dtoProject[projKey] = { key, name, desc, homePage };
|
|
65
|
+
// }
|
|
66
|
+
|
|
67
|
+
// 整合返回结构
|
|
68
|
+
preList.push({
|
|
69
|
+
model: dtoModel,
|
|
70
|
+
project: dtoProject,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return preList;
|
|
74
|
+
}, []);
|
|
75
|
+
this.success(ctx, dtoModelList);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module.exports = (app) => {
|
|
2
|
+
return class ViewController {
|
|
3
|
+
/**
|
|
4
|
+
* 渲染页面
|
|
5
|
+
* @param {object} ctx 上下文
|
|
6
|
+
*/
|
|
7
|
+
async renderPage(ctx) {
|
|
8
|
+
const { query, params } = ctx;
|
|
9
|
+
app.logger.info(`[ViewController] query: ${JSON.stringify(query)}`);
|
|
10
|
+
app.logger.info(`[ViewController] params: ${JSON.stringify(params)}`);
|
|
11
|
+
await ctx.render(`dist/entry.${ctx.params.page}`, {
|
|
12
|
+
projKey: ctx.query?.proj_key,
|
|
13
|
+
name: app.options?.name,
|
|
14
|
+
env: app.env.get(),
|
|
15
|
+
options: JSON.stringify(app.options),
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const log4js = require('log4js');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 日志工具
|
|
5
|
+
* 外部调用 app.logger.info app.logger.error
|
|
6
|
+
*/
|
|
7
|
+
module.exports = (app) => {
|
|
8
|
+
let logger;
|
|
9
|
+
if (app.env.isLocal()) {
|
|
10
|
+
// 打印在控制台即可
|
|
11
|
+
logger = console;
|
|
12
|
+
} else {
|
|
13
|
+
// 把日志输出并落地到磁盘(日志落盘)
|
|
14
|
+
log4js.configure({
|
|
15
|
+
appenders: {
|
|
16
|
+
console: {
|
|
17
|
+
type: 'console',
|
|
18
|
+
},
|
|
19
|
+
// 日志文件切分
|
|
20
|
+
dateFile: {
|
|
21
|
+
type: 'dateFile',
|
|
22
|
+
filename: './logs/application.log',
|
|
23
|
+
pattern: '.yyyy-MM-dd',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
categories: {
|
|
27
|
+
default: {
|
|
28
|
+
appenders: ['console', 'dateFile'],
|
|
29
|
+
level: 'trace',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
logger = log4js.getLogger();
|
|
34
|
+
}
|
|
35
|
+
return logger;
|
|
36
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
+
return async (ctx, next) => {
|
|
10
|
+
// 只对 Api 做参数校验
|
|
11
|
+
if (ctx.path.indexOf('/api/') < 0) {
|
|
12
|
+
return await next();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// 获取请求参数
|
|
16
|
+
const { body, query, headers } = ctx.request;
|
|
17
|
+
const { params, path, method } = ctx;
|
|
18
|
+
|
|
19
|
+
app.logger.info(`[${method} ${path}] body: ${JSON.stringify(body)}`);
|
|
20
|
+
app.logger.info(`[${method} ${path}] query: ${JSON.stringify(query)}`);
|
|
21
|
+
app.logger.info(`[${method} ${path}] params: ${JSON.stringify(params)}`);
|
|
22
|
+
app.logger.info(`[${method} ${path}] headers: ${JSON.stringify(headers)}`);
|
|
23
|
+
|
|
24
|
+
const schema = app.routerSchema[path]?.[method.toLowerCase()];
|
|
25
|
+
|
|
26
|
+
if (!schema) {
|
|
27
|
+
return await next();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let valid = true;
|
|
31
|
+
|
|
32
|
+
// ajv 校验器
|
|
33
|
+
let validate;
|
|
34
|
+
|
|
35
|
+
// 校验 headers
|
|
36
|
+
if (valid && headers && schema.headers) {
|
|
37
|
+
schema.headers.$schema = $schema;
|
|
38
|
+
validate = ajv.compile(schema.headers);
|
|
39
|
+
valid = validate(headers);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 校验 body
|
|
43
|
+
if (valid && body && schema.body) {
|
|
44
|
+
schema.body.$schema = $schema;
|
|
45
|
+
validate = ajv.compile(schema.body);
|
|
46
|
+
valid = validate(body);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 校验 query
|
|
50
|
+
if (valid && query && schema.query) {
|
|
51
|
+
schema.query.$schema = $schema;
|
|
52
|
+
validate = ajv.compile(schema.query);
|
|
53
|
+
valid = validate(query);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 校验 params
|
|
57
|
+
if (valid && params && schema.params) {
|
|
58
|
+
schema.params.$schema = $schema;
|
|
59
|
+
validate = ajv.compile(schema.params);
|
|
60
|
+
valid = validate(params);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!valid) {
|
|
64
|
+
ctx.status = 200;
|
|
65
|
+
ctx.body = {
|
|
66
|
+
success: false,
|
|
67
|
+
message: `request validate fail: ${ajv.errorsText(validate.errors)}`,
|
|
68
|
+
code: 442,
|
|
69
|
+
};
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
await next();
|
|
74
|
+
};
|
|
75
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const md5 = require('md5');
|
|
2
|
+
/**
|
|
3
|
+
* API 签名合法性校验
|
|
4
|
+
* @param {*} app
|
|
5
|
+
*/
|
|
6
|
+
module.exports = (app) => {
|
|
7
|
+
return async (ctx, next) => {
|
|
8
|
+
// 只对 Api 请求做签名校验
|
|
9
|
+
if (ctx.path.indexOf('/api') < 0) {
|
|
10
|
+
return await next();
|
|
11
|
+
}
|
|
12
|
+
const { path, method } = ctx;
|
|
13
|
+
const { headers } = ctx.request;
|
|
14
|
+
const { s_sign: sSign, s_t: st } = headers;
|
|
15
|
+
|
|
16
|
+
const signKey = 'apisignverifykey001';
|
|
17
|
+
const signature = md5(`${signKey}_${st}`);
|
|
18
|
+
app.logger.info(`[${method} ${path}] signature: ${signature}`);
|
|
19
|
+
|
|
20
|
+
if (
|
|
21
|
+
!sSign ||
|
|
22
|
+
!st ||
|
|
23
|
+
signature !== sSign.toLowerCase() ||
|
|
24
|
+
Date.now() - st > 600 * 1000
|
|
25
|
+
) {
|
|
26
|
+
ctx.status = 200;
|
|
27
|
+
ctx.body = {
|
|
28
|
+
success: false,
|
|
29
|
+
message: 'signature not correct or api timeout!',
|
|
30
|
+
code: 445,
|
|
31
|
+
};
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
await next();
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 运行时异常错误处理,兜底所有异常
|
|
3
|
+
* @param {[object]} app koa 实例
|
|
4
|
+
*/
|
|
5
|
+
module.exports = (app) => {
|
|
6
|
+
return async (ctx, next) => {
|
|
7
|
+
try {
|
|
8
|
+
await next();
|
|
9
|
+
} catch (err) {
|
|
10
|
+
// 异常处理
|
|
11
|
+
const { status, message, detail } = err;
|
|
12
|
+
app.logger.info(JSON.stringify(err));
|
|
13
|
+
app.logger.error('[-- exception --]:', err);
|
|
14
|
+
app.logger.error('[-- exception --]:', status, message, detail);
|
|
15
|
+
|
|
16
|
+
if (message && message.indexOf('template not found') > -1) {
|
|
17
|
+
// 页面重定向
|
|
18
|
+
ctx.status = 302; // 临时重定向
|
|
19
|
+
ctx.redirect(`${app.options?.homePage}`);
|
|
20
|
+
return; // 这里少了return不会重定向的,会输出下面的内容
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const resBody = {
|
|
24
|
+
success: false,
|
|
25
|
+
code: 50000,
|
|
26
|
+
message: '网络异常 请稍后重试',
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
ctx.status = 200;
|
|
30
|
+
ctx.body = resBody;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* projectHandler 相关项目处理内容
|
|
3
|
+
* @param {*} app
|
|
4
|
+
*/
|
|
5
|
+
module.exports = (app) => {
|
|
6
|
+
return async (ctx, next) => {
|
|
7
|
+
// 只对 业务 API 进行 proj_key 处理
|
|
8
|
+
if (ctx.path.indexOf('/api/proj/') < 0) {
|
|
9
|
+
return await next();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// 获取 projKey
|
|
13
|
+
const { proj_key: projKey } = ctx.request.headers;
|
|
14
|
+
if (!projKey) {
|
|
15
|
+
ctx.status = 200;
|
|
16
|
+
ctx.body = {
|
|
17
|
+
success: false,
|
|
18
|
+
message: 'proj_key not found',
|
|
19
|
+
code: 446,
|
|
20
|
+
};
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
ctx.projKey = projKey;
|
|
24
|
+
|
|
25
|
+
await next();
|
|
26
|
+
};
|
|
27
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
module.exports = (app) => {
|
|
4
|
+
// 配置静态根目录
|
|
5
|
+
const koaStatic = require('koa-static');
|
|
6
|
+
app.use(koaStatic(path.resolve(process.cwd(), './app/public')));
|
|
7
|
+
|
|
8
|
+
// 模板渲染引擎
|
|
9
|
+
const koaNunjucks = require('koa-nunjucks-2');
|
|
10
|
+
app.use(
|
|
11
|
+
koaNunjucks({
|
|
12
|
+
ext: 'tpl',
|
|
13
|
+
path: path.resolve(process.cwd(), './app/public'),
|
|
14
|
+
nunjucksConfig: {
|
|
15
|
+
noCache: true,
|
|
16
|
+
trimBlocks: true,
|
|
17
|
+
},
|
|
18
|
+
})
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
// 引入 ctx.body 解析中间件
|
|
22
|
+
const bodyParser = require('koa-bodyparser');
|
|
23
|
+
app.use(
|
|
24
|
+
bodyParser({
|
|
25
|
+
formList: '1000mb',
|
|
26
|
+
enableTypes: ['form', 'json', 'text'],
|
|
27
|
+
})
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// 引入异常捕获中间件
|
|
31
|
+
app.use(app.middlewares.errorHandler);
|
|
32
|
+
|
|
33
|
+
// 引入签名合法性校验
|
|
34
|
+
app.use(app.middlewares.apiSignVerify);
|
|
35
|
+
|
|
36
|
+
// 引入 API 参数校验
|
|
37
|
+
app.use(app.middlewares.apiParamsVerify);
|
|
38
|
+
|
|
39
|
+
// 引入 项目处理中间件
|
|
40
|
+
app.use(app.middlewares.projectHandler);
|
|
41
|
+
};
|