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); } }