正在显示
6 个修改的文件
包含
515 行增加
和
85 行删除
@@ -11,6 +11,7 @@ | @@ -11,6 +11,7 @@ | ||
11 | "ext-mbstring": "*", | 11 | "ext-mbstring": "*", |
12 | "ext-pdo": "*", | 12 | "ext-pdo": "*", |
13 | "ext-redis": "*", | 13 | "ext-redis": "*", |
14 | + "elasticsearch/elasticsearch": "7.x", | ||
14 | "phpmailer/phpmailer": "^6.7", | 15 | "phpmailer/phpmailer": "^6.7", |
15 | "swlib/saber": "^1.0" | 16 | "swlib/saber": "^1.0" |
16 | }, | 17 | }, |
@@ -35,7 +36,8 @@ | @@ -35,7 +36,8 @@ | ||
35 | "preferred-install": "dist", | 36 | "preferred-install": "dist", |
36 | "sort-packages": true, | 37 | "sort-packages": true, |
37 | "allow-plugins": { | 38 | "allow-plugins": { |
38 | - "pestphp/pest-plugin": true | 39 | + "pestphp/pest-plugin": true, |
40 | + "php-http/discovery": true | ||
39 | } | 41 | } |
40 | }, | 42 | }, |
41 | "minimum-stability": "stable", | 43 | "minimum-stability": "stable", |
@@ -16,17 +16,63 @@ class Test { | @@ -16,17 +16,63 @@ class Test { | ||
16 | 16 | ||
17 | 17 | ||
18 | public function home(){ | 18 | public function home(){ |
19 | - if(app()->request('sign')!='s1'){ | ||
20 | - http_response_code(404); | ||
21 | - app()->e(404,404); | ||
22 | - } | ||
23 | 19 | ||
24 | - $email = db()->first(emailSql::first(app()->request('mid'))); | ||
25 | 20 | ||
26 | - $config = (new \Lib\Imap\ImapConfig()) | ||
27 | - ->setEmail($email['email']) | ||
28 | - ->setPassword(base64_decode($email['password'])) | ||
29 | - ->setHost($email['imap']); | 21 | + |
22 | +// $urlGetSign = function ($mobile){ | ||
23 | +// $time = time(); | ||
24 | +// $s = 'MindrHbWGJaEfhw85oQW3LYTnUMebm4H'; | ||
25 | +// $data = [ | ||
26 | +// 'app_id' => 10002415, | ||
27 | +// 'timestamp' => $time | ||
28 | +// ]; | ||
29 | +// $data = http_build_query($data); | ||
30 | +// | ||
31 | +// $sign = md5(md5($data).$s); | ||
32 | +// | ||
33 | +// return 'http://local.admin.hagro.cn/'.app()->request('route').'?iframe_sign='. | ||
34 | +// base64_encode($mobile.'.'.$sign.'.'.$time); | ||
35 | +// }; | ||
36 | +// | ||
37 | +// | ||
38 | +// $url = $urlGetSign('18868868868'); | ||
39 | +// return '<html> | ||
40 | +//<head><title>403 您没有权限访问</title></head> | ||
41 | +//<body> | ||
42 | +//<div style="display: flex"> | ||
43 | +//<div style="width: 200px;"> | ||
44 | +//<p><a href="/?route=ai_search">ai搜索</a></p> | ||
45 | +//<p><a href="/?route=ai_company_search">企业洞察</a></p> | ||
46 | +//<p><a href="/?route=ai_keyperson">ai决策人</a></p> | ||
47 | +//<p><a href="/?route=customer_guide">海关数据</a></p> | ||
48 | +//<p><a href="/?route=tools/tool_mobile_verify">验证手机号</a></p> | ||
49 | +//<p><a href="/?route=tools/tool_translate">智能翻译</a></p> | ||
50 | +//<p><a href="/?route=tools/tool_exchange_rate">实时汇率</a></p> | ||
51 | +//<p><a href="/?route=tools/tool_mailbox_verify">邮箱验证</a></p> | ||
52 | +//<p><a href="/?route=tools/tool_vat">VAT查询</a></p> | ||
53 | +//<p><a href="/?route=pro/article/31">海外社媒(AI)</a></p> | ||
54 | +//<p><a href="/?route=pro/article/29">网站文案(AI)</a></p> | ||
55 | +//<p><a href="/?route=pro/article/73">邮件助手(AI)</a></p> | ||
56 | +//<p><a href="/?route=pro/article/74">企业应用(AI)</a></p> | ||
57 | +//<p><a href="/?route=pro/e/commerce">跨境电商(AI)</a></p> | ||
58 | +//</div> | ||
59 | +//<iframe src="'.$url.'" frameborder="0" style="width: 100%;height: 100vh;"></iframe> | ||
60 | +//</div> | ||
61 | +// | ||
62 | +//</body> | ||
63 | +//</html>'; | ||
64 | + | ||
65 | +// if(app()->request('sign')!='s1'){ | ||
66 | +// http_response_code(404); | ||
67 | +// app()->e(404,404); | ||
68 | +// } | ||
69 | +// | ||
70 | +//// $email = db()->first(emailSql::first(app()->request('mid'))); | ||
71 | +// | ||
72 | +// $config = (new \Lib\Imap\ImapConfig()) | ||
73 | +// ->setEmail('CService001@spicay-meart.com') | ||
74 | +// ->setPassword('5YmS77JBb7JJwuaD') | ||
75 | +// ->setHost('imap.exmail.qq.com'); | ||
30 | 76 | ||
31 | 77 | ||
32 | $imap = \Lib\Imap\ImapPool::get($config); | 78 | $imap = \Lib\Imap\ImapPool::get($config); |
@@ -41,23 +87,26 @@ class Test { | @@ -41,23 +87,26 @@ class Test { | ||
41 | // echo '|'; | 87 | // echo '|'; |
42 | // }; | 88 | // }; |
43 | $folder = $imap->folder('INBOX'); | 89 | $folder = $imap->folder('INBOX'); |
44 | -// echo '总共有:'; | ||
45 | -// echo $folder->getTotal(); | ||
46 | -// echo "<br>"; | ||
47 | -// echo "<br>"; | ||
48 | -// echo '<a href="?msgno='.($msgno+1).'">下一个</a>'; | ||
49 | -// echo "<br>"; | ||
50 | -// echo "<hr>"; | ||
51 | - | ||
52 | - $msg = $folder->msg()->msgno($msgno)->get()->first(); | ||
53 | - if ($msg) { | 90 | + echo '总共有:'; |
91 | + echo $folder->getTotal(); | ||
92 | + echo "<br>"; | ||
93 | + echo "<br>"; | ||
94 | + echo '<a href="?msgno='.($msgno+1).'">下一个</a>'; | ||
95 | + echo "<br>"; | ||
96 | + echo "<hr>"; | ||
97 | + | ||
98 | + $msgs = $folder->msg()->forPage(449)->get(); | ||
99 | + if ($msgs) { | ||
100 | + $msgs->each(function ($msg){ | ||
101 | + echo '<p>'.$msg->uid.'==>'.$msg->header->getSubject().' =====> '.date('Y-m-d H:i:s',strtotime($msg->date)).'</p>'; | ||
102 | + }); | ||
54 | // echo $msg->header->getSubject(); | 103 | // echo $msg->header->getSubject(); |
55 | // echo '<br>-------------------------------------------------<br>'; | 104 | // echo '<br>-------------------------------------------------<br>'; |
56 | - if($msg->body->getHtml()){ | ||
57 | - echo $msg->body->getHtml(); | ||
58 | - }else{ | ||
59 | - echo $msg->body->getText(); | ||
60 | - } | 105 | +// if($msg->body->getHtml()){ |
106 | +// echo $msg->body->getHtml(); | ||
107 | +// }else{ | ||
108 | +// echo $msg->body->getText(); | ||
109 | +// } | ||
61 | // $msg->header->getRaw() | 110 | // $msg->header->getRaw() |
62 | // echo $msg->body->getAttachment(); | 111 | // echo $msg->body->getAttachment(); |
63 | // foreach ($msg->body->getAttachment() as $attachment){ | 112 | // foreach ($msg->body->getAttachment() as $attachment){ |
@@ -96,66 +96,6 @@ class SyncMail { | @@ -96,66 +96,6 @@ class SyncMail { | ||
96 | return $this->db->update(listsSql::$table,['is_hots'=>1],dbWhere(['id'=>$id])); | 96 | return $this->db->update(listsSql::$table,['is_hots'=>1],dbWhere(['id'=>$id])); |
97 | } | 97 | } |
98 | 98 | ||
99 | - /** | ||
100 | - * 验证邮箱 | ||
101 | - * @param $email | ||
102 | - * @author:dc | ||
103 | - * @time 2024/8/28 14:33 | ||
104 | - */ | ||
105 | - public function checkEmail($email){ | ||
106 | - // mimecast@wsa.aero | ||
107 | - $filterEmail = [ | ||
108 | - 'mimecast@wsa.aero', | ||
109 | - 'support@sugardaddymeet.com', | ||
110 | - 'registration@facebookmail.com', | ||
111 | - ]; | ||
112 | - if(in_array($email,$filterEmail)){ | ||
113 | - return true; | ||
114 | - } | ||
115 | - | ||
116 | - if(preg_match("/^((no-?reply)|(postmaster)|(mailer-daemon)|(email-notifications)|(googleplay-noreply)|(postmaster-noreply)|(privacy-noreply))@/i",$email)){ | ||
117 | - return true; | ||
118 | - } | ||
119 | - | ||
120 | - if(preg_match("/((no-?reply)|(postmaster)|(mailer-daemon)|(email-notifications))/i",$email)){ | ||
121 | - return true; | ||
122 | - } | ||
123 | - | ||
124 | - | ||
125 | - return false; | ||
126 | - } | ||
127 | - | ||
128 | - /** | ||
129 | - * 验证标题是否存在某些关键词 | ||
130 | - * @param string $subject | ||
131 | - * @return bool | ||
132 | - * @author:dc | ||
133 | - * @time 2024/8/24 15:09 | ||
134 | - */ | ||
135 | - public function checkSubject(string $subject){ | ||
136 | - | ||
137 | - $keys = [ | ||
138 | - 'Automatic reply: ', | ||
139 | - 'Delivery Failure', | ||
140 | - 'Automatische Antwort:', | ||
141 | - 'Automatic_reply:', | ||
142 | - 'Undelivered Mail Returned', | ||
143 | - 'Mail delivery failed:', | ||
144 | - 'Delivery Status Notification', | ||
145 | - 'Undeliverable', | ||
146 | - 'failure', | ||
147 | - 'Undelivered', | ||
148 | - ]; | ||
149 | - | ||
150 | - foreach ($keys as $key){ | ||
151 | - if(stripos($subject,$key)!==false){ | ||
152 | - return true; | ||
153 | - } | ||
154 | - } | ||
155 | - | ||
156 | - return false; | ||
157 | - } | ||
158 | - | ||
159 | 99 | ||
160 | /** | 100 | /** |
161 | * 自动回复邮箱 | 101 | * 自动回复邮箱 |
event/SyncMailToEs.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | +namespace Event; | ||
4 | + | ||
5 | +use Lib\Es\Es; | ||
6 | +use Model\listsSql; | ||
7 | + | ||
8 | + | ||
9 | +/** | ||
10 | + * 同步数据到es | ||
11 | + * @author:dc | ||
12 | + * @time 2025/3/1 17:57 | ||
13 | + * Class SyncMailToEs | ||
14 | + * @package Event | ||
15 | + */ | ||
16 | +class SyncMailToEs { | ||
17 | + | ||
18 | + public $type = ''; | ||
19 | + public $table = ''; | ||
20 | + public $where = ''; | ||
21 | + public $data = []; | ||
22 | + | ||
23 | + public function __construct(string $type,int $table, $where=[],$data=[]) | ||
24 | + { | ||
25 | + $this->type = $type; | ||
26 | + $this->table = $table; | ||
27 | + if ($type == 'create'){ | ||
28 | + $this->data = $where; | ||
29 | + }else{ | ||
30 | + $this->where = is_array($where) ? dbWhere($where) : $where; | ||
31 | + $this->data = $data; | ||
32 | + } | ||
33 | + | ||
34 | + $this->handler(); | ||
35 | + } | ||
36 | + | ||
37 | + /** | ||
38 | + * @var \Lib\Es\Es | ||
39 | + */ | ||
40 | + public $es; | ||
41 | + | ||
42 | + public function handler(){ | ||
43 | + $this->es = new Es('hg_ai_emails'); | ||
44 | + if(in_array($this->table,['lists','lists_auto','lists_hot'])){ | ||
45 | + | ||
46 | + if($this->type=='create'){ | ||
47 | + $this->create(); | ||
48 | + } | ||
49 | + else if($this->type=='update'){ | ||
50 | + $this->update(); | ||
51 | + } | ||
52 | + else{ | ||
53 | + | ||
54 | + } | ||
55 | + | ||
56 | + } | ||
57 | + } | ||
58 | + | ||
59 | + /** | ||
60 | + * @author:dc | ||
61 | + * @time 2025/3/3 9:41 | ||
62 | + */ | ||
63 | + public function create(){ | ||
64 | + if($this->table == 'lists_hot'){ | ||
65 | + return ; | ||
66 | + } | ||
67 | + else if($this->table == 'lists'){ | ||
68 | + $id = $this->data['email_id'].'_'.$this->data['folder_id'].'_'.$this->data['uid']; | ||
69 | + if(empty($this->data['is_auto'])){ | ||
70 | + $this->data['is_auto'] = 0; | ||
71 | + } | ||
72 | + $this->es->create($this->data,$id); | ||
73 | + } | ||
74 | + else if($this->table == 'lists_auto'){ | ||
75 | + $id = db()->first(listsSql::first($this->data['lists_id'],'`email_id`,`folder_id`,`uid`')); | ||
76 | + $id = $id['email_id'].'_'.$id['folder_id'].'_'.$id['uid']; | ||
77 | + $this->es->update($id,['is_auto'=>1]); | ||
78 | + } | ||
79 | + | ||
80 | + } | ||
81 | + | ||
82 | + /** | ||
83 | + * 更新数据 | ||
84 | + * @author:dc | ||
85 | + * @time 2025/3/3 10:53 | ||
86 | + */ | ||
87 | + public function update(){ | ||
88 | +// if($this->table == 'lists_hot'){ | ||
89 | +// | ||
90 | +// } | ||
91 | + if($this->table == 'lists'){ | ||
92 | + // 更新es | ||
93 | + $lists = db()->all(listsSql::all($this->where,'`email_id`,`folder_id`,`uid`')); | ||
94 | + foreach ($lists as $list){ | ||
95 | + $id = $list['email_id'].'_'.$list['folder_id'].'_'.$list['uid']; | ||
96 | + $this->es->update($id,$this->data); | ||
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 | + |
@@ -142,6 +142,9 @@ trait DbQuery { | @@ -142,6 +142,9 @@ trait DbQuery { | ||
142 | $data['updated_at'] = empty($data['updated_at']) ? date('Y-m-d H:i:s') : $data['updated_at']; | 142 | $data['updated_at'] = empty($data['updated_at']) ? date('Y-m-d H:i:s') : $data['updated_at']; |
143 | } | 143 | } |
144 | 144 | ||
145 | + // es 同步数据 | ||
146 | + \Event\Event::call('\Event\SyncMailToEs','update',$table,$where,$data); | ||
147 | + | ||
145 | $sql = "update `{$table}` set ".dbUpdate($data). " where ".$where; | 148 | $sql = "update `{$table}` set ".dbUpdate($data). " where ".$where; |
146 | 149 | ||
147 | $data = $this->getData($data); | 150 | $data = $this->getData($data); |
@@ -215,6 +218,9 @@ trait DbQuery { | @@ -215,6 +218,9 @@ trait DbQuery { | ||
215 | $query = $this->query([$sql,$data]); | 218 | $query = $this->query([$sql,$data]); |
216 | 219 | ||
217 | if($query){ | 220 | if($query){ |
221 | + $data['uuid'] = $this->getClient()->lastInsertId(); | ||
222 | + // es 同步数据 | ||
223 | + \Event\Event::call('\Event\SyncMailToEs','create',$table, $data); | ||
218 | return true; | 224 | return true; |
219 | } | 225 | } |
220 | 226 | ||
@@ -235,6 +241,8 @@ trait DbQuery { | @@ -235,6 +241,8 @@ trait DbQuery { | ||
235 | if($upFiled){ | 241 | if($upFiled){ |
236 | return $this->update($table,[$upFiled === true ? 'deleted_at' : $upFiled =>time()],$where); | 242 | return $this->update($table,[$upFiled === true ? 'deleted_at' : $upFiled =>time()],$where); |
237 | } | 243 | } |
244 | + // es 同步数据 | ||
245 | + \Event\Event::call('\Event\SyncMailToEs','delete',$table, $where); | ||
238 | 246 | ||
239 | $sql = "delete from `{$table}` where ".dbWhere($where); | 247 | $sql = "delete from `{$table}` where ".dbWhere($where); |
240 | 248 |
lib/Es/Es.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | +namespace Lib\Es; | ||
4 | + | ||
5 | +use Elasticsearch\ClientBuilder; | ||
6 | + | ||
7 | +/** | ||
8 | + * @see https://github.com/elastic/elasticsearch-php/tree/7.17 | ||
9 | + * @author:dc | ||
10 | + * @time 2023/6/5 10:13 | ||
11 | + * Class Es | ||
12 | + * @package GlobalSo\Tool\Es | ||
13 | + */ | ||
14 | +class Es { | ||
15 | + | ||
16 | +// private $host = 'https://es-gqtfpyon.public.tencentelasticsearch.com:9200'; // 黑格那个服务器 | ||
17 | +// private $host = 'http://elastic:1qOtfZhqy4B7IXdIpl_W@192.168.80.129:9200'; | ||
18 | +// private $host = 'https://es-az664rii.public.tencentelasticsearch.com:9200'; // aicc 服务器 | ||
19 | + private $host = [ | ||
20 | +// 'http://elastic:1qOtfZhqy4B7IXdIpl_W@192.168.80.129:9200', | ||
21 | + 'https://elastic:rEHsd8xf4xGJKHdD@es-gqtfpyon.public.tencentelasticsearch.com:9200' | ||
22 | + ]; //内网地址 公网要加ip白名单 | ||
23 | + | ||
24 | + | ||
25 | + /** | ||
26 | + * @var \Elasticsearch\Client | ||
27 | + */ | ||
28 | + protected $client; | ||
29 | + | ||
30 | + /** | ||
31 | + * @var string | ||
32 | + */ | ||
33 | + protected $index; | ||
34 | + | ||
35 | + /** | ||
36 | + * Es constructor. | ||
37 | + * @param $index | ||
38 | + */ | ||
39 | + public function __construct($index) | ||
40 | + { | ||
41 | + | ||
42 | + $this->index = $index; | ||
43 | + | ||
44 | + $this->client = ClientBuilder::create() | ||
45 | + ->setHosts($this->host) | ||
46 | +// ->setBasicAuthentication($user, $password) | ||
47 | +// ->setCABundle('path/to/http_ca.crt') | ||
48 | + ->build(); | ||
49 | + } | ||
50 | + | ||
51 | + | ||
52 | + | ||
53 | + /** | ||
54 | + * 搜索 | ||
55 | + * @param array $body | ||
56 | + * @param int $from | ||
57 | + * @param int $size | ||
58 | + * @param array $sort | ||
59 | + * @return array | ||
60 | + * @author:dc | ||
61 | + * @time 2023/6/5 14:35 | ||
62 | + */ | ||
63 | + public function search(array $body,int $from=0,int $size=20,array $sort= []){ | ||
64 | + | ||
65 | + $params = [ | ||
66 | + 'index' => $this->getIndex(), | ||
67 | + 'body' => $body, | ||
68 | + 'from' => $from, | ||
69 | + 'size' => $size | ||
70 | + ]; | ||
71 | + // 排序 | ||
72 | + if($sort){ | ||
73 | + $params['sort'] = $sort; | ||
74 | + } | ||
75 | + | ||
76 | + | ||
77 | + try { | ||
78 | + $response = $this->client->search($params); | ||
79 | + }catch (\Throwable $e) { | ||
80 | + logs("搜索数据es:".$e->getMessage()); | ||
81 | + return []; | ||
82 | + } | ||
83 | + | ||
84 | + return $response; | ||
85 | + | ||
86 | + } | ||
87 | + | ||
88 | + | ||
89 | + /** | ||
90 | + * @param array $params | ||
91 | + * @return array|callable | ||
92 | + * @author:dc | ||
93 | + * @time 2023/6/9 15:50 | ||
94 | + */ | ||
95 | + public function get(array $param){ | ||
96 | + $params = [ | ||
97 | + 'index' => $this->getIndex() | ||
98 | + ]; | ||
99 | + if(!empty($param['id'])){ | ||
100 | + $params['id'] = $param['id']; | ||
101 | + } | ||
102 | + | ||
103 | + try { | ||
104 | + $response = $this->client->get($params); | ||
105 | + }catch (\Throwable $e) { | ||
106 | + logs("读取数据es:".$e->getMessage()); | ||
107 | + return []; | ||
108 | + } | ||
109 | + | ||
110 | + return $response; | ||
111 | + } | ||
112 | + | ||
113 | + | ||
114 | + | ||
115 | + /** | ||
116 | + * 获取索引 | ||
117 | + * @return string | ||
118 | + * @author:dc | ||
119 | + * @time 2023/6/5 10:51 | ||
120 | + */ | ||
121 | + public function getIndex():string{ | ||
122 | + return $this->index; | ||
123 | + } | ||
124 | + | ||
125 | + | ||
126 | + /** | ||
127 | + * 保存修改 | ||
128 | + * @param string $id | ||
129 | + * @param array $data | ||
130 | + * @return int | ||
131 | + * @author:dc | ||
132 | + * @time 2025/3/1 14:53 | ||
133 | + */ | ||
134 | + public function save(string $id, array $data){ | ||
135 | + | ||
136 | + $params['index'] = $this->getIndex(); | ||
137 | + $params['id'] = $id; | ||
138 | + $params['body'] = $data; | ||
139 | + | ||
140 | + try { | ||
141 | + $response = $this->client->index($params); | ||
142 | + }catch (\Throwable $e) { | ||
143 | + logs("创建或者修改es:".$e->getMessage()); | ||
144 | + return 500; | ||
145 | + } | ||
146 | + | ||
147 | + return $response['_shards']['successful']?200:500; | ||
148 | + } | ||
149 | + | ||
150 | + /** | ||
151 | + * 创建数据 | ||
152 | + * @param array $params | ||
153 | + * @return int | ||
154 | + * @author:dc | ||
155 | + * @time 2025/3/1 14:25 | ||
156 | + */ | ||
157 | + public function create(array $data, string $id = ''){ | ||
158 | + $params['index'] = $this->getIndex(); | ||
159 | + $params['body'] = $data; | ||
160 | + if($id){ | ||
161 | + $params['id'] = $id; | ||
162 | + } | ||
163 | + try { | ||
164 | + $response = $this->client->create($params); | ||
165 | + }catch (\Throwable $e) { | ||
166 | + logs("新增es:".$e->getMessage()); | ||
167 | + return 500; | ||
168 | + } | ||
169 | + | ||
170 | + return $response['_shards']['successful']?200:500; | ||
171 | + } | ||
172 | + | ||
173 | + /** | ||
174 | + * 更新文档 | ||
175 | + * @param string $id | ||
176 | + * @param array $data | ||
177 | + * @return int | ||
178 | + * @author:dc | ||
179 | + * @time 2025/3/1 14:30 | ||
180 | + */ | ||
181 | + public function update(string $id, array $data){ | ||
182 | + $params['index'] = $this->getIndex(); | ||
183 | + $params = [ | ||
184 | + 'index' => $this->getIndex(), | ||
185 | + 'id' => $id, | ||
186 | + 'body' => [ | ||
187 | + 'doc' => $data | ||
188 | + ] | ||
189 | + ]; | ||
190 | + try { | ||
191 | + $response = $this->client->update($params); | ||
192 | + }catch (\Throwable $e) { | ||
193 | + logs("更新es:".$e->getMessage()); | ||
194 | + return 500; | ||
195 | + } | ||
196 | + | ||
197 | + return $response['_shards']['successful']?200:500; | ||
198 | + } | ||
199 | + | ||
200 | + | ||
201 | + /** | ||
202 | + * 删除 | ||
203 | + * @param string $id | ||
204 | + * @return array|callable|false | ||
205 | + * @author:dc | ||
206 | + * @time 2023/6/5 16:26 | ||
207 | + */ | ||
208 | + public function delete(string $id){ | ||
209 | + | ||
210 | + | ||
211 | + try { | ||
212 | + $response = $this->client->delete([ | ||
213 | + 'index' => $this->getIndex(), | ||
214 | + 'id' => $id | ||
215 | + ]); | ||
216 | + } catch (\Throwable $e) { | ||
217 | + logs('删除es数据:'.$e->getMessage()); | ||
218 | + return false; | ||
219 | + } | ||
220 | + | ||
221 | + | ||
222 | + return $response; | ||
223 | + | ||
224 | + | ||
225 | + } | ||
226 | + | ||
227 | + | ||
228 | + /** | ||
229 | + * @return \Elasticsearch\Client | ||
230 | + * @author:dc | ||
231 | + * @time 2025/3/1 16:23 | ||
232 | + */ | ||
233 | + public function getClient() | ||
234 | + { | ||
235 | + return $this->client; | ||
236 | + } | ||
237 | + | ||
238 | + | ||
239 | + /** | ||
240 | + * 更新索引 Mapping | ||
241 | + * @param array $params | ||
242 | + * @author:dc | ||
243 | + * @time 2023/6/29 11:17 | ||
244 | + */ | ||
245 | + public function putMapping(array $params,array $setting=[]){ | ||
246 | + $params = [ | ||
247 | + "body" => [ | ||
248 | + "mappings" => $params, | ||
249 | + ] | ||
250 | + ]; | ||
251 | + $params['index'] = $this->getIndex(); | ||
252 | + if($setting){ | ||
253 | + $params['body']['settings'] = $setting; | ||
254 | + } | ||
255 | + | ||
256 | + try { | ||
257 | + $response = $this->client->indices()->create($params); | ||
258 | + }catch (\Throwable $e) { | ||
259 | + logs('es 创建索引 映射:'.$e->getMessage()); | ||
260 | + return false; | ||
261 | + } | ||
262 | + | ||
263 | + return $response['acknowledged']??false; | ||
264 | + } | ||
265 | + | ||
266 | + /** | ||
267 | + * @return array|false | ||
268 | + * @author:dc | ||
269 | + * @time 2024/1/9 10:12 | ||
270 | + */ | ||
271 | + public function getMapping(){ | ||
272 | + $params = []; | ||
273 | + $params['index'] = $this->getIndex(); | ||
274 | + | ||
275 | + try { | ||
276 | + $response = $this->client->indices()->getMapping($params); | ||
277 | + }catch (\Throwable $e) { | ||
278 | + return []; | ||
279 | + } | ||
280 | + | ||
281 | + return $response[$this->getIndex()]['mappings']['properties']??[]; | ||
282 | + } | ||
283 | + | ||
284 | + /** | ||
285 | + * 判断是否存在索引 | ||
286 | + * @return bool | ||
287 | + * @author:dc | ||
288 | + * @time 2023/6/29 12:37 | ||
289 | + */ | ||
290 | + public function hasIndex(){ | ||
291 | + $params['index'] = $this->getIndex(); | ||
292 | + $response = $this->client->indices()->exists($params); | ||
293 | + return $response; | ||
294 | + } | ||
295 | + | ||
296 | + | ||
297 | + | ||
298 | +} |
-
请 注册 或 登录 后发表评论