DocumentGroupController.class.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * @name eolinker ams open source,eolinker开源版本
  4. * @link https://www.eolinker.com/
  5. * @package eolinker
  6. * @author www.eolinker.com 广州银云信息科技有限公司 ©2015-2018
  7. * eoLinker是目前全球领先、国内最大的在线API接口管理平台,提供自动生成API文档、API自动化测试、Mock测试、团队协作等功能,旨在解决由于前后端分离导致的开发效率低下问题。
  8. * 如在使用的过程中有任何问题,欢迎加入用户讨论群进行反馈,我们将会以最快的速度,最好的服务态度为您解决问题。
  9. *
  10. * eoLinker AMS开源版的开源协议遵循Apache License 2.0,如需获取最新的eolinker开源版以及相关资讯,请访问:https://www.eolinker.com/#/os/download
  11. *
  12. * 官方网站:https://www.eolinker.com/
  13. * 官方博客以及社区:http://blog.eolinker.com/
  14. * 使用教程以及帮助:http://help.eolinker.com/
  15. * 商务合作邮箱:market@eolinker.com
  16. * 用户讨论QQ群:284421832
  17. */
  18. class DocumentGroupController
  19. {
  20. private $user_id;
  21. // return an json object
  22. // 返回json类型
  23. private $returnJson = array('type' => 'document_group');
  24. /**
  25. * Checkout login status
  26. * 验证登录状态
  27. */
  28. public function __construct()
  29. {
  30. // identity authentication
  31. // 身份验证
  32. $server = new GuestModule;
  33. if (!$server->checkLogin()) {
  34. $this->returnJson['statusCode'] = '120005';
  35. exitOutput($this->returnJson);
  36. } else {
  37. $this->user_id = $_SESSION['userID'];
  38. }
  39. }
  40. /**
  41. * 添加文档分组
  42. */
  43. public function addGroup()
  44. {
  45. $project_id = securelyInput('projectID');
  46. $name_length = mb_strlen(quickInput('groupName'), 'utf8');
  47. $group_name = securelyInput('groupName');
  48. $parent_group_id = securelyInput('parentGroupID', NULL);
  49. if (!preg_match('/^[0-9]{1,11}$/', $project_id)) {
  50. $this->returnJson['statusCode'] = '220005';
  51. } elseif ($name_length < 1 || $name_length > 32) {
  52. //分组名称格式非法
  53. $this->returnJson['statusCode'] = '220001';
  54. } elseif (!preg_match('/^[0-9]{1,11}$/', $parent_group_id) && $parent_group_id != NULL) {
  55. // 分组ID格式不合法
  56. $this->returnJson['statusCode'] = '220002';
  57. } else {
  58. $project_module = new ProjectModule();
  59. $user_type = $project_module->getUserType($project_id);
  60. if ($user_type < 0 || $user_type > 2) {
  61. $this->returnJson['statusCode'] = '120007';
  62. } else {
  63. $service = new DocumentGroupModule();
  64. $result = $service->addGroup($project_id, $this->user_id, $group_name, $parent_group_id);
  65. if ($result) {
  66. // 添加项目api分组成功
  67. $this->returnJson['statusCode'] = '000000';
  68. $this->returnJson['groupID'] = $result;
  69. } else {
  70. // 添加项目api分组失败
  71. $this->returnJson['statusCode'] = '220000';
  72. }
  73. }
  74. }
  75. exitOutput($this->returnJson);
  76. }
  77. /**
  78. * 删除api分组
  79. */
  80. public function deleteGroup()
  81. {
  82. $group_id = securelyInput('groupID');
  83. if (!preg_match('/^[0-9]{1,11}$/', $group_id)) {
  84. // 分组ID格式不合法
  85. $this->returnJson['statusCode'] = '220003';
  86. } else {
  87. // 分组ID格式合法
  88. $service = new DocumentGroupModule();
  89. $user_type = $service->getUserType($group_id);
  90. if ($user_type < 0 || $user_type > 2) {
  91. $this->returnJson['statusCode'] = '120007';
  92. } else {
  93. $result = $service->deleteGroup($this->user_id, $group_id);
  94. if ($result) {
  95. // 删除项目api分组成功
  96. $this->returnJson['statusCode'] = '000000';
  97. } else {
  98. // 删除api分组失败
  99. $this->returnJson['statusCode'] = '220000';
  100. }
  101. }
  102. }
  103. exitOutput($this->returnJson);
  104. }
  105. /**
  106. * 获取api分组
  107. */
  108. public function getGroupList()
  109. {
  110. $project_id = securelyInput('projectID');
  111. if (!preg_match('/^[0-9]{1,11}$/', $project_id)) {
  112. $this->returnJson['statusCode'] = '220005';
  113. } else {
  114. $service = new DocumentGroupModule();
  115. $result = $service->getGroupList($project_id, $this->user_id);
  116. //验证结果
  117. if ($result) {
  118. $this->returnJson['statusCode'] = '000000';
  119. $this->returnJson = array_merge($this->returnJson, $result);
  120. } else {
  121. $this->returnJson['statusCode'] = '220000';
  122. }
  123. }
  124. exitOutput($this->returnJson);
  125. }
  126. /**
  127. * 修改api分组
  128. */
  129. public function editGroup()
  130. {
  131. $name_length = mb_strlen(quickInput('groupName'), 'utf8');
  132. $group_id = securelyInput('groupID');
  133. $group_name = securelyInput('groupName');
  134. $parent_group_id = securelyInput('parentGroupID', NULL);
  135. // 判断分组ID和组名格式是否合法
  136. if ($name_length < 1 && $name_length > 32) {
  137. $this->returnJson['statusCode'] = '220001';
  138. } elseif (!preg_match('/^[0-9]{1,11}$/', $parent_group_id) && $parent_group_id != NULL) {
  139. // 父分组ID格式不合法
  140. $this->returnJson['statusCode'] = '220002';
  141. } elseif (!preg_match('/^[0-9]{1,11}$/', $group_id)) {
  142. // 分组ID格式不合法
  143. $this->returnJson['statusCode'] = '220003';
  144. } elseif ($group_id == $parent_group_id) {
  145. //父分组和子分组
  146. $this->returnJson['statusCode'] = '220006';
  147. } else {
  148. $service = new DocumentGroupModule();
  149. $user_type = $service->getUserType($group_id);
  150. if ($user_type < 0 || $user_type > 2) {
  151. $this->returnJson['statusCode'] = '120007';
  152. } else {
  153. $result = $service->editGroup($this->user_id, $group_id, $group_name, $parent_group_id);
  154. if ($result)
  155. // 修改api分组成功
  156. $this->returnJson['statusCode'] = '000000';
  157. else
  158. // 修改api分组失败
  159. $this->returnJson['statusCode'] = '220000';
  160. }
  161. }
  162. exitOutput($this->returnJson);
  163. }
  164. /**
  165. * 分组排序
  166. */
  167. public function sortDocumentGroup()
  168. {
  169. $project_id = securelyInput('projectID');
  170. $order_list = quickInput('orderList');
  171. if (!preg_match('/^[0-9]{1,11}$/', $project_id)) {
  172. $this->returnJson['statusCode'] = '220005';
  173. } //判断排序格式是否合法
  174. elseif (empty($order_list)) {
  175. //排序格式非法
  176. $this->returnJson['statusCode'] = '220004';
  177. } else {
  178. $project_module = new ProjectModule();
  179. $user_type = $project_module->getUserType($project_id);
  180. if ($user_type < 0 || $user_type > 2) {
  181. $this->returnJson['statusCode'] = '120007';
  182. } else {
  183. $service = new DocumentGroupModule();
  184. $result = $service->updateGroupOrder($project_id, $order_list, $this->user_id);
  185. if ($result) {
  186. //成功
  187. $this->returnJson['statusCode'] = '000000';
  188. } else {
  189. //失败
  190. $this->returnJson['statusCode'] = '220000';
  191. }
  192. }
  193. }
  194. exitOutput($this->returnJson);
  195. }
  196. /**
  197. * 导出分组
  198. */
  199. public function exportGroup()
  200. {
  201. //分组ID
  202. $group_id = securelyInput('groupID');
  203. if (!preg_match('/^[0-9]{1,11}$/', $group_id)) {
  204. // 分组ID格式不合法
  205. $this->returnJson['statusCode'] = '220003';
  206. } else {
  207. $service = new DocumentGroupModule();
  208. $user_type = $service->getUserType($group_id);
  209. if ($user_type < 0 || $user_type > 2) {
  210. $this->returnJson['statusCode'] = '120007';
  211. } else {
  212. $result = $service->exportGroup($group_id);
  213. if ($result) {
  214. $this->returnJson['statusCode'] = '000000';
  215. $this->returnJson['fileName'] = $result;
  216. } else {
  217. $this->returnJson['statusCode'] = '220000';
  218. }
  219. }
  220. }
  221. exitOutput($this->returnJson);
  222. }
  223. /**
  224. * 导入分组
  225. */
  226. public function importGroup()
  227. {
  228. $project_id = securelyInput('projectID');
  229. $json = quickInput('data');
  230. $data = json_decode($json, TRUE);
  231. if (!preg_match('/^[0-9]{1,11}$/', $project_id)) {
  232. $this->returnJson['statusCode'] = '220007';
  233. } //判断导入数据是否为空
  234. elseif (empty($data)) {
  235. $this->returnJson['statusCode'] = '220005';
  236. exitOutput($this->returnJson);
  237. } else {
  238. $service = new ProjectModule();
  239. $user_type = $service->getUserType($project_id);
  240. if ($user_type < 0 || $user_type > 2) {
  241. $this->returnJson['statusCode'] = '120007';
  242. }
  243. $server = new DocumentGroupModule();
  244. $result = $server->importGroup($project_id, $data);
  245. //验证结果
  246. if ($result) {
  247. $this->returnJson['statusCode'] = '000000';
  248. } else {
  249. $this->returnJson['statusCode'] = '220000';
  250. }
  251. }
  252. exitOutput($this->returnJson);
  253. }
  254. }