123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2020/6/11
- * Time: 下午5:06
- */
- namespace app\api\controller\v1;
- use app\common\helper\ArrayHelper;
- use app\main\model\object\ReturnObject;
- use app\main\service\LogService;
- use think\exception\HttpResponseException;
- use think\Request;
- use app\main\constants\ErrorCodeConstants;
- use think\Response;
- class Init
- {
- protected $format = 'json';
- protected $limited_formats = ['json', 'jsonp', 'xml'];
- /**
- * @var ReturnObject
- */
- protected $oReturn;
- public function __construct()
- {
- if ($format = Request::instance()->param('format')) {
- if (in_array($format, $this->limited_formats)) {
- $this->format = $format;
- }
- }
- $this->oReturn = new ReturnObject();
- }
- /**
- * 返回结果
- */
- public function getReturn()
- {
- $result = [
- 'code' => $this->oReturn->code,
- 'msg' => $this->oReturn->msg,
- 'time' => Request::instance()->server('REQUEST_TIME'),
- 'data' => $this->oReturn->data,
- ];
- $response = Response::create($result, $this->format);
- throw new HttpResponseException($response);
- }
- /**
- * @param $data
- * @return $this
- */
- public function setData($data)
- {
- $this->oReturn->data = $data;
- return $this;
- }
- /**
- * @param $msg
- * @return $this
- */
- public function setMsg($msg)
- {
- $this->oReturn->msg = $msg;
- return $this;
- }
- /**
- * @param $code
- * @return $this
- */
- public function setCode($code)
- {
- $this->oReturn->code = $code;
- return $this;
- }
- }
|