审查视图

controller/Home.php 9.9 KB
邓超 authored
1 2
<?php
1  
邓超 authored
3
namespace Controller;
邓超 authored
4
1  
邓超 authored
5
use Lib\Mail\Mail;
1  
邓超 authored
6
use Lib\Mail\MailFun;
1  
邓超 authored
7
use Lib\Verify;
1  
邓超 authored
8
use Model\emailSql;
1  
邓超 authored
9
use Model\folderSql;
1  
邓超 authored
10
use Model\listsSql;
1  
邓超 authored
11
1  
邓超 authored
12
邓超 authored
13 14
/**
 * @author:dc
1  
邓超 authored
15 16 17
 * @time 2023/2/13 11:28
 * Class Home
 * @package Controller
邓超 authored
18
 */
1  
邓超 authored
19
class Home extends Base {
1  
邓超 authored
20
邓超 authored
21
1  
邓超 authored
22 23 24 25 26 27 28 29 30 31 32
    /**
     * 邮件列表
     * @author:dc
     * @time 2023/2/17 14:12
     */
    public function lists(){

        // 分页 页数
        $page   =   app()->request('page',1,'intval');
        $page   =   $page ? $page : 1;
1  
邓超 authored
33 34 35 36 37 38 39 40 41 42 43
        $limit   =   app()->request('limit',20,'intval');
        $limit   =   $limit ? $limit : 1;

        // 指定id
        $ids = app()->request('mail_id');
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $i=>$d){
            if(!is_numeric($d)){
                unset($ids[$i]);
            }
        }
1  
邓超 authored
44
1  
邓超 authored
45 46
        // 附件
        $attachment =   app()->request('attachment');
1  
邓超 authored
47 48
        // 已读/未读
        $seen =   app()->request('seen',-1,'intval');
1  
邓超 authored
49 50
        // 软删
        $deleted =   app()->request('deleted',0,'intval');
1  
邓超 authored
51
1  
邓超 authored
52 53

        $where = ['email_id'=>$this->getEmails('id')];
1  
邓超 authored
54 55

        // 目录
1  
邓超 authored
56
        $folder = app()->request('folder','收件箱');
1  
邓超 authored
57 58 59 60 61 62 63 64 65 66
        $folderList = db()->all(folderSql::all($where['email_id']));
        $folder_id = [];
        // 文件夹id
        if($folderList){
            foreach ($folderList as $item){
                if($item['folder'] == $folder){
                    $folder_id[] = $item['id'];
                }
            }
        }
1  
邓超 authored
67 68 69
        if(!$folder_id){
            app()->e('folder_not_fount');
        }
1  
邓超 authored
70
1  
邓超 authored
71
        //目录
1  
邓超 authored
72
        $where['folder_id'] = $folder_id;
1  
邓超 authored
73 74
        if($ids) $where['id'] = $ids;
        if($attachment) $where['is_file'] = 1; //附件
1  
邓超 authored
75 76
        // 软删
        $where['deleted'] = $deleted;
1  
邓超 authored
77 78 79 80
        // 已读/未读
        if(in_array($seen,[0,1])){
            $where['seen'] = $seen;
        }
1  
邓超 authored
81 82 83

        $lists = db()->all(
            listsSql::lists(
1  
邓超 authored
84
                dbWhere($where),
1  
邓超 authored
85
                $page,
1  
邓超 authored
86
                $limit
1  
邓超 authored
87 88 89 90 91
            )
        );

        // 总数
        $total  = db()->count(
1  
邓超 authored
92
            listsSql::listCount(dbWhere($where))
1  
邓超 authored
93 94
        );
1  
邓超 authored
95
        app()->_json(listsPage($lists,$total,$page,$limit));
1  
邓超 authored
96 97 98

    }
1  
邓超 authored
99
1  
邓超 authored
100
1  
邓超 authored
101
    /**
1  
邓超 authored
102
     * 发送邮件
1  
邓超 authored
103
     * @author:dc
1  
邓超 authored
104
     * @time 2023/2/18 17:32
1  
邓超 authored
105
     */
