common.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. // 应用公共文件
  3. use app\common\model\setting\PostageRegion;
  4. use app\common\service\FileService;
  5. use think\helper\Str;
  6. /**
  7. * 计算两点之间的距离
  8. */
  9. function haversineDistance($lat1, $lon1, $lat2, $lon2) {
  10. // 地球平均半径,单位:千米
  11. $R = 6371;
  12. // 将角度转换为弧度
  13. $lat1 = deg2rad($lat1);
  14. $lon1 = deg2rad($lon1);
  15. $lat2 = deg2rad($lat2);
  16. $lon2 = deg2rad($lon2);
  17. // 计算纬度和经度的差值
  18. $dLat = $lat2 - $lat1;
  19. $dLon = $lon2 - $lon1;
  20. // Haversine 公式的计算步骤
  21. $a = sin($dLat / 2) * sin($dLat / 2) +
  22. cos($lat1) * cos($lat2) * sin($dLon / 2) * sin($dLon / 2);
  23. $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
  24. // 计算距离
  25. $distance = $R * $c;
  26. return $distance;
  27. }
  28. /**
  29. * @notes 生成密码加密密钥
  30. * @param string $plaintext
  31. * @param string $salt
  32. * @return string
  33. * @author 段誉
  34. * @date 2021/12/28 18:24
  35. */
  36. function create_password(string $plaintext, string $salt) : string
  37. {
  38. return md5($salt . md5($plaintext . $salt));
  39. }
  40. /**
  41. * @notes 随机生成token值
  42. * @param string $extra
  43. * @return string
  44. * @author 段誉
  45. * @date 2021/12/28 18:24
  46. */
  47. function create_token(string $extra = '') : string
  48. {
  49. $salt = env('project.unique_identification', 'likeadmin');
  50. $encryptSalt = md5( $salt . uniqid());
  51. return md5($salt . $extra . time() . $encryptSalt);
  52. }
  53. /**
  54. * @notes 截取某字符字符串
  55. * @param $str
  56. * @param string $symbol
  57. * @return string
  58. * @author 段誉
  59. * @date 2021/12/28 18:24
  60. */
  61. function substr_symbol_behind($str, $symbol = '.') : string
  62. {
  63. $result = strripos($str, $symbol);
  64. if ($result === false) {
  65. return $str;
  66. }
  67. return substr($str, $result + 1);
  68. }
  69. /**
  70. * @notes 对比php版本
  71. * @param string $version
  72. * @return bool
  73. * @author 段誉
  74. * @date 2021/12/28 18:27
  75. */
  76. function compare_php(string $version) : bool
  77. {
  78. return version_compare(PHP_VERSION, $version) >= 0 ? true : false;
  79. }
  80. /**
  81. * @notes 检查文件是否可写
  82. * @param string $dir
  83. * @return bool
  84. * @author 段誉
  85. * @date 2021/12/28 18:27
  86. */
  87. function check_dir_write(string $dir = '') : bool
  88. {
  89. $route = root_path() . '/' . $dir;
  90. return is_writable($route);
  91. }
  92. /**
  93. * 多级线性结构排序
  94. * 转换前:
  95. * [{"id":1,"pid":0,"name":"a"},{"id":2,"pid":0,"name":"b"},{"id":3,"pid":1,"name":"c"},
  96. * {"id":4,"pid":2,"name":"d"},{"id":5,"pid":4,"name":"e"},{"id":6,"pid":5,"name":"f"},
  97. * {"id":7,"pid":3,"name":"g"}]
  98. * 转换后:
  99. * [{"id":1,"pid":0,"name":"a","level":1},{"id":3,"pid":1,"name":"c","level":2},{"id":7,"pid":3,"name":"g","level":3},
  100. * {"id":2,"pid":0,"name":"b","level":1},{"id":4,"pid":2,"name":"d","level":2},{"id":5,"pid":4,"name":"e","level":3},
  101. * {"id":6,"pid":5,"name":"f","level":4}]
  102. * @param array $data 线性结构数组
  103. * @param string $symbol 名称前面加符号
  104. * @param string $name 名称
  105. * @param string $id_name 数组id名
  106. * @param string $parent_id_name 数组祖先id名
  107. * @param int $level 此值请勿给参数
  108. * @param int $parent_id 此值请勿给参数
  109. * @return array
  110. */
  111. function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0)
  112. {
  113. $tree = [];
  114. foreach ($data as $row) {
  115. if ($row[$parent_id_name] == $parent_id) {
  116. $temp = $row;
  117. $child = linear_to_tree($data, $sub_key_name, $id_name, $parent_id_name, $row[$id_name]);
  118. if ($child) {
  119. $temp[$sub_key_name] = $child;
  120. }
  121. $tree[] = $temp;
  122. }
  123. }
  124. return $tree;
  125. }
  126. /**
  127. * 根据父级ID获取所有子集的值
  128. * @param $data
  129. * @param $pid
  130. * @param $idField
  131. * @param $pidField
  132. * @return array
  133. */
  134. function get_tree_ids($data,$pid = 0, $idField = 'id',$pidField = 'pid')
  135. {
  136. $child = [];
  137. foreach($data as $val){
  138. if ($val[$pidField] == $pid) {
  139. $children = get_tree_ids($data, $val[$idField],$idField,$pidField,);
  140. if ( count($children) > 0) {
  141. $child = array_merge($child,$children);
  142. }
  143. $child[] = $val['id'];
  144. }
  145. }
  146. return $child;
  147. }
  148. function get_top_parent_info($data, $id, $idField = 'id', $pidField = 'pid' , $parentLevel = 0)
  149. {
  150. foreach ($data as $item) {
  151. if ($item[$idField] == $id) {
  152. if ($item[$pidField] == $parentLevel) {
  153. return $item;
  154. } else {
  155. return get_top_parent_info($data, $item[$pidField], $idField, $pidField);
  156. }
  157. }
  158. }
  159. return null;
  160. }
  161. /**
  162. * 根据子集的值获取所有最高父级信息
  163. * @param $data
  164. * @param $pid
  165. * @param $idField
  166. * @param $pidField
  167. * @return array
  168. */
  169. function get_parent_info($data,$ids = [])
  170. {
  171. $res = [];
  172. foreach ($ids as $item) {
  173. $topParentInfo = get_top_parent_info($data, $item);
  174. if ($topParentInfo !== null) {
  175. $res[$topParentInfo['id']] = $topParentInfo;
  176. }
  177. }
  178. return $res;
  179. }
  180. /**
  181. * @notes 删除目标目录
  182. * @param $path
  183. * @param $delDir
  184. * @return bool|void
  185. * @author 段誉
  186. * @date 2022/4/8 16:30
  187. */
  188. function del_target_dir($path, $delDir)
  189. {
  190. //没找到,不处理
  191. if (!file_exists($path)) {
  192. return false;
  193. }
  194. //打开目录句柄
  195. $handle = opendir($path);
  196. if ($handle) {
  197. while (false !== ($item = readdir($handle))) {
  198. if ($item != "." && $item != "..") {
  199. if (is_dir("$path/$item")) {
  200. del_target_dir("$path/$item", $delDir);
  201. } else {
  202. unlink("$path/$item");
  203. }
  204. }
  205. }
  206. closedir($handle);
  207. if ($delDir) {
  208. return rmdir($path);
  209. }
  210. } else {
  211. if (file_exists($path)) {
  212. return unlink($path);
  213. }
  214. return false;
  215. }
  216. }
  217. /**
  218. * @notes 下载文件
  219. * @param $url
  220. * @param $saveDir
  221. * @param $fileName
  222. * @return string
  223. * @author 段誉
  224. * @date 2022/9/16 9:53
  225. */
  226. function download_file($url, $saveDir, $fileName)
  227. {
  228. if (!file_exists($saveDir)) {
  229. mkdir($saveDir, 0775, true);
  230. }
  231. $fileSrc = $saveDir . $fileName;
  232. file_exists($fileSrc) && unlink($fileSrc);
  233. $ch = curl_init();
  234. curl_setopt($ch, CURLOPT_URL, $url);
  235. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  236. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  237. $file = curl_exec($ch);
  238. curl_close($ch);
  239. $resource = fopen($fileSrc, 'a');
  240. fwrite($resource, $file);
  241. fclose($resource);
  242. if (filesize($fileSrc) == 0) {
  243. unlink($fileSrc);
  244. return '';
  245. }
  246. return $fileSrc;
  247. }
  248. /**
  249. * @notes 去除内容图片域名
  250. * @param $content
  251. * @return array|string|string[]
  252. * @author 段誉
  253. * @date 2022/9/26 10:43
  254. */
  255. function clear_file_domain($content)
  256. {
  257. $fileUrl = FileService::getFileUrl();
  258. $pattern = '/<img[^>]*\bsrc=["\']'.preg_quote($fileUrl, '/').'([^"\']+)["\']/i';
  259. return preg_replace($pattern, '<img src="$1"', $content);
  260. }
  261. /**
  262. * @notes 设置内容图片域名
  263. * @param $content
  264. * @return array|string|string[]|null
  265. * @author 段誉
  266. * @date 2024/2/5 16:36
  267. */
  268. function get_file_domain($content)
  269. {
  270. $imgPreg = '/(<img .*?src=")[^https|^http](.*?)(".*?>)/is';
  271. $videoPreg = '/(<video .*?src=")[^https|^http](.*?\.mp4)(".*?>)/is';
  272. $fileUrl = FileService::getFileUrl();
  273. $content = preg_replace($imgPreg, "\${1}$fileUrl\${2}\${3}", $content);
  274. return preg_replace($videoPreg, "\${1}$fileUrl\${2}\${3}", $content);
  275. }
  276. /**
  277. * @notes uri小写
  278. * @param $data
  279. * @return array|string[]
  280. * @author 段誉
  281. * @date 2022/7/19 14:50
  282. */
  283. function lower_uri($data)
  284. {
  285. if (!is_array($data)) {
  286. $data = [$data];
  287. }
  288. return array_map(function ($item) {
  289. return strtolower(Str::camel($item));
  290. }, $data);
  291. }
  292. /**
  293. * @notes 获取无前缀数据表名
  294. * @param $tableName
  295. * @return mixed|string
  296. * @author 段誉
  297. * @date 2022/12/12 15:23
  298. */
  299. function get_no_prefix_table_name($tableName)
  300. {
  301. $tablePrefix = config('database.connections.mysql.prefix');
  302. $prefixIndex = strpos($tableName, $tablePrefix);
  303. if ($prefixIndex !== 0 || $prefixIndex === false) {
  304. return $tableName;
  305. }
  306. $tableName = substr_replace($tableName, '', 0, strlen($tablePrefix));
  307. return trim($tableName);
  308. }
  309. /**
  310. * @notes 生成编码
  311. * @param $table
  312. * @param $field
  313. * @param string $prefix
  314. * @param int $randSuffixLength
  315. * @param array $pool
  316. * @return string
  317. * @author 段誉
  318. * @date 2023/2/23 11:35
  319. */
  320. function generate_sn($table, $field, $prefix = '', $randSuffixLength = 4, $pool = []) : string
  321. {
  322. $suffix = '';
  323. for ($i = 0; $i < $randSuffixLength; $i++) {
  324. if (empty($pool)) {
  325. $suffix .= rand(0, 9);
  326. } else {
  327. $suffix .= $pool[array_rand($pool)];
  328. }
  329. }
  330. $sn = $prefix . date('YmdHis') . $suffix;
  331. if (app()->make($table)->where($field, $sn)->find()) {
  332. return generate_sn($table, $field, $prefix, $randSuffixLength, $pool);
  333. }
  334. return $sn;
  335. }
  336. /**
  337. * @notes 格式化金额
  338. * @param $float
  339. * @return int|mixed|string
  340. * @author 段誉
  341. * @date 2023/2/24 11:20
  342. */
  343. function format_amount($float)
  344. {
  345. if ($float == intval($float)) {
  346. return intval($float);
  347. } elseif ($float == sprintf('%.1f', $float)) {
  348. return sprintf('%.1f', $float);
  349. }
  350. return $float;
  351. }
  352. /**
  353. * curl提交
  354. * @param $str
  355. * @return string
  356. */
  357. function http_request($url , $data = NULL)
  358. {
  359. $ch = curl_init();
  360. curl_setopt($ch, CURLOPT_URL, $url);
  361. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  362. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  363. if (!empty($data)){
  364. curl_setopt($ch, CURLOPT_POST, 1);
  365. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  366. }
  367. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  368. $output = curl_exec($ch);
  369. // 检查是否有错误发生
  370. if(curl_errno($ch))
  371. {
  372. echo 'CURL ERROR CODE: '. curl_errno($ch) . ' , reason : ' . curl_error($ch);
  373. }
  374. curl_close($ch);
  375. $jsoninfo = json_decode($output , true);
  376. return $jsoninfo;
  377. }
  378. /**
  379. * sql语句打印
  380. * 需要打印sql时将record_sql()方法放到sql语句之前,或 config.database.trigger_sql设置为true
  381. */
  382. function record_sql()
  383. {
  384. if(!config("database.connections.mysql.trigger_sql")){
  385. $config = config('database');
  386. $config['connections']['mysql']['trigger_sql'] = true;
  387. app()->config->set($config,'database');
  388. }
  389. \think\facade\Db::listen(function ($sql,$time,$connection) {
  390. if(strpos($sql,'CONNECT') !== false){
  391. return;
  392. }
  393. if(strpos($sql,'SHOW FULL') !== false){
  394. return;
  395. }
  396. \think\facade\Log::debug( '打印sql: '.$sql. ' time:'.$time);
  397. });
  398. }
  399. // 前三后四星号字符
  400. function asteriskString($str) {
  401. if (strlen($str) > 7) {
  402. return substr($str, 0, 3) . '****' . substr($str, -4, 4);
  403. } else {
  404. return $str;
  405. }
  406. }
  407. function getPostageRegion() {
  408. $lists = cache('postageRegion');
  409. if(empty($lists)){
  410. $lists = PostageRegion::field(['*'])->select()->toArray();
  411. cache('postageRegion',$lists);
  412. }
  413. return $lists;
  414. }
  415. // 获取随机字符串
  416. function generateRandomString($length = 8,$basic_method = 4) {
  417. $characters = '';
  418. $num = '0123456789';
  419. $lowercase_letters = 'abcdefghijklmnopqrstuvwxyz';
  420. $capital_letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  421. switch ($basic_method){
  422. case 1:
  423. $characters = $num;
  424. break;
  425. case 2:
  426. $characters = $lowercase_letters;
  427. break;
  428. case 3:
  429. $characters = $num.$lowercase_letters;
  430. break;
  431. case 4:
  432. $characters = $num.$lowercase_letters.$capital_letters;
  433. break;
  434. }
  435. $charactersLength = strlen($characters);
  436. $randomString = '';
  437. for ($i = 0; $i < $length; $i++) {
  438. $randomString .= $characters[rand(0, $charactersLength - 1)];
  439. }
  440. return $randomString;
  441. }
  442. /**
  443. * 判断点是否在多边形内
  444. * @param $point
  445. * @param $polygon
  446. * @return bool
  447. */
  448. function isPointInPolygon($point, $polygon) {
  449. $x = $point['lng'];
  450. $y = $point['lat'];
  451. $inside = false;
  452. $j = count($polygon) - 1;
  453. for ($i = 0; $i < count($polygon); $i++) {
  454. $xi = $polygon[$i][0];
  455. $yi = $polygon[$i][1];
  456. $xj = $polygon[$j][0];
  457. $yj = $polygon[$j][1];
  458. $intersect = (($yi > $y) != ($yj > $y))
  459. && ($x < ($xj - $xi) * ($y - $yi) / ($yj - $yi) + $xi);
  460. if ($intersect) {
  461. $inside = !$inside;
  462. }
  463. $j = $i;
  464. }
  465. return $inside;
  466. }