AdminCommonResponseEntity.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace app\entity\admin\response;
  3. use app\entity\CommonEntity;
  4. /**
  5. * 管理员公共响应实体
  6. * 用于统一接口返回格式
  7. */
  8. class AdminCommonResponseEntity extends CommonEntity
  9. {
  10. /**
  11. * 状态码
  12. * @var int
  13. */
  14. public $code;
  15. /**
  16. * 消息内容
  17. * @var string
  18. */
  19. public $message;
  20. /**
  21. * 响应数据
  22. * @var mixed
  23. */
  24. public $data;
  25. /**
  26. * 时间戳
  27. * @var int
  28. */
  29. public $timestamp;
  30. /**
  31. * 获取状态码
  32. * @return int
  33. */
  34. public function getCode(): int
  35. {
  36. return $this->code;
  37. }
  38. /**
  39. * 设置状态码
  40. * @param int $code
  41. * @return AdminCommonResponseEntity
  42. */
  43. public function setCode(int $code): AdminCommonResponseEntity
  44. {
  45. $this->code = $code;
  46. return $this;
  47. }
  48. /**
  49. * 获取消息内容
  50. * @return string
  51. */
  52. public function getMessage(): string
  53. {
  54. return $this->message;
  55. }
  56. /**
  57. * 设置消息内容
  58. * @param string $message
  59. * @return AdminCommonResponseEntity
  60. */
  61. public function setMessage(string $message): AdminCommonResponseEntity
  62. {
  63. $this->message = $message;
  64. return $this;
  65. }
  66. /**
  67. * 获取响应数据
  68. * @return mixed
  69. */
  70. public function getData()
  71. {
  72. return $this->data;
  73. }
  74. /**
  75. * 设置响应数据
  76. * @param mixed $data
  77. * @return AdminCommonResponseEntity
  78. */
  79. public function setData($data): AdminCommonResponseEntity
  80. {
  81. $this->data = $data;
  82. return $this;
  83. }
  84. /**
  85. * 获取时间戳
  86. * @return int
  87. */
  88. public function getTimestamp(): int
  89. {
  90. return $this->timestamp;
  91. }
  92. /**
  93. * 设置时间戳
  94. * @param int $timestamp
  95. * @return AdminCommonResponseEntity
  96. */
  97. public function setTimestamp(int $timestamp): AdminCommonResponseEntity
  98. {
  99. $this->timestamp = $timestamp;
  100. return $this;
  101. }
  102. /**
  103. * 创建成功响应
  104. * @param mixed $data 响应数据
  105. * @param string $message 成功消息
  106. * @param int $code 状态码
  107. * @return AdminCommonResponseEntity
  108. */
  109. public static function success(string $message = '操作成功', int $code = 200, $data = null): AdminCommonResponseEntity
  110. {
  111. return (new self())
  112. ->setCode($code)
  113. ->setMessage($message)
  114. ->setData($data)
  115. ->setTimestamp(time());
  116. }
  117. /**
  118. * 创建失败响应
  119. * @param string $message 错误消息
  120. * @param int $code 状态码
  121. * @param mixed $data 额外数据
  122. * @return AdminCommonResponseEntity
  123. */
  124. public static function error(string $message = '操作失败', int $code = 400, $data = null): AdminCommonResponseEntity
  125. {
  126. return (new self())
  127. ->setCode($code)
  128. ->setMessage($message)
  129. ->setData($data)
  130. ->setTimestamp(time());
  131. }
  132. /**
  133. * 创建分页数据响应
  134. * @param array $list 数据列表
  135. * @param int $total 总记录数
  136. * @param int $page 当前页码
  137. * @param int $limit 每页数量
  138. * @param string $message 成功消息
  139. * @param int $code 状态码
  140. * @return AdminCommonResponseEntity
  141. */
  142. public static function paginate(array $list, int $total, int $page, int $limit, string $message = '获取成功', int $code = 200): AdminCommonResponseEntity
  143. {
  144. $paginationData = [
  145. 'list' => $list,
  146. 'total' => $total,
  147. 'page' => $page,
  148. 'limit' => $limit,
  149. 'total_page' => ceil($total / $limit)
  150. ];
  151. return (new self())
  152. ->setCode($code)
  153. ->setMessage($message)
  154. ->setData($paginationData)
  155. ->setTimestamp(time());
  156. }
  157. /**
  158. * 转换为数组格式
  159. * @return array
  160. */
  161. public function toResponseArray(): array
  162. {
  163. return [
  164. 'code' => $this->code,
  165. 'message' => $this->message,
  166. 'data' => $this->data,
  167. 'timestamp' => $this->timestamp
  168. ];
  169. }
  170. /**
  171. * 转换为JSON字符串
  172. * @return string
  173. */
  174. public function toJson(): string
  175. {
  176. return json_encode($this->toResponseArray(), JSON_UNESCAPED_UNICODE);
  177. }
  178. }