作者 邓超

m

@@ -51,6 +51,9 @@ class Folders{ @@ -51,6 +51,9 @@ class Folders{
51 $v = trim($v,'\\'); 51 $v = trim($v,'\\');
52 return $v; 52 return $v;
53 },$flags); 53 },$flags);
  54 + $flags = array_filter($flags,function ($v){
  55 + return !empty($v);
  56 + });
54 }else{ 57 }else{
55 $flags = []; 58 $flags = [];
56 } 59 }
@@ -4,7 +4,9 @@ namespace Service; @@ -4,7 +4,9 @@ namespace Service;
4 4
5 use Lib\Imap\ImapConfig; 5 use Lib\Imap\ImapConfig;
6 use Lib\Imap\ImapPool; 6 use Lib\Imap\ImapPool;
  7 +use Lib\Imap\Parse\Folder\Folder;
7 use Model\emailSql; 8 use Model\emailSql;
  9 +use Model\folderSql;
8 10
9 /** 11 /**
10 * 同步邮件 12 * 同步邮件
@@ -26,6 +28,11 @@ class SyncMail { @@ -26,6 +28,11 @@ class SyncMail {
26 protected $imap; 28 protected $imap;
27 29
28 /** 30 /**
  31 + * @var array
  32 + */
  33 + protected $email;
  34 +
  35 + /**
29 * SyncMail constructor. 36 * SyncMail constructor.
30 * @param int|string|array $email 37 * @param int|string|array $email
31 * @throws \Exception 38 * @throws \Exception
@@ -40,7 +47,7 @@ class SyncMail { @@ -40,7 +47,7 @@ class SyncMail {
40 abort('未查询到邮箱'); 47 abort('未查询到邮箱');
41 } 48 }
42 } 49 }
43 - 50 + $this->email = $email;
44 // 实例一个imap类 51 // 实例一个imap类
45 $this->imap = ImapPool::get( 52 $this->imap = ImapPool::get(
46 (new ImapConfig()) 53 (new ImapConfig())
@@ -49,12 +56,93 @@ class SyncMail { @@ -49,12 +56,93 @@ class SyncMail {
49 ->setPassword(base64_decode($email['password'])) 56 ->setPassword(base64_decode($email['password']))
50 ); 57 );
51 58
  59 + $this->login();
52 60
53 } 61 }
54 62
  63 + protected function emailId(){
  64 + return $this->email['id'];
  65 + }
55 66
56 - public function syncFolder(){ 67 + /**
  68 + * 登录imap
  69 + * @throws \Exception
  70 + * @author:dc
  71 + * @time 2024/9/26 9:58
  72 + */
  73 + private function login(){
  74 + $login = $this->imap->login();
  75 + if(!$login->isOk()){
  76 + abort($login->getMessage()?:'连接服务器异常');
  77 + }
  78 + }
  79 +
  80 + /**
  81 + * 同步文件夹
  82 + * @author:dc
  83 + * @time 2024/9/26 10:22
  84 + */
  85 + protected function folders(){
  86 + $uuids = [];
  87 + // 获取文件夹
  88 + $folders = $this->imap->getFolders();
  89 +
  90 + $tmp = function ($folder) use ($folders,&$uuids){
  91 + foreach ($folder as $item){
  92 + /** @var Folder $item*/
  93 +
  94 + $uuid = md5($this->emailId().$item->folder);
  95 + $uuids[$uuid] = $uuid;
  96 +
  97 + $folder_name = '';
  98 + // 已发送
  99 + if(in_array('Send',$item->flags)){
  100 + $folder_name = folderAlias('Send');
  101 + }
  102 + // 草稿
  103 + elseif(in_array('Drafts',$item->flags)){
  104 + $folder_name = folderAlias('Drafts');
  105 + }
  106 + // 垃圾
  107 + elseif(in_array('Junk',$item->flags)){
  108 + $folder_name = folderAlias('Junk');
  109 + }
  110 + // 回收站
  111 + elseif(in_array('Trash',$item->flags)){
  112 + $folder_name = folderAlias('Trash');
  113 + }
  114 + if(!$folder_name){
  115 + $fn = explode('/',$item->getParseFolder());
  116 + $folder_name = folderAlias(end($fn));
  117 + }
  118 +
  119 + // 是否存在
  120 + $id = $this->db->value(folderSql::has(['email_id'=>$this->emailId(),'uuid'=>$uuid]));
  121 + $data = [
  122 + 'email_id' => $this->emailId(),
  123 + 'folder' => folderAlias($folder_name),
  124 + 'origin_folder' => $folder['folder'],
  125 + 'uuid' => $uuid,
  126 + 'pid' => $pid
  127 + ];
  128 + if ($id){
  129 +
  130 + }
  131 + }
  132 + };
  133 +
  134 + $tmp($folders->getTopFolder());
  135 +
  136 + if($uuids){
  137 + // 删除以前的
  138 + $this->db->delete(folderSql::$table,['uuid.notin'=>$uuids,'email_id'=>$this->emailId()]);
  139 + }
  140 +
  141 + return $folders;
  142 + }
57 143
  144 + public function sync(){
  145 + $folders = $this->folders();
58 } 146 }
59 147
60 148