...
|
...
|
@@ -3,9 +3,11 @@ |
|
|
namespace Controller\v2;
|
|
|
|
|
|
use Controller\Base;
|
|
|
use Lib\Imap\ImapSearch;
|
|
|
use Lib\Mail\Mail;
|
|
|
use Model\folderSql;
|
|
|
use Model\listsSql;
|
|
|
use Service\SyncMail;
|
|
|
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -47,6 +49,9 @@ class Home extends Base { |
|
|
$folder_ids = [$folder_ids];
|
|
|
}
|
|
|
|
|
|
if(!$folder_ids){
|
|
|
return [-1];
|
|
|
}
|
|
|
return $folder_ids;
|
|
|
}
|
|
|
|
...
|
...
|
@@ -57,33 +62,44 @@ class Home extends Base { |
|
|
* @time 2023/2/17 14:12
|
|
|
*/
|
|
|
public function lists(){
|
|
|
logs('v2 home lists');
|
|
|
|
|
|
$limit = app()->request('limit',20,['intval','abs']);
|
|
|
|
|
|
$last_id = app()->request('last_id',0,['intval','abs']);
|
|
|
|
|
|
$udate = app()->request('udate',0,'intval');
|
|
|
|
|
|
$where = ['email_id' => $this->getEmail('id')];
|
|
|
|
|
|
//目录
|
|
|
$where['folder_id'] = $this->getFolderIds($where['email_id']);
|
|
|
|
|
|
|
|
|
$sql = "`id` > ".$last_id;
|
|
|
$range = [
|
|
|
'uuid' => [
|
|
|
'gte' => $last_id, // 大于等于开始日期
|
|
|
]
|
|
|
];
|
|
|
if($udate){
|
|
|
$sql .= " and `udate` > ".$udate;
|
|
|
$range['udate'] = [
|
|
|
'gte' => $udate, // 大于等于开始日期
|
|
|
];
|
|
|
}
|
|
|
|
|
|
$lists = db()->all(
|
|
|
sprintf(
|
|
|
"select `id`,`subject`,`from`,`from_name`,`seen`,`udate`,`email_id` from `%s` where %s and %s order by `id` asc limit %d"
|
|
|
,listsSql::$table
|
|
|
,$sql
|
|
|
,dbWhere($where)
|
|
|
,$limit
|
|
|
)
|
|
|
);
|
|
|
$query = [
|
|
|
'query' => [
|
|
|
'bool'=>[
|
|
|
'must'=>[
|
|
|
['term'=>[
|
|
|
'email_id'=>$this->getEmail('id')
|
|
|
]],
|
|
|
['terms'=>[
|
|
|
'folder_id'=>$this->getFolderIds($this->getEmail('id'))
|
|
|
]],
|
|
|
'range'=>$range
|
|
|
]
|
|
|
]
|
|
|
]
|
|
|
];
|
|
|
|
|
|
$lists = es()->search($query,0,$limit,[],false);
|
|
|
|
|
|
$lists = $lists['hits']['hits']??[];
|
|
|
|
|
|
app()->_json(['data'=>$lists?:[]]);
|
|
|
|
...
|
...
|
@@ -99,102 +115,20 @@ class Home extends Base { |
|
|
* @time 2023/8/2 16:19
|
|
|
*/
|
|
|
public function sync(){
|
|
|
logs('v2 home sync');
|
|
|
|
|
|
ignore_user_abort(true);
|
|
|
set_time_limit(0);
|
|
|
|
|
|
$email = $this->getEmail();
|
|
|
$udate = app()->request('udate',0,'intval');
|
|
|
if(!$udate){
|
|
|
return 'no udate';
|
|
|
}
|
|
|
// 读取文件夹
|
|
|
$fids = $this->getFolderIds($email['id']);
|
|
|
$folders = db()->all(folderSql::all($email['id']));
|
|
|
// 启动 协程
|
|
|
// 实例一个邮箱对象
|
|
|
$mail = new Mail($email['email'],base64_decode($email['password']),$email['imap']);
|
|
|
// 登录
|
|
|
if($mail->login()!=1){
|
|
|
return '登录失败';
|
|
|
}
|
|
|
|
|
|
if(!$folders){
|
|
|
$mail->syncFolder($email['id']);
|
|
|
return 'sync folder';
|
|
|
}
|
|
|
|
|
|
// 循环 文件夹
|
|
|
foreach ($folders as $folder){
|
|
|
// 是否在同步请求中
|
|
|
if(in_array($folder['id'],$fids)){
|
|
|
// 选择 文件夹
|
|
|
$mail->client->selectFolder($folder['origin_folder']);
|
|
|
// 最后的时间
|
|
|
$maxudate = db()->value(
|
|
|
sprintf(
|
|
|
"select `udate` from `%s` where `email_id` = %d and `folder_id` = %d order by `udate` desc limit 1",
|
|
|
listsSql::$table,
|
|
|
$email['id'],
|
|
|
$folder['id']
|
|
|
)
|
|
|
);
|
|
|
$udate = $udate > $maxudate ? $udate : $maxudate;
|
|
|
|
|
|
// 通过时间来搜索uid
|
|
|
$uids = $mail->client->search(['SINCE'=>date('d-M-Y',$udate)],true);
|
|
|
if ($uids){
|
|
|
|
|
|
$us = [];
|
|
|
foreach ($uids as $k=>$uid){
|
|
|
if(!isset($us[intval($k/100)])) $us[intval($k/100)] = [];
|
|
|
$us[intval($k/100)][] = trim($uid);
|
|
|
}
|
|
|
|
|
|
foreach ($us as $u){
|
|
|
// 已有的uid
|
|
|
$useuids = db()->all(
|
|
|
sprintf(
|
|
|
"select `uid` from `%s` where `email_id` = %d and `folder_id` = %d and `uid` in (%s)",
|
|
|
listsSql::$table,
|
|
|
$email['id'],
|
|
|
$folder['id'],
|
|
|
implode(',',$u)
|
|
|
)
|
|
|
);
|
|
|
$useuids = $useuids ? array_column($useuids,'uid') : [];
|
|
|
if($useuids){
|
|
|
// 删除多余的
|
|
|
foreach ($u as $k=>$ui){
|
|
|
if(in_array($ui,$useuids)){
|
|
|
unset($u[$k]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
if($u){
|
|
|
// 进行同步
|
|
|
$mail->syncUidEmail(
|
|
|
array_values($u),
|
|
|
$email['id'],
|
|
|
$folder['origin_folder'],
|
|
|
$folder['id'],
|
|
|
[],
|
|
|
[],
|
|
|
db()
|
|
|
);
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
(new SyncMail($this->getEmail()))
|
|
|
->isUidAfter(2)
|
|
|
->search(
|
|
|
(new ImapSearch())->dateGt($udate)
|
|
|
)->sync();
|
|
|
|
|
|
return 'ok';
|
|
|
}
|
...
|
...
|
|