AdminCommonResponseEntity.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. * @return array
  135. */
  136. public function toResponseArray(): array
  137. {
  138. return [
  139. 'code' => $this->code,
  140. 'message' => $this->message,
  141. 'data' => $this->data,
  142. 'timestamp' => $this->timestamp
  143. ];
  144. }
  145. /**
  146. * 转换为JSON字符串
  147. * @return string
  148. */
  149. public function toJson(): string
  150. {
  151. return json_encode($this->toResponseArray(), JSON_UNESCAPED_UNICODE);
  152. }
  153. }