mock平台

router.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. const koaRouter = require('koa-router');
  2. const interfaceController = require('./controllers/interface.js');
  3. const groupController = require('./controllers/group.js');
  4. const userController = require('./controllers/user.js');
  5. const interfaceColController = require('./controllers/interfaceCol.js');
  6. const testController = require('./controllers/test.js');
  7. const yapi = require('./yapi.js');
  8. const projectController = require('./controllers/project.js');
  9. const logController = require('./controllers/log.js');
  10. const followController = require('./controllers/follow.js');
  11. const openController = require('./controllers/open.js');
  12. const { createAction } = require('./utils/commons.js');
  13. const router = koaRouter();
  14. let INTERFACE_CONFIG = {
  15. interface: {
  16. prefix: '/interface/',
  17. controller: interfaceController
  18. },
  19. user: {
  20. prefix: '/user/',
  21. controller: userController
  22. },
  23. group: {
  24. prefix: '/group/',
  25. controller: groupController
  26. },
  27. project: {
  28. prefix: '/project/',
  29. controller: projectController
  30. },
  31. log: {
  32. prefix: '/log/',
  33. controller: logController
  34. },
  35. follow: {
  36. prefix: '/follow/',
  37. controller: followController
  38. },
  39. col: {
  40. prefix: '/col/',
  41. controller: interfaceColController
  42. },
  43. test: {
  44. prefix: '/test/',
  45. controller: testController
  46. },
  47. open: {
  48. prefix: '/open/',
  49. controller: openController
  50. }
  51. };
  52. let routerConfig = {
  53. group: [
  54. {
  55. action: 'getMyGroup',
  56. path: 'get_mygroup',
  57. method: 'get'
  58. },
  59. {
  60. action: 'list',
  61. path: 'list',
  62. method: 'get'
  63. },
  64. {
  65. action: 'add',
  66. path: 'add',
  67. method: 'post'
  68. },
  69. {
  70. action: 'up',
  71. path: 'up',
  72. method: 'post'
  73. },
  74. {
  75. action: 'del',
  76. path: 'del',
  77. method: 'post'
  78. },
  79. {
  80. action: 'addMember',
  81. path: 'add_member',
  82. method: 'post'
  83. },
  84. {
  85. action: 'changeMemberRole',
  86. path: 'change_member_role',
  87. method: 'post'
  88. },
  89. {
  90. action: 'delMember',
  91. path: 'del_member',
  92. method: 'post'
  93. },
  94. {
  95. action: 'getMemberList',
  96. path: 'get_member_list',
  97. method: 'get'
  98. },
  99. {
  100. action: 'get',
  101. path: 'get',
  102. method: 'get'
  103. }
  104. ],
  105. user: [
  106. {
  107. action: 'login',
  108. path: 'login',
  109. method: 'post'
  110. },
  111. {
  112. action: 'reg',
  113. path: 'reg',
  114. method: 'post'
  115. },
  116. {
  117. action: 'list',
  118. path: 'list',
  119. method: 'get'
  120. },
  121. {
  122. action: 'findById',
  123. path: 'find',
  124. method: 'get'
  125. },
  126. {
  127. action: 'update',
  128. path: 'update',
  129. method: 'post'
  130. },
  131. {
  132. action: 'del',
  133. path: 'del',
  134. method: 'post'
  135. },
  136. {
  137. action: 'getLoginStatus',
  138. path: 'status',
  139. method: 'get'
  140. },
  141. {
  142. action: 'logout',
  143. path: 'logout',
  144. method: 'get'
  145. },
  146. {
  147. action: 'loginByToken',
  148. path: 'login_by_token',
  149. method: 'all'
  150. },
  151. {
  152. action: 'getLdapAuth',
  153. path: 'login_by_ldap',
  154. method: 'all'
  155. },
  156. {
  157. action: 'upStudy',
  158. path: 'up_study',
  159. method: 'get'
  160. },
  161. {
  162. action: 'changePassword',
  163. path: 'change_password',
  164. method: 'post'
  165. },
  166. {
  167. action: 'search',
  168. path: 'search',
  169. method: 'get'
  170. },
  171. {
  172. action: 'project',
  173. path: 'project',
  174. method: 'get'
  175. },
  176. {
  177. action: 'avatar',
  178. path: 'avatar',
  179. method: 'get'
  180. },
  181. {
  182. action: 'uploadAvatar',
  183. path: 'upload_avatar',
  184. method: 'post'
  185. }
  186. ],
  187. project: [
  188. {
  189. action: 'upSet',
  190. path: 'upset',
  191. method: 'post'
  192. },
  193. {
  194. action: 'getEnv',
  195. path: 'get_env',
  196. method: 'get'
  197. },
  198. {
  199. action: 'add',
  200. path: 'add',
  201. method: 'post'
  202. },
  203. {
  204. action: 'list',
  205. path: 'list',
  206. method: 'get'
  207. },
  208. {
  209. action: 'get',
  210. path: 'get',
  211. method: 'get'
  212. },
  213. {
  214. action: 'up',
  215. path: 'up',
  216. method: 'post'
  217. },
  218. {
  219. action: 'del',
  220. path: 'del',
  221. method: 'post'
  222. },
  223. {
  224. action: 'addMember',
  225. path: 'add_member',
  226. method: 'post'
  227. },
  228. {
  229. action: 'delMember',
  230. path: 'del_member',
  231. method: 'post'
  232. },
  233. {
  234. action: 'changeMemberRole',
  235. path: 'change_member_role',
  236. method: 'post'
  237. },
  238. {
  239. action: 'changeMemberEmailNotice',
  240. path: 'change_member_email_notice',
  241. method: 'post'
  242. },
  243. {
  244. action: 'getMemberList',
  245. path: 'get_member_list',
  246. method: 'get'
  247. },
  248. {
  249. action: 'search',
  250. path: 'search',
  251. method: 'get'
  252. },
  253. {
  254. action: 'upEnv',
  255. path: 'up_env',
  256. method: 'post'
  257. },
  258. {
  259. action: 'upTag',
  260. path: 'up_tag',
  261. method: 'post'
  262. },
  263. {
  264. action: 'token',
  265. path: 'token',
  266. method: 'get'
  267. },
  268. {
  269. action: 'updateToken',
  270. path: 'update_token',
  271. method: 'get'
  272. },
  273. {
  274. action: 'checkProjectName',
  275. path: 'check_project_name',
  276. method: 'get'
  277. },
  278. {
  279. action: 'copy',
  280. path: 'copy',
  281. method: 'post'
  282. },
  283. {
  284. action: 'swaggerUrl',
  285. path: 'swagger_url',
  286. method: 'get'
  287. }
  288. ],
  289. interface: [
  290. {
  291. action: 'add',
  292. path: 'add',
  293. method: 'post'
  294. },
  295. {
  296. action: 'downloadCrx',
  297. path: 'download_crx',
  298. method: 'get'
  299. },
  300. {
  301. action: 'getCatMenu',
  302. path: 'getCatMenu',
  303. method: 'get'
  304. },
  305. {
  306. action: 'list',
  307. path: 'list',
  308. method: 'get'
  309. },
  310. {
  311. action: 'get',
  312. path: 'get',
  313. method: 'get'
  314. },
  315. {
  316. action: 'up',
  317. path: 'up',
  318. method: 'post'
  319. },
  320. {
  321. action: 'del',
  322. path: 'del',
  323. method: 'post'
  324. },
  325. {
  326. action: 'interUpload',
  327. path: 'interUpload',
  328. method: 'post'
  329. },
  330. {
  331. action: 'listByCat',
  332. path: 'list_cat',
  333. method: 'get'
  334. },
  335. {
  336. action: 'listByMenu',
  337. path: 'list_menu',
  338. method: 'get'
  339. },
  340. {
  341. action: 'listByOpen',
  342. path: 'list_open',
  343. method: 'get'
  344. },
  345. {
  346. action: 'addCat',
  347. path: 'add_cat',
  348. method: 'post'
  349. },
  350. {
  351. action: 'upCat',
  352. path: 'up_cat',
  353. method: 'post'
  354. },
  355. {
  356. action: 'delCat',
  357. path: 'del_cat',
  358. method: 'post'
  359. },
  360. {
  361. action: 'getCustomField',
  362. path: 'get_custom_field',
  363. method: 'get'
  364. },
  365. {
  366. action: 'save',
  367. path: 'save',
  368. method: 'post'
  369. },
  370. {
  371. action: 'upIndex',
  372. path: 'up_index',
  373. method: 'post'
  374. },
  375. {
  376. action: 'upCatIndex',
  377. path: 'up_cat_index',
  378. method: 'post'
  379. },
  380. {
  381. action: 'schema2json',
  382. path: 'schema2json',
  383. method: 'post'
  384. }
  385. ],
  386. log: [
  387. {
  388. action: 'list',
  389. path: 'list',
  390. method: 'get'
  391. },
  392. {
  393. action: 'listByUpdate',
  394. path: 'list_by_update',
  395. method: 'post'
  396. }
  397. ],
  398. follow: [
  399. {
  400. action: 'list',
  401. path: 'list',
  402. method: 'get'
  403. },
  404. {
  405. action: 'add',
  406. path: 'add',
  407. method: 'post'
  408. },
  409. {
  410. action: 'del',
  411. path: 'del',
  412. method: 'post'
  413. }
  414. ],
  415. col: [
  416. {
  417. action: 'addCol',
  418. path: 'add_col',
  419. method: 'post'
  420. },
  421. {
  422. action: 'addCaseList',
  423. path: 'add_case_list',
  424. method: 'post'
  425. },
  426. {
  427. action: 'cloneCaseList',
  428. path: 'clone_case_list',
  429. method: 'post'
  430. },
  431. {
  432. action: 'list',
  433. path: 'list',
  434. method: 'get'
  435. },
  436. {
  437. action: 'getCaseList',
  438. path: 'case_list',
  439. method: 'get'
  440. },
  441. {
  442. action: 'getCaseListByVariableParams',
  443. path: 'case_list_by_var_params',
  444. method: 'get'
  445. },
  446. {
  447. action: 'addCase',
  448. path: 'add_case',
  449. method: 'post'
  450. },
  451. {
  452. action: 'upCase',
  453. path: 'up_case',
  454. method: 'post'
  455. },
  456. {
  457. action: 'getCase',
  458. path: 'case',
  459. method: 'get'
  460. },
  461. {
  462. action: 'upCol',
  463. path: 'up_col',
  464. method: 'post'
  465. },
  466. {
  467. action: 'upCaseIndex',
  468. path: 'up_case_index',
  469. method: 'post'
  470. },
  471. {
  472. action: 'upColIndex',
  473. path: 'up_col_index',
  474. method: 'post'
  475. },
  476. {
  477. action: 'delCol',
  478. path: 'del_col',
  479. method: 'get'
  480. },
  481. {
  482. action: 'delCase',
  483. path: 'del_case',
  484. method: 'get'
  485. },
  486. {
  487. action: 'runCaseScript',
  488. path: 'run_script',
  489. method: 'post'
  490. },
  491. {
  492. action: 'getCaseEnvList',
  493. path: 'case_env_list',
  494. method: 'get'
  495. }
  496. ],
  497. test: [
  498. {
  499. action: 'testPost',
  500. path: 'post',
  501. method: 'post'
  502. },
  503. {
  504. action: 'testGet',
  505. path: 'get',
  506. method: 'get'
  507. },
  508. {
  509. action: 'testPut',
  510. path: 'put',
  511. method: 'put'
  512. },
  513. {
  514. action: 'testDelete',
  515. path: 'delete',
  516. method: 'del'
  517. },
  518. {
  519. action: 'testHead',
  520. path: 'head',
  521. method: 'head'
  522. },
  523. {
  524. action: 'testOptions',
  525. path: 'options',
  526. method: 'options'
  527. },
  528. {
  529. action: 'testPatch',
  530. path: 'patch',
  531. method: 'patch'
  532. },
  533. {
  534. action: 'testFilesUpload',
  535. path: 'files/upload',
  536. method: 'post'
  537. },
  538. {
  539. action: 'testSingleUpload',
  540. path: 'single/upload',
  541. method: 'post'
  542. },
  543. {
  544. action: 'testHttpCode',
  545. path: 'http/code',
  546. method: 'post'
  547. },
  548. {
  549. action: 'testRaw',
  550. path: 'raw',
  551. method: 'post'
  552. },
  553. {
  554. action: 'testResponse',
  555. path: 'response',
  556. method: 'get'
  557. }
  558. ],
  559. open: [
  560. {
  561. action: 'projectInterfaceData',
  562. path: 'project_interface_data',
  563. method: 'get'
  564. },
  565. {
  566. action: 'runAutoTest',
  567. path: 'run_auto_test',
  568. method: 'get'
  569. },
  570. {
  571. action: 'importData',
  572. path: 'import_data',
  573. method: 'post'
  574. }
  575. ]
  576. };
  577. let pluginsRouterPath = [];
  578. function addPluginRouter(config) {
  579. if (!config.path || !config.controller || !config.action) {
  580. throw new Error('Plugin Route config Error');
  581. }
  582. let method = config.method || 'GET';
  583. // let routerPath = '/plugin/' + config.path;
  584. // 支持 /api/open/plugin 前缀的 openApi
  585. let routerPath = (config.prefix || '') + '/plugin/' + config.path;
  586. if (pluginsRouterPath.indexOf(routerPath) > -1) {
  587. throw new Error('Plugin Route path conflict, please try rename the path');
  588. }
  589. pluginsRouterPath.push(routerPath);
  590. createAction(router, '/api', config.controller, config.action, routerPath, method, false);
  591. }
  592. yapi.emitHookSync('add_router', addPluginRouter);
  593. for (let ctrl in routerConfig) {
  594. let actions = routerConfig[ctrl];
  595. actions.forEach(item => {
  596. let routerController = INTERFACE_CONFIG[ctrl].controller;
  597. let routerPath = INTERFACE_CONFIG[ctrl].prefix + item.path;
  598. createAction(router, '/api', routerController, item.action, routerPath, item.method);
  599. });
  600. }
  601. module.exports = router;