|
@@ -3,9 +3,11 @@ |
|
@@ -3,9 +3,11 @@ |
3
|
namespace Controller\v2;
|
3
|
namespace Controller\v2;
|
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 Model\folderSql;
|
8
|
use Model\folderSql;
|
8
|
use Model\listsSql;
|
9
|
use Model\listsSql;
|
|
|
10
|
+use Service\SyncMail;
|
9
|
|
11
|
|
10
|
|
12
|
|
11
|
/**
|
13
|
/**
|
|
@@ -47,6 +49,9 @@ class Home extends Base { |
|
@@ -47,6 +49,9 @@ class Home extends Base { |
47
|
$folder_ids = [$folder_ids];
|
49
|
$folder_ids = [$folder_ids];
|
48
|
}
|
50
|
}
|
49
|
|
51
|
|
|
|
52
|
+ if(!$folder_ids){
|
|
|
53
|
+ return [-1];
|
|
|
54
|
+ }
|
50
|
return $folder_ids;
|
55
|
return $folder_ids;
|
51
|
}
|
56
|
}
|
52
|
|
57
|
|
|
@@ -57,33 +62,44 @@ class Home extends Base { |
|
@@ -57,33 +62,44 @@ class Home extends Base { |
57
|
* @time 2023/2/17 14:12
|
62
|
* @time 2023/2/17 14:12
|
58
|
*/
|
63
|
*/
|
59
|
public function lists(){
|
64
|
public function lists(){
|
60
|
- logs('v2 home lists');
|
65
|
+
|
61
|
$limit = app()->request('limit',20,['intval','abs']);
|
66
|
$limit = app()->request('limit',20,['intval','abs']);
|
62
|
|
67
|
|
63
|
$last_id = app()->request('last_id',0,['intval','abs']);
|
68
|
$last_id = app()->request('last_id',0,['intval','abs']);
|
64
|
|
69
|
|
65
|
$udate = app()->request('udate',0,'intval');
|
70
|
$udate = app()->request('udate',0,'intval');
|
66
|
|
71
|
|
67
|
- $where = ['email_id' => $this->getEmail('id')];
|
|
|
68
|
-
|
|
|
69
|
- //目录
|
|
|
70
|
- $where['folder_id'] = $this->getFolderIds($where['email_id']);
|
|
|
71
|
-
|
|
|
72
|
|
72
|
|
73
|
- $sql = "`id` > ".$last_id;
|
73
|
+ $range = [
|
|
|
74
|
+ 'uuid' => [
|
|
|
75
|
+ 'gte' => $last_id, // 大于等于开始日期
|
|
|
76
|
+ ]
|
|
|
77
|
+ ];
|
74
|
if($udate){
|
78
|
if($udate){
|
75
|
- $sql .= " and `udate` > ".$udate;
|
79
|
+ $range['udate'] = [
|
|
|
80
|
+ 'gte' => $udate, // 大于等于开始日期
|
|
|
81
|
+ ];
|
76
|
}
|
82
|
}
|
77
|
|
83
|
|
78
|
- $lists = db()->all(
|
|
|
79
|
- sprintf(
|
|
|
80
|
- "select `id`,`subject`,`from`,`from_name`,`seen`,`udate`,`email_id` from `%s` where %s and %s order by `id` asc limit %d"
|
|
|
81
|
- ,listsSql::$table
|
|
|
82
|
- ,$sql
|
|
|
83
|
- ,dbWhere($where)
|
|
|
84
|
- ,$limit
|
|
|
85
|
- )
|
|
|
86
|
- );
|
84
|
+ $query = [
|
|
|
85
|
+ 'query' => [
|
|
|
86
|
+ 'bool'=>[
|
|
|
87
|
+ 'must'=>[
|
|
|
88
|
+ ['term'=>[
|
|
|
89
|
+ 'email_id'=>$this->getEmail('id')
|
|
|
90
|
+ ]],
|
|
|
91
|
+ ['terms'=>[
|
|
|
92
|
+ 'folder_id'=>$this->getFolderIds($this->getEmail('id'))
|
|
|
93
|
+ ]],
|
|
|
94
|
+ 'range'=>$range
|
|
|
95
|
+ ]
|
|
|
96
|
+ ]
|
|
|
97
|
+ ]
|
|
|
98
|
+ ];
|
|
|
99
|
+
|
|
|
100
|
+ $lists = es()->search($query,0,$limit,[],false);
|
|
|
101
|
+
|
|
|
102
|
+ $lists = $lists['hits']['hits']??[];
|
87
|
|
103
|
|
88
|
app()->_json(['data'=>$lists?:[]]);
|
104
|
app()->_json(['data'=>$lists?:[]]);
|
89
|
|
105
|
|
|
@@ -99,102 +115,20 @@ class Home extends Base { |
|
@@ -99,102 +115,20 @@ class Home extends Base { |
99
|
* @time 2023/8/2 16:19
|
115
|
* @time 2023/8/2 16:19
|
100
|
*/
|
116
|
*/
|
101
|
public function sync(){
|
117
|
public function sync(){
|
102
|
- logs('v2 home sync');
|
118
|
+
|
103
|
ignore_user_abort(true);
|
119
|
ignore_user_abort(true);
|
104
|
set_time_limit(0);
|
120
|
set_time_limit(0);
|
105
|
|
121
|
|
106
|
- $email = $this->getEmail();
|
|
|
107
|
$udate = app()->request('udate',0,'intval');
|
122
|
$udate = app()->request('udate',0,'intval');
|
108
|
if(!$udate){
|
123
|
if(!$udate){
|
109
|
return 'no udate';
|
124
|
return 'no udate';
|
110
|
}
|
125
|
}
|
111
|
- // 读取文件夹
|
|
|
112
|
- $fids = $this->getFolderIds($email['id']);
|
|
|
113
|
- $folders = db()->all(folderSql::all($email['id']));
|
|
|
114
|
- // 启动 协程
|
|
|
115
|
- // 实例一个邮箱对象
|
|
|
116
|
- $mail = new Mail($email['email'],base64_decode($email['password']),$email['imap']);
|
|
|
117
|
- // 登录
|
|
|
118
|
- if($mail->login()!=1){
|
|
|
119
|
- return '登录失败';
|
|
|
120
|
- }
|
|
|
121
|
-
|
|
|
122
|
- if(!$folders){
|
|
|
123
|
- $mail->syncFolder($email['id']);
|
|
|
124
|
- return 'sync folder';
|
|
|
125
|
- }
|
|
|
126
|
|
126
|
|
127
|
- // 循环 文件夹
|
|
|
128
|
- foreach ($folders as $folder){
|
|
|
129
|
- // 是否在同步请求中
|
|
|
130
|
- if(in_array($folder['id'],$fids)){
|
|
|
131
|
- // 选择 文件夹
|
|
|
132
|
- $mail->client->selectFolder($folder['origin_folder']);
|
|
|
133
|
- // 最后的时间
|
|
|
134
|
- $maxudate = db()->value(
|
|
|
135
|
- sprintf(
|
|
|
136
|
- "select `udate` from `%s` where `email_id` = %d and `folder_id` = %d order by `udate` desc 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(['SINCE'=>date('d-M-Y',$udate)],true);
|
|
|
146
|
- if ($uids){
|
|
|
147
|
-
|
|
|
148
|
- $us = [];
|
|
|
149
|
- foreach ($uids as $k=>$uid){
|
|
|
150
|
- if(!isset($us[intval($k/100)])) $us[intval($k/100)] = [];
|
|
|
151
|
- $us[intval($k/100)][] = trim($uid);
|
|
|
152
|
- }
|
|
|
153
|
-
|
|
|
154
|
- foreach ($us as $u){
|
|
|
155
|
- // 已有的uid
|
|
|
156
|
- $useuids = db()->all(
|
|
|
157
|
- sprintf(
|
|
|
158
|
- "select `uid` from `%s` where `email_id` = %d and `folder_id` = %d and `uid` in (%s)",
|
|
|
159
|
- listsSql::$table,
|
|
|
160
|
- $email['id'],
|
|
|
161
|
- $folder['id'],
|
|
|
162
|
- implode(',',$u)
|
|
|
163
|
- )
|
|
|
164
|
- );
|
|
|
165
|
- $useuids = $useuids ? array_column($useuids,'uid') : [];
|
|
|
166
|
- if($useuids){
|
|
|
167
|
- // 删除多余的
|
|
|
168
|
- foreach ($u as $k=>$ui){
|
|
|
169
|
- if(in_array($ui,$useuids)){
|
|
|
170
|
- unset($u[$k]);
|
|
|
171
|
- }
|
|
|
172
|
- }
|
|
|
173
|
- }
|
|
|
174
|
-
|
|
|
175
|
-
|
|
|
176
|
- if($u){
|
|
|
177
|
- // 进行同步
|
|
|
178
|
- $mail->syncUidEmail(
|
|
|
179
|
- array_values($u),
|
|
|
180
|
- $email['id'],
|
|
|
181
|
- $folder['origin_folder'],
|
|
|
182
|
- $folder['id'],
|
|
|
183
|
- [],
|
|
|
184
|
- [],
|
|
|
185
|
- db()
|
|
|
186
|
- );
|
|
|
187
|
- }
|
|
|
188
|
-
|
|
|
189
|
-
|
|
|
190
|
- }
|
|
|
191
|
-
|
|
|
192
|
-
|
|
|
193
|
-
|
|
|
194
|
- }
|
|
|
195
|
-
|
|
|
196
|
- }
|
|
|
197
|
- }
|
127
|
+ (new SyncMail($this->getEmail()))
|
|
|
128
|
+ ->isUidAfter(2)
|
|
|
129
|
+ ->search(
|
|
|
130
|
+ (new ImapSearch())->dateGt($udate)
|
|
|
131
|
+ )->sync();
|
198
|
|
132
|
|
199
|
return 'ok';
|
133
|
return 'ok';
|
200
|
}
|
134
|
}
|