123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- <?php
- define('PI', 3.1415926535898);
- define('EARTH_RADIUS', 6378.137);
- //树形结构转一维数组
- function setlist($arr, $parentid = 0)
- {
- $array = array();
- foreach ($arr as $val) {
- $indata = array("id" => $val["id"], "parent_id" => $parentid);
- $array[] = $indata;
- if (isset($val["children"])) {
- $children = setlist($val["children"], $val["id"]);
- if ($children) {
- $array = array_merge($array, $children);
- }
- }
- }
- return $array;
- }
- //初始化树形结构
- function infiniteTree($data, $pid = 0)
- {
- if (!is_array($data) || empty($data)) return false;
- $tree = array();
- foreach ($data as $k => $v) {
- // 找出所有的儿子
- if ($v['parent_id'] == $pid) {
- // 将儿子数据赋值给sub键,递归看看儿子还有没有孙子
- $v['children'] = infiniteTree($data, $v['id']);
- $tree[] = $v;
- // 删除遍历过的数组数据
- unset($data[$k]);
- }
- }
- return $tree;
- }
- //对象转数组
- function object_array($array)
- {
- if (is_object($array)) {
- $array = (array)$array;
- }
- if (is_array($array)) {
- foreach ($array as $key => $value) {
- $array[$key] = object_array($value);
- }
- }
- return $array;
- }
- //求地图中心点
- function GetCenterFromDegrees($data)
- {
- if (!is_array($data)) return FALSE;
- $num_coords = count($data);
- $X = 0.0;
- $Y = 0.0;
- $Z = 0.0;
- foreach ($data as $coord) {
- $lat = deg2rad((float)$coord['lat']);
- $lon = deg2rad((float)$coord['lng']);
- $a = cos($lat) * cos($lon);
- $b = cos($lat) * sin($lon);
- $c = sin($lat);
- $X += $a;
- $Y += $b;
- $Z += $c;
- }
- $X /= $num_coords;
- $Y /= $num_coords;
- $Z /= $num_coords;
- $lon = atan2($Y, $X);
- $hyp = sqrt($X * $X + $Y * $Y);
- $lat = atan2($Z, $hyp);
- return array('lat' => rad2deg($lat), 'lng' => rad2deg($lon));
- }
- //计算两点之间坐标距离 单位 米
- function GetDistance($lat1, $lng1, $lat2, $lng2)
- {
- $radLat1 = $lat1 * (PI / 180);
- $radLat2 = $lat2 * (PI / 180);
- $a = $radLat1 - $radLat2;
- $b = ($lng1 * (PI / 180)) - ($lng2 * (PI / 180));
- $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
- $s = $s * EARTH_RADIUS;
- //$s = round($s * 10000) / 10000;
- return $s * 1000;
- }
- //获取一个多边形中心点 到此多边形个点的最大距离 单位 米
- function maxDistance(array $centre, array $fence)
- {
- $distanceArr = array();
- foreach ($fence as $v) {
- $distanceArr[] = GetDistance($centre['lat'], $centre['lng'], $v['lat'], $v['lng']);
- }
- return max($distanceArr);
- }
- // lat转latitude lng转longitude
- function latToLatitude($str)
- {
- if (is_array($str)) {
- $str = json_encode($str);
- }
- $pattern_lat = '/lat/';
- $replace_lat = 'latitude';
- $str_lat = preg_replace($pattern_lat, $replace_lat, $str);
- $pattern_lng = '/lng/';
- $replace_lng = 'longitude';
- $str = preg_replace($pattern_lng, $replace_lng, $str_lat);
- return $str;
- }
- // latitude转lat longitude转lng
- function latitudeTolat($str)
- {
- if (is_array($str)) {
- $str = json_encode($str);
- }
- $pattern_lat = '/latitude/';
- $replace_lat = 'lat';
- $str_lat = preg_replace($pattern_lat, $replace_lat, $str);
- $pattern_lng = '/longitude/';
- $replace_lng = 'lng';
- $str = preg_replace($pattern_lng, $replace_lng, $str_lat);
- return $str;
- }
- //高德地图绘制取精确数据 (前端已处理)
- function getAccurate(array $arr)
- {
- $data = [];
- foreach ($arr as $v) {
- $bata = [];
- $bata['lat'] = $v['P'];
- $bata['lng'] = $v['O'];
- $data[] = $bata;
- }
- return $data;
- }
- // 渲染高德地图, 电子围栏数据转换函数
- function toGaodeFenceData($str)
- {
- $arr = json_decode($str);
- $res = [];
- foreach ($arr as $v) {
- $arr2 = [$v->longitude, $v->latitude];
- $res[] = $arr2;
- }
- $res = json_encode($res);
- return $res;
- }
- // 渲染高德, 中心点数据转换函数
- function toGaodeCentreData($str)
- {
- $arr = json_decode($str);
- $res = [$arr->longitude, $arr->latitude];
- $res = json_encode($res);
- return $res;
- }
- /**
- * 获取图片的Base64编码(不支持url)
- * @date 2017-02-20 19:41:22
- *
- * @param $img_file 传入本地图片地址
- *
- * @return string
- */
- function imgToBase64($img_file)
- {
- $img_base64 = '';
- if (file_exists($img_file)) {
- $app_img_file = $img_file; // 图片路径
- $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
- //echo '<pre>' . print_r($img_info, true) . '</pre><br>';
- $fp = fopen($app_img_file, "r"); // 图片是否可读权限
- if ($fp) {
- $filesize = filesize($app_img_file);
- $content = fread($fp, $filesize);
- $file_content = chunk_split(base64_encode($content)); // base64编码
- switch ($img_info[2]) { //判读图片类型
- case 1:
- $img_type = "gif";
- break;
- case 2:
- $img_type = "jpg";
- break;
- case 3:
- $img_type = "png";
- break;
- }
- $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码
- }
- fclose($fp);
- }
- return $img_base64; //返回图片的base64
- }
- //可以发送get和post的请求方式
- function curl_request($url, $method = 'get', $data = null, $https = true)
- {
- $headers = ["Content-type: application/json;charset='utf-8'", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache",];
- //1.初识化curl
- $ch = curl_init($url);
- //2.根据实际请求需求进行参数封装
- //返回数据不直接输出
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //如果是https请求
- if ($https === true) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- //如果是post请求
- if ($method === 'post') {
- //开启发送post请求选项
- curl_setopt($ch, CURLOPT_POST, true);
- //发送post的数据
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
- //3.发送请求
- $result = curl_exec($ch);
- //4.返回返回值,关闭连接
- curl_close($ch);
- return $result;
- }
- function php2js($arr)
- {
- return json_encode($arr, true);
- }
- function js2php($json)
- {
- return json_decode($json, true);
- }
- function str2arr($str, $glue = ',')
- {
- return explode($glue, $str);
- }
- function arr2str($arr, $glue = ',')
- {
- return implode($glue, array_wrap($arr));
- }
- function objectToArray($object)
- {
- //先编码成json字符串,再解码成数组
- return json_decode(json_encode($object), true);
- }
- // 是否是序列化字符串
- function is_serialized( $data ) {
- $data = trim( $data );
- if ( 'N;' == $data )
- return true;
- if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
- return false;
- switch ( $badions[1] ) {
- case 'a' :
- case 'O' :
- case 's' :
- if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
- return true;
- break;
- case 'b' :
- case 'i' :
- case 'd' :
- if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
- return true;
- break;
- }
- return false;
- }
- function wechat_fee($money)
- {
- return ($money * 100);
- }
|