Until.php 5.2 KB

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