helpers.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. define('PI', 3.1415926535898);
  3. define('EARTH_RADIUS', 6378.137);
  4. //树形结构转一维数组
  5. function setlist($arr, $parentid = 0)
  6. {
  7. $array = array();
  8. foreach ($arr as $val) {
  9. $indata = array("id" => $val["id"], "parent_id" => $parentid);
  10. $array[] = $indata;
  11. if (isset($val["children"])) {
  12. $children = setlist($val["children"], $val["id"]);
  13. if ($children) {
  14. $array = array_merge($array, $children);
  15. }
  16. }
  17. }
  18. return $array;
  19. }
  20. //初始化树形结构
  21. function infiniteTree($data, $pid = 0)
  22. {
  23. if (!is_array($data) || empty($data)) return false;
  24. $tree = array();
  25. foreach ($data as $k => $v) {
  26. // 找出所有的儿子
  27. if ($v['parent_id'] == $pid) {
  28. // 将儿子数据赋值给sub键,递归看看儿子还有没有孙子
  29. $v['children'] = infiniteTree($data, $v['id']);
  30. $tree[] = $v;
  31. // 删除遍历过的数组数据
  32. unset($data[$k]);
  33. }
  34. }
  35. return $tree;
  36. }
  37. //对象转数组
  38. function object_array($array)
  39. {
  40. if (is_object($array)) {
  41. $array = (array)$array;
  42. }
  43. if (is_array($array)) {
  44. foreach ($array as $key => $value) {
  45. $array[$key] = object_array($value);
  46. }
  47. }
  48. return $array;
  49. }
  50. //求地图中心点
  51. function GetCenterFromDegrees($data)
  52. {
  53. if (!is_array($data)) return FALSE;
  54. $num_coords = count($data);
  55. $X = 0.0;
  56. $Y = 0.0;
  57. $Z = 0.0;
  58. foreach ($data as $coord) {
  59. $lat = deg2rad((float)$coord['lat']);
  60. $lon = deg2rad((float)$coord['lng']);
  61. $a = cos($lat) * cos($lon);
  62. $b = cos($lat) * sin($lon);
  63. $c = sin($lat);
  64. $X += $a;
  65. $Y += $b;
  66. $Z += $c;
  67. }
  68. $X /= $num_coords;
  69. $Y /= $num_coords;
  70. $Z /= $num_coords;
  71. $lon = atan2($Y, $X);
  72. $hyp = sqrt($X * $X + $Y * $Y);
  73. $lat = atan2($Z, $hyp);
  74. return array('lat' => rad2deg($lat), 'lng' => rad2deg($lon));
  75. }
  76. //计算两点之间坐标距离 单位 米
  77. function GetDistance($lat1, $lng1, $lat2, $lng2)
  78. {
  79. $radLat1 = $lat1 * (PI / 180);
  80. $radLat2 = $lat2 * (PI / 180);
  81. $a = $radLat1 - $radLat2;
  82. $b = ($lng1 * (PI / 180)) - ($lng2 * (PI / 180));
  83. $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
  84. $s = $s * EARTH_RADIUS;
  85. //$s = round($s * 10000) / 10000;
  86. return $s * 1000;
  87. }
  88. //获取一个多边形中心点 到此多边形个点的最大距离 单位 米
  89. function maxDistance(array $centre, array $fence)
  90. {
  91. $distanceArr = array();
  92. foreach ($fence as $v) {
  93. $distanceArr[] = GetDistance($centre['lat'], $centre['lng'], $v['lat'], $v['lng']);
  94. }
  95. return max($distanceArr);
  96. }
  97. // lat转latitude lng转longitude
  98. function latToLatitude($str)
  99. {
  100. if (is_array($str)) {
  101. $str = json_encode($str);
  102. }
  103. $pattern_lat = '/lat/';
  104. $replace_lat = 'latitude';
  105. $str_lat = preg_replace($pattern_lat, $replace_lat, $str);
  106. $pattern_lng = '/lng/';
  107. $replace_lng = 'longitude';
  108. $str = preg_replace($pattern_lng, $replace_lng, $str_lat);
  109. return $str;
  110. }
  111. // latitude转lat longitude转lng
  112. function latitudeTolat($str)
  113. {
  114. if (is_array($str)) {
  115. $str = json_encode($str);
  116. }
  117. $pattern_lat = '/latitude/';
  118. $replace_lat = 'lat';
  119. $str_lat = preg_replace($pattern_lat, $replace_lat, $str);
  120. $pattern_lng = '/longitude/';
  121. $replace_lng = 'lng';
  122. $str = preg_replace($pattern_lng, $replace_lng, $str_lat);
  123. return $str;
  124. }
  125. //高德地图绘制取精确数据 (前端已处理)
  126. function getAccurate(array $arr)
  127. {
  128. $data = [];
  129. foreach ($arr as $v) {
  130. $bata = [];
  131. $bata['lat'] = $v['P'];
  132. $bata['lng'] = $v['O'];
  133. $data[] = $bata;
  134. }
  135. return $data;
  136. }
  137. // 渲染高德地图, 电子围栏数据转换函数
  138. function toGaodeFenceData($str)
  139. {
  140. $arr = json_decode($str);
  141. $res = [];
  142. foreach ($arr as $v) {
  143. $arr2 = [$v->longitude, $v->latitude];
  144. $res[] = $arr2;
  145. }
  146. $res = json_encode($res);
  147. return $res;
  148. }
  149. // 渲染高德, 中心点数据转换函数
  150. function toGaodeCentreData($str)
  151. {
  152. $arr = json_decode($str);
  153. $res = [$arr->longitude, $arr->latitude];
  154. $res = json_encode($res);
  155. return $res;
  156. }
  157. /**
  158. * 获取图片的Base64编码(不支持url)
  159. * @date 2017-02-20 19:41:22
  160. *
  161. * @param $img_file 传入本地图片地址
  162. *
  163. * @return string
  164. */
  165. function imgToBase64($img_file)
  166. {
  167. $img_base64 = '';
  168. if (file_exists($img_file)) {
  169. $app_img_file = $img_file; // 图片路径
  170. $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
  171. //echo '<pre>' . print_r($img_info, true) . '</pre><br>';
  172. $fp = fopen($app_img_file, "r"); // 图片是否可读权限
  173. if ($fp) {
  174. $filesize = filesize($app_img_file);
  175. $content = fread($fp, $filesize);
  176. $file_content = chunk_split(base64_encode($content)); // base64编码
  177. switch ($img_info[2]) { //判读图片类型
  178. case 1:
  179. $img_type = "gif";
  180. break;
  181. case 2:
  182. $img_type = "jpg";
  183. break;
  184. case 3:
  185. $img_type = "png";
  186. break;
  187. }
  188. $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码
  189. }
  190. fclose($fp);
  191. }
  192. return $img_base64; //返回图片的base64
  193. }
  194. //可以发送get和post的请求方式
  195. function curl_request($url, $method = 'get', $data = null, $https = true)
  196. {
  197. $headers = ["Content-type: application/json;charset='utf-8'", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache",];
  198. //1.初识化curl
  199. $ch = curl_init($url);
  200. //2.根据实际请求需求进行参数封装
  201. //返回数据不直接输出
  202. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  203. //如果是https请求
  204. if ($https === true) {
  205. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  206. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  207. }
  208. //如果是post请求
  209. if ($method === 'post') {
  210. //开启发送post请求选项
  211. curl_setopt($ch, CURLOPT_POST, true);
  212. //发送post的数据
  213. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  214. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  215. }
  216. //3.发送请求
  217. $result = curl_exec($ch);
  218. //4.返回返回值,关闭连接
  219. curl_close($ch);
  220. return $result;
  221. }
  222. function php2js($arr)
  223. {
  224. return json_encode($arr, true);
  225. }
  226. function js2php($json)
  227. {
  228. return json_decode($json, true);
  229. }
  230. function str2arr($str, $glue = ',')
  231. {
  232. return explode($glue, $str);
  233. }
  234. function arr2str($arr, $glue = ',')
  235. {
  236. return implode($glue, array_wrap($arr));
  237. }
  238. function objectToArray($object)
  239. {
  240. //先编码成json字符串,再解码成数组
  241. return json_decode(json_encode($object), true);
  242. }
  243. // 是否是序列化字符串
  244. function is_serialized($data)
  245. {
  246. $data = trim($data);
  247. if ('N;' == $data)
  248. return true;
  249. if (!preg_match('/^([adObis]):/', $data, $badions))
  250. return false;
  251. switch ($badions[1]) {
  252. case 'a' :
  253. case 'O' :
  254. case 's' :
  255. if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data))
  256. return true;
  257. break;
  258. case 'b' :
  259. case 'i' :
  260. case 'd' :
  261. if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data))
  262. return true;
  263. break;
  264. }
  265. return false;
  266. }
  267. function wechat_fee($money)
  268. {
  269. return ($money * 100);
  270. }
  271. if (!function_exists('wechat_mini_config')) {
  272. /**
  273. * 获取微信小程序配置
  274. *
  275. * @param array|string|null $key
  276. * @param mixed $default
  277. * @return \Illuminate\Http\Request|string|array
  278. */
  279. function wechat_mini_config($merchant)
  280. {
  281. $config = [
  282. 'app_id' => $merchant['wxapp_app_id'],
  283. 'secret' => $merchant['wxapp_app_secret'],
  284. // 下面为可选项
  285. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  286. 'response_type' => 'array',
  287. 'log' => [
  288. 'level' => 'debug',
  289. 'file' => __DIR__ . '/' . $merchant['id'] . '/wechat.log',
  290. ],
  291. ];
  292. return $config;
  293. }
  294. }
  295. if (!function_exists('wechat_pay_config')) {
  296. /**
  297. * 获取微信小程序配置
  298. *
  299. * @param array|string|null $key
  300. * @param mixed $default
  301. * @return \Illuminate\Http\Request|string|array
  302. */
  303. function wechat_pay_config($merchant)
  304. {
  305. $config = [
  306. 'app_id' => $merchant['wxapp_app_id'],
  307. 'mch_id' => $merchant['pay_mch_id'],
  308. 'key' => $merchant['pay_key'], // API 密钥
  309. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  310. 'cert_path' => base_path() . '/storage/app/public/merchant/' . $merchant['pay_cert_path'], // XXX: 绝对路径!!!!
  311. 'key_path' => base_path() . '/storage/app/public/merchant/' . $merchant['pay_key_path'], // XXX: 绝对路径!!!!
  312. 'notify_url' => config('app.url') . '/api/payments/wechat-notify/' . $merchant['id'], // 默认支付结果通知地址
  313. ];
  314. return $config;
  315. }
  316. }
  317. if (!function_exists('alipay_mini_config')) {
  318. /**
  319. * 获取支付宝小程序配置
  320. *
  321. * @param array|string|null $key
  322. * @param mixed $default
  323. * @return \Illuminate\Http\Request|string|array
  324. */
  325. function alipay_mini_config($options,$merchant)
  326. {
  327. $options->protocol = 'https';
  328. $options->gatewayHost = 'openapi.alipay.com';
  329. $options->signType = 'RSA2';
  330. $options->appId = $merchant['alipaymini_appId'];
  331. // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
  332. $options->merchantPrivateKey = $merchant['alipaymini_merchantPrivateKey'];
  333. // $options->alipayCertPath = base_path() . '/database/zhifubao/'. $merchant['alipaymini_alipayCertPath'];
  334. // $options->alipayRootCertPath = base_path() . '/database/zhifubao/' . $merchant['alipaymini_alipayRootCertPath'];
  335. // $options->merchantCertPath = base_path() . '/database/zhifubao/' . $merchant['alipaymini_merchantCertPath'];
  336. $options->alipayCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayCertPath'];
  337. $options->alipayRootCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayRootCertPath'];
  338. $options->merchantCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_merchantCertPath'];
  339. //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
  340. // $options->alipayPublicKey = '<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->';
  341. //可设置异步通知接收服务地址(可选)
  342. // $options->notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址,例如:https://www.test.com/callback -->";
  343. //可设置AES密钥,调用AES加解密相关接口时需要(可选)
  344. $options->encryptKey = $merchant['alipaymini_aesKey'];
  345. return $options;
  346. }
  347. }
  348. function merchant_id()
  349. {
  350. return request()->header('merchant-id', request()->get('merchant_id', 0));
  351. }