作者 邓超

x

@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 namespace Controller\v3; 3 namespace Controller\v3;
4 4
5 use Controller\Base; 5 use Controller\Base;
  6 +use Lib\Imap\ImapSearch;
6 use Lib\Mail\Mail; 7 use Lib\Mail\Mail;
7 use Lib\Mail\MailFun; 8 use Lib\Mail\MailFun;
8 use Lib\UploadFile; 9 use Lib\UploadFile;
@@ -37,9 +38,14 @@ class Home extends Base { @@ -37,9 +38,14 @@ class Home extends Base {
37 public function sync(){ 38 public function sync(){
38 $id = app()->request('id'); 39 $id = app()->request('id');
39 if($id && is_numeric($id)){ 40 if($id && is_numeric($id)){
40 - (new SyncMail($id))->sync(); 41 + ob_start();
  42 + $num = (new SyncMail($id))->search((new ImapSearch())->date(date('Y-m-d')))->sync();
  43 + ob_clean();
  44 + if(is_array($num)){
  45 + app()->_json($num);
41 } 46 }
42 - 47 + }
  48 + return 'ok';
43 } 49 }
44 50
45 } 51 }
@@ -126,12 +126,12 @@ class Msg extends Request{ @@ -126,12 +126,12 @@ class Msg extends Request{
126 } 126 }
127 127
128 /** 128 /**
129 - * 只获取uid 129 + * 只获取uid 读取远程的
130 * @return array 130 * @return array
131 * @author:dc 131 * @author:dc
132 * @time 2024/10/12 17:52 132 * @time 2024/10/12 17:52
133 */ 133 */
134 - public function getUids():array { 134 + public function getOriginUids():array {
135 $this->folder->exec(); // 防止在其他文件夹下面 135 $this->folder->exec(); // 防止在其他文件夹下面
136 $this->cmd( 136 $this->cmd(
137 "%sFETCH %s (UID)", 137 "%sFETCH %s (UID)",
@@ -147,6 +147,35 @@ class Msg extends Request{ @@ -147,6 +147,35 @@ class Msg extends Request{
147 return $uids; 147 return $uids;
148 } 148 }
149 149
  150 + /**
  151 + * 读取当前对象下的uid
  152 + * @author:dc
  153 + * @time 2024/11/11 17:36
  154 + */
  155 + public function getUids(){
  156 + if($this->isUid){
  157 + return $this->number;
  158 + }
  159 + return [];
  160 + }
  161 +
  162 + /**
  163 + * 获取当前请求邮件的msgno或者uid
  164 + * @param false $isUid 是否返回uid
  165 + * @return array
  166 + * @author:dc
  167 + * @time 2024/11/11 17:40
  168 + */
  169 + public function getNumber($isUid = false){
  170 + if($isUid){
  171 + return $this->getUids();
  172 + }
  173 + if(!$this->isUid){
  174 + return $this->number;
  175 + }
  176 + return [];
  177 + }
  178 +
150 179
151 /** 180 /**
152 * 读取邮件列表 181 * 读取邮件列表
@@ -7,6 +7,7 @@ use Lib\Imap\Fun; @@ -7,6 +7,7 @@ use Lib\Imap\Fun;
7 use Lib\Imap\Imap; 7 use Lib\Imap\Imap;
8 use Lib\Imap\ImapConfig; 8 use Lib\Imap\ImapConfig;
9 use Lib\Imap\ImapPool; 9 use Lib\Imap\ImapPool;
  10 +use Lib\Imap\ImapSearch;
10 use Lib\Imap\Parse\Folder\Folder; 11 use Lib\Imap\Parse\Folder\Folder;
11 use Lib\Imap\Parse\MessageItem; 12 use Lib\Imap\Parse\MessageItem;
12 use Model\bodySql; 13 use Model\bodySql;
@@ -42,6 +43,11 @@ class SyncMail { @@ -42,6 +43,11 @@ class SyncMail {
42 protected $isStop = false; 43 protected $isStop = false;
43 44
44 /** 45 /**
  46 + * @var 搜索规则
  47 + */
  48 + protected $search = null;
  49 +
  50 + /**
45 * SyncMail constructor. 51 * SyncMail constructor.
46 * @param int|string|array $email 52 * @param int|string|array $email
47 * @throws \Exception 53 * @throws \Exception
@@ -78,6 +84,18 @@ class SyncMail { @@ -78,6 +84,18 @@ class SyncMail {
78 $this->isStop = true; 84 $this->isStop = true;
79 } 85 }
80 86
  87 + /**
  88 + * 搜索
  89 + * @param ImapSearch $search
  90 + * @return $this
  91 + * @author:dc
  92 + * @time 2024/11/11 17:29
  93 + */
  94 + public function search(ImapSearch $search){
  95 + $this->search = $search;
  96 + return $this;
  97 + }
  98 +
81 99
82 protected function emailId(){ 100 protected function emailId(){
83 return $this->email['id']; 101 return $this->email['id'];
@@ -175,7 +193,7 @@ class SyncMail { @@ -175,7 +193,7 @@ class SyncMail {
175 193
176 /** 194 /**
177 * @param bool $syncMail 195 * @param bool $syncMail
178 - * @return bool|void 196 + * @return bool|void|array
179 * @throws \Exception 197 * @throws \Exception
180 * @author:dc 198 * @author:dc
181 * @time 2024/10/18 17:53 199 * @time 2024/10/18 17:53
@@ -198,7 +216,7 @@ class SyncMail { @@ -198,7 +216,7 @@ class SyncMail {
198 216
199 217
200 /********************* 同步邮件 **********************/ 218 /********************* 同步邮件 **********************/
201 - 219 + $syncNum = [];
202 // 循环文件夹 220 // 循环文件夹
203 foreach ($folders->all() as $f){ 221 foreach ($folders->all() as $f){
204 if($this->isStop) return; 222 if($this->isStop) return;
@@ -210,6 +228,7 @@ class SyncMail { @@ -210,6 +228,7 @@ class SyncMail {
210 if ($folder->getTotal()){ 228 if ($folder->getTotal()){
211 $num = $this->mail($folder); 229 $num = $this->mail($folder);
212 if($num){ 230 if($num){
  231 + $syncNum[$folder->getName()] = $num;
213 _echo($this->emailId().' ===> '.$folder->getName().' ===> '.$num); 232 _echo($this->emailId().' ===> '.$folder->getName().' ===> '.$num);
214 } 233 }
215 } 234 }
@@ -239,7 +258,7 @@ class SyncMail { @@ -239,7 +258,7 @@ class SyncMail {
239 } 258 }
240 } 259 }
241 260
242 - 261 + return $syncNum;
243 } 262 }
244 263
245 /** 264 /**
@@ -275,13 +294,19 @@ class SyncMail { @@ -275,13 +294,19 @@ class SyncMail {
275 // 选择成功 294 // 选择成功
276 if($folder->isOk()){ 295 if($folder->isOk()){
277 $msg = $folder->msg(); 296 $msg = $folder->msg();
  297 +
  298 + // 是否搜索
  299 + if ($this->search instanceof ImapSearch){
  300 + $uids = $msg->search($this->search)->getUids();
  301 + }
  302 +
278 if($uids){ 303 if($uids){
279 $this->saveMail($folder_id,$msg->uid($uids)->get()->all(),$isBody); 304 $this->saveMail($folder_id,$msg->uid($uids)->get()->all(),$isBody);
280 }else{ 305 }else{
281 $p=1; 306 $p=1;
282 while (1){ 307 while (1){
283 if($this->isStop) return $sync_number; 308 if($this->isStop) return $sync_number;
284 - $uids = $msg->forPage($p)->getUids(); 309 + $uids = $msg->forPage($p)->getOriginUids();
285 if($uids){ 310 if($uids){
286 $p++; 311 $p++;
287 312