ResultService.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. class ResultService
  12. {
  13. const SUCCESSFUL_CODE = 200;
  14. const FAILED_CODE = 400;
  15. protected static function getStd()
  16. {
  17. return new \StdClass();
  18. }
  19. /**
  20. * 成功结果
  21. * @param string $msg
  22. * @param array $data
  23. * @param string $defaultMsg
  24. * @return StdClass
  25. */
  26. public static function successful($msg = 'ok', $data = [], $defaultMsg = 'ok')
  27. {
  28. if(is_array($msg)){
  29. $data = $msg;
  30. $msg = $defaultMsg;
  31. }
  32. if(is_callable($data)) $data = $data();
  33. $result = self::getStd();
  34. $result->code = self::SUCCESSFUL_CODE;
  35. $result->meg = $msg;
  36. $result->data = $data;
  37. return $result;
  38. }
  39. /**
  40. * 失败结果
  41. * @param $msg
  42. * @param array $data
  43. * @return StdClass
  44. */
  45. public static function failed($msg, $data = [])
  46. {
  47. $result = self::getStd();
  48. if(is_callable($data)) $data = $data();
  49. $result->code = self::FAILED_CODE;
  50. $result->meg = $msg;
  51. $result->data = $data;
  52. return $result;
  53. }
  54. }