审查视图

controller/fob_ai/MailList.php 8.7 KB
邓超 authored
1 2 3 4 5 6 7
<?php

namespace Controller\fob_ai;



use Controller\Base;
邓超 authored
8
use Lib\Verify;
邓超 authored
9
use Model\folderSql;
x  
邓超 authored
10
use function Swoole\Coroutine\Http\request;
邓超 authored
11 12 13 14 15 16 17 18 19 20 21

/**
 * 黑格 fob 那边专用 业务
 * 为定制逻辑
 * @author:dc
 * @time 2024/7/18 11:40
 * Class MailList
 * @package Controller\fob_ai
 */
class MailList extends Base {
邓超 authored
22 23 24 25 26 27 28 29 30 31
    /**
     * 当前邮箱下指定的文件夹
     * @param string $folder
     * @return array
     * @throws \Lib\Err
     * @author:dc
     * @time 2024/7/19 11:37
     */
    private function getFolderId(string $folder){
        // 查询 文件夹
邓超 authored
32 33 34 35
        static $folderList;
        if(!$folderList){
            $folderList = db()->all(folderSql::all($this->getEmails('id')));
        }
邓超 authored
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
        $folder_id = [];
        // 文件夹id
        if($folderList){
            foreach ($folderList as $item){
                if(folderAlias($item['folder']) == $folder){
                    $folder_id[] = $item['id'];
                }
            }
        }
        if(!$folder_id){
            app()->e('folder_not_fount');
        }

        return $folder_id;
    }
邓超 authored
52
邓超 authored
53
    private function from_not_in(){
54
        $where = [];
邓超 authored
55 56 57 58 59
        /**
         * 不查询哪些发件人的邮件
         */
        $form_not_in = app()->request('from_not_in');
        if($form_not_in){
邓超 authored
60 61 62
            if(is_string($form_not_in)){
                $form_not_in = explode(',',$form_not_in);
            }
邓超 authored
63 64 65 66 67 68 69 70
            $form_not_in = is_array($form_not_in) ? $form_not_in : [$form_not_in];
            $form_not_in = array_filter($form_not_in,function ($v){
                if(is_string($v) && Verify::sEmail($v)){
                    return true;
                }
                return false;
            });
            if($form_not_in){
71 72 73 74 75 76 77
                $where = array_merge($where,$form_not_in);
            }
        }
        // 过滤掉
        foreach ($where as $k=>$item){
            if(stripos($item,'postmaster@') === 0){
                unset($where[$k]);
邓超 authored
78 79 80
            }
        }
81
        return $where;
邓超 authored
82 83
    }
邓超 authored
84 85
    /**
     * 邮件列表
邓超 authored
86 87 88 89 90 91 92 93
     * 接收参数
     *  page 分页
     * limit 每页数量
     * mail_id 指定邮件id
     * attachment 是否附件
     * emails 邮箱
     * folder 文件夹
     * from_not_in 不在这些发件箱的邮件
邓超 authored
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
     * @throws \Lib\Err
     * @author:dc
     * @time 2024/7/18 11:40
     */
    public function lists(){

        // 分页 页数
        $page   =   app()->request('page',1,'intval');
        $page   =   $page ? $page : 1;

        $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]);
            }
        }
邓超 authored
116
        $where = ['lists|email_id'=>$this->getEmails('id')];
邓超 authored
117 118 119 120

        // 目录
        $folder = app()->request('folder','收件箱');
        // 只允许查询这里文件夹
邓超 authored
121
        if(!in_array($folder,['收件箱','发件箱','垃圾箱','星标邮件','预热收件箱','预热发件箱','Starred'])){
邓超 authored
122 123 124 125
            app()->e('folder_not_fount');
        }
        $extSql = ''; // 扩展sql
