1
0

common.php 17 KB

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