mm_os 4.3.7 → 5.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 (232) hide show
  1. package/README.md +101 -504
  2. package/index.js +26 -19
  3. package/lib/loader.js +787 -0
  4. package/lib/os.js +369 -0
  5. package/package.json +55 -84
  6. package/LICENSE +0 -21
  7. package/README_EN.md +0 -505
  8. package/adapter/adapter.js +0 -484
  9. package/adapter/custom_persistence.js +0 -661
  10. package/adapter/mqtt.js +0 -254
  11. package/adapter/socket.js +0 -244
  12. package/adapter/web.js +0 -92
  13. package/adapter/websocket.js +0 -43
  14. package/ai.js +0 -130
  15. package/com/api/README.md +0 -3
  16. package/com/api/com.json +0 -5
  17. package/com/api/config.tpl.json +0 -41
  18. package/com/api/drive.js +0 -732
  19. package/com/api/index.js +0 -201
  20. package/com/api/oauth.js +0 -200
  21. package/com/api/script.tpl.js +0 -32
  22. package/com/cmd/README.md +0 -11
  23. package/com/cmd/com.json +0 -5
  24. package/com/cmd/config.tpl.json +0 -48
  25. package/com/cmd/drive.js +0 -176
  26. package/com/cmd/index.js +0 -287
  27. package/com/cmd/job/config.json +0 -66
  28. package/com/cmd/job/index.js +0 -241
  29. package/com/cmd/job/test.js +0 -188
  30. package/com/cmd/msg.json +0 -48
  31. package/com/cmd/script.tpl.js +0 -12
  32. package/com/db/README.md +0 -3
  33. package/com/db/com.json +0 -5
  34. package/com/db/drive.js +0 -2206
  35. package/com/db/index.js +0 -241
  36. package/com/event/README.md +0 -4
  37. package/com/event/com.json +0 -5
  38. package/com/event/config.tpl.json +0 -20
  39. package/com/event/drive.js +0 -61
  40. package/com/event/index.js +0 -438
  41. package/com/event/script.tpl.js +0 -23
  42. package/com/mqtt/README.md +0 -47
  43. package/com/mqtt/com.json +0 -5
  44. package/com/mqtt/config.tpl.json +0 -42
  45. package/com/mqtt/drive.js +0 -704
  46. package/com/mqtt/index.js +0 -1052
  47. package/com/mqtt/index.test.js +0 -503
  48. package/com/mqtt/script.tpl.js +0 -719
  49. package/com/nav/README.md +0 -2
  50. package/com/nav/com.json +0 -5
  51. package/com/nav/config.tpl.json +0 -84
  52. package/com/nav/drive.js +0 -702
  53. package/com/nav/index.js +0 -228
  54. package/com/nav/tpl/admin_pc/page_channel.vue +0 -305
  55. package/com/nav/tpl/admin_pc/page_config.vue +0 -280
  56. package/com/nav/tpl/admin_pc/page_config_form.vue +0 -194
  57. package/com/nav/tpl/admin_pc/page_default.vue +0 -247
  58. package/com/nav/tpl/admin_pc/page_form.vue +0 -180
  59. package/com/nav/tpl/admin_pc/page_lang.vue +0 -264
  60. package/com/nav/tpl/admin_pc/page_nav.vue +0 -321
  61. package/com/nav/tpl/admin_pc/page_table.vue +0 -296
  62. package/com/nav/tpl/admin_pc/page_type.vue +0 -321
  63. package/com/nav/tpl/admin_pc/page_view.vue +0 -124
  64. package/com/nav/tpl/dev_pc/page_channel.vue +0 -253
  65. package/com/nav/tpl/dev_pc/page_config.vue +0 -253
  66. package/com/nav/tpl/dev_pc/page_default.vue +0 -247
  67. package/com/nav/tpl/dev_pc/page_form.vue +0 -162
  68. package/com/nav/tpl/dev_pc/page_nav.vue +0 -253
  69. package/com/nav/tpl/dev_pc/page_table.vue +0 -247
  70. package/com/nav/tpl/dev_pc/page_type.vue +0 -313
  71. package/com/nav/tpl/home_pc/page_channel.vue +0 -234
  72. package/com/nav/tpl/home_pc/page_default.vue +0 -234
  73. package/com/nav/tpl/home_pc/page_form.vue +0 -137
  74. package/com/nav/tpl/home_pc/page_list.vue +0 -234
  75. package/com/nav/tpl/home_pc/page_nav.vue +0 -221
  76. package/com/nav/tpl/home_pc/page_type.vue +0 -234
  77. package/com/nav/tpl/home_pc/page_view.vue +0 -125
  78. package/com/nav/tpl/home_phone/page_channel.vue +0 -234
  79. package/com/nav/tpl/home_phone/page_default.vue +0 -234
  80. package/com/nav/tpl/home_phone/page_form.vue +0 -137
  81. package/com/nav/tpl/home_phone/page_list.vue +0 -234
  82. package/com/nav/tpl/home_phone/page_nav.vue +0 -237
  83. package/com/nav/tpl/home_phone/page_type.vue +0 -234
  84. package/com/nav/tpl/home_phone/page_view.vue +0 -125
  85. package/com/nav/viewmodel.js +0 -446
  86. package/com/param/README.md +0 -2
  87. package/com/param/com.json +0 -5
  88. package/com/param/config.tpl.json +0 -136
  89. package/com/param/drive.js +0 -502
  90. package/com/param/index.js +0 -158
  91. package/com/param/script.tpl.js +0 -12
  92. package/com/pendant/README.md +0 -3
  93. package/com/pendant/com.json +0 -5
  94. package/com/pendant/config.tpl.json +0 -32
  95. package/com/pendant/drive.js +0 -204
  96. package/com/pendant/index.js +0 -467
  97. package/com/pendant/pendant.html +0 -16
  98. package/com/pendant/script.tpl.js +0 -18
  99. package/com/socket/README.md +0 -47
  100. package/com/socket/com.json +0 -5
  101. package/com/socket/config.tpl.json +0 -12
  102. package/com/socket/drive.js +0 -679
  103. package/com/socket/index.js +0 -402
  104. package/com/socket/script.tpl.js +0 -41
  105. package/com/sql/README.md +0 -3
  106. package/com/sql/com.json +0 -5
  107. package/com/sql/config.tpl.json +0 -74
  108. package/com/sql/drive.js +0 -1328
  109. package/com/sql/index.js +0 -152
  110. package/com/sql/script.tpl.js +0 -47
  111. package/com/static/README.md +0 -2
  112. package/com/static/com.json +0 -5
  113. package/com/static/config.tpl.json +0 -24
  114. package/com/static/drive.js +0 -194
  115. package/com/static/index.js +0 -237
  116. package/com/static/script.tpl.js +0 -28
  117. package/com/task/README.md +0 -3
  118. package/com/task/com.json +0 -5
  119. package/com/task/config.tpl.json +0 -24
  120. package/com/task/drive.js +0 -428
  121. package/com/task/index.js +0 -159
  122. package/com/task/script.tpl.js +0 -40
  123. package/com/template/com.json +0 -5
  124. package/com/template/config.tpl.json +0 -16
  125. package/com/template/drive.js +0 -81
  126. package/com/template/index.js +0 -141
  127. package/com.js +0 -237
  128. package/common/README.md +0 -2
  129. package/common/middleware/web_after/index.js +0 -26
  130. package/common/middleware/web_after/middleware.json +0 -9
  131. package/common/middleware/web_base/index.js +0 -113
  132. package/common/middleware/web_base/middleware.json +0 -19
  133. package/common/middleware/web_before/index.js +0 -33
  134. package/common/middleware/web_before/middleware.json +0 -9
  135. package/common/middleware/web_cors/index.js +0 -87
  136. package/common/middleware/web_cors/middleware.json +0 -24
  137. package/common/middleware/web_error/index.js +0 -131
  138. package/common/middleware/web_error/middleware.json +0 -19
  139. package/common/middleware/web_ip/index.js +0 -15
  140. package/common/middleware/web_ip/middleware.json +0 -14
  141. package/common/middleware/web_logger/index.js +0 -156
  142. package/common/middleware/web_logger/middleware.json +0 -14
  143. package/common/middleware/web_main/index.js +0 -26
  144. package/common/middleware/web_main/middleware.json +0 -9
  145. package/common/middleware/web_static/index.js +0 -73
  146. package/common/middleware/web_static/middleware.json +0 -54
  147. package/common/middleware/web_waf/index.js +0 -395
  148. package/common/middleware/web_waf/middleware.json +0 -13
  149. package/core/app/config.tpl.json +0 -67
  150. package/core/app/index.js +0 -711
  151. package/core/app/script.tpl.js +0 -52
  152. package/core/channel/index.js +0 -899
  153. package/core/channel/matcher.js +0 -585
  154. package/core/com/config.tpl.json +0 -16
  155. package/core/com/index.js +0 -77
  156. package/core/com/script.tpl.js +0 -5
  157. package/core/component/component.js +0 -42
  158. package/core/component/config.tpl.json +0 -63
  159. package/core/component/index.js +0 -273
  160. package/core/component/script.tpl.js +0 -19
  161. package/core/controller/config.tpl.json +0 -14
  162. package/core/controller/index.js +0 -371
  163. package/core/controller/script.tpl.js +0 -27
  164. package/core/factory/config.tpl.json +0 -14
  165. package/core/factory/entity.js +0 -275
  166. package/core/factory/index.js +0 -241
  167. package/core/factory/script.tpl.js +0 -16
  168. package/core/game/bat/index.js +0 -137
  169. package/core/game/bat/world.js +0 -622
  170. package/core/game/config.tpl.json +0 -16
  171. package/core/game/entity_admin.js +0 -230
  172. package/core/game/index.js +0 -186
  173. package/core/handler/config.tpl.json +0 -22
  174. package/core/handler/index.js +0 -181
  175. package/core/handler/script.tpl.js +0 -23
  176. package/core/logic/config.tpl.json +0 -14
  177. package/core/logic/index.js +0 -59
  178. package/core/logic/script.tpl.js +0 -19
  179. package/core/middleware/config.tpl.json +0 -16
  180. package/core/middleware/index.js +0 -117
  181. package/core/middleware/script.tpl.js +0 -37
  182. package/core/mod/config.tpl.json +0 -22
  183. package/core/mod/index.js +0 -136
  184. package/core/mod/script.tpl.js +0 -34
  185. package/core/model/config.tpl.json +0 -219
  186. package/core/model/index.js +0 -272
  187. package/core/model/model.js +0 -27
  188. package/core/model/script.tpl.js +0 -20
  189. package/core/notifier/config.tpl.json +0 -14
  190. package/core/notifier/index.js +0 -77
  191. package/core/notifier/script.tpl.js +0 -20
  192. package/core/plugin/config.tpl.json +0 -36
  193. package/core/plugin/index.js +0 -267
  194. package/core/plugin/script.tpl.js +0 -51
  195. package/core/pusher/config.tpl.json +0 -14
  196. package/core/pusher/index.js +0 -158
  197. package/core/pusher/script.tpl.js +0 -20
  198. package/core/room/bat/index.js +0 -170
  199. package/core/room/bat/room.js +0 -524
  200. package/core/room/config.tpl.json +0 -20
  201. package/core/room/index.js +0 -249
  202. package/core/room/room.js +0 -61
  203. package/core/scene/config.tpl.json +0 -14
  204. package/core/scene/index.js +0 -467
  205. package/core/scene/loop.js +0 -1306
  206. package/core/scene/map.js +0 -28
  207. package/core/scene/script.tpl.js +0 -22
  208. package/core/sender/config.tpl.json +0 -14
  209. package/core/sender/index.js +0 -79
  210. package/core/sender/script.tpl.js +0 -20
  211. package/core/service/config.tpl.json +0 -14
  212. package/core/service/index.js +0 -100
  213. package/core/service/script.tpl.js +0 -25
  214. package/core/store/config.tpl.json +0 -26
  215. package/core/store/index.js +0 -1794
  216. package/core/store/script.tpl.js +0 -22
  217. package/core/system/config.tpl.json +0 -18
  218. package/core/system/index.js +0 -312
  219. package/core/system/script.tpl.js +0 -77
  220. package/core/view/config.tpl.json +0 -14
  221. package/core/view/index.js +0 -91
  222. package/core/view/script.tpl.js +0 -20
  223. package/core/zone/bat/index.js +0 -744
  224. package/core/zone/config.tpl.json +0 -54
  225. package/core/zone/index.js +0 -655
  226. package/core/zone/script.tpl.js +0 -10
  227. package/core/zone/zone_bat.js +0 -136
  228. package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +0 -24
  229. package/os.js +0 -53
  230. package/server.js +0 -602
  231. package/ulits/id_gen.js +0 -134
  232. package/ulits/word.js +0 -206
