123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace app\controller;
- use think\App;
- use think\Response;
- use think\exception\HttpResponseException;
- class Base extends \app\BaseController{
- public function __construct(App $app) {parent::__construct($app);}
- /**
- * @param string $message
- * @param int $code
- * @param null $data
- */
- public function error($message='',$code=0,$data=null){
- $this->result($message,$data,$code);
- }
- /**
- * @param string $message
- * @param int $code
- * @param null $data
- */
- public function success($message='',$data=null,$code=1){
- $this->result($message,$data,$code);
- }
-
- /**
- * @param string $msg
- * @param null $data
- * @param int $code
- * @param string|null $type
- * @param array $header
- * @param array $options
- */
- private function result(string $msg, $data = null, int $code = 0, string $type = null, array $header = [], array
- $options = [])
- {
- $result = [
- 'code' => $code,
- 'message' => $msg,
- 'data' => $data,
- ];
- // 如果未设置类型则自动判断
- $type = $type ?: 'json';
- $code = 200;
- if (isset($header['statuscode'])) {
- $code = $header['statuscode'];
- unset($header['statuscode']);
- }
- $response = Response::create($result, $type, $code)->header($header)->options($options);
- throw new HttpResponseException($response);
- }
-
- }
|