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