123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566 |
- <?php
- namespace App\Server;
- use Illuminate\Support\Arr;
- use \Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Storage;
- class ToolServer
- {
- /**
- * curl 请求工具
- * @param string $method
- * @param string $url
- * @param array $params
- * @param bool $json
- * @param bool $isDecode
- * @return bool|mixed|string
- */
- public static function curl(string $method, string $url, array $params = [], bool $json = false, bool $isDecode = false)
- {
- $method = strtoupper($method);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HEADER, 0);
- if ($method == 'GET') {
- $url_params = '';
- foreach ($params as $key => $val) {
- if (empty($url_params)) {
- $url_params .= '?';
- } else {
- $url_params .= '&';
- }
- $url_params .= $key . '=' . $val;
- }
- if (!empty($url_params)) {
- $url .= $url_params;
- }
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- } elseif ($method == 'POST') {
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- if ($json == true) {
- $params = json_encode($params, JSON_UNESCAPED_UNICODE);
- $len = strlen($params);
- $headers = array("Content-type: application/json;charset=UTF-8","Content-Length: $len");
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- } else {
- $params = http_build_query($params);
- }
- curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
- }
- $data = curl_exec($ch);
- curl_close($ch);
- if ($isDecode) {
- $data = json_decode($data, true);
- }
- return $data;
- }
- /**
- * 自动收起距离单位
- * @param float $distance
- * @return float|int|string
- */
- public static function distance(float $distance)
- {
- if ($distance == -1) {
- return -1;
- }
- if ($distance > 1000) {
- $distance = round($distance / 1000, 2) . 'km';
- } else {
- $distance .= 'm';
- }
- return $distance;
- }
- /**
- * 获取地址距离
- * @param array $from [起点坐标(经纬度),例如:array(118.012951,36.810024)]
- * @param array $to [终点坐标(经纬度)]
- * @param bool $km 是否以公里为单位 false:米 true:公里(千米)
- * @param int $decimal $decimal 精度 保留小数位数
- * @return int|string 距离数值
- */
- public static function getDistance(array $from, array $to, bool $km = true, int $decimal = 2)
- {
- sort($from);
- sort($to);
- $EARTH_RADIUS = 6370.996; // 地球半径系数
- $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;
- if ($km) {
- $distance = $distance / 1000;
- }
- return round($distance, $decimal);
- }
- /**
- * 加密(可对接java)
- * @param array $data
- * @param string $key
- * @param string $secret
- * @return string
- */
- public static function encodeOld(array $data, string $key, string $secret)
- {
- $plaintext = urldecode(http_build_query($data)) . '@';
- $key = strtoupper(md5($key . $secret));
- $size = 16;
- $iv = str_repeat("\0", $size);
- $padding = $size - strlen($plaintext) % $size;
- $plaintext .= str_repeat(chr($padding), $padding);
- $encrypted = openssl_encrypt($plaintext, 'AES-192-CBC', base64_decode($key), OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
- return base64_encode($encrypted);
- }
- /**
- * 加密
- * @param array $data
- * @param string $key
- * @return string
- */
- public static function encode(array $data, string $key)
- {
- //数据集合转化字符串 再加个 @
- $plaintext = urldecode(http_build_query($data)) . '@';
- //md5 加密 key
- $key_md5 = $key;
- $size = 16;
- //生成 16 位偏移量
- //$iv = str_repeat("\0", $size);
- $iv = "0987654321098765";
- // 使用 PKCS5Padding 填充
- $padding = $size - strlen($plaintext) % $size;
- $plaintext .= str_repeat(chr($padding), $padding);
- $encrypted = openssl_encrypt($plaintext, 'AES-128-CBC', $key_md5, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
- return base64_encode($encrypted);
- }
- public static function encodeStr(string $data, string $key)
- {
- //数据集合转化字符串 再加个 @
- $plaintext = $data . '@';
- //md5 加密 key
- $key_md5 = $key;
- $size = 16;
- //生成 16 位偏移量
- $iv = str_repeat("\0", $size);
- //$iv = "0987654321098765";
- // 使用 PKCS5Padding 填充
- $padding = $size - strlen($plaintext) % $size;
- $plaintext .= str_repeat(chr($padding), $padding);
- $encrypted = openssl_encrypt($plaintext, 'AES-128-CBC', $key_md5, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
- return base64_encode($encrypted);
- }
- /**
- * 解密(可对接java)
- * @param string $content
- * @param string $key
- * @return array
- */
- public static function decode(string $content, string $key)
- {
- $key_md5 = $key;
- $size = 16;
- //$iv = str_repeat("\0", $size);
- $iv = "0987654321098765";
- $decrypted = openssl_decrypt(base64_decode($content), 'AES-128-CBC', $key_md5, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
- $data_decode = explode('@', $decrypted);
- //$data = [];
- //parse_str($data_decode[0], $data);
- return json_decode($data_decode[0], true);
- }
- /**
- * 解密(php)
- * @param string $content
- * @param string $key
- * @param string $secret
- * @return mixed
- */
- public static function decodeOld(string $content, string $key, string $secret)
- {
- $key = strtoupper(md5($key . $secret));
- $size = 16;
- $iv = str_repeat("\0", $size);
- $decrypted = openssl_decrypt(base64_decode($content), 'AES-192-CBC', base64_decode($key), OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
- $data_decode = explode('@', $decrypted);
- //$data = [];
- //parse_str($data_decode[0], $data);
- //return $data;
- return json_decode($data_decode[0], true);
- }
- public static function toStr($bytes)
- {
- $str = '';
- foreach ($bytes as $ch) {
- //dd(chr($ch));
- try {
- $str .= chr($ch);
- } catch (\Exception $e) {
- dd(chr($ch));
- }
- }
- return $str;
- }
- /**
- * 签名算法
- * @param array $params
- * @param string $secret
- * @return array
- */
- public static function sign(array $params, string $secret)
- {
- $p = ksort($params);
- $str = '';
- if ($p) {
- foreach ($params as $key => $items) {
- $str .= "{$key}";
- $str .= "=";
- $str .= "{$items}";
- $str .= "&";
- }
- }
- $string = substr($str, 0, -1);
- //$string = "appKey=$this->appKey&nonce=$this->nonce×tamp=$this->timestamp";
- $stringSignTemp = "$string&secret=$secret";
- //echo $stringSignTemp;
- $sign = strtoupper(MD5($stringSignTemp));
- $params['sign'] = $sign;
- return $params;
- }
- /**
- *
- * @param string $key
- * @param string $value
- * @return array
- */
- public static function user_admin_config($key = null, $value = null)
- {
- $session = session();
- if (! $config = $session->get('admin.config')) {
- $config = config('admin');
- $config['lang'] = config('app.locale');
- }
- if (is_array($key)) {
- // 保存
- foreach ($key as $k => $v) {
- Arr::set($config, $k, $v);
- }
- $session->put('admin.config', $config);
- return;
- }
- if ($key === null) {
- return $config;
- }
- return Arr::get($config, $key, $value);
- }
- /**
- * 生成随机码
- * @return string
- */
- public static function create_invite_code() {
- $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $rand = $code[rand(0,25)]
- .strtoupper(dechex(date('m')))
- .date('d')
- .substr(time(),-5)
- .substr(microtime(),2,5)
- .sprintf('%02d',rand(0,99));
- for(
- $a = md5( $rand, true ),
- $s = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
- $d = '',
- $f = 0;
- $f < 6;
- $g = ord( $a[ $f ] ),
- $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
- $f++
- );
- return $d;
- }
-
- /**
- * 时间格式化(时间戳)
- * @param $ptime
- * @return false|string
- */
- public static function uc_time_ago($ptime)
- {
- date_default_timezone_set('PRC');
- $etime = time() - $ptime;
- switch ($etime) {
- case $etime <= 60:
- $msg = '刚刚';
- break;
- case $etime > 60 && $etime <= 60 * 60:
- $msg = floor($etime / 60) . '分钟前';
- break;
- case $etime > 60 * 60 && $etime <= 24 * 60 * 60:
- $msg = date('Ymd', $ptime) == date('Ymd', time()) ? '今天 ' . date('H:i', $ptime) : '昨天 ' . date('H:i', $ptime);
- break;
- case $etime > 24 * 60 * 60 && $etime <= 2 * 24 * 60 * 60:
- $msg = date('Ymd', $ptime) + 1 == date('Ymd', time()) ? '昨天 ' . date('H:i', $ptime) : '前天 ' . date('H:i', $ptime);
- break;
- case $etime > 2 * 24 * 60 * 60 && $etime <= 12 * 30 * 24 * 60 * 60:
- $msg = date('Y', $ptime) == date('Y', time()) ? date('m-d H:i', $ptime) : date('Y-m-d H:i', $ptime);
- break;
- default:
- $msg = date('Y-m-d H:i', $ptime);
- }
- return $msg;
- }
- /**
- * 获取IP地址归属地
- * @param $ip
- * @return string
- */
- public static function get_ip_address($ip)
- {
- if ('127.0.0.1' == $ip) return 'Localhost';
- $url = 'http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip;
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_TIMEOUT, 10);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
- $location = curl_exec($ch);
- $location = json_decode($location, true);
- curl_close($ch);
- if (false != $location && 0 === $location['code']) {
- return $location['data']['region'] . $location['data']['city'] . $location['data']['county'] . '・' . $location['data']['isp'];
- } else {
- return 'unknown';
- }
- }
- /**
- * 递归查询获取分类树结构
- * @param $data
- * @param int $pid
- * @param int $level
- * @param array $tree
- * @param string $pidField
- * @param string $showField
- * @return array
- */
- public static function get_tree_list(&$data, $pid = 0, $level = 0, &$tree = [], $pidField = 'pid', $showField = 'name')
- {
- foreach ($data as $key => &$value) {
- if ($value[$pidField] == $pid) {
- $value['level'] = $level;
- $value['level'] && $value[$showField] = ' ' . $value[$showField];
- $value[$showField] = str_repeat('ㅡ', $value['level']) . $value[$showField];
- $tree[] = $value;
- unset($data[$key]);
- self::get_tree_list($data, $value['id'], $level + 1, $tree);
- }
- }
- unset($value);
- return $tree;
- }
- /**
- * 递归查询获取分类树结构带child
- * @param $data
- * @param int $pid
- * @param int $level
- * @param string $pidField
- * @return array
- */
- public static function get_tree_list_with_child(&$data, $pid = 0, $level = 0, $pidField = 'pid')
- {
- $tree = [];
- foreach ($data as $key => &$value) {
- if ($value[$pidField] == $pid) {
- $value['level'] = $level;
- $value['child'] = self::get_tree_list_with_child($data, $value['id'], $level + 1);
- $tree[] = $value;
- unset($data[$key]);
- }
- }
- unset($value);
- return $tree;
- }
- /**
- * 打印sql语句,在sql语句之前调用
- */
- public static function dump_sql()
- {
- \DB::listen(function ($query) {
- $bindings = $query->bindings;
- $i = 0;
- $rawSql = preg_replace_callback('/\?/', function ($matches) use ($bindings, &$i) {
- $item = isset($bindings[$i]) ? $bindings[$i] : $matches[0];
- $i++;
- return gettype($item) == 'string' ? "'$item'" : $item;
- }, $query->sql);
- echo $rawSql . "\n<br /><br />\n";
- });
- }
- public static function create_guid($namespace = null)
- {
- static $guid = '';
- $uid = uniqid("", true);
- $data = $namespace;
- $data .= $_SERVER ['REQUEST_TIME']; // 请求那一刻的时间戳
- $data .= $_SERVER ['HTTP_USER_AGENT']; // 获取访问者在用什么操作系统
- $data .= $_SERVER ['SERVER_ADDR']; // 服务器IP
- $data .= $_SERVER ['SERVER_PORT']; // 端口号
- $data .= $_SERVER ['REMOTE_ADDR']; // 远程IP
- $data .= $_SERVER ['REMOTE_PORT']; // 端口信息
- $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
- $guid = substr($hash, 0, 8);
- return $guid;
- }
- public static function create_order_number()
- {
- return date('Ymd') . str_pad(mt_rand(1, 999999), 6, '0', STR_PAD_LEFT);
- }
- /**
- * curl 请求
- * @param $url
- * @param null $header
- * @param null $data
- * @return mixed
- */
- public static function curlRequest($url, $header = null, $data = null)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
- curl_setopt($ch, CURLOPT_HEADER, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- if ($data) {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- }
- if ($header) {
- curl_setopt($ch, CURLOPT_HEADER, $header);
- }
- $ret = curl_exec($ch);
- curl_close($ch);
- return $ret;
- }
- /**
- * 数字金额转换成中文大写金额的函数
- * @param $num
- * @return string
- */
- public static function cny($num)
- {
- $c1 = "零壹贰叁肆伍陆柒捌玖";
- $c2 = "分角元拾佰仟万拾佰仟亿";
- $num = round($num, 2);
- $num = $num * 100;
- if (strlen($num) > 10) {
- return "数据太长,没有这么大的钱吧,检查下";
- }
- $i = 0;
- $c = "";
- while (1) {
- if ($i == 0) {
- $n = substr($num, strlen($num) - 1, 1);
- } else {
- $n = $num % 10;
- }
- $p1 = substr($c1, 3 * $n, 3);
- $p2 = substr($c2, 3 * $i, 3);
- if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
- $c = $p1 . $p2 . $c;
- } else {
- $c = $p1 . $c;
- }
- $i = $i + 1;
- $num = $num / 10;
- $num = (int)$num;
- if ($num == 0) {
- break;
- }
- }
- $j = 0;
- $slen = strlen($c);
- while ($j < $slen) {
- $m = substr($c, $j, 6);
- if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
- $left = substr($c, 0, $j);
- $right = substr($c, $j + 3);
- $c = $left . $right;
- $j = $j - 3;
- $slen = $slen - 3;
- }
- $j = $j + 3;
- }
- if (substr($c, strlen($c) - 3, 3) == '零') {
- $c = substr($c, 0, strlen($c) - 3);
- }
- if (empty($c)) {
- return "零元整";
- } else {
- return $c . "整";
- }
- }
- /**
- * 路径助手函数
- * @param $url
- * @param null $disk_name
- * @param false $temp
- * @param null|DateTimeInterface $expiration
- * @return string|null
- */
- public static function valid_url($url, $disk_name = null, $temp = false, $expiration = null)
- {
- if (is_null($url)) {
- return null;
- }
- if (filter_var($url, FILTER_VALIDATE_URL)) {
- return $url;
- }
- if ($temp) {
- $expiration = $expiration ?: now()->addMinutes(30);
- return Storage::disk($disk_name)->temporaryUrl($url, $expiration);
- }
- return Storage::disk($disk_name)->url($url);
- }
- }
|