Until.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Author: luzheng.liu
  4. * Time: 2020/8/26 16:08
  5. */
  6. namespace app\common\until;
  7. use think\Response;
  8. use think\Validate;
  9. class Until {
  10. /**
  11. * @param array $data
  12. * @param int $code
  13. * @param string $message
  14. */
  15. public static function output(array $data = [],string $message = 'success',int $code = Enum::SUCCESS_CODE) {
  16. $re = [
  17. 'code' => $code,
  18. 'message' => $message,
  19. 'data' => $data
  20. ];
  21. header('Content-Type: application/json; charset=utf-8');
  22. echo json_encode($re);
  23. die;
  24. }
  25. /**
  26. * @param string $showMsg
  27. * @param string $systemErrorMsg
  28. * @param array $data
  29. * @param int $code
  30. */
  31. public static function outputSystemError(string $showMsg = '',string $systemErrorMsg = '', $data = [], $code = Enum::THROW_ERR_CODE) {
  32. $output = ['code' => $code, 'msg' => $showMsg, 'systemErrorMsg' => $systemErrorMsg, 'data' => $data];
  33. header('Content-Type: application/json; charset=utf-8');
  34. echo json_encode($output);
  35. die;
  36. }
  37. public static function modelToArray($data):array {
  38. if (empty($data)) {
  39. return [];
  40. }
  41. return json_decode(json_encode($data), true);
  42. }
  43. public static function check(array $rule, array $data) {
  44. $validate = new Validate();
  45. if (!$validate->check($data, $rule)) {
  46. self::output([],(string)$validate->getError(),Enum::THROW_ERR_CODE);
  47. }
  48. }
  49. public static function getInput(): array {
  50. $input = file_get_contents("php://input");
  51. return json_decode($input, true);
  52. }
  53. }