Controller.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Heanup\App\Api;
  3. use Exception;
  4. use Heanup\App\Api\Config\GlobalConfig;
  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. return true;
  27. }
  28. /**
  29. * 输出模板,自动查找模板路径
  30. *
  31. * @param string $layout
  32. */
  33. protected function render($layout = 'Common/Layout')
  34. {
  35. header("Content-type: text/html; charset=utf-8");
  36. $this->getTemplate()->render(false, $layout);
  37. }
  38. /**
  39. * 检测缓存是否存在,缓存存在则直接输出
  40. */
  41. public function checkCache()
  42. {
  43. if ($data = Memcached::instance()->get($this->cache_key)) {
  44. header('Content-type: application/json');
  45. echo $data;
  46. die();
  47. }
  48. $this->need_cache = true;
  49. }
  50. /**
  51. * 输出json信息
  52. *
  53. * @param int $code
  54. * @param string $msg
  55. * @param array $data
  56. * @param string $callback
  57. */
  58. protected function output($code = 0, $msg = '', $data = array(), $callback = "")
  59. {
  60. $output = array(
  61. 'code' => $code,
  62. 'msg' => $msg,
  63. 'update' => date("Y-m-d H:i:s"),
  64. 'data' => $data ?: array()
  65. );
  66. header('Content-type: application/json');
  67. if ($callback) {
  68. $response = $callback . "(" . json_encode($output) . ")";
  69. } else {
  70. $response = json_encode($output, JSON_UNESCAPED_UNICODE);
  71. }
  72. if ($this->need_cache) {
  73. Memcached::instance()->set($this->cache_key, $response, ALL_CACHE_TIME);
  74. }
  75. echo $response;
  76. die();
  77. // 记录调试信息
  78. }
  79. protected function directOutput($response)
  80. {
  81. header('Content-type: application/json');
  82. if (is_array($response)) {
  83. $response = json_encode($response,JSON_UNESCAPED_UNICODE);
  84. }
  85. if ($this->need_cache) {
  86. Memcached::instance()->set($this->cache_key, $response, ALL_CACHE_TIME);
  87. }
  88. echo $response;
  89. }
  90. /**
  91. * 输出错误信息
  92. *
  93. * @param int $code
  94. * @param string $message
  95. */
  96. protected function showError($code = 0, $message = '')
  97. {
  98. $output = array(
  99. 'code' => intval($code),
  100. 'message' => $message,
  101. 'data' => []
  102. );
  103. header('Content-type: application/json');
  104. echo json_encode($output);
  105. exit();
  106. }
  107. /**
  108. * 显示成功信息
  109. * @param $data
  110. */
  111. protected function showSuccess($data = array())
  112. {
  113. $this->output(0, '请求成功', $data);
  114. }
  115. }