123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- <?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);
- }
- if (!function_exists('wechat_mini_config')) {
- /**
- * 获取微信小程序配置
- *
- * @param array|string|null $key
- * @param mixed $default
- * @return \Illuminate\Http\Request|string|array
- */
- function wechat_mini_config($merchant)
- {
- $config = [
- 'app_id' => $merchant['wxapp_app_id'],
- 'secret' => $merchant['wxapp_app_secret'],
- // 下面为可选项
- // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
- 'response_type' => 'array',
- 'log' => [
- 'level' => 'debug',
- 'file' => __DIR__ . '/' . $merchant['id'] . '/wechat.log',
- ],
- ];
- return $config;
- }
- }
- if (!function_exists('wechat_pay_config')) {
- /**
- * 获取微信小程序配置
- *
- * @param array|string|null $key
- * @param mixed $default
- * @return \Illuminate\Http\Request|string|array
- */
- function wechat_pay_config($merchant)
- {
- $config = [
- 'app_id' => $merchant['wxapp_app_id'],
- 'mch_id' => $merchant['pay_mch_id'],
- 'key' => $merchant['pay_key'], // API 密钥
- // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
- 'cert_path' => base_path() . '/storage/app/public/merchant/' . $merchant['pay_cert_path'], // XXX: 绝对路径!!!!
- 'key_path' => base_path() . '/storage/app/public/merchant/' . $merchant['pay_key_path'], // XXX: 绝对路径!!!!
- 'notify_url' => config('app.url') . '/api/payments/wechat-notify/' . $merchant['id'], // 默认支付结果通知地址
- ];
- return $config;
- }
- }
- if (!function_exists('alipay_mini_config')) {
- /**
- * 获取支付宝小程序配置
- *
- * @param array|string|null $key
- * @param mixed $default
- * @return \Illuminate\Http\Request|string|array
- */
- function alipay_mini_config($options,$merchant)
- {
- $options->protocol = 'https';
- $options->gatewayHost = 'openapi.alipay.com';
- $options->signType = 'RSA2';
- $options->appId = $merchant['alipaymini_appId'];
- // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
- $options->merchantPrivateKey = $merchant['alipaymini_merchantPrivateKey'];
- // $options->alipayCertPath = base_path() . '/database/zhifubao/'. $merchant['alipaymini_alipayCertPath'];
- // $options->alipayRootCertPath = base_path() . '/database/zhifubao/' . $merchant['alipaymini_alipayRootCertPath'];
- // $options->merchantCertPath = base_path() . '/database/zhifubao/' . $merchant['alipaymini_merchantCertPath'];
- $options->alipayCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayCertPath'];
- $options->alipayRootCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayRootCertPath'];
- $options->merchantCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_merchantCertPath'];
- //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
- // $options->alipayPublicKey = '<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->';
- //可设置异步通知接收服务地址(可选)
- // $options->notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址,例如:https://www.test.com/callback -->";
- //可设置AES密钥,调用AES加解密相关接口时需要(可选)
- $options->encryptKey = $merchant['alipaymini_aesKey'];
- return $options;
- }
- }
- function merchant_id()
- {
- return request()->header('merchant-id', request()->get('merchant_id', 0));
- }
|