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