MockController.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 MockController
  19. {
  20. /**
  21. * 返回示例结果(简易mock)
  22. */
  23. public function simple()
  24. {
  25. header('Access-Control-Allow-Origin:*');
  26. header('Access-Control-Allow-Methods:POST,GET,PUT,DELETE,PATCH,OPTIONS');
  27. header('Access-Control-Allow-Headers:x-requested-with,content-type,x-custom-header,Accept,Authorization,other_header,x-csrf-token');
  28. header("Content-type: text/html; charset=UTF-8");
  29. $project_id = $_GET['projectID'];
  30. $result_type = $_GET['resultType'] ? $_GET['resultType'] : 'success';
  31. $type = array(
  32. 'POST' => '0',
  33. 'GET' => '1',
  34. 'PUT' => '2',
  35. 'DELETE' => '3',
  36. 'HEAD' => '4',
  37. 'OPTIONS' => '5',
  38. 'PATCH' => '6'
  39. );
  40. $request_type = $type[$_SERVER['REQUEST_METHOD']];
  41. $api_uri = $_GET['uri'];
  42. $service = new MockModule();
  43. switch ($result_type) {
  44. case 'success':
  45. {
  46. $result = $service->success($project_id, $api_uri, $request_type);
  47. break;
  48. }
  49. case 'failure':
  50. {
  51. $result = $service->failure($project_id, $api_uri, $request_type);
  52. break;
  53. }
  54. default:
  55. {
  56. exit('error result type.');
  57. }
  58. }
  59. if ($result) {
  60. exit($result);
  61. } else {
  62. exit('找不到mock数据: (1)服务端未设置mock支持 (2)POST请求不支持直接在浏览器输入地址请求');
  63. }
  64. }
  65. /**
  66. * 获取高级mock结果
  67. */
  68. public function mock()
  69. {
  70. header('Access-Control-Allow-Origin:*');
  71. header('Access-Control-Allow-Methods:POST,GET,PUT,DELETE,PATCH,OPTIONS');
  72. header('Access-Control-Allow-Headers:x-requested-with,content-type,x-custom-header,Accept,Authorization,other_header,x-csrf-token');
  73. header("Content-type: application/json; charset=UTF-8");
  74. $project_id = $_GET['projectID'];
  75. $type = array(
  76. 'POST' => '0',
  77. 'GET' => '1',
  78. 'PUT' => '2',
  79. 'DELETE' => '3',
  80. 'HEAD' => '4',
  81. 'OPTIONS' => '5',
  82. 'PATCH' => '6'
  83. );
  84. $request_type = $type[$_SERVER['REQUEST_METHOD']];
  85. $api_uri = $_GET['uri'];
  86. $module = new MockModule();
  87. $result = $module->getMockResult($project_id, $api_uri, $request_type);
  88. if ($result) {
  89. $decoded_result = htmlspecialchars_decode($result);
  90. if ($decoded_result) {
  91. exit($decoded_result);
  92. }
  93. exit($result);
  94. } else {
  95. exit('找不到mock数据: (1)服务端未设置mock支持 (2)POST请求不支持直接在浏览器输入地址请求');
  96. }
  97. }
  98. }