Until.php 5.1 KB

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