...
|
...
|
@@ -8,6 +8,7 @@ use Helper\Mail\Mail; |
|
|
use Illuminate\Console\Command;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
use Swoole\Coroutine\MySQL;
|
|
|
use Swoole\Coroutine\Redis;
|
|
|
use function Co\run;
|
|
|
|
...
|
...
|
@@ -45,14 +46,8 @@ class SyncMailList extends Command |
|
|
'hook_flags'=>SWOOLE_HOOK_TCP, // redis需要的配置
|
|
|
]);
|
|
|
|
|
|
// redis 配置
|
|
|
$redis_config = [
|
|
|
'host' => env('REDIS_HOST','127.0.0.1'),
|
|
|
'port' => env('REDIS_PORT',6379),
|
|
|
'password' => env('REDIS_PASSWORD',null)
|
|
|
];
|
|
|
|
|
|
run(function () use ($rand,$max_coroutine,$redis_config){
|
|
|
run(function () use ($rand,$max_coroutine){
|
|
|
// 获取邮箱总数量
|
|
|
$size = Email::where([])->count();
|
|
|
// 最后一条数据的id
|
...
|
...
|
@@ -62,11 +57,11 @@ class SyncMailList extends Command |
|
|
if ($max_coroutine){
|
|
|
for ($i = $max_coroutine; $i > 0; $i--) {
|
|
|
// 创建携程
|
|
|
go(function () use ($size,$rand,$lastId,$redis_config){
|
|
|
go(function () use ($size,$rand,$lastId){
|
|
|
|
|
|
// redis 携程中无法使用laravel的cache的redis驱动
|
|
|
$redis = swoole_redis();
|
|
|
|
|
|
$db = swoole_db();
|
|
|
$n = 1;
|
|
|
while ($n <= $lastId){
|
|
|
echo 'syncMail'.$rand.':'.$n;echo PHP_EOL;
|
...
|
...
|
@@ -76,11 +71,19 @@ class SyncMailList extends Command |
|
|
);
|
|
|
// 是否已存在
|
|
|
if($add) {
|
|
|
$this->sync($n);
|
|
|
try {
|
|
|
$this->sync($n,$db);
|
|
|
}catch (\Throwable $e){
|
|
|
echo "协程(".\co::getCid()."):".$e->getMessage();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
$n++;
|
|
|
}
|
|
|
$redis->close();
|
|
|
$db->close();
|
|
|
$redis = $db = null;
|
|
|
|
|
|
});
|
|
|
}
|
...
|
...
|
@@ -93,35 +96,40 @@ class SyncMailList extends Command |
|
|
|
|
|
/**
|
|
|
* 开始同步执行
|
|
|
* @param int $n 第几条数据
|
|
|
* @param int $n
|
|
|
* @param MySQL $db
|
|
|
* @author:dc
|
|
|
* @time 2023/2/5 17:21
|
|
|
* @time 2023/2/9 11:40
|
|
|
*/
|
|
|
private function sync(int $n = 0){
|
|
|
private function sync(int $n,MySQL $db){
|
|
|
|
|
|
/** @var $email Email */
|
|
|
$email = Email::where(['id'=>$n])->first();
|
|
|
$email = swoole_db_first(
|
|
|
$db,
|
|
|
'select * from `emails` where `id` = '.$n
|
|
|
);
|
|
|
|
|
|
// 密码没有错误,且状态正常的
|
|
|
if ($email && $email->pwd_error == 0 && $email->status == Email::STATUS_ACTIVE){
|
|
|
if ($email && $email['pwd_error'] == 0 && $email['status'] == Email::STATUS_ACTIVE){
|
|
|
// 登录imap服务器
|
|
|
Mail::login($email->email,$email->password,$email->imap);
|
|
|
Mail::login($email['email'],base64_decode($email['password']),$email['imap']);
|
|
|
// 设置id
|
|
|
Mail::$client[$email->email]->setId($email->id);
|
|
|
Mail::$client[$email['email']]->setId($email['id']);
|
|
|
// 同步文件夹
|
|
|
Mail::syncFolder($email->email);
|
|
|
Mail::syncFolder($email['email']);
|
|
|
|
|
|
// 获取当前邮箱的所有文件夹
|
|
|
$folders = Folder::_all($email->id);
|
|
|
$folders = Folder::_all($email['id']);
|
|
|
// 目前只发现最高2级
|
|
|
foreach ($folders as $folder){
|
|
|
if(empty($folder['_child'])){
|
|
|
// 同步邮件
|
|
|
Mail::syncMail($email->email,$email->id,$folder['id'],$folder['origin_folder']);
|
|
|
Mail::syncMail($email['email'],$email['id'],$folder['id'],$folder['origin_folder']);
|
|
|
}else{
|
|
|
// 循环子级目录,有子级的情况,父级不可操作,且不会有邮件
|
|
|
foreach ($folder['_child'] as $f){
|
|
|
// 同步邮件
|
|
|
Mail::syncMail($email->email,$email->id,$f['id'],$folder['origin_folder'].'/'.$f['origin_folder']);
|
|
|
Mail::syncMail($email['email'],$email['id'],$f['id'],$folder['origin_folder'].'/'.$f['origin_folder']);
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
|