ToolServer.php 7.3 KB

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