StatusCodeController.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 StatusCodeController
  19. {
  20. // 返回json类型
  21. private $returnJson = array('type' => 'status_code');
  22. /**
  23. * 检查登录状态
  24. */
  25. public function __construct()
  26. {
  27. // 身份验证
  28. $server = new GuestModule;
  29. if (!$server->checkLogin()) {
  30. $this->returnJson['statusCode'] = '120005';
  31. exitOutput($this->returnJson);
  32. }
  33. }
  34. /**
  35. * 添加状态码
  36. */
  37. public function addCode()
  38. {
  39. $codeLen = mb_strlen(quickInput('code'), 'utf8');
  40. $codeDescLen = mb_strlen(quickInput('codeDesc'), 'utf8');
  41. $groupID = securelyInput('groupID');
  42. $module = new StatusCodeGroupModule();
  43. $userType = $module->getUserType($groupID);
  44. if ($userType < 0 || $userType > 2) {
  45. $this->returnJson['statusCode'] = '120007';
  46. exitOutput($this->returnJson);
  47. }
  48. $code = securelyInput('code');
  49. $codeDesc = securelyInput('codeDesc');
  50. if (!preg_match('/^[0-9]{1,11}$/', $groupID)) {
  51. //分组ID格式不合法
  52. $this->returnJson['statusCode'] = '190002';
  53. } elseif (!($codeLen >= 1 && $codeLen <= 255)) {
  54. //状态码格式不合法
  55. $this->returnJson['statusCode'] = '190008';
  56. } elseif (!($codeDescLen >= 1 && $codeDescLen <= 255)) {
  57. //状态码描述格式不合法
  58. $this->returnJson['statusCode'] = '190003';
  59. } else {
  60. $service = new StatusCodeModule;
  61. $result = $service->addCode($groupID, $codeDesc, $code);
  62. if ($result) {
  63. $this->returnJson['statusCode'] = '000000';
  64. $this->returnJson['codeID'] = $result;
  65. } else {
  66. $this->returnJson['statusCode'] = '190004';
  67. }
  68. }
  69. exitOutput($this->returnJson);
  70. }
  71. /**
  72. * 删除状态码
  73. */
  74. public function deleteCode()
  75. {
  76. // 状态码ID数组
  77. $ids = quickInput('codeID');
  78. $arr = json_decode($ids);
  79. $arr = preg_grep('/^[0-9]{1,11}$/', $arr);
  80. if (empty ($arr)) {
  81. // 状态码ID格式不合法
  82. $this->returnJson ['statusCode'] = '190003';
  83. } else {
  84. $code_ids = implode(',', $arr);
  85. $service = new StatusCodeModule();
  86. $result = $service->deleteCodes($code_ids);
  87. if ($result) {
  88. //成功
  89. $this->returnJson ['statusCode'] = '000000';
  90. } else {
  91. //失败
  92. $this->returnJson ['statusCode'] = '190000';
  93. }
  94. }
  95. exitOutput($this->returnJson);
  96. }
  97. /**
  98. * 获取状态码列表
  99. */
  100. public function getCodeList()
  101. {
  102. $groupID = securelyInput('groupID');
  103. if (!preg_match('/^[0-9]{1,11}$/', $groupID)) {
  104. //分组ID格式不合法
  105. $this->returnJson['statusCode'] = '190002';
  106. } else {
  107. $service = new StatusCodeModule;
  108. $result = $service->getCodeList($groupID);
  109. if ($result) {
  110. $this->returnJson['statusCode'] = '000000';
  111. $this->returnJson['codeList'] = $result;
  112. } else {
  113. $this->returnJson['statusCode'] = '190001';
  114. }
  115. }
  116. exitOutput($this->returnJson);
  117. }
  118. /**
  119. * 获取所有状态码列表
  120. */
  121. public function getAllCodeList()
  122. {
  123. $projectID = securelyInput('projectID');
  124. if (!preg_match('/^[0-9]{1,11}$/', $projectID)) {
  125. //项目ID格式不合法
  126. $this->returnJson['statusCode'] = '190007';
  127. } else {
  128. $service = new StatusCodeModule;
  129. $result = $service->getAllCodeList($projectID);
  130. if ($result) {
  131. $this->returnJson['statusCode'] = '000000';
  132. $this->returnJson['codeList'] = $result;
  133. } else {
  134. $this->returnJson['statusCode'] = '190001';
  135. }
  136. }
  137. exitOutput($this->returnJson);
  138. }
  139. /**
  140. * 修改状态码
  141. */
  142. public function editCode()
  143. {
  144. $codeLen = mb_strlen(quickInput('code'), 'utf8');
  145. $codeDescLen = mb_strlen(quickInput('codeDesc'), 'utf8');
  146. $codeID = securelyInput('codeID');
  147. $module = new StatusCodeModule();
  148. $userType = $module->getUserType($codeID);
  149. if ($userType < 0 || $userType > 2) {
  150. $this->returnJson['statusCode'] = '120007';
  151. exitOutput($this->returnJson);
  152. }
  153. $groupID = securelyInput('groupID');
  154. $code = securelyInput('code');
  155. $codeDesc = securelyInput('codeDesc');
  156. if (!preg_match('/^[0-9]{1,11}$/', $codeID)) {
  157. //状态码ID格式非法
  158. $this->returnJson['statusCode'] = '190005';
  159. } elseif (!preg_match('/^[0-9]{1,11}$/', $groupID)) {
  160. //分组ID格式非法
  161. $this->returnJson['statusCode'] = '190002';
  162. } elseif (!($codeLen >= 1 && $codeLen <= 255)) {
  163. //状态码格式非法
  164. $this->returnJson['statusCode'] = '190008';
  165. } elseif (!($codeDescLen >= 1 && $codeDescLen <= 255)) {
  166. //状态码描述格式非法
  167. $this->returnJson['statusCode'] = '190003';
  168. } else {
  169. $service = new StatusCodeModule;
  170. $result = $service->editCode($groupID, $codeID, $code, $codeDesc);
  171. if ($result) {
  172. $this->returnJson['statusCode'] = '000000';
  173. } else {
  174. $this->returnJson['statusCode'] = '190009';
  175. }
  176. }
  177. exitOutput($this->returnJson);
  178. }
  179. /**
  180. * 搜索状态码
  181. */
  182. public function searchStatusCode()
  183. {
  184. $projectID = securelyInput('projectID');
  185. $tipsLen = mb_strlen(quickInput('tips'), 'utf8');
  186. $tips = securelyInput('tips');
  187. if (!preg_match('/^[0-9]{1,11}$/', $projectID)) {
  188. //项目ID格式不合法
  189. $this->returnJson['statusCode'] = '190007';
  190. } elseif (!($tipsLen >= 1 && $tipsLen <= 255)) {
  191. $this->returnJson['statusCode'] = '190008';
  192. } else {
  193. $service = new StatusCodeModule;
  194. $result = $service->searchStatusCode($projectID, $tips);
  195. if ($result) {
  196. $this->returnJson['statusCode'] = '000000';
  197. $this->returnJson['codeList'] = $result;
  198. } else {
  199. $this->returnJson['statusCode'] = '190001';
  200. }
  201. }
  202. exitOutput($this->returnJson);
  203. }
  204. /*
  205. * 获取状态码数量
  206. */
  207. public function getStatusCodeNum()
  208. {
  209. $projectID = securelyInput('projectID');
  210. if (!preg_match('/^[0-9]{1,11}$/', $projectID)) {
  211. //项目ID格式不合法
  212. $this->returnJson['statusCode'] = '190007';
  213. } else {
  214. $service = new StatusCodeModule;
  215. $result = $service->getStatusCodeNum($projectID);
  216. if ($result) {
  217. $this->returnJson['statusCode'] = '000000';
  218. $this->returnJson['num'] = $result['num'];
  219. } else
  220. $this->returnJson['statusCode'] = '190010';
  221. }
  222. exitOutput($this->returnJson);
  223. }
  224. /**
  225. * 通过Excel批量添加状态码
  226. */
  227. public function addStatusCodeByExcel()
  228. {
  229. quickRequire(PATH_EXTEND . 'excel/PHPExcel.php');
  230. quickRequire(PATH_EXTEND . 'excel/PHPExcel/IOFactory.php');
  231. $filename = $_FILES['excel']['tmp_name'];
  232. $group_id = securelyInput('groupID');
  233. if (!preg_match('/^[0-9]{1,11}$/', $group_id)) {
  234. //分组ID格式非法
  235. $this->returnJson['statusCode'] = '190002';
  236. } else {
  237. //检查权限
  238. $service = new StatusCodeGroupModule();
  239. $user_type = $service->getUserType($group_id);
  240. if ($user_type < 0 || $user_type > 2) {
  241. $this->returnJson['statusCode'] = '120007';
  242. } else {
  243. $status_code_list = array();
  244. try {
  245. $PHPExcel = \PHPExcel_IOFactory::load($filename);
  246. $currentSheet = $PHPExcel->getSheet(0); // 读取第一个工作簿
  247. $all_row = $currentSheet->getHighestRow(); // 所有行数
  248. for ($i = 3; $i <= $all_row; $i++) {
  249. $code = $currentSheet->getCell('A' . $i)->getValue();
  250. $code_desc = $currentSheet->getCell('B' . $i)->getValue();
  251. if (empty($code)) {
  252. continue;
  253. }
  254. $status_code_list[] = array(
  255. 'code' => $code,
  256. 'codeDesc' => $code_desc ? $code_desc : ''
  257. );
  258. }
  259. if ($status_code_list) {
  260. $service = new StatusCodeModule();
  261. $result = $service->addStatusCodeByExcel($group_id, $status_code_list);
  262. if ($result) {
  263. $this->returnJson['statusCode'] = '000000';
  264. } else {
  265. $this->returnJson['statusCode'] = '190000';
  266. }
  267. } else {
  268. //内容为空
  269. $this->returnJson['statusCode'] = '190006';
  270. }
  271. } catch (\Exception $e) {
  272. //读取Excel文件失败
  273. $this->returnJson['statusCode'] = '190005';
  274. }
  275. }
  276. }
  277. exitOutput($this->returnJson);
  278. }
  279. }
  280. ?>