restapi.html.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * The api view file of dev module of ZenTaoPMS.
  4. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  5. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  6. * @author Wang Yidong<yidong@easycorp.ltd>
  7. * @package dev
  8. * @link https://www.zentao.net
  9. */
  10. namespace zin;
  11. $featureBarItems = array();
  12. foreach($lang->dev->featureBar['api'] as $key => $label)
  13. {
  14. $featureBarItems[] = array
  15. (
  16. 'text' => $label,
  17. 'active' => ($selectedModule == $key || ($key == 'index' and $selectedModule != 'restapi')),
  18. 'url' => inlink('api', "module=$key")
  19. );
  20. }
  21. $parseTree = function($data, $typeList, $level = 0) use (&$parseTree)
  22. {
  23. global $lang;
  24. $trList = array();
  25. $tdList = array();
  26. $field = '';
  27. for($i = 0; $i < $level; $i++) $field .= ' '. ($i == $level - 1 ? '∟' : ' ') . ' ';
  28. $field .= $data['field'];
  29. $tdList[] = h::td($field);
  30. $tdList[] = h::td(zget($typeList, $data['paramsType'], ''));
  31. $tdList[] = h::td(setClass('text-center'), zget($lang->api->boolList, $data['required'], ''));
  32. $tdList[] = h::td(html($data['desc']));
  33. $trList[] = h::tr($tdList);
  34. if(isset($data['children']) && count($data['children']) > 0)
  35. {
  36. $level++;
  37. foreach($data['children'] as $item) $trList = array_merge($trList, $parseTree($item, $typeList, $level));
  38. }
  39. return $trList;
  40. };
  41. $fnGetHeaderContent = function($api)
  42. {
  43. if(empty($api->params['header'])) return null;
  44. global $lang;
  45. $headerTr = array();
  46. foreach($api->params['header'] as $param)
  47. {
  48. $headerTr[] = h::tr
  49. (
  50. h::td($param['field']),
  51. h::td('String'),
  52. h::td($lang->api->boolList[$param['required']]),
  53. h::td(html($param['desc']))
  54. );
  55. }
  56. $content = array();
  57. $content[] = h3(setClass('title'), $lang->api->header);
  58. $content[] = h::table
  59. (
  60. setClass('table bordered paramsTable'),
  61. h::thead
  62. (
  63. h::tr
  64. (
  65. h::th($lang->api->req->name),
  66. h::th($lang->api->req->type),
  67. h::th($lang->api->req->required),
  68. h::th($lang->api->req->desc)
  69. )
  70. ),
  71. h::tbody($headerTr)
  72. );
  73. return $content;
  74. };
  75. $fnGetQueryContent = function($api)
  76. {
  77. if(empty($api->params['query'])) return null;
  78. global $lang;
  79. $queryTr = array();
  80. foreach($api->params['query'] as $param)
  81. {
  82. $queryTr[] = h::tr
  83. (
  84. h::td($param['field']),
  85. h::td('String'),
  86. h::td($lang->api->boolList[$param['required']]),
  87. h::td(html($param['desc']))
  88. );
  89. }
  90. $content = array();
  91. $content[] = h3(setClass('title'), $lang->api->query);
  92. $content[] = h::table
  93. (
  94. setClass('table bordered paramsTable'),
  95. h::thead
  96. (
  97. h::tr
  98. (
  99. h::th($lang->api->req->name),
  100. h::th($lang->api->req->type),
  101. h::th($lang->api->req->required),
  102. h::th($lang->api->req->desc)
  103. )
  104. ),
  105. h::tbody($queryTr)
  106. );
  107. return $content;
  108. };
  109. $fnGetParamsContent = function($api) use(&$parseTree, $typeList)
  110. {
  111. if(empty($api->params['params'])) return null;
  112. global $lang;
  113. $paramsTr = array();
  114. foreach($api->params['params'] as $item) $paramsTr = array_merge($paramsTr, $parseTree($item, $typeList));
  115. $content = array();
  116. $content[] = h3(setClass('title'), $lang->api->params);
  117. $content[] = h::table
  118. (
  119. setClass('table bordered paramsTable'),
  120. h::thead
  121. (
  122. h::tr
  123. (
  124. h::th($lang->api->req->name),
  125. h::th($lang->api->req->type),
  126. h::th($lang->api->req->required),
  127. h::th($lang->api->req->desc)
  128. )
  129. ),
  130. h::tbody($paramsTr)
  131. );
  132. if(!empty($api->paramsExample))
  133. {
  134. $content[] = h3(setClass('title'), $lang->api->paramsExample);
  135. $content[] = h::pre(h::code(html($api->paramsExample)));
  136. }
  137. return $content;
  138. };
  139. $fnGetResponseContent = function($api) use($parseTree, $typeList)
  140. {
  141. if(empty($api->response)) return null;
  142. global $lang;
  143. $responseTr = array();
  144. foreach($api->response as $item) $responseTr = array_merge($responseTr, $parseTree($item, $typeList));
  145. $content = array();
  146. $content[] = h3(setClass('title'), $lang->api->response);
  147. $content[] = h::table
  148. (
  149. setClass('table bordered paramsTable'),
  150. h::thead
  151. (
  152. h::tr
  153. (
  154. h::th($lang->api->req->name),
  155. h::th($lang->api->req->type),
  156. h::th($lang->api->req->required),
  157. h::th($lang->api->req->desc)
  158. )
  159. ),
  160. h::tbody($responseTr)
  161. );
  162. if(!empty($api->responseExample))
  163. {
  164. $content[] = h3(setClass('title'), $lang->api->responseExample);
  165. $content[] = h::pre(h::code(html($api->responseExample)));
  166. }
  167. return $content;
  168. };
  169. $fnBuildAPIContent = function() use($api, $fnGetHeaderContent, $fnGetQueryContent, $fnGetParamsContent, $fnGetResponseContent)
  170. {
  171. global $lang, $app;
  172. $content = array();
  173. $content[] = div
  174. (
  175. setClass('pb-3 font-bold'),
  176. $lang->dev->apiBaseUrl . ': ' . commonModel::getSysURL() . $app->config->webRoot . 'api.php/v1'
  177. );
  178. $content[] = div
  179. (
  180. setClass('panel-heading'),
  181. div(setClass('http-method label'), $api->method),
  182. div(setClass('path'), set::title($api->path), $api->path)
  183. );
  184. $content[] = div
  185. (
  186. setClass('panel-body'),
  187. h2(setClass('title'), set::title($api->title), $api->title),
  188. div(setClass('desc'), html($api->desc)),
  189. div
  190. (
  191. $fnGetHeaderContent($api),
  192. $fnGetQueryContent($api),
  193. $fnGetParamsContent($api),
  194. $fnGetResponseContent($api)
  195. )
  196. );
  197. return $content;
  198. };
  199. $activeGroup = '';
  200. foreach($moduleTree as $module)
  201. {
  202. if($module->active) $activeGroup = $module->id;
  203. }
  204. h::css("
  205. .sidebar .tree [data-level=\"0\"][id=\"{$activeGroup}\"] {color: var(--color-primary-600); font-weight:bolder}
  206. .sidebar .tree [data-level=\"1\"][id=\"{$apiID}\"] {color: var(--color-primary-600); font-weight:bolder}
  207. ");
  208. featureBar
  209. (
  210. set::items($featureBarItems),
  211. icon(set('title', $lang->dev->apiTips), 'help')
  212. );
  213. sidebar
  214. (
  215. setClass('bg-white'),
  216. h::header
  217. (
  218. setClass('h-10 flex items-center pl-4 flex-none gap-3'),
  219. div(setClass('text-lg font-semibold flex items-center'), icon(setClass('pr-2'), 'list'), span($lang->dev->moduleList))
  220. ),
  221. treeEditor(set(array('className' => 'pl-3', 'items' => $moduleTree, 'canEdit' => false, 'canDelete' => false, 'canSplit' => false))),
  222. set::toggleBtn(false)
  223. );
  224. div
  225. (
  226. setClass('bg-white p-3 panel'),
  227. $selectedModule ? $fnBuildAPIContent() : null
  228. );