JsonService.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/10/24
  6. */
  7. namespace crmeb\services;
  8. class JsonService
  9. {
  10. private static $SUCCESSFUL_DEFAULT_MSG = 'ok';
  11. private static $FAIL_DEFAULT_MSG = 'no';
  12. public static function result($code, $msg = '', $data = [], $count = 0)
  13. {
  14. exit(json_encode(compact('code', 'msg', 'data', 'count')));
  15. }
  16. public static function successlayui($count = 0, $data = [], $msg = '')
  17. {
  18. if (is_array($count)) {
  19. if (isset($count['data'])) $data = $count['data'];
  20. if (isset($count['count'])) $count = $count['count'];
  21. }
  22. if (false == is_string($msg)) {
  23. $data = $msg;
  24. $msg = self::$SUCCESSFUL_DEFAULT_MSG;
  25. }
  26. return self::result(0, $msg, $data, $count);
  27. }
  28. public static function successful($msg = 'ok', $data = [], $status = 200)
  29. {
  30. if (false == is_string($msg)) {
  31. $data = $msg;
  32. $msg = self::$SUCCESSFUL_DEFAULT_MSG;
  33. }
  34. return self::result($status, $msg, $data);
  35. }
  36. public static function status($status, $msg, $result = [])
  37. {
  38. $status = strtoupper($status);
  39. if (true == is_array($msg)) {
  40. $result = $msg;
  41. $msg = self::$SUCCESSFUL_DEFAULT_MSG;
  42. }
  43. return self::result(200, $msg, compact('status', 'result'));
  44. }
  45. public static function fail($msg, $data = [], $code = 400)
  46. {
  47. if (true == is_array($msg)) {
  48. $data = $msg;
  49. $msg = self::$FAIL_DEFAULT_MSG;
  50. }
  51. return self::result($code, $msg, $data);
  52. }
  53. public static function success($msg, $data = [])
  54. {
  55. if (true == is_array($msg)) {
  56. $data = $msg;
  57. $msg = self::$SUCCESSFUL_DEFAULT_MSG;
  58. }
  59. return self::result(200, $msg, $data);
  60. }
  61. /*
  62. * 设置返回数据
  63. * @param int $code 响应code
  64. * @param string $msg 提示语
  65. * @param array $data 返回数据
  66. * @return array
  67. * */
  68. public static function returnData($code, $msg = '', $data = [])
  69. {
  70. return compact('code', 'msg', 'data');
  71. }
  72. }