common.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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)
  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. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  370. $output = curl_exec($ch);
  371. // 检查是否有错误发生
  372. if(curl_errno($ch))
  373. {
  374. echo 'CURL ERROR CODE: '. curl_errno($ch) . ' , reason : ' . curl_error($ch);
  375. }
  376. curl_close($ch);
  377. $jsoninfo = json_decode($output , true);
  378. return $jsoninfo;
  379. }
  380. /**
  381. * sql语句打印
  382. * 需要打印sql时将record_sql()方法放到sql语句之前,或 config.database.trigger_sql设置为true
  383. */
  384. function record_sql()
  385. {
  386. if(!config("database.connections.mysql.trigger_sql")){
  387. $config = config('database');
  388. $config['connections']['mysql']['trigger_sql'] = true;
  389. app()->config->set($config,'database');
  390. }
  391. \think\facade\Db::listen(function ($sql,$time,$connection) {
  392. if(strpos($sql,'CONNECT') !== false){
  393. return;
  394. }
  395. if(strpos($sql,'SHOW FULL') !== false){
  396. return;
  397. }
  398. \think\facade\Log::debug( '打印sql: '.$sql. ' time:'.$time);
  399. });
  400. }
  401. // 前三后四星号字符
  402. function asteriskString($str) {
  403. if (strlen($str) > 7) {
  404. return substr($str, 0, 3) . '****' . substr($str, -4, 4);
  405. } else {
  406. return $str;
  407. }
  408. }
  409. function getPostageRegion($ids = []) {
  410. $id_str = '';
  411. if(!empty($ids)){
  412. $id_str = implode(',',$ids);
  413. }
  414. $lists = cache('postageRegion'.$id_str);
  415. if(empty($lists)){
  416. $lists = PostageRegion::field(['*'])->whereIn('id',$ids)->select()->toArray();
  417. cache('postageRegion'.$id_str,$lists);
  418. }
  419. return $lists;
  420. }
  421. // 通过市查询省id
  422. function getProvinceByCityId($city_id) {
  423. $postageRegion = array_column(getPostageRegion([$city_id]), null, 'id');
  424. return $postageRegion[$city_id]['pid'];
  425. }
  426. // 获取随机字符串
  427. function generateRandomString($length = 8,$basic_method = 4) {
  428. $characters = '';
  429. $num = '0123456789';
  430. $lowercase_letters = 'abcdefghijklmnopqrstuvwxyz';
  431. $capital_letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  432. switch ($basic_method){
  433. case 1:
  434. $characters = $num;
  435. break;
  436. case 2:
  437. $characters = $lowercase_letters;
  438. break;
  439. case 3:
  440. $characters = $num.$lowercase_letters;
  441. break;
  442. case 4:
  443. $characters = $num.$lowercase_letters.$capital_letters;
  444. break;
  445. }
  446. $charactersLength = strlen($characters);
  447. $randomString = '';
  448. for ($i = 0; $i < $length; $i++) {
  449. $randomString .= $characters[rand(0, $charactersLength - 1)];
  450. }
  451. return $randomString;
  452. }
  453. /**
  454. * 判断点是否在多边形内
  455. * @param $point
  456. * @param $polygon
  457. * @return bool
  458. */
  459. function isPointInPolygon($point, $polygon) {
  460. $x = $point['lng'];
  461. $y = $point['lat'];
  462. $inside = false;
  463. $j = count($polygon) - 1;
  464. for ($i = 0; $i < count($polygon); $i++) {
  465. $xi = $polygon[$i][0];
  466. $yi = $polygon[$i][1];
  467. $xj = $polygon[$j][0];
  468. $yj = $polygon[$j][1];
  469. $intersect = (($yi > $y) != ($yj > $y))
  470. && ($x < ($xj - $xi) * ($y - $yi) / ($yj - $yi) + $xi);
  471. if ($intersect) {
  472. $inside = !$inside;
  473. }
  474. $j = $i;
  475. }
  476. return $inside;
  477. }
  478. /**
  479. * 获取自己和上级id
  480. * @param $point
  481. * @param $polygon
  482. * @return bool
  483. */
  484. function getSuperiorId($all_category,$id,&$all_category_ids)
  485. {
  486. $all_category_ids[] = $id;
  487. $tmp_pid = $all_category[$id];
  488. if($tmp_pid > 0) {
  489. getSuperiorId($all_category,$tmp_pid,$all_category_ids);
  490. }
  491. return $all_category_ids;
  492. }
  493. /**
  494. * 获取子分类上级返回树
  495. * @param $category_ids array 分类id数组
  496. * @return array 分类树结构
  497. */
  498. function getSuperiorCategoryTree($category_ids)
  499. {
  500. $all_category = GoodsCategory::where('status', 1)->column('pid', 'id');
  501. $all_category_ids = [];
  502. foreach ($category_ids as $v) {
  503. getSuperiorId($all_category,$v,$all_category_ids);
  504. }
  505. $tree_data = GoodsCategory::field('id,pid,name')
  506. ->where('status', 1)
  507. ->whereIn('id', array_unique($all_category_ids))
  508. ->select()
  509. ->toArray();
  510. return linear_to_tree($tree_data, 'children');
  511. }
  512. // 获取选项数据
  513. function getOptionDataByTable($table) {
  514. if (method_exists(TableDataLogic::class, $table)) {
  515. $lists = TableDataLogic::$table();
  516. }
  517. return $lists??[];
  518. }