You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.0 KiB

преди 3 години
  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. $login_user = $this->checkLogin();
  10. $uid = $login_user['uid'] ;
  11. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  12. if(!$this->checkItemCreator($uid , $page['item_id'])){
  13. $this->sendError(10303);
  14. return ;
  15. }
  16. $json = json_decode(htmlspecialchars_decode($template)) ;
  17. if(!$json){
  18. $this->sendError(10101,'为了服务器安全,只允许写符合json语法的字符串');
  19. return ;
  20. }
  21. $unique_key = md5(time().rand()."gbgdhbdgtfgfK3@bv45342asfsdfjhyfgkj54fofgfbv45342asfsdg");
  22. //假如已经该页面存在mock
  23. $mock_page = D("Mock")->where(" page_id = '$page_id' ")->find();
  24. if($mock_page){
  25. $unique_key = $mock_page['unique_key'] ;
  26. D("Mock")->where("page_id = '$page_id' ")->save(array(
  27. "uid"=>$uid ,
  28. "template"=>$template ,
  29. "last_update_time" => date("Y-m-d H:i:s"),
  30. ));
  31. }else{
  32. $id = D("Mock")->add(array(
  33. "unique_key"=>$unique_key ,
  34. "uid"=>$uid ,
  35. "page_id"=>$page_id ,
  36. "item_id"=> $page['item_id'] ,
  37. "template"=>$template ,
  38. "addtime" => date("Y-m-d H:i:s"),
  39. "last_update_time" => date("Y-m-d H:i:s"),
  40. "view_times"=>0
  41. ));
  42. }
  43. $this->sendResult(array(
  44. "page_id"=>$page_id ,
  45. "unique_key"=>$unique_key
  46. ));
  47. }
  48. // 根据页面id获取mock信息
  49. public function infoByPageId(){
  50. $page_id = I("page_id/d");
  51. $uid = $login_user['uid'] ;
  52. $page = D("Mock")->where(" page_id = '$page_id' ")->find();
  53. $login_user = $this->checkLogin(false);
  54. if (!$this->checkItemVisit($login_user['uid'] , $page['item_id'])) {
  55. $this->sendError(10103);
  56. return;
  57. }
  58. $this->sendResult($page);
  59. }
  60. // 根据唯一key获取mock的响应数据
  61. public function infoByKey(){
  62. $unique_key = $_REQUEST["unique_key"];
  63. $page = D("Mock")->where(" unique_key = '%s' ",array($unique_key))->find();
  64. $template = $page['template'] ;
  65. $res = http_post("http://127.0.0.1:7123/mock",array(
  66. "template"=> htmlspecialchars_decode($page['template'])
  67. ));
  68. if($res){
  69. $json = json_decode($res) ;
  70. if(!$json){
  71. $this->sendError(10101,'为了服务器安全,只允许写符合json语法的字符串');
  72. return ;
  73. }
  74. echo $res ;
  75. }else{
  76. echo "mock服务暂时不可用。网站管理员需要另行安装mock服务,详情请打开https://www.showdoc.com.cn/p/1ee8a176dd0ccc65609005f3a36c2cc7";
  77. }
  78. }
  79. }