functions.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-9-23
  6. * Time: 上午10:56
  7. */
  8. use Illuminate\Support\Facades\Log;
  9. use App\Exceptions\ExitOutException;
  10. use App\Models\User;
  11. //统一输出格式话的json数据
  12. if (!function_exists('out')) {
  13. function out($data = null, $status = 0, $message = 'success', $exceptionData = false)
  14. {
  15. $out = ['status' => $status, 'message' => $message, 'data' => $data];
  16. if ($exceptionData !== false) {
  17. trace([$message => $exceptionData], 'error');
  18. }
  19. return response()->json($out);
  20. }
  21. }
  22. //统一异常输出格式话的json数据
  23. if (!function_exists('exit_out')) {
  24. function exit_out($data = null, $status = 0, $message = 'success', $exceptionData = false)
  25. {
  26. $out = ['status' => $status, 'message' => $message, 'data' => $data];
  27. if ($exceptionData !== false) {
  28. trace([$message => $exceptionData], 'error');
  29. }
  30. $json = json_encode($out, JSON_UNESCAPED_UNICODE);
  31. throw new ExitOutException($json);
  32. }
  33. }
  34. //日志记录
  35. if (!function_exists('trace')) {
  36. function trace($log = '', $level = 'info')
  37. {
  38. Log::log($level, $log);
  39. }
  40. }
  41. //AES加密
  42. if (!function_exists('aes_encrypt')) {
  43. function aes_encrypt($data)
  44. {
  45. if (is_array($data)) {
  46. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  47. }
  48. $key = config('config.aes_key');
  49. $iv = config('config.aes_iv');
  50. $cipher_text = openssl_encrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
  51. $cipher_text = base64_encode($cipher_text);
  52. return urlencode($cipher_text);
  53. }
  54. }
  55. //AES解密
  56. if (!function_exists('aes_decrypt')) {
  57. function aes_decrypt($encryptData)
  58. {
  59. $encryptData = urldecode($encryptData);
  60. $encryptData = base64_decode($encryptData);
  61. $key = config('config.aes_key');
  62. $iv = config('config.aes_iv');
  63. $original_plaintext = openssl_decrypt($encryptData, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
  64. return json_decode($original_plaintext, true);
  65. }
  66. }
  67. //获取distance的sql字段
  68. if (!function_exists('get_distance_field')) {
  69. function get_distance_field($latitude, $longitude)
  70. {
  71. if (empty($latitude) || empty($longitude)) {
  72. return '999999999 distance';
  73. }
  74. return 'if(longitude=0 and latitude=0,999999999,round(6378.138*2*asin(sqrt(pow(sin( (' . $latitude . '*pi()/180-latitude*pi()/180)/2),2)+cos(' . $latitude . '*pi()/180)*cos(latitude*pi()/180)* pow(sin((' . $longitude . '*pi()/180-longitude*pi()/180)/2),2)))*1000)) distance';
  75. }
  76. }
  77. //构建单号
  78. if (!function_exists('build_sn')) {
  79. function build_sn($id, $len = 2, $prefix = '')
  80. {
  81. $idx = 0 - $len;
  82. $suffix = substr($id, $idx);
  83. $suffix = str_pad($suffix, $len, '0', STR_PAD_LEFT);
  84. $sn = $prefix.substr(date('YmdHis'), 2).$suffix;
  85. return $sn;
  86. }
  87. }
  88. //生日转年龄
  89. if (!function_exists('birthday_to_age')){
  90. function birthday_to_age($birthday)
  91. {
  92. list($year, $month, $day) = explode("-", $birthday);
  93. $year_diff = (date("Y") - $year) > 0 ? date("Y") - $year.'岁':'';
  94. $month_diff = (date("m") - $month) > 0 ? date("m") - $month.'个月':'';
  95. $day_diff = (date("d") - $day) > 0 ? date("d") - $day.'天':'';
  96. if ($day_diff < 0 || $month_diff < 0) {
  97. $year_diff--;
  98. }
  99. return $year_diff.$month_diff.$day_diff ;
  100. }
  101. }
  102. //计算经纬度两点之间距离(返回为米)
  103. if (!function_exists('get_distance')) {
  104. function get_distance($lat1, $lng1, $lat2, $lng2)
  105. {
  106. if (empty($lat1) || empty($lng1) || empty($lat2) || empty($lng2)) {
  107. return '999999999';
  108. }
  109. $earthRadius = 6378138;
  110. $lat1 = ($lat1 * pi()) / 180;
  111. $lng1 = ($lng1 * pi()) / 180;
  112. $lat2 = ($lat2 * pi()) / 180;
  113. $lng2 = ($lng2 * pi()) / 180;
  114. $calcLongitude = $lng2 - $lng1;
  115. $calcLatitude = $lat2 - $lat1;
  116. $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
  117. $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
  118. $calculatedDistance = $earthRadius * $stepTwo;
  119. return number_format($calculatedDistance, 2, '.', '');
  120. }
  121. }
  122. //获取用户坐标
  123. if (!function_exists('get_user_coordinate')) {
  124. function get_user_coordinate($user)
  125. {
  126. $req = request()->post();
  127. if (empty($req['latitude']) || empty($req['longitude'])) {
  128. $latitude = $user['latitude'];
  129. $longitude = $user['longitude'];
  130. }
  131. else {
  132. $latitude = $req['latitude'];
  133. $longitude = $req['longitude'];
  134. }
  135. return ['latitude' => $latitude, 'longitude' => $longitude];
  136. }
  137. }
  138. //获取用户距离
  139. if (!function_exists('get_user_distance')) {
  140. function get_user_distance($user, $lat, $lng)
  141. {
  142. $coordinate = get_user_coordinate($user);
  143. $data = get_distance($coordinate['latitude'], $coordinate['longitude'], $lat, $lng);
  144. return $data;
  145. }
  146. }