helper.php
32.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
<?php
use App\Models\File\Image;
use App\Models\File\File as FileModel;
use App\Models\Project\DeployOptimize;
use App\Models\Project\ProjectKeyword;
use App\Models\RouteMap\RouteMap;
use App\Services\CosService;
use App\Utils\EncryptUtils;
use App\Utils\LogUtils;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
define('HTTP_OPENAI_URL', 'http://openai.waimaoq.com/');
/**
* 生成路由标识
* @param $string
* @return string
* @author zbj
* @date 2023/4/15
*/
if (!function_exists('generateRoute')) {
function generateRoute($string)
{
if(is_array($string)){
$string = $string[0];
}
$sign = str_replace(".", "", trim(strtolower(preg_replace('/[^\w.]+/', '-', trim($string))), '-'));
return $sign;
}
}
/**
* 手动记录错误日志
* @param $title
* @param $params
* @param Throwable $exception
* @author zbj
* @date 2023/4/27
*/
function errorLog($title, $params, Throwable $exception)
{
$exceptionMessage = "错误CODE:" . $exception->getCode() .
"-----错误message:" . $exception->getMessage() .
'------错误文件:' . $exception->getFile() .
'-------错误行数:' . $exception->getLine();
LogUtils::error($title, $params, $exceptionMessage);
}
if (!function_exists('http_post')) {
/**
* 发送http post请求
* @param type $url
* @param type $post_data
*/
function http_post($url, $post_data, $header = [],$is_json = true)
{
if (empty($header)) {
$header = array(
"Accept: application/json",
"Content-Type:application/json;charset=utf-8",
);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
if (curl_errno($ch)) {
$error_message = curl_error($ch);
@file_put_contents(storage_path('logs/lyh_error.log'), var_export($error_message, true) . PHP_EOL, FILE_APPEND);
}
curl_close($ch);
if($is_json){
return json_decode($res, true);
}
return trim($res);
}
}
if (!function_exists('http_get')) {
/**
* 发送http get请求
* @param type $url
* @return []
*/
function http_get($url, $header = [])
{
if (empty($header)) {
$header[] = "content-type: application/json";
}
$ch1 = curl_init();
$timeout = 0;
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_ENCODING, '');
curl_setopt($ch1, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch1, CURLOPT_TIMEOUT, 120);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch1, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$access_txt = curl_exec($ch1);
if (curl_errno($ch1)) {
$error_message = curl_error($ch1);
@file_put_contents(storage_path('logs/lyh_error.log'), var_export($error_message, true) . PHP_EOL, FILE_APPEND);
}
curl_close($ch1);
return json_decode($access_txt, true);
}
}
if (!function_exists('curl_get')) {
function curl_get($url,$is_array=true)
{
$header = array(
'Expect:',
'Content-Type: application/json; charset=utf-8'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 'all');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$content = curl_exec($ch);
curl_close($ch);
return $is_array ? json_decode($content, true) : $content;
}
}
/**
* @remark :判断是否为俄语
* @name :contains_russian
* @author :lyh
* @method :post
* @time :2024/6/5 10:38
*/
function contains_russian($text) {
// 使用正则表达式检查是否包含俄语字符
return preg_match('/[\x{0400}-\x{04FF}]/u', $text) > 0;
}
if (!function_exists('curl_c')) {
/**
* @param $url
* @param $is_array
* @return []
* @author Akun
* @date 2023/11/22 11:33
*/
function curl_c($url,$is_array=true){
$header = array(
'Expect:',
'Content-Type: application/json; charset=utf-8'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 'all');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$content = curl_exec($ch);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
if($http_code == 200){
return $is_array ? json_decode($content, true) : $content;
}else{
return false;
}
}
}
if (!function_exists('_get_child')) {
/**
* 菜单权限->得到子级数组
* @param int
* @return array
*/
function _get_child($my_id, $arr)
{
$new_arr = array();
foreach ($arr as $v) {
$v = (array)$v;
if ($v['pid'] == $my_id) {
$v['sub'] = _get_child($v['id'], $arr);
$new_arr[] = $v;
}
}
return $new_arr ? $new_arr : [];
}
}
if (!function_exists('_get_all_sub')) {
/**
* 獲取所有子集id
* @param int
* @return array
*/
function _get_all_sub($my_id,$id_Arr)
{
$new_arr[] = $my_id;
foreach ($id_Arr as $v) {
if ($v['pid'] == $my_id) {
$new_arr[] = $v['id'];
// 递归查找子节点的子节点
$new_arr = array_merge($new_arr, _get_all_sub($v['id'], $id_Arr));
}
}
return $new_arr ? $new_arr : [];
}
}
if (!function_exists('checkDomain')) {
/**
* 检查并补全域名协议
* @return false|string
* @author zbj
* @date 2023/5/5
*/
function checkDomain($value)
{
$urlParts = parse_url(strtolower($value));
if (empty($urlParts['host'])) {
$urlParts = parse_url('https://' . $value);
}
$host = $urlParts['host'] ?? '';
$scheme = $urlParts['scheme'] ?? 'https';
if (!in_array($scheme, ['http', 'https'])) {
return false;
}
if (preg_match('/^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}$/', $host)) {
return $scheme . '://' . $host . '/';
} else {
return false;
}
}
}
if (!function_exists('page_init')) {
/**
* amp分页初始化
* @param $page
* @param $total
* @author Akun
* @date 2024/01/29 15:43
*/
function page_init($page, $total)
{
//中间页处理
$center_page = [];
if ($total <= 5) {
for ($i = 1; $i <= $total; $i++) {
$center_page[] = $i;
}
} else {
if ($page < 5) {
for ($i = 1; $i <= 5; $i++) {
$center_page[] = $i;
}
} else {
if ($page == $total) {
for ($i = $total - 5; $i <= $total; $i++) {
$center_page[] = $i;
}
} else if ($page <= $total) {
if ($total - $page <= 5) {
if ($total - $page == 1) {
for ($i = $total - 4; $i <= $total; $i++) {
$center_page[] = $i;
}
} else {
for ($i = $page - 2; $i <= $page + 2; $i++) {
$center_page[] = $i;
}
}
} else {
for ($i = $page - 2; $i <= $page + 2; $i++) {
$center_page[] = $i;
}
}
}
}
}
return $center_page;
}
}
/**
* 把返回的数据集转换成Tree
* @param $list array 数据列表
* @param string|int $pk 主键|root
* @param string $pid 父id
* @param string $child 子键
* @param int $root 获取哪个id下面
* @param bool $empty_child 当子数据不存在,是否要返回空子数据
* @return array
*/
function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0, $empty_child = true)
{
// 如果是数字,则是root
if (is_numeric($pk)) {
$root = $pk;
$pk = 'id';
}
// 创建Tree
$tree = array();
if (is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
if ($empty_child) {
$list[$key][$child] = [];
}
$refer[$data[$pk]] =& $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] =& $list[$key];
} else {
if (isset($refer[$parentId])) {
$refer[$parentId][$child][] = &$list[$key];
}
}
}
}
return $tree;
}
if (!function_exists('special2str')) {
/**
* 特殊字符串替换
* @param $str
* @return string|string[]
* @author Akun
* @date 2024/01/30 17:46
*/
function special2str($str)
{
if (strpos($str, ';') === false) {
return $str;
}
$list = [
'<' => '<',
'>' => '>',
'&' => '&',
'´' => '´',
'"' => '“',
' ' => ' '
];
foreach ($list as $k => $v) {
$str = str_replace($k, $v, $str);
}
return $str;
}
}
/**
* tree数据转list
* @param $tree
* @param string $child
* @return array
* @author:dc
* @time 2022/1/11 10:13
*/
function tree_to_list($tree, $child = '_child')
{
$lists = [];
foreach ($tree as $item) {
$c = $item[$child] ?? [];
unset($item[$child]);
$lists[] = $item;
if ($c) {
$lists = array_merge($lists, tree_to_list($c, $child));
}
}
return $lists;
}
if (!function_exists('getThisWeekStarDate')) {
/**
* 获取本周一的日期
* @return mixed
* @author zbj
* @date 2023/5/11
*/
function getThisWeekStarDate()
{
return Carbon::now()->startOfWeek()->toDateString();
}
}
if (!function_exists('object_to_array')) {
/**
* 获取本周一的日期
* @return mixed
* @author zbj
* @date 2023/5/11
*/
function object_to_array($data)
{
if (is_object($data)) {
$data = (array)$data;
} else {
foreach ($data as $k => $v) {
$data[$k] = object_to_array($v);
}
}
return $data;
}
}
if (!function_exists('getPreviousDaysDate')) {
/**
* 获取当前指定前几天日期,默认获取前三天日期
* @param int $day
* @return array
*/
function getPreviousDaysDate(int $day = 3)
{
$days = [];
while ($day > 0) {
$days[] = date("Y-m-d", strtotime("-{$day} days"));
$day -= 1;
}
return $days;
}
}
if (!function_exists('getPreviousMonthsDate')) {
/**
* 获取当前指定前几天日期,默认获取前三天日期
* @param int $month
* @return array
*/
function getPreviousMonthsDate(int $month = 3)
{
$months = [];
while ($month > 0) {
$months[] = date("Y-m", strtotime("-{$month} months"));
$month -= 1;
}
return $months;
}
}
if (!function_exists('getInquiryInformation')) {
/**
* 获取第三方询盘信息
* @return array|string
* @throws GuzzleException
*/
function getInquiryInformation($domain, $sta_date)
{
$token = md5($domain . date("Y-m-d"));
$source = '1,3';
$url = "https://www.globalso.site/api/external-interface/country_con/15243d63ed5a5738?domain={$domain}&token={$token}&source={$source}&sta_date={$sta_date}";
$client = new Client(['verify' => false]);
$http = $client->get($url);
$data = [];
if ($http->getStatusCode() != 200) {
return $data;
}
$content = $http->getBody()->getContents();
$json = json_decode($content, true);
if ($json['status'] != 200) {
return $content;
}
$data['count'] = $json['data']['count'];
$data['lists'] = $json['data']['data'];
return $data;
}
}
if (!function_exists('stringUnderlineLowercase')) {
/**
* 正则 - 名字转为小写并将空格转为下划线
* @param $name
* @return string
*/
function stringUnderlineLowercase($name)
{
return trim(strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $name)));
}
}
if (!function_exists('checkIsGreaterMonth')) {
/**
* 判断传入日期是否大于当月
* @param $date
* @return bool
*/
function checkIsGreaterMonth($date)
{
// 传入日期的时间戳
$timestamp = strtotime($date);
// 当前月份的时间戳
$nowMonth = strtotime(date('Y-m'));
// 判断传入日期是否大于当前月份
return $timestamp > $nowMonth;
}
}
if (!function_exists('checkIsMonth')) {
/**
* 判断传入日期是否是当月
* @param $date
* @return bool
*/
function checkIsMonth($date)
{
// 获取当前时间戳
$now = time();
// 获取当月的起始时间戳和结束时间戳
$firstDay = strtotime(date('Y-m-01', $now));
$lastDay = strtotime(date('Y-m-t', $now));
// 传入日期的时间戳
$timestamp = strtotime($date);
// 判断传入日期是否在当月范围内
return $timestamp >= $firstDay && $timestamp <= $lastDay;
}
}
if (!function_exists('getDateDays')) {
/**
* 返回当月到今天的天数
* @param string|null $date 日期,格式:Y-m
* @return array
*/
function getDateDays(string $date = null)
{
list($year, $month, $day) = explode('-', date('Y-m-d'));
// 获取当前月的第一天
$first_day_of_month = "{$year}-{$month}-01";
// 获取今天的日期
$today = "{$year}-{$month}-{$day}";
if (!is_null($date)) {
$dd = explode('-', $date);
if (!checkIsGreaterMonth($date) && !checkIsMonth($date)) {
$year = $dd[0];
$month = $dd[1];
$first_day_of_month = "{$year}-{$month}-01";
return getDateArray("{$year}-{$month}-" . date("t", strtotime($first_day_of_month)));
}
}
$day_timestamp = strtotime($today) - strtotime($first_day_of_month);
return getDateArray("{$year}-{$month}-" . date('d', $day_timestamp));
}
}
if (!function_exists('getDateArray')) {
/**
* 获取当月获取日期
* @param string $date 日期,格式:Y-m-d
* @return array
*/
function getDateArray(string $date)
{
list($year, $month, $day) = explode('-', date($date));
$i = 1;
$days = [];
while ($i <= $day) {
$days[] = "{$year}-{$month}-" . str_pad($i, 2, "0", STR_PAD_LEFT);
$i++;
}
return $days;
}
}
if (!function_exists('getImageUrl')) {
/**
* @remark :获取图片链接
* @name :getImageUrl
* @author :lyh
* @method :post
* @time :2023/7/20 16:46
*/
function getImageUrl($path,$storage_type = 0,$location = 0){
if(is_array($path)){
$url =[];
foreach ($path as $v){
$url[] = getImageUrl($v,$storage_type,$location);
}
}else{
if(empty($path)){
return '';
}
if((strpos($path,'https://')!== false) || (strpos($path,'http://') !== false)){
return $path;
}
if(substr($path,0,2) == '//'){
return 'https:'.$path;
}
if($location == 0){
$cos = config('filesystems.disks.cos');
$cosCdn = ($storage_type == 0) ? $cos['cdn'] : $cos['cdn1'];
// $cosCdn = 'https://file.globalso.com';//TODO::暂时使用
$url = $cosCdn.$path;
}else{
$s3 = config('filesystems.disks.s3');
$cdn = $s3['cdn'];
$url = $cdn.$path;
}
}
return $url;
}
}
if (!function_exists('getFileUrl')) {
/**
* @remark :获取文件链接
* @name :getImageUrl
* @author :lyh
* @method :post
* @time :2023/7/20 16:46
*/
function getFileUrl($path,$storage_type = 0,$location = 0,$file_cdn = 0){
if(is_array($path)){
$url =[];
foreach ($path as $v){
$url[] = getFileUrl($v,$storage_type,$location,$file_cdn);
}
}else{
if(empty($path)){
return '';
}
if((strpos($path,'https://')!== false) || (strpos($path,'http://') !== false)){
return $path;
}
if(substr($path,0,2) == '//'){
return 'https:'.$path;
}
$file_type = pathinfo($path, PATHINFO_EXTENSION);
$fileTypeArr = ['zip', 'pdf', 'mp4', 'doc', 'docx', 'm4v', 'xlsx'];
if(in_array(strtolower($file_type),$fileTypeArr) && ($file_cdn == 0)){
$cdn2 = config('filesystems.disks.cos')['cdn2'];
return $cdn2.$path;
}
if($location == 0){
$cos = config('filesystems.disks.cos');
$cosCdn = ($storage_type == 0) ? $cos['cdn'] : $cos['cdn1'];
return $cosCdn.$path;
}else{
$s3 = config('filesystems.disks.s3');
$cdn = $s3['cdn'];
return $cdn.$path;
}
}
return $url;
}
}
/**
* @remark :字符串截取
* @name :characterTruncation
* @author :lyh
* @method :post
* @time :2023/6/28 17:39
*/
function characterTruncation($string,$pattern){
preg_match($pattern, $string, $matches);
if (isset($matches[0])) {
$result = $matches[0];
return $result; // 输出:这是footer标签的内容
} else {
return '';
}
}
/**
* @remark :字符串截取
* @name :characterTruncationStr
* @author :lyh
* @method :post
* @time :2024/5/14 16:24
*/
function characterTruncationStr($string,$startStr,$endStr){
$start = strpos($string, $startStr);
$end = strpos($string, $endStr) + strlen($endStr);
return substr($string, $start, $end - $start);
}
if (!function_exists('getAutoLoginCode')) {
/**
* @remark :自动登录加密
* @name :getAutoLoginCode
* @author :lyh
* @method :post
* @time :2023/8/7 9:47
*/
function getAutoLoginCode($project_id)
{
$encrypt = new EncryptUtils();
return $encrypt->authcode(json_encode(['project_id' => $project_id]), 'ENCODE', 'autologin', 300);
}
}
if (!function_exists('str_replace_url')) {
/**
* @remark :截取域名以外的部分
* @name :str_replace_url
* @author :lyh
* @method :post
* @time :2023/8/31 14:57
*/
function str_replace_url($url)
{
$cos = config('filesystems.disks.cos');
$cosCdn = $cos['cdn'];
$cosCdn1 = $cos['cdn1'];
$cosCdn2 = $cos['cdn2'];
$cosCdn3 = config('filesystems.disks.s3')['cdn'];
if($url && ((strpos($url,$cosCdn) !== false) || (strpos($url,$cosCdn1) !== false) || (strpos($url,$cosCdn2) !== false) || (strpos($url,$cosCdn3) !== false))){
// 外部URL无需解析
// 使用 parse_url 函数来解析 URL
$urlParts = parse_url($url);
// 检查是否存在 host(域名)部分
if (isset($urlParts['path'])) {
$urlWithoutDomain = $urlParts['path'];
return $urlWithoutDomain;
}
}
return $url;
}
}
if(!function_exists('curlGet')){
/**
* @remark :忽略证书curl请求
* @name :curlGet
* @author :lyh
* @method :post
* @time :2023/9/12 10:10
*/
function curlGet($url){
$ch1 = curl_init();
$timeout = 0;
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_ENCODING, '');
curl_setopt($ch1, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch1, CURLOPT_HTTPHEADER, array());
curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch1, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$access_txt = curl_exec($ch1);
curl_close($ch1);
return json_decode($access_txt, true);
}
}
function ends_with($string, $suffix)
{
return substr($string, -strlen($suffix)) === $suffix;
}
/**
* @remark :获取二级路由
* @name :getRouteMap
* @author :lyh
* @method :post
* @time :2023/11/10 14:29
*/
function getRouteMap($source,$source_id){
$route = '';
$routeMapModel = new RouteMap();
$info = $routeMapModel->read(['source'=>$source,'source_id'=>$source_id]);
if($info !== false){
if(!empty($info['path'])){
if($info['path'] == 'blog'){
$info['path'] = $info['path'].'s';
}
$route = $info['path'].'/'.$info['route'];
}else{
$route = $info['route'];
}
}
return $route;
}
function redis_get($key){
return Redis::connection()->client()->get($key);
}
function redis_del(...$key){
return Redis::connection()->client()->del(...$key);
}
function redis_set($key,$val,$ttl=3600){
return Redis::connection()->client()->set($key,$val,$ttl);
}
/**
* 添加缓存,存在则失败
* @param $key
* @param $val
* @param int $ttl
* @return mixed
* @author:dc
* @time 2023/10/25 9:48
*/
function redis_add($key,$val,$ttl=3600){
return Redis::connection()->client()->eval(
"return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])", [$key, $val, $ttl], 1
);
}
/**
* 判断远程地址是否需要下载
* @param $url
* @param $project_id
* @param $domain
* @param $is_complete
* @author Akun
* @return bool
* @date 2023/12/08 14:17
*/
function check_remote_url_down($url,$project_id,$domain,$is_complete=0){
if (!$url) {
return '';
}
$arr = parse_url($url);
$scheme = $arr['scheme'] ?? '';
$host = $arr['host'] ?? '';
$host_arr = explode('.',$host);
$path = $arr['path'] ?? '';
if(strpos($host_arr[0], 'cdn') !== false){
return $url;
}
if($host_arr[0] == 'file' && $host_arr[1] == 'globalso'){
return $url;
}
//475项目特殊处理
if($project_id == 475 && $host == 'www.ebuyplc.com'){
$host = 'g934.goodao.net';
}
if($path && substr($path,0,1) != '/'){
$path = '/'.$path;
}
if (
(empty($scheme) || $scheme == 'https' || $scheme == 'http')
&& (empty($host) || (strpos($host_arr[0], 'cdn') === false))
&& $path
&& (strpos($path, '.') !== false)
) {
$url_complete = ($scheme ?: 'https') . '://' . ($host ?: $domain) . $path;
$new_url = CosService::uploadRemote($project_id,'image_product',$url_complete);
if($new_url){
return $is_complete ? getImageUrl($new_url) : $new_url;
}else{
return false;
}
}else{
return false;
}
}
/**
* 文本框转内容数组
* @author zbj
* @date 2024/3/29
*/
function textareaToArr($content, $separator = ','){
return array_values(array_filter(array_unique(array_map(function ($v){
return trim($v);
},explode($separator, $content)))));
}
/**
* @remark :字符串
* @name :base62_encode
* @author :lyh
* @method :post
* @time :2024/6/26 10:46
*/
function ip_to_unique_string($ip) {
// 将IP地址转换为数值表示
$ip_number = ip2long($ip);
// 使用哈希函数生成唯一数值
$hash = hash('sha256', $ip_number, false);
$hash_number = hexdec(substr($hash, 0, 15)); // 取前15位作为大整数
// 将哈希值转换为Base62编码
$unique_string = base62_encode($hash_number);
// 确保唯一字符串为6位,如果不足则补齐,超出则截取前6位
$unique_string = str_pad($unique_string, 6, '0', STR_PAD_LEFT);
$unique_string = substr($unique_string, 0, 6);
return strtolower($unique_string);
}
function base62_encode($num) {
$characters = '23456789abcdefghijkmnpqrstuvwxyz';
$base = strlen($characters);
$result = '';
while ($num > 0) {
$result = $characters[$num % $base] . $result;
$num = intval($num / $base);
}
return $result;
}
/**
* @remark :腾讯云安全的base64
* @name :urlSafeBase64Encode
* @author :lyh
* @method :post
* @time :2024/8/19 14:21
*/
function urlSafeBase64Encode($data = '') {
if(empty($data)){
return $data;
}
// 1. 使用标准的 BASE64 编码
$base64 = base64_encode($data);
// 2. 将加号(+)替换成连接号(-)
$base64 = str_replace('+', '-', $base64);
// 3. 将正斜线(/)替换成下划线(_)
$base64 = str_replace('/', '_', $base64);
// 4. 去掉末尾的等号(=)
$base64 = rtrim($base64, '=');
return $base64;
}
/**
* @remark :获取随机位数字符串
* @name :generateRandomString
* @author :lyh
* @method :post
* @time :2024/9/14 16:45
*/
function generateRandomString($length) {
return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length);
}
if (!function_exists('check_domain_record')) {
/**
* 验证是否cname或者A记录解析到目标服务器
* @param $domain
* @param $server_info
* @return bool
* @author Akun
* @date 2024/10/14 11:02
*/
function check_domain_record($domain, $server_info)
{
try {
$records = dns_get_record($domain,DNS_A);
if(count($records) != 1){
return false;
}
$record = $records[0];
if($record['host'] == $server_info['domain'] || $record['ip'] == $server_info['ip']){
return $domain;
}else{
return false;
}
}catch (\Exception $e){
errorLog('dns_get_record',['domain'=>$domain],$e);
return false;
}
}
}
if (!function_exists('check_curl_status')) {
/**
* 获取域名访问状态码
* @param $url
* @return int
* @author Akun
* @date 2024/12/12 15:52
*/
function check_curl_status($url){
$header = array(
'Expect:',
'Content-Type: application/json; charset=utf-8'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 'all');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_exec($ch);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
return $http_code;
}
}
/**
* 邮箱脱敏
* @author zbj
* @date 2024/10/25
*/
function email_desensitize($email){
$parts = explode('@', $email);
$username = $parts[0] ?? '';
$domain = $parts[1] ?? '';
$maskedUsername = substr($username, 0, -4) . '****';
$maskedDomain = '****.' . substr($domain, -5);
return $maskedUsername . '@' . $maskedDomain;
}
/**
* 按比例取值 [10,30,60]
* @author zbj
* @date 2024/10/25
*/
function getRandByRatio($proArr){
$result = '';
$proSum = array_sum($proArr);
foreach ($proArr as $key => $proCur) {
$randNum = mt_rand(1, $proSum);
if ($randNum <= $proCur) {
$result = $key;
break;
} else {
$proSum -= $proCur;
}
}
unset ($proArr);
return $result;
}
/**
* @remark :随机获取前后缀
* @name :getPrefixKeyword
* @author :lyh
* @method :post
* @time :2025/2/11 14:41
*/
function getPrefixKeyword($project_id, $type, $num)
{
$str = '';
$info = getDeployOptimize($project_id);
if (!empty($info['keyword_' . $type])) {
$fix_keyword = explode(",", $info['keyword_' . $type]);
$fix_keyword = array_filter($fix_keyword);
//随机取
shuffle($fix_keyword);
if (count($fix_keyword) < $num)
return $str;
$keyword = array_slice($fix_keyword, 0, $num);
$str = implode(", ", $keyword);
foreach ($keyword as $k=>$v){
$tmp = rtrim($v, 's');
if (substr_count($str, $tmp) > 1) {
unset($keyword[$k]);
$str = implode(", ", $keyword);
}
}
}
return $str;
}
/**
* @remark :获取客户选择的关键词
* @name :getDeployOptimize
* @author :lyh
* @method :post
* @time :2025/2/11 14:58
*/
function getDeployOptimize($project_id){
$cache_key = 'project_deploy_optimize_info_' . $project_id;
$info = Cache::get($cache_key);
if(!$info){
$projectOptimizeModel = new DeployOptimize();
$info = $projectOptimizeModel->read(['project_id' => $project_id], ['id', 'company_en_name', 'company_en_description', 'keyword_prefix', 'keyword_suffix']);
$projectKeywordModel = new ProjectKeyword();
$keywordInfo = $projectKeywordModel->read(['project_id'=>$project_id]);
$info['main_keyword'] = '';
if(!empty($keywordInfo['main_keyword'])){
$info['main_keyword'] = $keywordInfo['main_keyword'];
}
Cache::put($cache_key, $info, 600);
}
return $info;
}