Until.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Author: luzheng.liu
  4. * Time: 2020/8/26 16:08
  5. */
  6. namespace app\common\until;
  7. use think\Db;
  8. use think\facade\Request;
  9. use think\Response;
  10. use think\Validate;
  11. class Until {
  12. public static $userId = 0;
  13. public static $isAdmin = false;
  14. public static $startTime = 0;
  15. public static $endTime = 0;
  16. /**
  17. * @param array $data
  18. * @param int $code
  19. * @param string $message
  20. */
  21. public static function output(array $data = [], string $message = 'success', int $code = Enum::SUCCESS_CODE) {
  22. $data = self::convertUnderlineArray($data);
  23. $re = [
  24. 'code' => $code,
  25. 'message' => $message,
  26. 'data' => $data
  27. ];
  28. self::addLog($re);
  29. header('Content-Type: application/json; charset=utf-8');
  30. echo json_encode($re);
  31. die;
  32. }
  33. /**
  34. * @param string $showMsg
  35. * @param string $systemErrorMsg
  36. * @param array $data
  37. * @param int $code
  38. */
  39. public static function outputSystemError(string $showMsg = '', string $systemErrorMsg = '', $data = [], $code = Enum::THROW_ERR_CODE) {
  40. $output = ['code' => $code, 'msg' => $showMsg, 'systemErrorMsg' => $systemErrorMsg, 'data' => $data];
  41. header('Content-Type: application/json; charset=utf-8');
  42. echo json_encode($output);
  43. die;
  44. }
  45. public static function modelToArray($data): array {
  46. if (empty($data)) {
  47. return [];
  48. }
  49. return json_decode(json_encode($data), true);
  50. }
  51. public static function check(array $rule, array $data) {
  52. $validate = new Validate();
  53. if (!$validate->check($data, $rule)) {
  54. self::output([], (string)$validate->getError(), Enum::THROW_ERR_CODE);
  55. }
  56. }
  57. public static function getInput(): array {
  58. self::$startTime = time();
  59. $input = file_get_contents("php://input");
  60. $data = json_decode($input, true);
  61. if (empty($data)) {
  62. return input();
  63. }
  64. return json_decode($input, true);
  65. }
  66. /**
  67. * 公共下划线转驼峰(新)
  68. * 由于convertUnderlineArray会默认把字段值的类型全部转成string,再转回来以后,会造成类型的丢失.
  69. * 所以这个新的转换方法,默认不转换.需要的时候才会去转换. 这样就不会有来回转换造成类型丢失的问题了.
  70. * @param array $arr
  71. * @param bool $ucFirst 首字母大写
  72. * @param bool $strictMode 严格模式: 严格区分int和字符串,默认为严格模式
  73. * @return array
  74. */
  75. public static function convertUnderlineArray($arr = [], $ucFirst = false, $strictMode = true): array {
  76. if (empty($arr)) {
  77. return [];
  78. }
  79. $newArray = [];
  80. foreach ($arr as $key => $value) {
  81. $str = ucwords(str_replace('_', ' ', $key));
  82. $str = str_replace(' ', '', lcfirst($str));
  83. $str = $ucFirst ? ucfirst($str) : $str;
  84. //如果是数组重构这个数组用递归的方法
  85. if (is_array($value)) {
  86. $newArray[$str] = self::convertUnderlineArray($value, $ucFirst, $strictMode);
  87. continue;
  88. }
  89. // 非严格模式下全部转成字符串
  90. if ($strictMode === false) {
  91. $newArray[$str] = (string)$value;
  92. continue;
  93. }
  94. $newArray[$str] = $value ?? '';
  95. }
  96. return $newArray;
  97. }
  98. public static function returnSquarePoint($lng, $lat, $distance = 99) {
  99. $dlng = 2 * asin(sin($distance / (2 * 6371)) / cos(deg2rad($lat)));
  100. $dlng = rad2deg($dlng);
  101. $dlat = $distance / 6371;
  102. $dlat = rad2deg($dlat);
  103. return [
  104. 'left-top' => ['lat' => $lat + $dlat, 'lng' => $lng - $dlng],
  105. 'right-top' => ['lat' => $lat + $dlat, 'lng' => $lng + $dlng],
  106. 'left-bottom' => ['lat' => $lat - $dlat, 'lng' => $lng - $dlng],
  107. 'right-bottom' => ['lat' => $lat - $dlat, 'lng' => $lng + $dlng]
  108. ];
  109. }
  110. /**
  111. * 生成订单编号
  112. * @return string
  113. */
  114. public static function createSn(): string {
  115. return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid('', true), 7, 13), 1))), 0, 12);
  116. }
  117. public static function addLog($re) {
  118. try {
  119. Db::table('log')->insert([
  120. // 'admin_id' => self::$adminId,
  121. 'url' => Request::server('REQUEST_SCHEME').'://'.Request::host().Request::url(),
  122. 'token' => Request::header('token') ?? '',
  123. 'input' => file_get_contents("php://input") ?: json_encode(Request::input()),
  124. 'output' => json_encode($re),
  125. 'time' => time() - self::$startTime
  126. ]);
  127. } catch (\Exception $e) {
  128. throw new \RuntimeException($e->getMessage());
  129. }
  130. }
  131. public static function msectime() {
  132. list($msec, $sec) = explode(' ', microtime());
  133. return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  134. }
  135. }