//        $origin_folder = $folder;
邓超 authored
126
        if($folder=='星标邮件'||$folder=='Starred'){
邓超 authored
127 128 129 130 131 132 133 134 135
            $folder = '收件箱';
            $where['flagged'] = 1; // 星标
        }elseif ($folder=='预热收件箱'){
            $folder = '收件箱';
            $extSql = "s";
        }elseif ($folder=='预热发件箱'){
            $folder = '发件箱';
            $extSql = "f";
        }
邓超 authored
136 137

        $folder_id = $this->getFolderId($folder);
邓超 authored
138 139 140 141 142

        //目录
        $where['folder_id'] = $folder_id;
        if($ids) $where['id'] = $ids;
邓超 authored
143
        if(app()->request('attachment',0,'bool_Val')){
邓超 authored
144
            // 附件
邓超 authored
145
            $where['is_file'] = 1; //附件
邓超 authored
146 147 148
        }

        // 软删
邓超 authored
149
        $where['deleted'] = 0;
邓超 authored
150
邓超 authored
151
        $where['from.notin'] = $this->from_not_in();
152
        $where['_'] = "`from` not like 'postmaster@%%'";
邓超 authored
153
        if(!$where['from.notin']) unset($where['from.notin']);
邓超 authored
154
邓超 authored
155
        $filed = '`id`,`uid`,`subject`,`from`,`from_name`,`flagged`,`seen`,`udate`,`folder_id`,`is_file`,`description`,`lists`.`email_id`,`to_name`';
邓超 authored
156 157

        if($extSql){
邓超 authored
158
            $sql = "select %s from `lists` left join `fob_hot_mail` on `lists`.`id` = `fob_hot_mail`.`lists_id` where `fob_hot_mail`.`folder` = '{$extSql}' and ".dbWhere($where);
邓超 authored
159
        }else{
x  
邓超 authored
160
            if(in_array($folder,['收件箱','发件箱','垃圾箱'])){
邓超 authored
161
                $sql = "select %s from `lists` left join `fob_hot_mail` on `lists`.`id` = `fob_hot_mail`.`lists_id` where ISNULL(`lists_id`) and ".dbWhere($where);
邓超 authored
162 163 164 165
            }else{
                $sql = "select %s from `lists` where ".dbWhere($where);
            }
邓超 authored
166
        }
邓超 authored
167
邓超 authored
168 169
        // 查询列表数据
        $lists = db()->all(sprintf($sql,$filed)." order by `udate` desc limit {$limit} offset ".(($page-1)*$limit));
邓超 authored
170 171 172

        // map
        $lists = array_map(function ($v){
邓超 authored
173
            $v['uuid'] = md5($v['email_id'].'-'.$v['folder_id'].'-'.$v['uid']);
邓超 authored
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
            if(!empty($v['description'])){
                $v['description'] = @html_entity_decode($v['description'], ENT_COMPAT, 'UTF-8');
            }
            $v['to_name'] = @json_decode($v['to_name'],true);
            $v['to_name'] = $v['to_name']?:[];
            if($v['to_name']){
                if(!empty($v['to_name'][0]['email'])){
                    $v['to'] = $v['to_name'][0]['email'];
                }
                $v['to_name'] = $v['to_name'][0]['name']??'';
            }
            if(is_array($v['to_name'])){
                $v['to_name'] = '';
            }
            return $v;
邓超 authored
189
        },$lists?:[]);
邓超 authored
190 191

        // 总数
x  
邓超 authored
192
        $total  = db()->cache(600)->count(sprintf($sql,"count(*)"));
邓超 authored
193 194 195 196 197 198

        app()->_json(listsPage($lists,$total,$page,$limit));

    }

