ToolServer.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace App\Server;
  3. class ToolServer
  4. {
  5. /**
  6. * curl 请求工具
  7. * @param string $method
  8. * @param string $url
  9. * @param array $params
  10. * @param bool $json
  11. * @param bool $isDecode
  12. * @return bool|mixed|string
  13. */
  14. public static function curl(string $method, string $url, array $params = [], bool $json = false, bool $isDecode = false)
  15. {
  16. $method = strtoupper($method);
  17. $ch = curl_init();
  18. curl_setopt($ch, CURLOPT_HEADER, 0);
  19. if ($method == 'GET') {
  20. $url_params = '';
  21. foreach ($params as $key => $val) {
  22. if (empty($url_params)) {
  23. $url_params .= '?';
  24. } else {
  25. $url_params .= '&';
  26. }
  27. $url_params .= $key . '=' . $val;
  28. }
  29. if (!empty($url_params)) {
  30. $url .= $url_params;
  31. }
  32. curl_setopt($ch, CURLOPT_URL, $url);
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  34. } elseif ($method == 'POST') {
  35. curl_setopt($ch, CURLOPT_URL, $url);
  36. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  37. curl_setopt($ch, CURLOPT_POST, 1);
  38. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  39. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  40. if ($json == true) {
  41. $params = json_encode($params, JSON_UNESCAPED_UNICODE);
  42. $len = strlen($params);
  43. $headers = array("Content-type: application/json;charset=UTF-8","Content-Length: $len");
  44. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  45. } else {
  46. $params = http_build_query($params);
  47. }
  48. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  49. }
  50. $data = curl_exec($ch);
  51. curl_close($ch);
  52. if ($isDecode) {
  53. $data = json_decode($data, true);
  54. }
  55. return $data;
  56. }
  57. /**
  58. * 自动收起距离单位
  59. * @param float $distance
  60. * @return float|int|string
  61. */
  62. public static function distance(float $distance)
  63. {
  64. if ($distance == -1) {
  65. return -1;
  66. }
  67. if ($distance > 1000) {
  68. $distance = round($distance / 1000, 2) . 'km';
  69. } else {
  70. $distance .= 'm';
  71. }
  72. return $distance;
  73. }
  74. /**
  75. * 获取地址距离
  76. * @param array $from [起点坐标(经纬度),例如:array(118.012951,36.810024)]
  77. * @param array $to [终点坐标(经纬度)]
  78. * @param bool $km 是否以公里为单位 false:米 true:公里(千米)
  79. * @param int $decimal $decimal 精度 保留小数位数
  80. * @return int|string 距离数值
  81. */
  82. public static function getDistance(array $from, array $to, bool $km = true, int $decimal = 2)
  83. {
  84. sort($from);
  85. sort($to);
  86. $EARTH_RADIUS = 6370.996; // 地球半径系数
  87. $distance = $EARTH_RADIUS * 2 * asin(sqrt(pow(sin(($from[0] * pi() / 180 - $to[0] * pi() / 180) / 2), 2) + cos($from[0] * pi() / 180) * cos($to[0] * pi() / 180) * pow(sin(($from[1] * pi() / 180 - $to[1] * pi() / 180) / 2), 2))) * 1000;
  88. if ($km) {
  89. $distance = $distance / 1000;
  90. }
  91. return round($distance, $decimal);
  92. }
  93. /**
  94. * 加密(可对接java)
  95. * @param array $data
  96. * @param string $key
  97. * @param string $secret
  98. * @return string
  99. */
  100. public static function encodeOld(array $data, string $key, string $secret)
  101. {
  102. $plaintext = urldecode(http_build_query($data)) . '@';
  103. $key = strtoupper(md5($key . $secret));
  104. $size = 16;
  105. $iv = str_repeat("\0", $size);
  106. $padding = $size - strlen($plaintext) % $size;
  107. $plaintext .= str_repeat(chr($padding), $padding);
  108. $encrypted = openssl_encrypt($plaintext, 'AES-192-CBC', base64_decode($key), OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
  109. return base64_encode($encrypted);
  110. }
  111. /**
  112. * 加密
  113. * @param array $data
  114. * @param string $key
  115. * @return string
  116. */
  117. public static function encode(array $data, string $key)
  118. {
  119. //数据集合转化字符串 再加个 @
  120. $plaintext = urldecode(http_build_query($data)) . '@';
  121. //md5 加密 key
  122. $key_md5 = $key;
  123. $size = 16;
  124. //生成 16 位偏移量
  125. //$iv = str_repeat("\0", $size);
  126. $iv = "0987654321098765";
  127. // 使用 PKCS5Padding 填充
  128. $padding = $size - strlen($plaintext) % $size;
  129. $plaintext .= str_repeat(chr($padding), $padding);
  130. $encrypted = openssl_encrypt($plaintext, 'AES-128-CBC', $key_md5, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
  131. return base64_encode($encrypted);
  132. }
  133. public static function encodeStr(string $data, string $key)
  134. {
  135. //数据集合转化字符串 再加个 @
  136. $plaintext = $data . '@';
  137. //md5 加密 key
  138. $key_md5 = $key;
  139. $size = 16;
  140. //生成 16 位偏移量
  141. $iv = str_repeat("\0", $size);
  142. //$iv = "0987654321098765";
  143. // 使用 PKCS5Padding 填充
  144. $padding = $size - strlen($plaintext) % $size;
  145. $plaintext .= str_repeat(chr($padding), $padding);
  146. $encrypted = openssl_encrypt($plaintext, 'AES-128-CBC', $key_md5, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
  147. return base64_encode($encrypted);
  148. }
  149. /**
  150. * 解密(可对接java)
  151. * @param string $content
  152. * @param string $key
  153. * @return array
  154. */
  155. public static function decode(string $content, string $key)
  156. {
  157. $key_md5 = $key;
  158. $size = 16;
  159. //$iv = str_repeat("\0", $size);
  160. $iv = "0987654321098765";
  161. $decrypted = openssl_decrypt(base64_decode($content), 'AES-128-CBC', $key_md5, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
  162. $data_decode = explode('@', $decrypted);
  163. //$data = [];
  164. //parse_str($data_decode[0], $data);
  165. return json_decode($data_decode[0], true);
  166. }
  167. /**
  168. * 解密(php)
  169. * @param string $content
  170. * @param string $key
  171. * @param string $secret
  172. * @return mixed
  173. */
  174. public static function decodeOld(string $content, string $key, string $secret)
  175. {
  176. $key = strtoupper(md5($key . $secret));
  177. $size = 16;
  178. $iv = str_repeat("\0", $size);
  179. $decrypted = openssl_decrypt(base64_decode($content), 'AES-192-CBC', base64_decode($key), OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
  180. $data_decode = explode('@', $decrypted);
  181. //$data = [];
  182. //parse_str($data_decode[0], $data);
  183. //return $data;
  184. return json_decode($data_decode[0], true);
  185. }
  186. public static function toStr($bytes)
  187. {
  188. $str = '';
  189. foreach ($bytes as $ch) {
  190. //dd(chr($ch));
  191. try {
  192. $str .= chr($ch);
  193. } catch (\Exception $e) {
  194. dd(chr($ch));
  195. }
  196. }
  197. return $str;
  198. }
  199. /**
  200. * 签名算法
  201. * @param array $params
  202. * @param string $secret
  203. * @return array
  204. */
  205. public static function sign(array $params, string $secret)
  206. {
  207. $p = ksort($params);
  208. $str = '';
  209. if ($p) {
  210. foreach ($params as $key => $items) {
  211. $str .= "{$key}";
  212. $str .= "=";
  213. $str .= "{$items}";
  214. $str .= "&";
  215. }
  216. }
  217. $string = substr($str, 0, -1);
  218. //$string = "appKey=$this->appKey&nonce=$this->nonce&timestamp=$this->timestamp";
  219. $stringSignTemp = "$string&secret=$secret";
  220. //echo $stringSignTemp;
  221. $sign = strtoupper(MD5($stringSignTemp));
  222. $params['sign'] = $sign;
  223. return $params;
  224. }
  225. }