Controller.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace PhpLife\App\Api;
  3. use PhpLife\App\Admin\Config\CountryList;
  4. use PhpLife\Config\McAlive;
  5. use PhpLife\Frame\Logger;
  6. use PhpLife\Frame\Request;
  7. use PhpLife\Library\Area;
  8. use PhpLife\Library\Des;
  9. use PhpLife\Library\Memcache;
  10. abstract class Controller extends \PhpLife\Frame\Controller
  11. {
  12. protected $cookie;
  13. public $cache_key;
  14. protected $need_cache = false;
  15. /**
  16. * 验证登录信息
  17. * 无需验证或者单独验证的类可以覆盖该方法
  18. *
  19. * @param bool $checkLogin
  20. * @return bool
  21. * @throws \Exception
  22. */
  23. protected function checkAuth($checkLogin = false)
  24. {
  25. return true;
  26. }
  27. /**
  28. * 输出模板,自动查找模板路径
  29. *
  30. * @param
  31. * $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. * 输出json信息
  40. *
  41. * @param int $code
  42. * @param string $msg
  43. * @param array $data
  44. * @param string $callback
  45. * @param bool $need_cache 是否需要进行全局缓存
  46. */
  47. protected function output($code = 0, $msg = '', $data = array(), $callback = "")
  48. {
  49. $output = array(
  50. 'code' => $code,
  51. 'msg' => $msg,
  52. 'data' => $data ?: array()
  53. );
  54. header('Content-type: application/json');
  55. if ($callback) {
  56. echo $response = $callback . "(" . json_encode($output) . ")";
  57. if ($this->need_cache) {
  58. Memcache::instance()->set($this->cache_key, $response, ALL_CACHE_TIME);
  59. }
  60. exit();
  61. } else {
  62. echo $response = json_encode($output);
  63. if ($this->need_cache) {
  64. Memcache::instance()->set($this->cache_key, $response, ALL_CACHE_TIME);
  65. }
  66. exit();
  67. }
  68. // 记录调试信息
  69. die();
  70. }
  71. }