@mangjuxiong/elpis 1.0.2

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