123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace app\common\util;
- use think\Model;
- /**
- * 通用返回值
- */
- class Result
- {
- /**
- * $code 业务码
- *
- * @var mixed
- */
- protected $code = -1;
- /**
- * $data 数据
- *
- * @var mixed
- */
- protected $data;
- /**
- * $msg 提示消息
- *
- * @var string
- */
- protected $msg = 'Never init error';
- public function __construct($data, $code = 0, $msg = '')
- {
- $this->code = $code;
- $this->data = $data;
- $this->msg = $msg;
- }
- public static function of($data, $code = 0, $msg = 'success')
- {
- return new Result($data, $code, $msg);
- }
- public static function success()
- {
- return new Result(null, 0, 'success');
- }
- public static function failed($code = -1, $msg = 'failed')
- {
- return new Result(null, $code, $msg);
- }
- public function toArray() {
- return [
- 'code' => $this->code,
- 'msg' => $this->msg,
- 'data' => $this->data,
- ];
- }
- /**
- * 生成 Http Json Result 数据
- *
- * @param mixed $data
- * @param int $code
- * @param string $msg
- *
- * @return \think\Response\Json
- */
- public static function rest($data = null, $code = 0, $msg = 'success')
- {
- return json(self::of($data, $code, $msg)->toArray());
- }
- /**
- * 生成 Http Json Result 失败的数据
- *
- * @param int $code
- * @param string $msg
- *
- * @return \think\Response\Json
- */
- public static function restf($code, $msg)
- {
- return self::rest(null, $code, $msg);
- }
- }
|