HelpHander.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace app\hander;
  3. class HelpHander
  4. {
  5. public static function Response($code, $msg, $data = [])
  6. {
  7. $type = gettype($data);
  8. if($type == 'object'){
  9. $data = json_decode(json_encode($data),true);
  10. }
  11. if(is_array($data)){
  12. $data = array_change_line_to_hump($data);
  13. }
  14. $json = [
  15. 'code' => $code,
  16. 'message' => $msg,
  17. 'data' => $data
  18. ];
  19. throw new \think\exception\HttpResponseException(json($json));
  20. return;
  21. }
  22. public static function success($data = [], $msg = '')
  23. {
  24. $type = gettype($data);
  25. if($type == 'object'){
  26. $data = json_decode(json_encode($data),true);
  27. }
  28. if(is_array($data)){
  29. $data = array_change_line_to_hump($data);
  30. }
  31. $json = [
  32. 'code' => 0,
  33. 'message' => $msg,
  34. 'data' => $data
  35. ];
  36. throw new \think\exception\HttpResponseException(json($json));
  37. return;
  38. }
  39. public static function error($msg, $code = 1, $data =null)
  40. {
  41. $code = $code == 0 ? 1 : $code; // code不能等于0
  42. $type = gettype($data);
  43. if($type == 'object'){
  44. $data = json_decode(json_encode($data),true);
  45. }
  46. if(is_array($data)){
  47. $data = array_change_line_to_hump($data);
  48. }
  49. $json = [
  50. 'code' => $code,
  51. 'message' => $msg,
  52. 'data' => $data
  53. ];
  54. throw new \think\exception\HttpResponseException(json($json));
  55. return;
  56. }
  57. }