Common.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace addons\service\library;
  3. use fast\Random;
  4. class Common
  5. {
  6. /**
  7. * 计算距离
  8. * @param $lng1
  9. * @param $lat1
  10. * @param $lng2
  11. * @param $lat2
  12. * @return string
  13. */
  14. public static function distance($lng1,$lat1,$lng2,$lat2)
  15. {
  16. $radLat1 = deg2rad((float)$lat1);
  17. $radLat2 = deg2rad((float)$lat2);
  18. $radLng1 = deg2rad((float)$lng1);
  19. $radLng2 = deg2rad((float)$lng2);
  20. $a = $radLat1-$radLat2;
  21. $b = $radLng1-$radLng2;
  22. $s = 2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137*1000;
  23. return sprintf("%.2f",round($s,2)/1000);
  24. }
  25. /**
  26. * 高德地图获取城市地址信息
  27. * @param $params
  28. * @return mixed
  29. */
  30. public static function getAreaList($params)
  31. {
  32. $config = \app\api\model\service\ProjectConfigure::getProjectConfig();
  33. $url = "https://restapi.amap.com/v3/place/text";
  34. $requestUrl = $url . "?key=" . $config['gaodekey'] . "&keywords=" . $params['name']. "&city=" . $params['city'].'&citylimit=true&children=1';
  35. $curl = curl_init($requestUrl);
  36. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  37. // 添加 SSL 配置解决 HTTPS 请求问题
  38. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书验证
  39. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 跳过主机验证
  40. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // 允许重定向
  41. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时时间30秒
  42. // 添加 User-Agent
  43. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');
  44. $response = curl_exec($curl);
  45. // 添加错误处理
  46. if (curl_errno($curl)) {
  47. $error = curl_error($curl);
  48. curl_close($curl);
  49. throw new \Exception("cURL Error: " . $error);
  50. }
  51. curl_close($curl);
  52. return json_decode($response, true);
  53. }
  54. /**
  55. * 创建二维码
  56. * @param $str
  57. * @return string
  58. */
  59. public static function createQrcode($str)
  60. {
  61. require_once ROOT_PATH.'addons/service/library/qrcode/phpqrcode.php';
  62. $path ='/public/uploads/qrcode/'.date("Y-m-d").'/';
  63. if (!is_dir(ROOT_PATH.$path))
  64. {
  65. mkdir(ROOT_PATH.$path,0777,true);
  66. }
  67. $file = "/uploads/qrcode/".date("Y-m-d").'/'.time().Random::alnum().".png";
  68. $fileName = $_SERVER['DOCUMENT_ROOT'].$file;
  69. $img = ImageCreate(900,900);
  70. $color = ImageColorAllocate($img,255,255,255);
  71. ImageFill($img,0,0,$color);
  72. imagepng($img,$fileName);
  73. $link = $str;
  74. $errorCorrectionLevel = "L";
  75. $matrixPointSize = "4";
  76. \QRcode::png($link, $fileName, $errorCorrectionLevel, $matrixPointSize);
  77. return $file;
  78. }
  79. public static function getDistrict($areaData)
  80. {
  81. // 按照首字母拼音排序
  82. $district = array();
  83. foreach( $areaData as $name )
  84. {
  85. $snameFirstChar = $name['first'];
  86. $district[$snameFirstChar][] =$name;
  87. }
  88. ksort($district);//排序
  89. return $district;
  90. }
  91. }