package/com/api/drive.js DELETED
@@ -1,732 +0,0 @@
1
- const fs = require('fs');
2
- const Item = require('mm_machine').Drive;
3
- const Param = require('../param/drive.js'); // 是MM自带的参数机制,可以不使用
4
- const Sql = require('../sql/drive.js'); // 是MM自带的参数机制,可以不使用
5
- const Oauth = require('./oauth.js'); // 是MM自带的身份验证机制,基于Oauth2.0,可以不使用
6
-
7
- if (!$.dict) {
8
- if (!$.dict.session_id) {
9
- $.dict.session_id = 'mm:uuid';
10
- }
11
- }
12
-
13
- /**
14
- * Api接口驱动类
15
- * @augments {Item}
16
- * @class
17
- */
18
- class Drive extends Item {
19
- static config = {
20
- // 应用名称 例如:demo_app
21
- app: 'server',
22
- // 插件名称 例如:demo_plugin
23
- plugin: 'sys',
24
- // 路径 例如:/demo
25
- path: '',
26
- // 接口类型,api表示服务端数据类型,web表示网页类型,app表示第三方应用类型
27
- type: 'api',
28
- // 请求方式, POST或GET, 为空或ALL表示都可以
29
- method: 'ALL',
30
- // 缓存时长 (单位:秒) 默认:60秒,建议600秒
31
- cache: 0,
32
- // 是否用客户端缓存,即 http 304 状态
33
- client_cache: false,
34
- // param参数验证路径 例如: ./param.json
35
- param_path: '',
36
- // sql模板路径 例如: ./sql.json
37
- sql_path: '',
38
- // 是否验证参数
39
- check_param: true,
40
- // oauth身份验证模型, 参考oauth文件
41
- oauth: {
42
- scope: true,
43
- sign_in: false,
44
- vip: 0,
45
- gm: 0,
46
- mc: 0,
47
- user_admin: [],
48
- user_group: [],
49
- },
50
- };
51
- /**
52
- * 构造函数
53
- * @param {object} config 配置项
54
- * @param {object} parent 父对象
55
- * @class
56
- */
57
- constructor(config, parent) {
58
- super({ ...Drive.config, ...config }, parent);
59
-
60
- /* 通用项 */
61
- // 默认启用热更新
62
- this.mode = 3;
63
- }
64
- }
65
-
66
- /**
67
- * 预设配置
68
- */
69
- Drive.prototype._preset = function () {
70
- this.save_dir = './static/file/';
71
- this.url_path = '/file/';
72
-
73
- // oauth身份验证配置 + 函数
74
- this.oauth = null;
75
- // param参数配置 + 函数
76
- this.param = null;
77
- // sql模板配置 + 函数
78
- this.sql = null;
79
-
80
- var cg = this.config;
81
- this._loadParam(cg.param_path);
82
- this._loadSql(cg.sql_path);
83
- this._loadOauth();
84
- };
85
-
86
- /**
87
- * 设置参数对象
88
- * @param {object} param 参数对象
89
- */
90
- Drive.prototype.updateParam = function (param) {
91
- if ($.param) {
92
- var lt = $.param.list;
93
- if (lt) {
94
- var has = false;
95
- var len = lt.length;
96
- for (var i = 0; i < len; i++) {
97
- var o = lt[i];
98
- if (param.config_file === o.config_file) {
99
- $.param.list[i] = param;
100
- has = true;
101
- break;
102
- }
103
- }
104
- if (!has) {
105
- $.param.list.push(param);
106
- }
107
- }
108
- }
109
- };
110
-
111
- /**
112
- * 获取现有参数
113
- * @param {string} file 文件名
114
- * @returns {object} 获取的对象
115
- */
116
- Drive.prototype.getParam = function (file) {
117
- if ($.param) {
118
- var obj = $.param.list.getObj({
119
- config_file: file,
120
- });
121
- if (obj) {
122
- return obj;
123
- }
124
- }
125
- return null;
126
- };
127
-
128
- /**
129
- * 加载参数
130
- * @param {string} file_path 文件路径
131
- */
132
- Drive.prototype._loadParam = async function (file_path) {
133
- if (file_path) {
134
- var p = file_path.fullname(this.getDir());
135
- var param = this.getParam(p);
136
- if (param) {
137
- this.param = param;
138
- } else {
139
- this.param = new Param({}, this);
140
- await this.param.call('loadConfig', p);
141
- this.updateParam(this.param);
142
- this.param.do('load');
143
- }
144
- }
145
- };
146
-
147
- /**
148
- * 设置Sql
149
- * @param {object} sql sql对象
150
- * @param {string} sql.config_file 文件名
151
- * @param {object} sql.sql sql语句
152
- */
153
- Drive.prototype.updateSql = function (sql) {
154
- if ($.sql) {
155
- var lt = $.sql.list;
156
- if (lt) {
157
- var has = false;
158
- var len = lt.length;
159
- for (var i = 0; i < len; i++) {
160
- var o = lt[i];
161
- if (sql.config_file === o.config_file) {
162
- $.sql.list[i] = sql;
163
- has = true;
164
- break;
165
- }
166
- }
167
- if (!has) {
168
- $.sql.list.push(sql);
169
- }
170
- }
171
- }
172
- };
173
-
174
- /**
175
- * 获取现有sql模板
176
- * @param {string} file 文件名
177
- * @returns {object} 获取的对象
178
- */
179
- Drive.prototype.getSql = function (file) {
180
- if ($.sql) {
181
- var lt = $.sql.list;
182
- if (lt) {
183
- var obj = lt.getObj({
184
- config_file: file,
185
- });
186
- if (obj) {
187
- return obj;
188
- }
189
- }
190
- }
191
- return null;
192
- };
193
-
194
- /**
195
- * 加载sql模板
196
- * @param {string} file_path 文件路径
197
- */
198
- Drive.prototype._loadSql = async function (file_path) {
199
- if (file_path) {
200
- var p = file_path.fullname(this.getDir());
201
- var sql = this.getSql(p);
202
- if (sql) {
203
- this.sql = sql;
204
- } else {
205
- this.sql = new Sql(
206
- {
207
- name: this.config.name,
208
- },
209
- this,
210
- );
211
- await this.sql.call('loadConfig', p);
212
- this.updateSql(this.sql);
213
- this.sql.call('loadScript');
214
- }
215
- }
216
- };
217
-
218
- /**
219
- * 加载身份验证配置
220
- */
221
- Drive.prototype._loadOauth = function () {
222
- var cg = this.config.oauth;
223
- if (cg) {
224
- this.oauth = new Oauth(this.getDir());
225
- this.oauth.loadObj(cg);
226
- }
227
- };
228
-
229
- /**
230
- * 保存文件
231
- * @param {object} files 上传的文件
232
- * @param {string} files.file 上传的文件对象
233
- * @returns {object} 返回文件路径和URL
234
- */
235
- Drive.prototype.saveFile = function (files) {
236
- var file;
237
- var url;
238
- if (files.file) {
239
- var f = files.file;
240
- // 安全处理文件名: 移除路径穿越字符和特殊字符
241
- var name = f.name || f.originalFilename;
242
- name = name.replace(/[\\/]|\.\./g, '_');
243
- file = ('./' + name).fullname(this.save_dir);
244
- file.addDir();
245
- // 创建写入流
246
- var read_stream = fs.createReadStream(f.filepath);
247
-
248
- try {
249
- let write_stream = fs.createWriteStream(file);
250
- read_stream.pipe(write_stream);
251
-
252
- read_stream.on('error', (err) => {
253
- this.log('error', '读取文件流失败', err);
254
- write_stream.destroy();
255
- });
256
- write_stream.on('error', (err) => {
257
- this.log('error', '写入文件流失败', err);
258
- read_stream.destroy();
259
- });
260
-
261
- url = this.url_path + name;
262
- } catch (err) {
263
- this.log('error', '保存文件失败', err);
264
- }
265
- }
266
- return {
267
- file,
268
- url,
269
- };
270
- };
271
-
272
- /* 回调函数集 */
273
- $.getState = async function (db, token) {
274
- var db1 = db.new('user_state', 'user_id');
275
- var state = await db1.getObj(
276
- {
277
- token,
278
- },
279
- null,
280
- null,
281
- false,
282
- );
283
- if (!state) {
284
- return null;
285
- }
286
- var now = new Date();
287
- if (state.time_sign.interval(now, 'day') <= 30) {
288
- db.table = 'user_account';
289
- var user = await db.getObj({
290
- user_id: state.user_id,
291
- });
292
- var o = { ...user };
293
- o.password_pay = o.password_pay ? '******' : '';
294
- o.password = o.password ? '******' : '';
295
- delete o.salt;
296
- delete o.time_create;
297
- return {
298
- state,
299
- user: o,
300
- };
301
- } else {
302
- return null;
303
- }
304
- };
305
-
306
- /**
307
- * 获取登录态
308
- * @param {object} ctx 请求上下文
309
- * @param {object} db 数据管理器
310
- * @returns {object} 执行结果
311
- */
312
- Drive.prototype.getState = async function (ctx, db) {
313
- var o = await this._getUserFromSession(ctx);
314
- if (!o) {
315
- o = await this._getUserFromToken(ctx, db);
316
- }
317
- return this._processUserInfo(o);
318
- };
319
-
320
- /**
321
- * 从 session 中获取用户信息
322
- * @param {object} ctx 请求上下文
323
- * @returns {object|null} 用户信息
324
- * @private
325
- */
326
- Drive.prototype._getUserFromSession = function (ctx) {
327
- var u = ctx.session?.user;
328
- if (u) {
329
- var o = { ...u };
330
- o.token = ctx.session.uuid;
331
- return o;
332
- }
333
- return null;
334
- };
335
-
336
- /**
337
- * 从 token 中获取用户信息
338
- * @param {object} ctx 请求上下文
339
- * @param {object} db 数据管理器
340
- * @returns {object|null} 用户信息
341
- * @private
342
- */
343
- Drive.prototype._getUserFromToken = async function (ctx, db) {
344
- var token = ctx.headers[$.dict.token];
345
- if (!token) {
346
- return null;
347
- }
348
- var u = await $.cache.get($.dict.session_id + '_' + token);
349
- if (u) {
350
- return await this._processUserFromCache(u, ctx, db, token);
351
- }
352
- return await this._getUserFromDb(token, ctx, db);
353
- };
354
-
355
- /**
356
- * 处理缓存中的用户信息
357
- * @param {object|string} u 缓存中的用户信息
358
- * @param {object} ctx 请求上下文
359
- * @param {object} db 数据管理器
360
- * @param {string} token token
361
- * @returns {object|null} 用户信息
362
- * @private
363
- */
364
- Drive.prototype._processUserFromCache = async function (u, ctx, db, token) {
365
- var o;
366
- if (typeof u === 'string') {
367
- o = { ...u.toJson() };
368
- } else if (Object.keys(u).length > 0) {
369
- o = { ...u };
370
- }
371
- if (!o) {
372
- return null;
373
- }
374
- if (!o.user_id) {
375
- return await this._getUserFromDb(token, ctx, db);
376
- }
377
- o.token = token;
378
- return o;
379
- };
380
-
381
- /**
382
- * 从数据库中获取用户信息
383
- * @param {string} token token
384
- * @param {object} ctx 请求上下文
385
- * @param {object} db 数据管理器
386
- * @returns {object|null} 用户信息
387
- * @private
388
- */
389
- Drive.prototype._getUserFromDb = async function (token, ctx, db) {
390
- var obj = await $.getState(db, token);
391
- if (obj) {
392
- var o = { ...obj.user };
393
- ctx.session.user = obj.user;
394
- o.token = ctx.session.uuid;
395
- obj.state.token = o.token;
396
- return o;
397
- }
398
- return null;
399
- };
400
-
401
- /**
402
- * 处理用户信息
403
- * @param {object|null} o 用户信息
404
- * @returns {object|null} 处理后的用户信息
405
- * @private
406
- */
407
- Drive.prototype._processUserInfo = function (o) {
408
- if (o) {
409
- o.password_pay = o.password_pay ? '******' : '';
410
- o.password = o.password ? '******' : '';
411
- delete o.salt;
412
- delete o.time_create;
413
- }
414
- return o;
415
- };
416
-
417
- /**
418
- * 获取登录态
419
- * @param {object} ctx 请求上下文
420
- * @param {object} db 数据管理器
421
- * @returns {object} 执行结果
422
- */
423
- Drive.prototype.before = async function (ctx, db) {
424
- if (this.config.oauth && this.config.oauth.sign_in) {
425
- db.user = await this.getState(ctx, db);
426
- }
427
- };
428
-
429
- /**
430
- * 主要函数
431
- * @param {object} ctx 请求上下文
432
- * @param {object} db 数据管理器
433
- * @returns {object} 执行结果
434
- */
435
- Drive.prototype.main = async function (ctx, db) {
436
- if (this.sql) {
437
- var req = ctx.request;
438
- // 获取文件
439
- if (req.files) {
440
- var fobj = this.saveFile(req.files);
441
- req.body.file = fobj.file;
442
- req.body.url = fobj.url;
443
- }
444
- var ret = await this.sql.run(req.query, req.body, db);
445
- return ret;
446
- } else {
447
- return $.ret.error(10000, '接口函数未定义');
448
- }
449
- };
450
-
451
- /**
452
- * 调用函数
453
- * @param {object} ctx 请求上下文
454
- * @param {object} db 数据管理器
455
- * @returns {object} 执行结果
456
- */
457
- Drive.prototype.run = async function (ctx, db) {
458
- try {
459
- var body = await this.getCache(ctx);
460
- if (!body) {
461
- var req = ctx.request;
462
- var md = this.config['method'].toLocaleUpperCase();
463
- if (md !== req.method && md !== 'ALL') {
464
- return null;
465
- }
466
- await this.before(ctx, db);
467
- var ret = await this.check(ctx, db);
468
- if (!ret) {
469
- try {
470
- ret = await this.main(ctx, db);
471
- } catch (err) {
472
- this.log('error', '脚本文件错误', req.path, err);
473
- ret = $.ret.error(10000, '脚本文件错误:' + err.toString());
474
- }
475
- }
476
- var res = ctx.response;
477
- body = this.body(ret, res, req.type);
478
- this.setCache(ctx, body, res.type);
479
- }
480
- } catch (err) {
481
- this.log('error', '接口错误', ctx.path, err);
482
- }
483
- return body;
484
- };
485
-
486
- /**
487
- * 获取缓存
488
- * @param {object} ctx 请求上下文
489
- * @returns {string} 响应内容
490
- */
491
- Drive.prototype.getCache = async function (ctx) {
492
- var cg = this.config;
493
- if (cg.cache) {
494
- var req = ctx.request;
495
- if (cg.client_cache) {
496
- var etag = req.headers['if-none-match'];
497
- if (etag) {
498
- var stamp = number(etag.replace('"', '').replace('"', ''));
499
- var cha = stamp * 1000 - Date.parse(new Date());
500
- if (cha > 0) {
501
- ctx.response.status = 304;
502
- return ' ';
503
- }
504
- }
505
- } else {
506
- var data = await $.cache.get('api_' + req.url);
507
- if (data) {
508
- var obj = JSON.parse(data);
509
- ctx.response.type = obj.type;
510
- return obj.body;
511
- }
512
- }
513
- }
514
- return null;
515
- };
516
-
517
- /**
518
- * 设置缓存
519
- * @param {object} ctx 请求上下文
520
- * @param {object} body 正文参数
521
- * @param {string} type 响应类型
522
- */
523
- Drive.prototype.setCache = async function (ctx, body, type = null) {
524
- var cg = this.config;
525
- if (cg.cache && ctx.method === 'GET') {
526
- if (cg.client_cache) {
527
- var age = cg.cache * 60;
528
- ctx.set('Cache-Control', 'max-age=' + age);
529
- ctx.etag = Date.parse(new Date()) / 1000 + age;
530
- } else {
531
- var req = ctx.request;
532
- var o = {};
533
- o.body = body;
534
- o.type = ctx.response.type;
535
- $.cache.set('api_' + req.url, JSON.stringify(o), cg.cache * 60);
536
- }
537
- }
538
- };
539
-
540
- /**
541
- * 调用函数
542
- * @param {object} result 设置响应结果
543
- * @param {object} res 响应器
544
- * @param {string} t 请求类型
545
- * @returns {string} 处理后的响应结果
546
- */
547
- Drive.prototype.body = function (result, res, t) {
548
- var ret = result;
549
- var type = res.type;
550
- if (!ret) {
551
- return ret;
552
- }
553
- var tp = typeof ret;
554
- if (tp === 'object') {
555
- type = this._handleObjectType(res, type, t);
556
- return this._convertObject(ret, type);
557
- } else if (tp === 'string') {
558
- ret = ret.trim();
559
- this._handleStringType(res, type, ret);
560
- }
561
- return ret;
562
- };
563
-
564
- /**
565
- * 处理对象类型
566
- * @param {object} res 响应器
567
- * @param {string} type 类型
568
- * @param {string} t 请求类型
569
- * @returns {string} 处理后的类型
570
- * @private
571
- */
572
- Drive.prototype._handleObjectType = function (res, type, t) {
573
- var new_type = type;
574
- if (!new_type) {
575
- new_type = t.indexOf('xml') !== -1 ? t : 'application/json; charset=utf-8';
576
- res.type = new_type;
577
- } else {
578
- new_type = this._normType(new_type);
579
- res.type = new_type;
580
- }
581
- return new_type;
582
- };
583
-
584
- /**
585
- * 标准化类型
586
- * @param {string} type 类型
587
- * @returns {string} 标准化后的类型
588
- * @private
589
- */
590
- Drive.prototype._normType = function (type) {
591
- if (type.indexOf('json') !== -1) {
592
- return 'application/json; charset=utf-8';
593
- } else if (type.indexOf('html') !== -1) {
594
- return 'text/html';
595
- } else if (type.indexOf('xml') !== -1) {
596
- return 'text/xml; charset=utf-8';
597
- }
598
- return 'text/plain; charset=utf-8';
599
- };
600
-
601
- /**
602
- * 转换对象
603
- * @param {object} obj 对象
604
- * @param {string} type 类型
605
- * @returns {string} 转换后的字符串
606
- * @private
607
- */
608
- Drive.prototype._convertObject = function (obj, type) {
609
- return type.indexOf('/xml') !== -1 ? $.toXml(obj) : JSON.stringify(obj);
610
- };
611
-
612
- /**
613
- * 处理字符串类型
614
- * @param {object} res 响应器
615
- * @param {string} type 类型
616
- * @param {string} str 字符串
617
- * @private
618
- */
619
- Drive.prototype._handleStringType = function (res, type, str) {
620
- if (!type) {
621
- if (
622
- (str.startsWith('{') && str.endsWith('}')) ||
623
- (str.startsWith('[') && str.endsWith(']'))
624
- ) {
625
- res.type = 'application/json; charset=utf-8';
626
- } else if (str.endsWith('</html>')) {
627
- res.type = 'text/html';
628
- } else if (str.startsWith('<?xml')) {
629
- res.type = 'text/xml; charset=utf-8';
630
- } else {
631
- res.type = 'text/plain; charset=utf-8';
632
- }
633
- }
634
- };
635
-
636
- /**
637
- * 验证参数
638
- * @param {object} query url参数
639
- * @param {object} body 内容参数
640
- * @param {string} method 方法
641
- * @returns {object} 验证结果
642
- */
643
- Drive.prototype.checkParam = function (query, body, method) {
644
- if (this.param) {
645
- var msg = this.param.check(query, body, method);
646
- if (msg) {
647
- var code;
648
- if (msg.indexOf('必须') !== -1 || msg.indexOf('为空') !== -1) {
649
- code = 30001;
650
- } else {
651
- code = 30002;
652
- }
653
- return $.ret.error(code, msg);
654
- }
655
- }
656
- return null;
657
- };
658
-
659
- /**
660
- * 验证参数
661
- * @param {object} ctx 内容上下文
662
- * @returns {object} 验证结果
663
- */
664
- Drive.prototype.check = async function (ctx) {
665
- var error;
666
- if (this.config.check_param) {
667
- var req = ctx.request;
668
- error = this.checkParam(req.query, req.body, req.method);
669
- }
670
- if (!error) {
671
- error = await this.checkOauth(ctx);
672
- if (error) {
673
- error = {
674
- error,
675
- };
676
- }
677
- }
678
- return error;
679
- };
680
-
681
- /**
682
- * 验证身份
683
- * @param {object} ctx 内容上下文
684
- * @returns {object} 验证结果
685
- */
686
- Drive.prototype.checkOauth = async function (ctx) {
687
- var aouth = this.oauth;
688
- if (aouth) {
689
- return await aouth.check(ctx);
690
- } else {
691
- return null;
692
- }
693
- };
694
-
695
- /**
696
- * 获取插件
697
- * @param {string} app 应用名称
698
- * @param {string} name 插件名称
699
- * @returns {object} 返回获取到的插件
700
- */
701
- Drive.prototype.plugin = function (app, name) {
702
- var l = $.slash;
703
- var app_name = app || this.config_file.between('app' + l, l);
704
- var plugin_name = name || this.config_file.between('plugin' + l, l);
705
- var plus;
706
- var plugins = $.pool.plugin[app_name];
707
- if (plugins) {
708
- plus = plugins.get(plugin_name);
709
- }
710
- return plus;
711
- };
712
-
713
- /**
714
- * 获取模型
715
- * @param {string} type 模型类型
716
- * @returns {object} 返回获取到的模型
717
- */
718
- Drive.prototype.getModel = function (type) {
719
- let model = { ...this.config };
720
- let dir = this.getDir();
721
- let l = $.slash;
722
- let app_name = dir.between('app' + l, l);
723
- let plugin_name = dir.between('plugin' + l, l);
724
- let name = dir.basename();
725
- model.app = app_name;
726
- model.plugin = plugin_name;
727
- model.name = model.name || name;
728
- model.path = '/' + app_name + '/' + name;
729
- return model;
730
- };
731
-
732
- module.exports = Drive;