123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- /**
- * 统一返回处理类(ajax)允许跨域
- *
- * 使用: **
- * use Gucci\ServerResponse;
- * return ServerResponse::createBySuccess("成功");
- * ServerResponse::createByError("断点失败了");
- */
- namespace app\common\base\response;
- use think\Response;
- abstract class BaseResponse
- {
- protected $code = 0;
- protected $msg = '';
- protected $error = null;
- protected $data = null;
- public function __construct($model){
- $this->beforeInit($model);
- $this->zskkInit($model);
- $this->afterInit($model);
- }
- protected function beforeInit($model) {}
- protected function afterInit($model) {}
- protected function zskkInit($model) {
- if(is_null($model)) {
- return;
- }
- if(isset($model->code)) {
- $this->setCode($model->code);
- }
- if(isset($model->msg)) {
- $this->setMsg($model->msg);
- }
- if(isset($model->data)) {
- $this->setData($model->data);
- }
- if(isset($model->error)) {
- $this->setError($model->error);
- }
- }
- public function setData($data)
- {
- $this->data = $data;
- return $this;
- }
- /**
- * @return null
- */
- public function getData()
- {
- return $this->data;
- }
- /**
- * @param null $error
- */
- public function setError($error): void
- {
- $this->error = $error;
- }
- /**
- * @return null
- */
- public function getError()
- {
- return $this->error;
- }
- public function setMsg(string $msg)
- {
- $this->msg = $msg;
- return $this;
- }
- /**
- * @return string
- */
- public function getMsg(): string
- {
- return $this->msg;
- }
- /**
- * @param int $code
- */
- public function setCode(int $code)
- {
- $this->code = $code;
- return $this;
- }
- /**
- * @return int
- */
- public function getCode(): int
- {
- return $this->code;
- }
- protected function setResponse($code = 0, $msg = '', $data = null, $error = null) {
- $this->setCode($code);
- $this->setMsg($msg);
- $this->setData($data);
- $this->setError($error);
- return $this;
- }
- protected function generateResponse() {
- return [
- "code" => $this->getCode(),
- "msg" => $this->getMsg(),
- "data" => $this->getData(),
- "error" => $this->getError(),
- ];
- }
- }
|