| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace app\entity\admin\response;
- use app\entity\CommonEntity;
- /**
- * 管理员公共响应实体
- * 用于统一接口返回格式
- */
- class AdminCommonResponseEntity extends CommonEntity
- {
- /**
- * 状态码
- * @var int
- */
- public $code;
- /**
- * 消息内容
- * @var string
- */
- public $message;
- /**
- * 响应数据
- * @var mixed
- */
- public $data;
- /**
- * 时间戳
- * @var int
- */
- public $timestamp;
- /**
- * 获取状态码
- * @return int
- */
- public function getCode(): int
- {
- return $this->code;
- }
- /**
- * 设置状态码
- * @param int $code
- * @return AdminCommonResponseEntity
- */
- public function setCode(int $code): AdminCommonResponseEntity
- {
- $this->code = $code;
- return $this;
- }
- /**
- * 获取消息内容
- * @return string
- */
- public function getMessage(): string
- {
- return $this->message;
- }
- /**
- * 设置消息内容
- * @param string $message
- * @return AdminCommonResponseEntity
- */
- public function setMessage(string $message): AdminCommonResponseEntity
- {
- $this->message = $message;
- return $this;
- }
- /**
- * 获取响应数据
- * @return mixed
- */
- public function getData()
- {
- return $this->data;
- }
- /**
- * 设置响应数据
- * @param mixed $data
- * @return AdminCommonResponseEntity
- */
- public function setData($data): AdminCommonResponseEntity
- {
- $this->data = $data;
- return $this;
- }
- /**
- * 获取时间戳
- * @return int
- */
- public function getTimestamp(): int
- {
- return $this->timestamp;
- }
- /**
- * 设置时间戳
- * @param int $timestamp
- * @return AdminCommonResponseEntity
- */
- public function setTimestamp(int $timestamp): AdminCommonResponseEntity
- {
- $this->timestamp = $timestamp;
- return $this;
- }
- /**
- * 创建成功响应
- * @param mixed $data 响应数据
- * @param string $message 成功消息
- * @param int $code 状态码
- * @return AdminCommonResponseEntity
- */
- public static function success(string $message = '操作成功', int $code = 200, $data = null): AdminCommonResponseEntity
- {
- return (new self())
- ->setCode($code)
- ->setMessage($message)
- ->setData($data)
- ->setTimestamp(time());
- }
- /**
- * 创建失败响应
- * @param string $message 错误消息
- * @param int $code 状态码
- * @param mixed $data 额外数据
- * @return AdminCommonResponseEntity
- */
- public static function error(string $message = '操作失败', int $code = 400, $data = null): AdminCommonResponseEntity
- {
- return (new self())
- ->setCode($code)
- ->setMessage($message)
- ->setData($data)
- ->setTimestamp(time());
- }
- /**
- * 创建分页数据响应
- * @param array $list 数据列表
- * @param int $total 总记录数
- * @param int $page 当前页码
- * @param int $limit 每页数量
- * @param string $message 成功消息
- * @param int $code 状态码
- * @return AdminCommonResponseEntity
- */
- public static function paginate(array $list, int $total, int $page, int $limit, string $message = '获取成功', int $code = 200): AdminCommonResponseEntity
- {
- $paginationData = [
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit,
- 'total_page' => ceil($total / $limit)
- ];
- return (new self())
- ->setCode($code)
- ->setMessage($message)
- ->setData($paginationData)
- ->setTimestamp(time());
- }
- /**
- * 转换为数组格式
- * @return array
- */
- public function toResponseArray(): array
- {
- return [
- 'code' => $this->code,
- 'message' => $this->message,
- 'data' => $this->data,
- 'timestamp' => $this->timestamp
- ];
- }
- /**
- * 转换为JSON字符串
- * @return string
- */
- public function toJson(): string
- {
- return json_encode($this->toResponseArray(), JSON_UNESCAPED_UNICODE);
- }
- }
|