common.php 15 KB

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