@shhhwm/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.
Files changed (78) hide show
  1. package/.eslintignore +3 -0
  2. package/.eslintrc +55 -0
  3. package/README.md +214 -0
  4. package/app/controller/base.js +43 -0
  5. package/app/controller/project.js +72 -0
  6. package/app/controller/view.js +22 -0
  7. package/app/extend/logger.js +43 -0
  8. package/app/middleware/api-params-verify.js +90 -0
  9. package/app/middleware/api-sign-verify.js +33 -0
  10. package/app/middleware/error-handler.js +34 -0
  11. package/app/middleware/project-handler.js +29 -0
  12. package/app/middleware.js +38 -0
  13. package/app/pages/asserts/custom.css +12 -0
  14. package/app/pages/boot.js +50 -0
  15. package/app/pages/common/curl.js +88 -0
  16. package/app/pages/common/utils.js +2 -0
  17. package/app/pages/dashboard/complex-view/header-view/complex-view/sub-menu/sub-menu.vue +20 -0
  18. package/app/pages/dashboard/complex-view/header-view/header-view.vue +127 -0
  19. package/app/pages/dashboard/complex-view/iframe-view/iframe-view.vue +44 -0
  20. package/app/pages/dashboard/complex-view/schema-view/complex-view/search-panel/search-panel.vue +41 -0
  21. package/app/pages/dashboard/complex-view/schema-view/complex-view/table-panel/table-panel.vue +137 -0
  22. package/app/pages/dashboard/complex-view/schema-view/components/component-config.js +23 -0
  23. package/app/pages/dashboard/complex-view/schema-view/components/create-form/create-form.vue +102 -0
  24. package/app/pages/dashboard/complex-view/schema-view/components/detail-panel/detail-panel.vue +110 -0
  25. package/app/pages/dashboard/complex-view/schema-view/components/edit-form/edit-form.vue +130 -0
  26. package/app/pages/dashboard/complex-view/schema-view/hook/schema.js +129 -0
  27. package/app/pages/dashboard/complex-view/schema-view/schema-view.vue +100 -0
  28. package/app/pages/dashboard/complex-view/sider-view/complex-view/sub-menu/sub-menu.vue +25 -0
  29. package/app/pages/dashboard/complex-view/sider-view/sider-view.vue +124 -0
  30. package/app/pages/dashboard/dashboard.vue +98 -0
  31. package/app/pages/dashboard/entry.dashboard.js +46 -0
  32. package/app/pages/store/index.js +3 -0
  33. package/app/pages/store/menu.js +73 -0
  34. package/app/pages/store/project.js +17 -0
  35. package/app/pages/widgets/header-container/asserts/avatar.png +0 -0
  36. package/app/pages/widgets/header-container/asserts/logo.png +0 -0
  37. package/app/pages/widgets/header-container/header-container.vue +110 -0
  38. package/app/pages/widgets/schema-form/complex-view/input/input.vue +137 -0
  39. package/app/pages/widgets/schema-form/complex-view/input-number/input-number.vue +137 -0
  40. package/app/pages/widgets/schema-form/complex-view/select/select.vue +115 -0
  41. package/app/pages/widgets/schema-form/form-item-config.js +23 -0
  42. package/app/pages/widgets/schema-form/schema-form.vue +132 -0
  43. package/app/pages/widgets/schema-search-bar/complex-view/date-range/date-range.vue +47 -0
  44. package/app/pages/widgets/schema-search-bar/complex-view/dynamic-select/dynamic-select.vue +64 -0
  45. package/app/pages/widgets/schema-search-bar/complex-view/input/input.vue +41 -0
  46. package/app/pages/widgets/schema-search-bar/complex-view/select/select.vue +48 -0
  47. package/app/pages/widgets/schema-search-bar/schema-search-bar.vue +131 -0
  48. package/app/pages/widgets/schema-search-bar/search-item-config.js +27 -0
  49. package/app/pages/widgets/schema-table/schema-table.vue +245 -0
  50. package/app/pages/widgets/sider-container/sider-container.vue +30 -0
  51. package/app/public/static/logo.png +0 -0
  52. package/app/public/static/normalize.css +239 -0
  53. package/app/router/project.js +18 -0
  54. package/app/router/view.js +8 -0
  55. package/app/router-schema/project.js +30 -0
  56. package/app/service/base.js +13 -0
  57. package/app/service/project.js +47 -0
  58. package/app/view/entry.tpl +29 -0
  59. package/app/webpack/config/webpack.base.js +266 -0
  60. package/app/webpack/config/webpack.dev.js +62 -0
  61. package/app/webpack/config/webpack.prod.js +112 -0
  62. package/app/webpack/dev.js +54 -0
  63. package/app/webpack/libs/blank.js +1 -0
  64. package/app/webpack/prod.js +23 -0
  65. package/config/config.default.js +3 -0
  66. package/elpis-core/env.js +23 -0
  67. package/elpis-core/index.js +98 -0
  68. package/elpis-core/loader/config.js +83 -0
  69. package/elpis-core/loader/controller.js +75 -0
  70. package/elpis-core/loader/extend.js +63 -0
  71. package/elpis-core/loader/middleware.js +73 -0
  72. package/elpis-core/loader/router-schema.js +54 -0
  73. package/elpis-core/loader/router.js +42 -0
  74. package/elpis-core/loader/service.js +75 -0
  75. package/index.js +38 -0
  76. package/model/index.js +110 -0
  77. package/package.json +104 -0
  78. package/test/controller/project.test.js +216 -0
