1
0

common.php 22 KB

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