Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

125 rader
3.7 KiB

3 år sedan
  1. <?php
  2. namespace Api\Model;
  3. use Api\Model\BaseModel;
  4. /**
  5. *
  6. * @author star7th
  7. */
  8. class ExportModel {
  9. Protected $autoCheckFields = false; //一定要关闭字段缓存,不然会报找不到表的错误
  10. //把runapi的格式内容转换为markdown格式。如果不是runapi格式,则会返回原数据
  11. public function runapiToMd($content_json){
  12. $content_json = htmlspecialchars_decode($content_json) ;
  13. $content = json_decode($content_json , true) ;
  14. if(!$content || !$content['info'] || !$content['info']['url'] ){
  15. return $content_json ;
  16. }
  17. $new_content = "
  18. ##### 简要描述
  19. - ".($content['info']['description'] ? $content['info']['description'] :'无') ."
  20. ##### 请求URL
  21. - `{$content['info']['url']}`
  22. ##### 请求方式
  23. - {$content['info']['method']}
  24. ";
  25. if($content['request']['headers'] && $content['request']['headers'][0] && $content['request']['headers'][0]['name']){
  26. $new_content .= "
  27. ##### Header
  28. |header|必选|类型|说明|
  29. |:----- |:-----|-----|
  30. ";
  31. foreach ($content['request']['headers'] as $key => $value) {
  32. $value['require'] = $value['require'] > 0 ? "" : "" ;
  33. $value['remark'] = $value['remark'] ? $value['remark'] : ;
  34. $new_content .= "|{$value['name']}| {$value['require']} | {$value['type']} | {$value['remark']} | \n";
  35. }
  36. }
  37. $params = $content['request']['params'][$content['request']['params']['mode']];
  38. if ($params && is_array($params) && $params[0] && $params[0]['name']){
  39. $new_content .= "
  40. ##### 请求参数
  41. |参数名|必选|类型|说明|
  42. |:----- |:-----|-----|
  43. ";
  44. foreach ($params as $key => $value) {
  45. $value['require'] = $value['require'] > 0 ? "" : "" ;
  46. $value['remark'] = $value['remark'] ? $value['remark'] : ;
  47. $new_content .= "|{$value['name']}| {$value['require']} | {$value['type']} | {$value['remark']} | \n";
  48. }
  49. }
  50. //如果参数类型为json
  51. if($content['request']['params']['mode'] == 'json' && $params){
  52. $new_content .= "
  53. ##### 请求参数示例
  54. ```
  55. {$params}
  56. ```
  57. ";
  58. }
  59. // json字段说明
  60. $jsonDesc = $content['request']['params']['jsonDesc'] ;
  61. if ($content['request']['params']['mode'] == 'json' && $jsonDesc && $jsonDesc[0] && $jsonDesc[0]['name']){
  62. $new_content .= "
  63. ##### json字段说明
  64. |字段名|必选|类型|说明|
  65. |:----- |:-----|-----|
  66. ";
  67. foreach ($jsonDesc as $key => $value) {
  68. $value['require'] = $value['require'] > 0 ? "" : "" ;
  69. $value['remark'] = $value['remark'] ? $value['remark'] : ;
  70. $new_content .= "|{$value['name']}| {$value['require']} | {$value['type']} | {$value['remark']} | \n";
  71. }
  72. }
  73. //返回示例
  74. if($content['response']['responseExample']){
  75. $new_content .= "
  76. ##### 返回示例
  77. ```
  78. {$content['response']['responseExample']}
  79. ```
  80. ";
  81. }
  82. //返回示例说明
  83. if($content['response']['responseParamsDesc'] && $content['response']['responseParamsDesc'][0] && $content['response']['responseParamsDesc'][0]['name']){
  84. $new_content .= "
  85. ##### 返回参数说明
  86. |参数名|类型|说明|
  87. |:----- |:-----|-----|
  88. ";
  89. foreach ($content['response']['responseParamsDesc'] as $key => $value) {
  90. $value['remark'] = $value['remark'] ? $value['remark'] : ;
  91. $new_content .= "|{$value['name']}| {$value['type']} | {$value['remark']} | \n";
  92. }
  93. }
  94. $new_content .= "
  95. ##### 备注
  96. {$content['info']['remark']}
  97. ";
  98. return $new_content ;
  99. }
  100. }