| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace Heanup\App\Api;
- use Exception;
- abstract class Controller extends \Heanup\Frame\Controller
- {
- protected function main()
- {
- }
- /**
- * 验证登录信息
- * 无需验证或者单独验证的类可以覆盖该方法
- *
- * @param bool $checkLogin
- * @return bool
- * @throws Exception
- */
- protected function checkAuth($checkLogin = false)
- {
- return true;
- }
- /**
- * 输出模板,自动查找模板路径
- *
- * @param string $layout
- */
- protected function render($layout = 'Common/Layout')
- {
- header("Content-type: text/html; charset=utf-8");
- $this->getTemplate()->render(false, $layout);
- }
- /**
- * 输出json信息
- *
- * @param int $code
- * @param string $msg
- * @param array $data
- * @param string $callback
- */
- protected function output($code = 0, $msg = '', $data = array(), $callback = "")
- {
- $output = array(
- 'code' => $code,
- 'msg' => $msg,
- 'data' => $data ?: array()
- );
- header('Content-type: application/json');
- if ($callback) {
- echo $response = $callback . "(" . json_encode($output) . ")";
- exit();
- } else {
- echo $response = json_encode($output);
- exit();
- }
- // 记录调试信息
- }
- /**
- * 输出错误信息
- *
- * @param int $code
- * @param string $message
- */
- protected function showError($code = 0, $message = '')
- {
- $output = array(
- 'code' => intval($code),
- 'message' => $message,
- 'data' => []
- );
- header('Content-type: application/json');
- echo json_encode($output);
- exit();
- }
- /**
- * 显示成功信息
- * @param $data
- */
- protected function showSuccess($data = array())
- {
- $this->output(0, '请求成功', $data);
- }
- }
|