BaseErrorModel.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace app\common\base\error\model;
  3. abstract class BaseErrorModel {
  4. protected $code = 0;
  5. protected $msg = '';
  6. protected $error = null;
  7. protected $data = null;
  8. public function __construct(int $code = 0,string $msg = '', $data = null, $error = null,bool $single = true) {
  9. $this->beforeInit($code, $msg, $data , $error, $single);
  10. $this->zskkInit($code, $msg, $data , $error, $single );
  11. $this->afterInit($code, $msg, $data , $error, $single);
  12. }
  13. protected function beforeInit(int $code = 0,string $msg = '', $data = null, $error = null,bool $single = true) { }
  14. protected function afterInit(int $code = 0,string $msg = '', $data = null, $error = null,bool $single = true) { }
  15. protected function zskkInit(int $code, string $msg, $data, $error, bool $single) {
  16. if($single) {
  17. $this->defaultInit();
  18. } else {
  19. $this->customInit($code, $msg, $data , $error);
  20. }
  21. }
  22. private function defaultInit() {
  23. $this->setResponse($this->code, $this->msg, $this->data , $this->error);
  24. }
  25. private function customInit(int $code, string $msg, $data, $error) {
  26. $this->setResponse($code, $msg, $data , $error);
  27. }
  28. public function setResponse($code = 0, $msg = '', $data = null, $error = null) {
  29. $this->setCode($code);
  30. $this->setMsg($msg);
  31. $this->setData($data);
  32. $this->setError($error);
  33. }
  34. public function getResponse() {
  35. return [
  36. "code" => $this->getCode(),
  37. "msg" => $this->getMsg(),
  38. "data" => $this->getData(),
  39. "error" => $this->getError(),
  40. "timestamp" => $this->getTimestamp()
  41. ];
  42. }
  43. private function getTimestamp() {
  44. return time();
  45. }
  46. /**
  47. * @return int
  48. */
  49. public function getCode(): int
  50. {
  51. return $this->code;
  52. }
  53. /**
  54. * @param int $code
  55. */
  56. public function setCode(int $code)
  57. {
  58. $this->code = $code;
  59. return $this;
  60. }
  61. /**
  62. * @return string
  63. */
  64. public function getMsg(): string
  65. {
  66. return $this->msg;
  67. }
  68. /**
  69. * @param string $msg
  70. */
  71. public function setMsg(string $msg)
  72. {
  73. $this->msg = $msg;
  74. return $this;
  75. }
  76. /**
  77. * @return null
  78. */
  79. public function getError()
  80. {
  81. return $this->error;
  82. }
  83. /**
  84. * @param null $error
  85. */
  86. public function setError($error)
  87. {
  88. $this->error = $error;
  89. return $this;
  90. }
  91. /**
  92. * @return null
  93. */
  94. public function getData()
  95. {
  96. return $this->data;
  97. }
  98. /**
  99. * @param null $data
  100. */
  101. public function setData($data)
  102. {
  103. $this->data = $data;
  104. return $this;
  105. }
  106. }