Common.php
8.4 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
<?php
namespace App\Helper;
use App\Models\Ai\AiCommand as AiCommandModel;
use App\Models\Project\Project;
use App\Models\User\UserLog as UserLogModel;
use App\Models\User\UserLogin as UserLoginModel;
use Illuminate\Encryption\Encrypter;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
/**
* @name:
*/
class Common
{
public $key = '66537a12d4fff992f6d8b67fdda6192f';
public $method = 'AES-256-CBC';
/**
* @name :生成用户操作日志
* @return void
* @author :liyuhang
* @method
*/
public static function set_user_log($param = []){
$data = [
'operator_id'=>$param['operator_id'],
'model'=>$param['model'],
'remark'=>$param['remark'],
'project_id'=>$param['project_id']
];
$model = new UserLogModel();
return $model->add($data);
}
/**
* @name :写入登录日志
* @return void
* @author :liyuhang
* @method
*/
public static function set_user_login($param = []){
$data = [
'user_id'=>$param['user_id'],
'ip'=>$param['ip'],
'project_id'=>$param['project_id'] ?: 0,
'remark'=>$param['remark'] ?? '',
];
$model = new UserLoginModel();
return $model->add($data);
}
/**
* @name :ai自动生成
* @return mixed
* @author :liyuhang
* @method
*/
public static function send_openai_msg($url,$param,$name = ''){
$url = HTTP_OPENAI_URL.$url;
$aiCommandModel = New AiCommandModel();
//指定库获取指令
$info = $aiCommandModel->read(['key'=>$param['key']]);
if($info === false){
response('指令不存在',400);
}
if (strpos($param['keywords'], '{') !== false && strpos($param['keywords'], '}') !== false) {
$pattern = '/\{([^}]+)\}/'; // 匹配大括号及其内容
if (preg_match($pattern, $param['keywords'], $matches)) {
$lang = $matches[1]; // 获取捕获的内容
}
} else {
//带原语种翻译
$result = Translate::translateSl($param['keywords']);
if (isset($result['texts']['sl']) && isset(Translate::$tls_list[$result['texts']['sl']])) {
$lang = Translate::$tls_list[$result['texts']['sl']]['text'];
} else {
$lang = 'English';
}
}
$str = 'Please answer in '.$lang;
//替换关键字
$content = str_replace('$keyword$', $param['keywords'], $info['ai']);
//获取公司名称
//$company$变量时替换为公司名
$content = str_replace('$company$', $name , $content);
$data = [
'messages'=>[
// ['role'=>'system','content'=>$info['scene']],
['role'=>'user','content'=>$content.$str],
]
];
return http_post($url,json_encode($data));
}
/**
* @name :获取缓存
* @return void
* @author :liyuhang
* @method
*/
public static function get_user_cache($table,$id,$type = 'B'){
$data = [];
$cache = config('cache.user_is_cache');
if(isset($cache) && ($cache['is_cache'] == true)){
$key = 'cache_'.$table.'_'.$id.'_type';
$data = Cache::store('file')->get($key);
}
return $data;
}
/**
* @name :写入缓存
* @return bool
* @author :liyuhang
* @method
*/
public static function set_user_cache($data = [],$table,$id,$type = 'B'){
$cache = config('cache.user_is_cache');
if(isset($cache) && ($cache['is_cache'] == true)){
$key = 'cache_'.$table.'_'.$id.'_type';
Cache::store('file')->set($key,$data,3600);
}
return true;
}
/**
* @name :清除缓存
* @return bool
* @author :liyuhang
* @method
*/
public static function del_user_cache($table,$id,$type = 'B'){
$cache = config('cache.user_is_cache');
if(isset($cache) && ($cache['is_cache'] == true)){
if(is_array($id)){
foreach ($id as $v){
if(is_array($v)){
continue;
}
$key = 'cache_'.$table.'_'.$v.'_type';
Cache::store('file')->pull($key);
}
}else{
$key = 'cache_'.$table.'_'.$id.'_type';
Cache::store('file')->pull($key);
}
}
return true;
}
/**
* @name :(多维数组去重)array_deduplication
* @author :lyh
* @method :post
* @time :2023/5/9 10:47
*/
public static function uniqueMultiArray($arr) {
$arr = array_map('serialize', $arr);
$arr = array_unique($arr);
$arr = array_map('unserialize', $arr);
return $arr;
}
/**
* @param $targetDateTime
* @name :(获取时间差,精确时分秒,返回天数)getDaysToTargetDate
* @author :lyh
* @method :post
* @time :2023/5/24 9:38
*/
public static function getDaysToTargetDate($targetDateTime)
{
$currentTimestamp = time();
$targetTimestamp = strtotime($targetDateTime);
$days = floor(($currentTimestamp - $targetTimestamp) / (60 * 60 * 24));
return (int)$days;
}
// 生成授授权码
public function encrypt($data)
{
$crypt = new Encrypter($this->key, $this->method);
return $crypt->encrypt($data);
}
// 解密授权码
public function decrypt($string)
{
$crypt = new Encrypter($this->key, $this->method);
return $crypt->decrypt($string);
}
//处理关键词
public static function deal_keywords($data){
$str = ['1. ','2. ','3. ','4. ','5. ','6. ','7. ','8. ','1) ','2) ','3) ','4) ','5) ','6) ','7) ','7) ','8) '];
$ar_keywords_t = explode("\n",$data);
$ar_keywords = [];
foreach ($ar_keywords_t as $v){
if(trim($v)){
$keyword = trim($v);
if(strpos($keyword,'search keyword') !== false){
$tmp_first = explode('1. ',$keyword);
if(count($tmp_first) > 1){
$keyword = $tmp_first[1];
}else{
$tmp_first = explode('1) ',$keyword);
if(count($tmp_first) > 1){
$keyword = $tmp_first[1];
}
}
}
$keyword = str_replace($str,'',$keyword);
$keyword = trim($keyword,'.');
if( (strpos(strtolower($keyword),'hope') === false || strpos(strtolower($keyword),'hopein') !== false)
&& (strpos(strtolower($keyword),'remember') === false || strpos(strtolower($keyword),'rememberance') !== false)
&& strpos(strtolower($keyword),'help') === false
&& strpos(strtolower($keyword),'website') === false
&& strpos(strtolower($keyword),'search keywords') === false
&& strpos(strtolower($keyword),'here are 8') === false
&& strpos(strtolower($keyword),'search keywords') === false
&& strpos(strtolower($keyword),'thank you') === false
&& (strpos(strtolower($keyword),'thanks') === false || strpos(strtolower($keyword),'thanksgiving') !== false)
&& strpos(strtolower($keyword),'copywriter') === false ){
$ar_keywords[] = $keyword;
}
}
}
return implode(', ',$ar_keywords);
}
//过滤特殊字符
public static function deal_str($str){
$str = str_replace([',,'],',',$str);
return str_replace(['{','}','”','“','"', '**SEO Title:**'],'',$str);
}
/**
* 获取网页 title
* @param $url
* @author zbj
* @date 2024/12/31
*/
public static function getUrlTitle($url)
{
try {
$response = Http::withoutVerifying()->get($url);
$html = $response->body();
$dom = new \DOMDocument();
@$dom->loadHTML($html); // 加载 HTML
$title = $dom->getElementsByTagName('title')->item(0); // 获取 <title> 标签
return $title->nodeValue;
} catch (\Exception $e) {
return '';
}
}
}