Controller.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Heanup\App\Api;
  3. use Exception;
  4. use Heanup\App\Api\Controller\Search;
  5. use Heanup\Library\Memcached;
  6. abstract class Controller extends \Heanup\Frame\Controller
  7. {
  8. protected $need_cache = false;
  9. /**
  10. * @var string
  11. */
  12. protected $cache_key;
  13. protected function main()
  14. {
  15. }
  16. /**
  17. * 验证登录信息
  18. * 无需验证或者单独验证的类可以覆盖该方法
  19. *
  20. * @param bool $checkLogin
  21. * @return bool
  22. * @throws Exception
  23. */
  24. protected function checkAuth($checkLogin = false)
  25. {
  26. if ($_GET['kw']){
  27. $con=new Search();
  28. $con->main();
  29. }
  30. return true;
  31. }
  32. /**
  33. * 输出模板,自动查找模板路径
  34. *
  35. * @param string $layout
  36. */
  37. protected function render($layout = 'Common/Layout')
  38. {
  39. header("Content-type: text/html; charset=utf-8");
  40. $this->getTemplate()->render(false, $layout);
  41. }
  42. /**
  43. * 检测缓存是否存在,缓存存在则直接输出
  44. */
  45. public function checkCache()
  46. {
  47. if ($data = Memcached::instance()->get($this->cache_key)) {
  48. header('Content-type: application/json');
  49. echo $data;
  50. die();
  51. }
  52. $this->need_cache = true;
  53. }
  54. /**
  55. * 输出json信息
  56. *
  57. * @param int $code
  58. * @param string $msg
  59. * @param array $data
  60. * @param string $callback
  61. */
  62. protected function output($code = 0, $msg = '', $data = array(), $callback = "")
  63. {
  64. $output = array(
  65. 'code' => $code,
  66. 'msg' => $msg,
  67. 'update' => date("Y-m-d H:i:s"),
  68. 'data' => $data ?: array()
  69. );
  70. header('Content-type: application/json');
  71. if ($callback) {
  72. $response = $callback . "(" . json_encode($output) . ")";
  73. } else {
  74. $response = json_encode($output, JSON_UNESCAPED_UNICODE);
  75. }
  76. if ($this->need_cache) {
  77. Memcached::instance()->set($this->cache_key, $response, ALL_CACHE_TIME);
  78. }
  79. echo $response;
  80. die();
  81. // 记录调试信息
  82. }
  83. protected function directOutput($response)
  84. {
  85. header('Content-type: application/json');
  86. if (is_array($response)) {
  87. $response = json_encode($response,JSON_UNESCAPED_UNICODE);
  88. }
  89. if ($this->need_cache) {
  90. Memcached::instance()->set($this->cache_key, $response, ALL_CACHE_TIME);
  91. }
  92. echo $response;
  93. }
  94. /**
  95. * 输出错误信息
  96. *
  97. * @param int $code
  98. * @param string $message
  99. */
  100. protected function showError($code = 0, $message = '')
  101. {
  102. $output = array(
  103. 'code' => intval($code),
  104. 'message' => $message,
  105. 'data' => []
  106. );
  107. header('Content-type: application/json');
  108. echo json_encode($output);
  109. exit();
  110. }
  111. /**
  112. * 显示成功信息
  113. * @param $data
  114. */
  115. protected function showSuccess($data = array())
  116. {
  117. $this->output(0, '请求成功', $data);
  118. }
  119. }