邓超 authored
199 200 201 202 203 204
    /**
     * 统计收件箱的数量
     * @author:dc
     * @time 2024/7/19 10:15
     */
    public function count(){
邓超 authored
205
        $emails = $this->getEmails('id');
邓超 authored
206
x  
邓超 authored
207 208 209
        // show_count_filed
        $show_count_filed = app()->requestArr('show_count_filed',['inbox', 'send', 'unseen', 'flagged', 'junk', 'hot_inbox', 'hot_send']);
邓超 authored
210
        $sql = "select count(*) from `lists` left join `fob_hot_mail` on `lists`.`id` = `fob_hot_mail`.`lists_id` where ";
邓超 authored
211
邓超 authored
212
        $where = ['lists|email_id'=>$emails];
邓超 authored
213
        $where['lists|deleted']   =   0; //未删状态
邓超 authored
214 215
        // 屏蔽哪些邮件
        $where['from.notin'] = $this->from_not_in();
216
        $where['_'] = "`from` not like 'postmaster@%'";
邓超 authored
217
        if(!$where['from.notin']) unset($where['from.notin']);
邓超 authored
218
x  
邓超 authored
219 220 221 222 223
        // 发件箱
        if(in_array('hot_send',$show_count_filed)){
            $where['lists|folder_id'] = $this->getFolderId('发件箱');
            // 预热发件箱
            $where['fob_hot_mail|folder'] = 'f';
x  
邓超 authored
224
            $fCount = db()->cache(600)->count($sql.dbWhere($where));
x  
邓超 authored
225
        }
邓超 authored
226
x  
邓超 authored
227 228 229 230
        // 预热收件箱
        if(in_array('hot_inbox',$show_count_filed)) {
            $where['lists|folder_id'] = $this->getFolderId('收件箱');
            $where['fob_hot_mail|folder'] = 's';
x  
邓超 authored
231
            $sCount = db()->cache(600)->count($sql . dbWhere($where));
x  
邓超 authored
232
        }
邓超 authored
233
        unset($where['fob_hot_mail|folder']);
邓超 authored
234
邓超 authored
235
        $sql = $sql." ISNULL(`lists_id`) and ";
x  
邓超 authored
236 237
        if(in_array('send',$show_count_filed)) {
            $where['lists|folder_id'] = $this->getFolderId('发件箱');
x  
邓超 authored
238
            $faCount = db()->cache(600)->count($sql . dbWhere($where));
x  
邓超 authored
239
        }
邓超 authored
240
// 垃圾箱
x  
邓超 authored
241 242
        if(in_array('junk',$show_count_filed)) {
            $where['lists|folder_id'] = $this->getFolderId('垃圾箱');
x  
邓超 authored
243
            $lajiCount = db()->cache(600)->count($sql . dbWhere($where));
x  
邓超 authored
244
        }
邓超 authored
245
// 收件箱
x  
邓超 authored
246 247
        if(in_array('inbox',$show_count_filed)) {
            $where['lists|folder_id'] = $this->getFolderId('收件箱');
x  
邓超 authored
248
            $shouCount = db()->cache(600)->count($sql . dbWhere($where));
x  
邓超 authored
249
        }
邓超 authored
250
        // 未读
x  
邓超 authored
251 252
        if(in_array('unseen',$show_count_filed)) {
            $where['seen'] = 0;
x  
邓超 authored
253
            $seenCount = db()->cache(600)->count($sql . dbWhere($where));
x  
邓超 authored
254 255
            unset($where['seen']);
        }
邓超 authored
256
        // 星标
x  
邓超 authored
257 258
        if(in_array('flagged',$show_count_filed)) {
            $where['flagged'] = 1;
x  
邓超 authored
259
            $flaggedCount = db()->cache(600)->count($sql . dbWhere($where));
x  
邓超 authored
260 261 262 263 264 265 266 267 268 269 270
        }
        $data = [];
        if(isset($shouCount)) $data['inbox'] = $shouCount;
        if(isset($faCount)) $data['send'] = $faCount;
        if(isset($seenCount)) $data['unseen'] = $seenCount;
        if(isset($flaggedCount)) $data['flagged'] = $flaggedCount;
        if(isset($lajiCount)) $data['junk'] = $lajiCount;
        if(isset($sCount)) $data['hot_inbox'] = $sCount;
        if(isset($fCount)) $data['hot_send'] = $fCount;

        app()->_json($data);
邓超 authored
271 272 273 274

    }

邓超 authored
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

}