1  
邓超 authored
106
    public function send_mail(){
1  
邓超 authored
107
1  
邓超 authored
108 109 110 111 112 113
        $email = $this->getEmail();

        $formData = Verify::checks([
            'nickname|'.__('nickname')  =>  ['required','max'=>50],
            'subject|'.__('subject')  =>  ['required','max'=>150],
            'body|'.__('body_email')  =>  ['required'],
1  
邓超 authored
114 115 116 117 118
            'to|'.__('to_email')  =>  ['required','array|string','email'],
            'priority|'.__('priority_email')  =>  ['in'=>[1,3,5]],
            'files|'.__('files_email')  =>  [
                'file'=>[
                    'ext'   =>  [],
1  
邓超 authored
119
                    'size'  =>  500,
1  
邓超 authored
120 121 122 123
                    'mine'  =>  []
                ]
            ],
            'receipt|'.__('receipt_email')  =>  []
1  
邓超 authored
124 125 126 127 128 129 130 131 132 133 134 135
        ],[

        ]);

        $ret = MailFun::sendEmail(
            $email['smtp'],
            $email['email'],
            base64_decode($email['password']),
            $formData['nickname']??'',
            $formData['to'],
            $formData['subject'],
            $formData['body'],
1  
邓超 authored
136 137 138
            $formData['files']??'',
            $formData['receipt']??'',
            $formData['priority']??3,
1  
邓超 authored
139
        );
1  
邓超 authored
140
1  
邓超 authored
141 142 143 144 145 146
        if($ret[0]===true) {
            app()->_json(['messageId'=>$ret[1]]);
        }else {
            app()->e($ret[1]);
        }
    }
1  
邓超 authored
147 148

1  
邓超 authored
149 150 151 152 153 154
    /**
     * 收到前端的同步请求操作
     * @author:dc
     * @time 2023/3/10 10:38
     */
    public function sync(){
1  
邓超 authored
155
1  
邓超 authored
156
        $emails = web_request_emails();
1  
邓超 authored
157
1  
邓超 authored
158
        if(empty($emails)){
1  
邓超 authored
159 160 161
            app()->e('sync_request_param_error');
        }else{
            // 查询id
1  
邓超 authored
162 163 164
            if(count($emails)===1){
                $emails = $emails[0];
            }
1  
邓超 authored
165 166
            $datas = db()->all(emailSql::getValues(['email'=>$emails],'`id`,`email`,`pwd_error`'));
            foreach ($datas as $k=>$v){
1  
邓超 authored
167 168 169
                if(!$v['pwd_error']){
                    redis()->rPush('sync_email_lists', $v['id']);
                }
1  
邓超 authored
170
                $datas[$k]['have_new'] = redis()->getDel('have_new_mail_'.$v['id']);
1  
邓超 authored
171 172
            }
            // 返回成功的参数值
1  
邓超 authored
173
            app()->_json($datas);
1  
邓超 authored
174 175 176 177
        }

    }
1  
邓超 authored
178
1  
邓超 authored
179 180 181 182 183 184 185
    /**
     * 标记为已读
     * @throws \Lib\Err
     * @author:dc
     * @time 2023/3/17 16:15
     */
    public function seen_2_unseen(){
1  
邓超 authored
186 187 188 189 190 191
        $this->setFlags('seen');
    }



    /**
1  
邓超 authored
192
     * 邮件移动
1  
邓超 authored
193 194 195
     * @author:dc
     * @time 2023/3/21 11:41
     */
1  
邓超 authored
196 197 198 199 200 201 202 203 204 205
    public function move(){
        $emails = $this->getEmails();

        $mail_ids = app()->request('mail_ids');
        foreach ($mail_ids as $k=>$id){
            if(!is_numeric($id)){
                unset($mail_ids[$k]);
            }
        }
        // 移动到的文件夹
1  
邓超 authored
206 207 208 209
        $to_folder = app()->request('folder');
        if(empty($to_folder)){
            app()->e('folder_move_error');
        }
1  
邓超 authored
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

        $data  = db()->all(listsSql::first(dbWhere(['id'=>$mail_ids,'email_id'=>array_column($emails,'id')]),'`id`,`uid`,`email_id`,`folder_id`'));
        if($data){
            // 查询邮箱
            $emails = array_column($emails,null,'id');
            $uids = [];
            foreach ($data as $datum){
                if(empty($uids[$datum['email_id']])){
                    $uids[$datum['email_id']][$datum['folder_id']] = [];
                }
                $uids[$datum['email_id']][$datum['folder_id']][] = [
                    'uid'   =>   $datum['uid'],
                    'id'   =>   $datum['id'],
                ];
            }

            foreach ($uids as $eid=>$arr){
1  
邓超 authored
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
                // 查询需要移动的文件夹
                $to_origin_folder = db()->first(folderSql::first(['email_id'=>$eid,'folder'=>$to_folder]));
                if($to_origin_folder){
                    foreach ($arr as $fid=>$uid){
                        // 查询目录
                        $folder = db()->first(folderSql::first($fid));
                        if($folder){
                            // 开始远程
                            $mailInstance = new Mail($emails[$eid]['email'],base64_decode($emails[$eid]['password']),$emails[$eid]['imap']);

                            if($mailInstance->login()){
                                // TODO:: 这个过程无法保证原子性。没办法
                                // 先复制
                                $ret = $mailInstance->move(array_column($uid,'uid'),$folder['origin_folder'],$to_origin_folder['origin_folder']);
                                if($ret){
                                    $uret = db()->update(listsSql::$table,['deleted'=>1],dbWhere(['id'=>array_column($uid,'id')]));
                                }

                                $mailInstance = null;
                            }
1  
邓超 authored
247
1  
邓超 authored
248 249
                        }
                        $folder = null;
1  
邓超 authored
250 251
                    }
                }
1  
邓超 authored
252 253

1  
邓超 authored
254 255 256 257 258 259 260 261
            }

        }


        app()->_json([
            'mail_id'    =>  $mail_ids
        ]);
1  
邓超 authored
262 263 264 265 266
    }



