作者 邓超

v2 调整

@@ -12,7 +12,7 @@ use Model\emailSql; @@ -12,7 +12,7 @@ use Model\emailSql;
12 use Model\folderSql; 12 use Model\folderSql;
13 use Model\listsSql; 13 use Model\listsSql;
14 use Model\sendJobsSql; 14 use Model\sendJobsSql;
15 - 15 +use function Co\run;
16 16
17 17
18 /** 18 /**
@@ -24,23 +24,9 @@ use Model\sendJobsSql; @@ -24,23 +24,9 @@ use Model\sendJobsSql;
24 class Home extends Base { 24 class Home extends Base {
25 25
26 26
27 - /**  
28 - * 邮件列表 针对aicc应用那边  
29 - * @author:dc  
30 - * @time 2023/2/17 14:12  
31 - */  
32 - public function lists(){  
33 -  
34 - $limit = app()->request('limit',20,['intval','abs']);  
35 -  
36 - $last_id = app()->request('last_id',0,['intval','abs']);  
37 - 27 + private function getFolderIds($email_id){
38 $folder_ids = app()->request('folder_ids',[],['intval','abs']); 28 $folder_ids = app()->request('folder_ids',[],['intval','abs']);
39 29
40 - $udate = app()->request('udate',0,'intval');  
41 -  
42 - $where = ['email_id' => $this->getEmail('id')];  
43 -  
44 foreach ($folder_ids as $k=>$folder_id){ 30 foreach ($folder_ids as $k=>$folder_id){
45 if(!$folder_id){ 31 if(!$folder_id){
46 unset($folder_ids[$k]); 32 unset($folder_ids[$k]);
@@ -55,12 +41,34 @@ class Home extends Base { @@ -55,12 +41,34 @@ class Home extends Base {
55 sprintf( 41 sprintf(
56 "select `id` from `%s` where `email_id` = %d and `origin_folder` = 'INBOX'", 42 "select `id` from `%s` where `email_id` = %d and `origin_folder` = 'INBOX'",
57 folderSql::$table 43 folderSql::$table
58 - ,$where['email_id'] 44 + ,$email_id
59 ) 45 )
60 ); 46 );
  47 +
  48 + $folder_ids = [$folder_ids];
  49 + }
  50 +
  51 + return $folder_ids;
61 } 52 }
  53 +
  54 +
  55 + /**
  56 + * 邮件列表 针对aicc应用那边
  57 + * @author:dc
  58 + * @time 2023/2/17 14:12
  59 + */
  60 + public function lists(){
  61 +
  62 + $limit = app()->request('limit',20,['intval','abs']);
  63 +
  64 + $last_id = app()->request('last_id',0,['intval','abs']);
  65 +
  66 + $udate = app()->request('udate',0,'intval');
  67 +
  68 + $where = ['email_id' => $this->getEmail('id')];
  69 +
62 //目录 70 //目录
63 - $where['folder_id'] = $folder_ids; 71 + $where['folder_id'] = $this->getFolderIds($where['email_id']);
64 72
65 73
66 $sql = "`id` > ".$last_id; 74 $sql = "`id` > ".$last_id;
@@ -83,6 +91,79 @@ class Home extends Base { @@ -83,6 +91,79 @@ class Home extends Base {
83 } 91 }
84 92
85 93
  94 + /**
  95 + * v2 版本
  96 + * 同步规定 时间 之后的邮件
  97 + * @return string
  98 + * @throws \Lib\Err
  99 + * @author:dc
  100 + * @time 2023/8/2 16:19
  101 + */
  102 + public function sync(){
  103 + $emails = web_request_emails();
  104 + $udate = app()->request('udate',0,'intval');
  105 + if(!$udate){
  106 + return '';
  107 + }
  108 +
  109 + // 查询邮箱
  110 + $emails = db()->all(emailSql::all(dbWhere(['email'=>$emails])));
  111 + if(!$emails){
  112 + return '';
  113 + }
  114 +
  115 + // 启用协程来处理
  116 + run(function () use ($emails,$udate){
  117 + foreach ($emails as $email){
  118 + // 读取文件夹
  119 + $fids = $this->getFolderIds($email['id']);
  120 + $folders = db()->all(folderSql::all($email['id']));
  121 + // 循环 文件夹
  122 + foreach ($folders as $folder){
  123 + // 是否在同步请求中
  124 + if(in_array($folder['id'],$fids)){
  125 + // 启动 协程
  126 + go(function () use ($email,$udate,$folder){
  127 + // 实例一个邮箱对象
  128 + $mail = new Mail($email['email'],base64_decode($email['password']),$email['imap']);
  129 + // 登录
  130 + $mail->login();
  131 + // 选择 文件夹
  132 + $mail->client->selectFolder($folder['origin_folder']);
  133 + // 最后的时间
  134 + $maxudate = db()->value(
  135 + sprintf(
  136 + "select max(`udate`) from `%s` where `email_id` = %d and `folder_id` = %d limit 1",
  137 + listsSql::$table,
  138 + $email['id'],
  139 + $folder['id']
  140 + )
  141 + );
  142 + $udate = $udate > $maxudate ? $udate : $maxudate;
  143 +
  144 + // 通过时间来搜索uid
  145 + $uids = $mail->client->search(['ON'=>date('Y-m-d H:i:s',$udate)]);
  146 + // 进行同步
  147 + $mail->syncUidEmail(
  148 + $uids,
  149 + $email['id'],
  150 + $folder['origin_folder'],
  151 + $folder['id'],
  152 + [],
  153 + [],
  154 + db()
  155 + );
  156 +
  157 + });
  158 + }
  159 + }
  160 +
  161 + }
  162 +
  163 + });
  164 +
  165 + }
  166 +
86 } 167 }
87 168
88 169
@@ -239,6 +239,46 @@ class Mail { @@ -239,6 +239,46 @@ class Mail {
239 239
240 // 开始同步 240 // 开始同步
241 if($uids){ 241 if($uids){
  242 + $this->syncUidEmail(
  243 + $uids,
  244 + $email_id,
  245 + $folder,
  246 + $folder_id,
  247 + $blacklist,
  248 + $blackFolder,
  249 + $db
  250 + );
  251 +
  252 + }
  253 +
  254 + }
  255 +
  256 + // 更新数量
  257 + $db->update(
  258 + folderSql::$table,
  259 + ['last_sync_time' => time()],
  260 + dbWhere(['id'=>$folder_id]),
  261 + false
  262 + );
  263 +
  264 + return true;
  265 +
  266 + }
  267 +
  268 + /**
  269 + * 同步邮件 只通过 uid获取
  270 + * @param array $uids
  271 + * @param $email_id
  272 + * @param $folder
  273 + * @param $folder_id
  274 + * @param $blacklist
  275 + * @param $blackFolder
  276 + * @param $db
  277 + * @throws \Exception
  278 + * @author:dc
  279 + * @time 2023/8/2 15:35
  280 + */
  281 + public function syncUidEmail(array $uids,$email_id,$folder,$folder_id,$blacklist,$blackFolder,$db){
242 $results = $this->client->fetchHeader($uids,true); 282 $results = $this->client->fetchHeader($uids,true);
243 283
244 if($results && is_array($results)){ 284 if($results && is_array($results)){
@@ -346,21 +386,6 @@ class Mail { @@ -346,21 +386,6 @@ class Mail {
346 $results[$key] = []; 386 $results[$key] = [];
347 } 387 }
348 } 388 }
349 -  
350 - }  
351 -  
352 - }  
353 -  
354 - // 更新数量  
355 - $db->update(  
356 - folderSql::$table,  
357 - ['last_sync_time' => time()],  
358 - dbWhere(['id'=>$folder_id]),  
359 - false  
360 - );  
361 -  
362 - return true;  
363 -  
364 } 389 }
365 390
366 391
@@ -28,6 +28,8 @@ return [ @@ -28,6 +28,8 @@ return [
28 'send' => [\Controller\Home::class, 'send_mail'], 28 'send' => [\Controller\Home::class, 'send_mail'],
29 // 同步请求 29 // 同步请求
30 'sync' => [\Controller\Home::class, 'sync'], 30 'sync' => [\Controller\Home::class, 'sync'],
  31 + // v2 版本的
  32 + 'v2/sync' => [\Controller\v2\Home::class, 'sync'],
31 // 标记为已读 33 // 标记为已读
32 'seen_2_unseen' => [\Controller\Home::class, 'seen_2_unseen'], 34 'seen_2_unseen' => [\Controller\Home::class, 'seen_2_unseen'],
33 // 标记为已回复/未回复 35 // 标记为已回复/未回复