Home.php
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Controller\v2;
use Controller\Base;
use Lib\Mail\Mail;
use Lib\Mail\MailFun;
use Lib\UploadFile;
use Lib\Verify;
use Model\bodySql;
use Model\emailSql;
use Model\folderSql;
use Model\listsSql;
use Model\sendJobsSql;
use function Co\run;
/**
* @author:dc
* @time 2023/2/13 11:28
* Class Home
* @package Controller
*/
class Home extends Base {
private function getFolderIds($email_id){
$folder_ids = app()->request('folder_ids',[],['intval','abs']);
foreach ($folder_ids as $k=>$folder_id){
if(!$folder_id){
unset($folder_ids[$k]);
}
}
if($folder_ids){
$folder_ids = array_values($folder_ids);
}
// 默认查询 inbox
if(!is_array($folder_ids) || !$folder_ids){
$folder_ids = db()->value(
sprintf(
"select `id` from `%s` where `email_id` = %d and `origin_folder` = 'INBOX'",
folderSql::$table
,$email_id
)
);
$folder_ids = [$folder_ids];
}
return $folder_ids;
}
/**
* 邮件列表 针对aicc应用那边
* @author:dc
* @time 2023/2/17 14:12
*/
public function 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;
if($udate){
$sql .= " and `udate` > ".$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
)
);
app()->_json(['data'=>$lists?:[]]);
}
/**
* v2 版本
* 同步规定 时间 之后的邮件
* @return string
* @throws \Lib\Err
* @author:dc
* @time 2023/8/2 16:19
*/
public function sync(){
$email = $this->getEmail();
$udate = app()->request('udate',0,'intval');
if(!$udate){
return '';
}
// 读取文件夹
$fids = $this->getFolderIds($email['id']);
$folders = db()->all(folderSql::all($email['id']));
// 启动 协程
// 实例一个邮箱对象
$mail = new Mail($email['email'],base64_decode($email['password']),$email['imap']);
// 登录
$mail->login();
// 循环 文件夹
foreach ($folders as $folder){
// 是否在同步请求中
if(in_array($folder['id'],$fids)){
// 选择 文件夹
$mail->client->selectFolder($folder['origin_folder']);
// 最后的时间
$maxudate = db()->value(
sprintf(
"select max(`udate`) from `%s` where `email_id` = %d and `folder_id` = %d 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);
// 进行同步
$mail->syncUidEmail(
$uids,
$email['id'],
$folder['origin_folder'],
$folder['id'],
[],
[],
db()
);
}
}
}
}