Controller.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Heanup\App\Api;
  3. use Exception;
  4. abstract class Controller extends \Heanup\Frame\Controller
  5. {
  6. protected function main()
  7. {
  8. }
  9. /**
  10. * 验证登录信息
  11. * 无需验证或者单独验证的类可以覆盖该方法
  12. *
  13. * @param bool $checkLogin
  14. * @return bool
  15. * @throws Exception
  16. */
  17. protected function checkAuth($checkLogin = false)
  18. {
  19. return true;
  20. }
  21. /**
  22. * 输出模板,自动查找模板路径
  23. *
  24. * @param string $layout
  25. */
  26. protected function render($layout = 'Common/Layout')
  27. {
  28. header("Content-type: text/html; charset=utf-8");
  29. $this->getTemplate()->render(false, $layout);
  30. }
  31. /**
  32. * 输出json信息
  33. *
  34. * @param int $code
  35. * @param string $msg
  36. * @param array $data
  37. * @param string $callback
  38. */
  39. protected function output($code = 0, $msg = '', $data = array(), $callback = "")
  40. {
  41. $output = array(
  42. 'code' => $code,
  43. 'msg' => $msg,
  44. 'data' => $data ?: array()
  45. );
  46. header('Content-type: application/json');
  47. if ($callback) {
  48. echo $response = $callback . "(" . json_encode($output) . ")";
  49. exit();
  50. } else {
  51. echo $response = json_encode($output);
  52. exit();
  53. }
  54. // 记录调试信息
  55. }
  56. /**
  57. * 输出错误信息
  58. *
  59. * @param int $code
  60. * @param string $message
  61. */
  62. protected function showError($code = 0, $message = '')
  63. {
  64. $output = array(
  65. 'code' => intval($code),
  66. 'message' => $message,
  67. 'data' => []
  68. );
  69. header('Content-type: application/json');
  70. echo json_encode($output);
  71. exit();
  72. }
  73. /**
  74. * 显示成功信息
  75. * @param $data
  76. */
  77. protected function showSuccess($data = array())
  78. {
  79. $this->output(0, '请求成功', $data);
  80. }
  81. }