审查视图

controller/v2/Home.php 5.6 KB
x  
邓超 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?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;
邓超 authored
15
use function Co\run;
x  
邓超 authored
16 17 18 19 20 21 22 23 24 25 26


/**
 * @author:dc
 * @time 2023/2/13 11:28
 * Class Home
 * @package Controller
 */
class Home extends Base {

邓超 authored
27
    private function getFolderIds($email_id){
x  
邓超 authored
28 29
        $folder_ids   =   app()->request('folder_ids',[],['intval','abs']);
邓超 authored
30 31 32 33 34
        if(is_array($folder_ids)){
            foreach ($folder_ids as $k=>$folder_id){
                if(!$folder_id){
                    unset($folder_ids[$k]);
                }
x  
邓超 authored
35 36
            }
邓超 authored
37 38 39
            if($folder_ids){
                $folder_ids = array_values($folder_ids);
            }
邓超 authored
40
        }
x  
邓超 authored
41
x  
邓超 authored
42
        // 默认查询 inbox
x  
邓超 authored
43
        if(!is_array($folder_ids) || !$folder_ids){
x  
邓超 authored
44 45 46 47
            $folder_ids = db()->value(
                sprintf(
                    "select `id` from `%s` where `email_id` = %d and `origin_folder` = 'INBOX'",
                    folderSql::$table
邓超 authored
48
                    ,$email_id
x  
邓超 authored
49 50
                )
            );
邓超 authored
51 52

            $folder_ids = [$folder_ids];
x  
邓超 authored
53
        }
邓超 authored
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

        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')];
x  
邓超 authored
74
        //目录
邓超 authored
75
        $where['folder_id'] = $this->getFolderIds($where['email_id']);
x  
邓超 authored
76
邓超 authored
77 78

        $sql = "`id` > ".$last_id;
邓超 authored
79 80
        if($udate){
            $sql .= " and `udate` > ".$udate;
邓超 authored
81 82
        }
x  
邓超 authored
83 84
        $lists = db()->all(
            sprintf(
邓超 authored
85
                "select `id`,`subject`,`from`,`from_name`,`seen`,`udate`,`email_id` from `%s` where %s and %s order by `id` asc limit %d"
x  
邓超 authored
86
                ,listsSql::$table
邓超 authored
87
                ,$sql
x  
邓超 authored
88 89 90 91 92
                ,dbWhere($where)
                ,$limit
            )
        );
x  
邓超 authored
93
        app()->_json(['data'=>$lists?:[]]);
x  
邓超 authored
94 95 96 97

    }

邓超 authored
98 99 100 101 102 103 104 105 106
    /**
     * v2 版本
     * 同步规定 时间 之后的邮件
     * @return string
     * @throws \Lib\Err
     * @author:dc
     * @time 2023/8/2 16:19
     */
    public function sync(){
x  
邓超 authored
107 108 109
        ignore_user_abort(true);
        set_time_limit(0);
邓超 authored
110 111 112
        $email = $this->getEmail();
        $udate = app()->request('udate',0,'intval');
        if(!$udate){
x  
邓超 authored
113
            return 'no udate';
邓超 authored
114
        }
邓超 authored
115 116 117 118 119 120 121
        // 读取文件夹
        $fids = $this->getFolderIds($email['id']);
        $folders = db()->all(folderSql::all($email['id']));
        // 启动 协程
        // 实例一个邮箱对象
        $mail = new Mail($email['email'],base64_decode($email['password']),$email['imap']);
        // 登录
x  
邓超 authored
122 123 124
        if($mail->login()!=1){
            return '登录失败';
        }
邓超 authored
125
x  
邓超 authored
126 127 128 129 130
        if(!$folders){
            $mail->syncFolder($email['id']);
            return '';
        }
邓超 authored
131 132 133 134 135 136 137 138 139
        // 循环 文件夹
        foreach ($folders as $folder){
            // 是否在同步请求中
            if(in_array($folder['id'],$fids)){
                // 选择 文件夹
                $mail->client->selectFolder($folder['origin_folder']);
                // 最后的时间
                $maxudate = db()->value(
                    sprintf(
x  
邓超 authored
140
                        "select `udate` from `%s` where `email_id` = %d and `folder_id` = %d order by `udate` desc limit 1",
邓超 authored
141 142 143 144 145 146 147 148
                        listsSql::$table,
                        $email['id'],
                        $folder['id']
                    )
                );
                $udate = $udate > $maxudate ? $udate : $maxudate;

                // 通过时间来搜索uid
邓超 authored
149
                $uids = $mail->client->search(['SINCE'=>date('d-M-Y',$udate)],true);
邓超 authored
150
                if ($uids){
邓超 authored
151 152 153 154

                    $us = [];
                    foreach ($uids as $k=>$uid){
                        if(!isset($us[intval($k/100)])) $us[intval($k/100)] = [];
邓超 authored
155
                        $us[intval($k/100)][] = trim($uid);
邓超 authored
156
                    }
邓超 authored
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

                    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]);
                                }
                            }
x  
邓超 authored
177 178
                        }
邓超 authored
179 180

                        if($u){
x  
邓超 authored
181 182 183 184 185 186 187 188 189 190 191 192
                            // 进行同步
                            $mail->syncUidEmail(
                                array_values($u),
                                $email['id'],
                                $folder['origin_folder'],
                                $folder['id'],
                                [],
                                [],
                                db()
                            );
                        }
邓超 authored
193
邓超 authored
194
                    }
邓超 authored
195
邓超 authored
196 197

邓超 authored
198
                }
邓超 authored
199
邓超 authored
200
            }
邓超 authored
201
        }
邓超 authored
202 203 204 205


    }
x  
邓超 authored
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
}