helpers.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/8/10
  6. * Time: 2:09 PM
  7. */
  8. function route_class()
  9. {
  10. return strtolower(str_replace([
  11. '.',
  12. '::'
  13. ], '-', \Illuminate\Support\Facades\Route::currentRouteName()));
  14. }
  15. function dda(...$args)
  16. {
  17. foreach ($args as &$x) {
  18. if (method_exists($x, 'toArray')) {
  19. $x = $x->toArray();
  20. }
  21. }
  22. dd(...$args);
  23. }
  24. function path_to_url($path, $disk = 'public')
  25. {
  26. // 如果 image 字段本身就已经是完整的 url 就直接返回
  27. if (\Illuminate\Support\Str::startsWith($path, ['http://', 'https://'])) {
  28. return $path;
  29. }
  30. return \Illuminate\Support\Facades\Storage::disk($disk)->url($path);
  31. }
  32. function api_route($name, $v = 'v1')
  33. {
  34. return app('Dingo\Api\Routing\UrlGenerator')->version($v)->route($name);
  35. }
  36. function f_money($money, $sed = ',')
  37. {
  38. return number_format($money, 2, '.', $sed);
  39. }
  40. function php2js($arr)
  41. {
  42. return json_encode($arr, true);
  43. }
  44. function js2php($json)
  45. {
  46. return json_decode($json, true);
  47. }
  48. function arr2option($arr)
  49. {
  50. return collect($arr)->map(function ($key, $val) {
  51. return [
  52. 'label' => $key,
  53. 'value' => $val,
  54. 's_value' => (string)$val
  55. ];
  56. })->values();
  57. }
  58. function str2arr($str, $glue = ',')
  59. {
  60. return explode($glue, $str);
  61. }
  62. function arr2str($arr, $glue = ',')
  63. {
  64. return implode($glue, array_wrap($arr));
  65. }
  66. function wechat_fee($money)
  67. {
  68. return bcmul($money, 100, 0);
  69. }
  70. /**
  71. * 通过身份证号获取年龄
  72. * @param $id_card
  73. * @return bool|false|string
  74. * User: Mead
  75. */
  76. function idCard2age($id_card)
  77. {
  78. $year = substr($id_card, 6, 4);
  79. $monthDay = substr($id_card, 10, 4);
  80. $age = date('Y') - $year;
  81. if ($monthDay > date('md')) {
  82. $age--;
  83. }
  84. return $age;
  85. }
  86. function now()
  87. {
  88. return date('Y-m-d H:i:s');
  89. }
  90. /**
  91. * 计算两点地理坐标之间的距离
  92. * @param float $longitude1 起点经度
  93. * @param float $latitude1 起点纬度
  94. * @param float $longitude2 终点经度
  95. * @param float $latitude2 终点纬度
  96. * @param Int $unit 单位 1:米 2:千米
  97. * @param Int $decimal 精度 保留小数位数
  98. * @return float|int
  99. */
  100. function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit = 2, $decimal = 2)
  101. {
  102. $EARTH_RADIUS = 6370.996; // 地球半径系数
  103. $PI = 3.1415926;
  104. $radLat1 = $latitude1 * $PI / 180.0;
  105. $radLat2 = $latitude2 * $PI / 180.0;
  106. $radLng1 = $longitude1 * $PI / 180.0;
  107. $radLng2 = $longitude2 * $PI / 180.0;
  108. $a = $radLat1 - $radLat2;
  109. $b = $radLng1 - $radLng2;
  110. $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
  111. $distance = $distance * $EARTH_RADIUS * 1000;
  112. if ($unit == 2) {
  113. $distance = $distance / 1000;
  114. }
  115. $distance = round($distance, $decimal);
  116. return $distance;
  117. }
  118. /**
  119. * 手机号中间四位隐藏
  120. * @param $mobile
  121. * @return mixed
  122. * User: Mead
  123. */
  124. function mobile_hidden($mobile)
  125. {
  126. return substr_replace($mobile, '****', 3, 4);
  127. }
  128. function format_mileage($km)
  129. {
  130. if ($km >= 1) {
  131. return "{$km}km";
  132. } else {
  133. $m = $km * 1000;
  134. return "{$m}m";
  135. }
  136. }
  137. /**
  138. * 格式化分钟
  139. * @param $min
  140. * @return string
  141. * User: Mead
  142. */
  143. function format_minutes($min)
  144. {
  145. $hours = 0;
  146. if ($min >= 60) {
  147. $hours = floor($min / 60);
  148. }
  149. $minute = $min % 60;
  150. $str = '';
  151. if ($hours) {
  152. $str .= "{$hours}小时";
  153. }
  154. if ($minute) {
  155. $str .= "{$minute}分";
  156. }
  157. return $str;
  158. }
  159. /**
  160. * 重新生成订单号
  161. * @param $no
  162. * User: Mead
  163. */
  164. function order_repeat($no)
  165. {
  166. $order = str2arr($no, '-')[0];
  167. return $order . '-' . rand(1, 9);
  168. }
  169. function curl_get($url)
  170. {
  171. $curl = curl_init();
  172. //设置抓取的url
  173. curl_setopt($curl, CURLOPT_URL, $url);
  174. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设置获取的信息以文件流的形式返回,而不是直接输出
  175. $data = curl_exec($curl); //执行命令
  176. curl_close($curl); //关闭URL请求
  177. return js2php($data);
  178. }
  179. function makeNoTag($tag)
  180. {
  181. return config('bike.no_tag') . $tag;
  182. }
  183. function log_record($name, $data)
  184. {
  185. \Illuminate\Support\Facades\Log::error("==============>{$name}<==============");
  186. \Illuminate\Support\Facades\Log::error($data);
  187. \Illuminate\Support\Facades\Log::error("<==============完结==============>");
  188. }