1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?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 = '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);
- }
-
- }
|