MockController.class.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class MockController extends BaseController {
  5. //添加
  6. public function add(){
  7. $page_id = I("page_id/d");
  8. $template = I("template");
  9. $path = I("path") ? I("path") : '/';
  10. $login_user = $this->checkLogin();
  11. $uid = $login_user['uid'] ;
  12. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  13. if(!$this->checkItemEdit($uid , $page['item_id'])){
  14. $this->sendError(10103);
  15. return ;
  16. }
  17. if(substr($path, 0, 1) !== '/'){
  18. $path = '/' .$path ;
  19. }
  20. $item_id = $page['item_id'] ;
  21. $json = json_decode(htmlspecialchars_decode($template)) ;
  22. if(!$json){
  23. $this->sendError(10101,'为了服务器安全,只允许写符合json语法的字符串');
  24. return ;
  25. }
  26. $unique_key = md5(time().rand()."gbgdhbdgtfgfK3@bv45342asfsdfjhyfgkj54fofgfbv45342asfsdg");
  27. //假如已经该页面存在mock
  28. $mock_page = D("Mock")->where(" page_id = '$page_id' ")->find();
  29. if($mock_page){
  30. $unique_key = $mock_page['unique_key'] ;
  31. D("Mock")->where("page_id = '$page_id' ")->save(array(
  32. "uid"=>$uid ,
  33. "template"=>$template ,
  34. "path"=>$path ,
  35. "last_update_time" => date("Y-m-d H:i:s"),
  36. ));
  37. }else{
  38. $id = D("Mock")->add(array(
  39. "unique_key"=>$unique_key ,
  40. "uid"=>$uid ,
  41. "page_id"=>$page_id ,
  42. "item_id"=> $item_id ,
  43. "template"=>$template ,
  44. "path"=>$path ,
  45. "addtime" => date("Y-m-d H:i:s"),
  46. "last_update_time" => date("Y-m-d H:i:s"),
  47. "view_times"=>0
  48. ));
  49. }
  50. $this->sendResult(array(
  51. "page_id"=>$page_id ,
  52. "path"=>$path ,
  53. "unique_key"=>$unique_key
  54. ));
  55. }
  56. // 根据页面id获取mock信息
  57. public function infoByPageId(){
  58. $page_id = I("page_id/d");
  59. $uid = $login_user['uid'] ;
  60. $page = D("Mock")->where(" page_id = '$page_id' ")->find();
  61. $login_user = $this->checkLogin(false);
  62. if (!$this->checkItemVisit($login_user['uid'] , $page['item_id'])) {
  63. $this->sendError(10103);
  64. return;
  65. }
  66. $this->sendResult($page);
  67. }
  68. // 根据唯一key获取mock的响应数据
  69. public function infoByKey(){
  70. $unique_key = I("unique_key") ;
  71. $page = D("Mock")->where(" unique_key = '%s' ",array($unique_key))->find();
  72. $template = $page['template'] ;
  73. $res = http_post("http://127.0.0.1:7123/mock",array(
  74. "template"=> htmlspecialchars_decode($page['template'])
  75. ));
  76. if($res){
  77. $json = json_decode($res) ;
  78. if(!$json){
  79. $this->sendError(10101,'为了服务器安全,只允许写符合json语法的字符串');
  80. return ;
  81. }
  82. echo $res ;
  83. }else{
  84. echo "mock服务暂时不可用。网站管理员需要另行安装mock服务,详情请打开https://www.showdoc.com.cn/p/1ee8a176dd0ccc65609005f3a36c2cc7";
  85. }
  86. }
  87. // 根据item_id和path获取
  88. public function infoByPath(){
  89. // 这里如果用I("path")方法来获取参数,那么接受post请求的时候有问题。原因暂时不明。应该跟路由里使用正则有关。
  90. $item_id = $_REQUEST["item_id"] ;
  91. $path = $_REQUEST['path'] ? $_REQUEST['path']: '/' ;
  92. $page = D("Mock")->where(" item_id = '%s' and path = '%s' ",array($item_id,$path) )->find();
  93. if(!$page){
  94. echo 'no such path';
  95. return;
  96. }
  97. $template = $page['template'] ;
  98. $mock_host = env("MOCK_HOST",'127.0.0.1');
  99. $mock_port = env('MOCK_PORT','7123');
  100. $res = http_post("http://{$mock_host}:{$mock_port}/mock",array(
  101. "template"=> htmlspecialchars_decode($page['template'])
  102. ));
  103. if($res){
  104. $sql = " update mock set view_times = view_times + 1 where id = {$page['id']} ";
  105. D("Mock")->execute($sql);
  106. $json = json_decode($res) ;
  107. if(!$json){
  108. $this->sendError(10101,'为了服务器安全,只允许写符合json语法的字符串');
  109. return ;
  110. }
  111. echo $res ;
  112. }else{
  113. echo "mock服务暂时不可用。网站管理员安装完showdoc后需要另行安装mock服务,详情请打开https://www.showdoc.com.cn/p/1ee8a176dd0ccc65609005f3a36c2cc7";
  114. }
  115. }
  116. }