1  
邓超 authored
267 268 269 270 271 272 273
    /**
     * 远程标签
     * @param $d
     * @throws \Lib\Err
     * @author:dc
     * @time 2023/3/21 14:28
     */
1  
邓超 authored
274
    private function setFlags($d){
1  
邓超 authored
275 276 277 278 279 280 281 282 283
        $emails = $this->getEmails();

        $mail_ids = app()->request('mail_ids');
        foreach ($mail_ids as $k=>$id){
            if(!is_numeric($id)){
                unset($mail_ids[$k]);
            }
        }
        // 已读或未读
1  
邓超 authored
284 285
        $fv = (int) app()->request($d);
        $fv = $fv ? 1 : 0;
1  
邓超 authored
286
1  
邓超 authored
287
        $data  = db()->all(listsSql::first(dbWhere(['id'=>$mail_ids,'email_id'=>array_column($emails,'id')]),'`id`,`uid`,`email_id`,`folder_id`'));
1  
邓超 authored
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
        if($data){
            // 查询邮箱
            $emails = array_column($emails,null,'id');
            $uids = [];
            foreach ($data as $datum){
                if(empty($uids[$datum['email_id']])){
                    $uids[$datum['email_id']][$datum['folder_id']] = [];
                }
                $uids[$datum['email_id']][$datum['folder_id']][] = [
                    'uid'   =>   $datum['uid'],
                    'id'   =>   $datum['id'],
                ];
            }

            foreach ($uids as $eid=>$arr){
                foreach ($arr as $fid=>$uid){
                    // 查询目录
                    $folder = db()->first(folderSql::first($fid));
                    if($folder){
                        // 开始远程
                        $mailInstance = new Mail($emails[$eid]['email'],base64_decode($emails[$eid]['password']),$emails[$eid]['imap']);
1  
邓超 authored
310
                        if($mailInstance->login()){
1  
邓超 authored
311 312 313 314 315 316 317
                            switch ($d){
                                // 已读 未读
                                case 'seen':{
                                    $mailInstance->seen(array_column($uid,'uid'),$folder['origin_folder'],$fv);
                                    break;
                                }
                                // 回收站,已删 未删,软删
1  
邓超 authored
318 319 320 321
//                                case 'deleted':{
//                                    $mailInstance->recycle(array_column($uid,'uid'),$folder['origin_folder'],$fv);
//                                    break;
//                                }
1  
邓超 authored
322
                            }
1  
邓超 authored
323 324 325 326

                            $mailInstance = null;
                            // 更新数据
                            db()->update(listsSql::$table,[
1  
邓超 authored
327
                                $d  =>  $fv
1  
邓超 authored
328 329 330 331
                            ],dbWhere([
                                'id'    =>  array_column($uid,'id')
                            ]));
                        }
1  
邓超 authored
332 333 334 335 336 337 338 339 340 341 342 343 344 345

                    }
                    $folder = null;
                }
            }

        }


        app()->_json([
            'mail_id'    =>  $mail_ids
        ]);

    }
1  
邓超 authored
346 347 348


邓超 authored
349
}
1  
邓超 authored
350 351 352 353 354 355 356 357 358 359 360 361 362 363