Base.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Base.php UTF-8
  4. * 基础函数
  5. *
  6. * @date : 2017/11/16 17:23
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\common;
  13. use huolib\status\CommonStatus;
  14. use think\Log;
  15. class Base {
  16. protected $code = 200;
  17. protected $msg = '';
  18. protected $data = [];
  19. /**
  20. * 实例化
  21. *
  22. * @return static
  23. */
  24. public static function ins() {
  25. return new static();
  26. }
  27. /**
  28. * log 记录所有错误信息
  29. *
  30. * @param mixed $msg 输出的异常信息
  31. * @param string $level
  32. *
  33. */
  34. protected function errorLog($msg, $level = 'error') {
  35. Log::write($msg, $level);
  36. }
  37. /**
  38. * 返回码
  39. *
  40. * @access public
  41. * @return int
  42. */
  43. public function getCode() {
  44. return $this->code;
  45. }
  46. /**
  47. * @param int $code
  48. */
  49. public function setCode($code) {
  50. $this->code = $code;
  51. }
  52. /**
  53. * @return string
  54. */
  55. public function getMessage() {
  56. return $this->msg;
  57. }
  58. /**
  59. * @param string $msg
  60. */
  61. public function setMsg($msg) {
  62. $this->msg = $msg;
  63. }
  64. /**
  65. * @return array
  66. */
  67. public function getData() {
  68. return $this->data;
  69. }
  70. /**
  71. * @param mixed $data
  72. */
  73. public function setData($data) {
  74. $this->data = $data;
  75. }
  76. /**
  77. * 返回错误信息
  78. *
  79. * @param int $code
  80. * @param string $msg
  81. * @param array $data
  82. *
  83. * @return array
  84. */
  85. public function huoError($code = 400, $msg = '', $data = []) {
  86. return $this->huoSuccess($code, $msg, $data);
  87. }
  88. /**
  89. * 逻辑处理返回信息
  90. *
  91. * @param int $code
  92. * @param string $msg
  93. * @param array $data
  94. *
  95. * @return array
  96. */
  97. public function huoSuccess($code = 200, $msg = '', $data = []) {
  98. $_rdata['code'] = $code;
  99. $_rdata['msg'] = $msg;
  100. $_rdata['data'] = $data;
  101. return $_rdata;
  102. }
  103. /**
  104. * @param mixed $_data
  105. */
  106. public function huoReturn($_data) {
  107. return $this->huoSuccess($_data['code'], $_data['msg'], $_data['data']);
  108. }
  109. /**
  110. * @param array $data
  111. * @param string|int $key
  112. * @param string $default
  113. *
  114. * @return string
  115. */
  116. protected function getVal($data, $key, $default = '') {
  117. if (empty($key) || empty($data) || !isset($data[$key])) {
  118. return $default;
  119. }
  120. return $data[$key];
  121. }
  122. /**
  123. * @param int $code
  124. * @param array $data
  125. *
  126. * @return array
  127. */
  128. protected function retSucMsg($code, $data = []) {
  129. $_msg = CommonStatus::getMsg($code);
  130. return $this->huoSuccess($code, $_msg, $data);
  131. }
  132. /**
  133. * @param $code
  134. *
  135. * @return mixed
  136. */
  137. protected function retErrMsg($code) {
  138. $_err_msg = CommonStatus::getMsg($code);
  139. return $this->huoError($code, $_err_msg);
  140. }
  141. }