package/.eslintignore ADDED
@@ -0,0 +1,3 @@
1
+ node_modules/
2
+ public/
3
+ docs/
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,214 @@
1
+ # elpis
2
+ ## 一个企业级全栈应用框架
3
+
4
+ ### model 配置
5
+
6
+ ```Javascript
7
+ {
8
+ mode: "dashboard", // 模板类型,不同模板类型对应不一样的模板数据结构
9
+ name: '', // 名称 (模型名称 || 项目名称)
10
+ desc: '', // 描述
11
+ icon: '', // icon
12
+ homePage: '', // 首页(项目配置)
13
+
14
+ // 头部菜单
15
+ menu: [
16
+ {
17
+ key: "", // 菜单唯一描述
18
+ name: "", // 菜单名称
19
+ menuType: "", // 菜单类型 枚举值- group / module
20
+
21
+ // 当 menuType == group 时, 可填
22
+ subMenu: [{ /* 可递归 menuItem */ }, ...],
23
+
24
+ // 当 menuType == module 时, 可填
25
+ moduleType: '', // 枚举值: sider/iframe/custom/schema
26
+
27
+ // 当 moduleType == sider 时
28
+ siderConfig: {
29
+ menu: [{ /* 可递归 menuItem(除 moduleType === sider) */ }, ...]
30
+ },
31
+
32
+ // 当 moduleType == iframe 时
33
+ iframeConfig: {
34
+ path: '', // iframe 路径
35
+ },
36
+
37
+ // 当 moduleType == custom 时
38
+ customConfig: {
39
+ path: '', // 自定义路由路径
40
+ },
41
+
42
+ // 当 moduleType == schema 时
43
+ schemaConfig: {
44
+ api: '', // 数据源API(遵循 RESTFUL 规范)
45
+ schema: { // 板块数据结构
46
+ type: 'object',
47
+ properties: {
48
+ key: {
49
+ ...schema, // 标准 schema 配置
50
+ type: '', // 字段类型
51
+ label: '', // 字段的中文名
52
+
53
+ // 字段在 table 中的相关配置
54
+ tableOption: {
55
+ ...elTableColumnConfig, // 标准 el-table-column 配置
56
+ toFixed: 0, // 保留小数点后几位
57
+ visible: false, // 默认为 true (false 表示不在表单中显示, true 或 不配置时都显示)
58
+ },
59
+
60
+ // 字段在 search-bar 中的相关配置
61
+ searchOption: {
62
+ ...elComponentConfig, // 标准 el-component 配置
63
+ comType: '', // 配置组件类型 input/select/...
64
+ default: '', // 默认值
65
+
66
+ // comType 为 select 时
67
+ enumList: [], // 下拉框可选项
68
+
69
+ // comType 为 dynamicSelect 时
70
+ api: '',
71
+ },
72
+
73
+ // 字段在 createForm 中的相关配置
74
+ createFormOption: {
75
+ ...elComponentConfig, // 标准 el-component 配置
76
+ comType: '', // 控件类型 input/select/input-number/...
77
+ visible: true, // 是否展示(true,false),默认为true
78
+ disabled: false, // 是否禁用 (true,false),默认为false
79
+ default: '', // 默认值
80
+
81
+ // comType 为 select 时
82
+ enumList: [], // 枚举列表
83
+ },
84
+
85
+ // 字段在 editForm 表单中的相关配置
86
+ editFormOption: {
87
+ ...elComponentConfig, // 标准 el-component 配置
88
+ comType: '', // 控件类型 input/select/input-number/...
89
+ visible: true, // 是否展示(true,false),默认为true
90
+ disabled: false, // 是否禁用 (true,false),默认为false
91
+ default: '', // 默认值
92
+
93
+ // comType 为 select 时
94
+ enumList: [], // 枚举列表
95
+ },
96
+
97
+ // 字段在 detailPanel 中的相关配置
98
+ detailPanelOption: {
99
+ ...elComponentConfig, // 标准 el-component 配置
100
+ }
101
+ },
102
+ ...
103
+ },
104
+ required: [], // 标记哪些字段是必填项
105
+ },
106
+
107
+ // table 相关配置
108
+ tableConfig: {
109
+ headerButtons: [{
110
+ label: '', // 按钮中文名
111
+ eventKey: '', // 按钮事件名
112
+ eventOption: {
113
+ // 当 eventKey === 'showComponent'
114
+ comName: '', // 组件名称
115
+ },
116
+ ...elButtonConfig, // 标准 el-button 配置
117
+ }, ...],
118
+ rowButtons: [{
119
+ label: '', // 按钮中文名
120
+ eventKey: '', // 按钮事件名
121
+ eventOption: {
122
+ // 当 eventKey === 'showComponent'
123
+ comName: '', // 组件名称
124
+
125
+ // 当 eventKey === 'remove'
126
+ params: {
127
+ // paramKey = 参数的键值
128
+ // rowValueKey = 参数值,格式为 schema::tableKey 的时候, 到 table 中找相应的字段
129
+ paramKey: rowValueKey
130
+ }
131
+ },
132
+ ...elButtonConfig, // 标准 el-button 配置
133
+ }, ...]
134
+ },
135
+
136
+ // search-bar 相关配置
137
+ searchConfig: {},
138
+
139
+ // 动态组件 相关配置
140
+ componentConfig: {
141
+ // create-form 表单相关配置
142
+ createForm: {
143
+ title: '', // 表单标题
144
+ saveBtnText: '', // 保存按钮文案
145
+ },
146
+ // edit-form 表单相关配置
147
+ editForm: {
148
+ mainKey: '', // 表单主键,用于唯一标识要修改的数据对象
149
+ title: '', // 表单标题
150
+ saveBtnText: '', // 保存按钮文案
151
+ },
152
+ // detail-panel 相关配置
153
+ detailPanel: {
154
+ mainKey: '', // 表单主键,用于唯一标识要修改的数据对象
155
+ title: '', // 表单标题
156
+ }
157
+ // ...支持用户动态拓展
158
+ },
159
+ },
160
+ }, ...]
161
+ }
162
+ ```
163
+
164
+
165
+ ### 服务端启动
166
+ ```Javascript
167
+ const {
168
+ serverStart
169
+ } = require("@shhhwm/elpis");
170
+
171
+ // 启动 elpis 服务
172
+ const app = serverStart({});
173
+ ```
174
+
175
+
176
+ ### 自定义服务端
177
+ - router-schema
178
+ - router
179
+ - contraller
180
+ - service
181
+ - extend
182
+ - config
183
+
184
+
185
+ ### 前端构建
186
+ ```Javascript
187
+ const { frontendBuild } = require("@shhhwm/elpis");
188
+
189
+ // 编译构建前端工程
190
+ frontendBuild(process.env._ENV);
191
+ ```
192
+
193
+
194
+ ### 自定义页面扩展
195
+ * 在 `app/pages/` 目录下写入口 entry.xxx.js
196
+
197
+ ### 自定义 view
198
+ 1. 在 `app/pages/dashboard` 下写页面
199
+ 2. 配置在 `app/pages/dashboard/router.js`
200
+
201
+ ### dashboard / custom-view 自定义页面扩展
202
+ * 在 `app/pages/dashboard/xxxx` 下写页面
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/component-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`
@@ -0,0 +1,43 @@
1
+ module.exports = (app) =>
2
+ class BaseController {
3
+ /**
4
+ * controller 基类
5
+ * 统一收拢 controller 相关的公共方法
6
+ * @param {*} app
7
+ * @returns
8
+ */
9
+ constructor() {
10
+ this.app = app;
11
+ this.config = app.config;
12
+ this.service = app.service;
13
+ }
14
+
15
+ /**
16
+ * API 处理成功时统一返回结构
17
+ * @param {object} ctx 上下文
18
+ * @param {object} data 核心数据
19
+ * @param {object} metadata 附加数据
20
+ */
21
+ success(ctx, data = {}, metadata = {}) {
22
+ ctx.status = 200;
23
+ ctx.body = {
24
+ success: true,
25
+ data,
26
+ metadata,
27
+ };
28
+ }
29
+
30
+ /**
31
+ * API 处理失败时统一返回结构
32
+ * @param {object} ctx 上下文
33
+ * @param {object} message 错误信息
34
+ * @param {object} code 错误码
35
+ */
36
+ fail(ctx, message, code) {
37
+ ctx.body = {
38
+ success: false,
39
+ message,
40
+ code,
41
+ };
42
+ }
43
+ };
@@ -0,0 +1,72 @@
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 } = this.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
+ * 获取当前 projKey, 对应模型下的项目列表 (如果无 projKey, 全量获取)
22
+ */
23
+ getList(ctx) {
24
+ const { proj_key: projKey } = ctx.request.query;
25
+
26
+ const { project: projectService } = this.service;
27
+ const projectList = projectService.getList({ projKey });
28
+
29
+ // 构造关键数据 list
30
+ const dtoProjectList = projectList.map((item) => {
31
+ const { modelKey, key, name, desc, homePage } = item;
32
+ return { modelKey, key, name, desc, homePage };
33
+ });
34
+
35
+ this.success(ctx, dtoProjectList);
36
+ }
37
+
38
+ /**
39
+ * 获取所有模型与项目的结构化数据
40
+ */
41
+ async getModelList(ctx) {
42
+ const { project: projectService } = this.service;
43
+ const modeList = await projectService.getModelList();
44
+
45
+ // 构造返回结果,只返回关键数据
46
+ const dtoModeList = modeList.reduce((preList, item) => {
47
+ const { model, project } = item;
48
+
49
+ // 构造 model 关键数据
50
+ const { key, name, desc } = model;
51
+ const dtoModel = { key, name, desc };
52
+
53
+ // 构造 project 关键数据
54
+ const dtoProject = Object.keys(project).reduce((preObj, projKey) => {
55
+ const { key, name, desc, homePage } = project[projKey];
56
+ preObj[projKey] = { key, name, desc, homePage };
57
+ return preObj;
58
+ }, {});
59
+
60
+ // 整合返回结构
61
+ preList.push({
62
+ model: dtoModel,
63
+ project: dtoProject,
64
+ });
65
+
66
+ return preList;
67
+ }, []);
68
+
69
+ this.success(ctx, dtoModeList);
70
+ }
71
+ };
72
+ };
@@ -0,0 +1,22 @@
1
+ module.exports = (app) => {
2
+ return class ViewController {
3
+ /**
4
+ * 渲染页面
5
+ * @param {object} ctx 上下文
6
+ */
7
+ async renderPage(ctx) {
8
+ // 获取请求参数
9
+ const { query } = ctx.request;
10
+ const { params } = ctx;
11
+
12
+ app.logger.info(`[ViewController] query: ${JSON.stringify(query)}`);
13
+ app.logger.info(`[ViewController] params: ${JSON.stringify(params)}`);
14
+ await ctx.render(`dist/entry.${ctx.params.page}`, {
15
+ projKey: ctx.query?.proj_key,
16
+ name: app.options?.name,
17
+ env: app.env.get(),
18
+ options: JSON.stringify(app.options),
19
+ });
20
+ }
21
+ };
22
+ };
@@ -0,0 +1,43 @@
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
+ // 定义日志输出源(“附加器”)
18
+ console: {
19
+ type: "console", // 输出到控制台
20
+ },
21
+ // 日志文件切分
22
+ dateFile: {
23
+ // 按日期切分的文件输出源
24
+ type: "dateFile", // 类型:按日期滚动的文件
25
+ filename: "./logs/application.log", // 日志文件基础路径+名称
26
+ pattern: ".yyyy-MM-dd", // 文件名后缀(按“年-月-日”切分)
27
+ },
28
+ },
29
+ categories: {
30
+ // 定义日志分类(“类别”),关联输出源
31
+ default: {
32
+ // 默认分类(调用 log4js.getLogger() 时默认使用)
33
+ appenders: ["console", "dateFile"], // 该分类的日志会输出到 console 和 dateFile
34
+ level: "trace", // 日志级别(log4js 日志级别从低到高为:trace < debug < info < warn < error < fatal)。设置为 trace 表示 “输出所有级别日志”,实际生产环境可根据需求调整(如 info,过滤 trace/debug 调试日志)
35
+ },
36
+ },
37
+ });
38
+
39
+ logger = log4js.getLogger();
40
+ }
41
+
42
+ return logger;
43
+ };
@@ -0,0 +1,90 @@
1
+ const { match } = require("path-to-regexp");
2
+ const Ajv = require("ajv");
3
+ const ajv = new Ajv();
4
+
5
+ /**
6
+ * API 参数校验
7
+ * @param {*} app
8
+ * @returns
9
+ */
10
+ module.exports = (app) => {
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
+ // 获取请求参数
20
+ const { body, query, headers } = ctx.request;
21
+ const { params, path, method } = ctx;
22
+
23
+ app.logger.info(`[${method} ${path}] body: ${JSON.stringify(body)}`);
24
+ app.logger.info(`[${method} ${path}] query: ${JSON.stringify(query)}`);
25
+ app.logger.info(`[${method} ${path}] params: ${JSON.stringify(params)}`);
26
+ app.logger.info(`[${method} ${path}] headers: ${JSON.stringify(headers)}`);
27
+
28
+ let schema = app.routerSchema[path]?.[method.toLowerCase()];
29
+
30
+ // 动态匹配路径
31
+ if (!schema) {
32
+ for (const routeKey of Object.keys(app.routerSchema)) {
33
+ const m = match(routeKey, { decode: decodeURIComponent });
34
+ if (m(path)) {
35
+ schema = app.routerSchema[routeKey]?.[method.toLowerCase()];
36
+ break;
37
+ }
38
+ }
39
+ }
40
+
41
+ if (!schema) {
42
+ return await next();
43
+ }
44
+
45
+ let valid = true;
46
+
47
+ // ajv 检验器
48
+ let validate;
49
+
50
+ // 校验 headers
51
+ if (valid && headers && schema.headers) {
52
+ schema.headers.$schema = $schema; // 加上 JSON Schema 的版本
53
+ validate = ajv.compile(schema.headers); // ajv 解析 出标准格式
54
+ valid = validate(headers); // 检验
55
+ }
56
+
57
+ // 校验 body
58
+ if (valid && body && schema.body) {
59
+ schema.body.$schema = $schema; // 加上 JSON Schema 的版本
60
+ validate = ajv.compile(schema.body); // ajv 解析 出标准格式
61
+ valid = validate(body); // 检验
62
+ }
63
+
64
+ // 校验 query
65
+ if (valid && query && schema.query) {
66
+ schema.query.$schema = $schema; // 加上 JSON Schema 的版本
67
+ validate = ajv.compile(schema.query); // ajv 解析 出标准格式
68
+ valid = validate(query); // 检验
69
+ }
70
+
71
+ // 校验 params
72
+ if (valid && params && schema.params) {
73
+ schema.params.$schema = $schema; // 加上 JSON Schema 的版本
74
+ validate = ajv.compile(schema.params); // ajv 解析 出标准格式
75
+ valid = validate(params); // 检验
76
+ }
77
+
78
+ if (!valid) {
79
+ ctx.status = 200;
80
+ ctx.body = {
81
+ success: false,
82
+ message: `request validate fail: ${ajv.errorsText(validate.errors)}`,
83
+ code: 442,
84
+ };
85
+ return;
86
+ }
87
+
88
+ await next();
89
+ };
90
+ };
@@ -0,0 +1,33 @@
1
+ const md5 = require("md5");
2
+
3
+ /**
4
+ * API 签名合法性校验
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
+
13
+ const { path, method } = ctx;
14
+ const { headers } = ctx.request;
15
+ const { s_sign: sSign, s_t: st } = headers;
16
+
17
+ const signKey = "95da0407-a7e2-2852-715e-2bbf48d89e99"; // 前后端都有这个key 这是一种对称加密
18
+ const signature = md5(`${signKey}_${st}`);
19
+ app.logger.info(`[${method} ${path}] signature: ${signature}`);
20
+
21
+ if (!sSign || !st || signature !== sSign.toLowerCase() || Date.now() - st > 600000) {
22
+ ctx.status = 200;
23
+ ctx.body = {
24
+ success: false,
25
+ message: 'signature not correct or api timeout!!',
26
+ code: 445
27
+ }
28
+ return;
29
+ }
30
+
31
+ await next();
32
+ };
33
+ };
@@ -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("[-- exception --]", 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
+ succes: false,
26
+ code: 50000,
27
+ message: "网络异常 请稍后重试",
28
+ };
29
+
30
+ ctx.status = 200;
31
+ ctx.body = resBody;
32
+ }
33
+ };
34
+ };
@@ -0,0 +1,29 @@
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
+
14
+ if (!projKey) {
15
+ ctx.status = 200;
16
+ ctx.body = {
17
+ success: false,
18
+ message: "proj_key not found",
19
+ code: 446,
20
+ };
21
+
22
+ return;
23
+ }
24
+
25
+ ctx.projKey = projKey;
26
+
27
+ await next();
28
+ };
29
+ };
@@ -0,0 +1,38 @@
1
+ const path = require("path");
2
+
3
+ module.exports = (app) => {
4
+ // 引入异常捕获中间件
5
+ app.use(app.middlewares.errorHandler);
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(
14
+ koaNunjucks({
15
+ ext: "tpl",
16
+ path: path.resolve(process.cwd(), "./app/public"),
17
+ nunjucksConfig: {
18
+ noCache: true, // 默认值:(false)每次都不要使用缓存并重新编译模板(服务器端)
19
+ trimBlocks: true, //(默认值:false)自动从块/标记中删除尾随换行符
20
+ },
21
+ })
22
+ );
23
+
24
+ // 引入 ctx.body 解析中间件
25
+ const bodyParser = require("koa-bodyparser");
26
+ app.use(
27
+ bodyParser({
28
+ formLimit: "1000mb", // 正文的限制 默认值为56kb
29
+ enableTypes: ["form", "json", "text"], //解析器只有当请​​求类型命中 enableTypes 时才会解析, 默认为['json', 'form']
30
+ })
31
+ );
32
+
33
+ // 引入签名合法性校验
34
+ app.use(app.middlewares.apiSignVerify);
35
+
36
+ // 引入 项目处理中间件
37
+ app.use(app.middlewares.projectHandler);
38
+ };