MailListV2.php 9.5 KB
<?php

namespace Controller\fob_ai;



use Controller\Base;
use Lib\Verify;
use Model\folderSql;
use function Swoole\Coroutine\Http\request;

/**
 * 黑格 fob 那边专用 业务
 * 为定制逻辑
 * @author:dc
 * @time 2024/7/18 11:40
 * Class MailList
 * @package Controller\fob_ai
 */
class MailListV2 extends Base {

    private $cache = 600;

    /**
     * 当前邮箱下指定的文件夹
     * @param string $folder
     * @return array
     * @throws \Lib\Err
     * @author:dc
     * @time 2024/7/19 11:37
     */
    private function getFolderId(string $folder){
        // 查询 文件夹
        static $folderList;
        if(!$folderList){
            $folderList = db()->all(folderSql::all($this->getEmails('id')));
        }
        $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;
    }


    private function from_not_in(){
        $where = [];
        /**
         * 不查询哪些发件人的邮件
         */
        $form_not_in = app()->request('from_not_in');
        if($form_not_in){
            if(is_string($form_not_in)){
                $form_not_in = explode(',',$form_not_in);
            }
            $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){
                $where = array_merge($where,$form_not_in);
            }
        }
        // 过滤掉
        foreach ($where as $k=>$item){
            if(stripos($item,'postmaster@') === 0){
                unset($where[$k]);
            }
        }

        $where[] = 'noreply@google.com';

        return $where;
    }

    /**
     * 邮件列表
     * 接收参数
     *  page 分页
     * limit 每页数量
     * mail_id 指定邮件id
     * attachment 是否附件
     * emails 邮箱
     * folder 文件夹
     * from_not_in 不在这些发件箱的邮件
     * @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]);
            }
        }

        $where = ['lists|email_id'=>$this->getEmails('id')];

        // 目录
        $folder = app()->request('folder','收件箱');
        // 只允许查询这里文件夹
        if(!in_array($folder,['收件箱','发件箱','垃圾箱','星标邮件','预热收件箱','预热发件箱','Starred'])){
            app()->e('folder_not_fount');
        }
        $where['is_hots'] = 0;

        if($folder=='星标邮件'||$folder=='Starred'){
            $folder = '收件箱';
            $where['flagged'] = 1; // 星标
        }elseif ($folder=='预热收件箱'){
            $folder = '收件箱';
            $where['is_hots'] = 1;
        }elseif ($folder=='预热发件箱'){
            $folder = '发件箱';
            $where['is_hots'] = 1;
        } elseif ($folder=='自动回复收件箱'){
            $is_auto_inbox = 1;
            $folder = '收件箱';
            $where['is_hots'] = 0;
        }

        $seen =   app()->request('seen',-1,'intval');
        // 已读/未读
        if(paramHas('seen')){
            if(in_array($seen,[0,1])){
                $where['seen'] = $seen;
            }
        }

        $folder_id = $this->getFolderId($folder);

        //目录
        $where['folder_id'] = $folder_id;
        if($ids) $where['id'] = $ids;

        if(app()->request('attachment',0,'bool_Val')){
            // 附件
            $where['is_file'] = 1; //附件
        }

        // 软删
        $where['deleted'] = 0;

        $where['from.notin'] = $this->from_not_in();
        $where['_'] = "`from` not like 'postmaster@%%'";
        if(!$where['from.notin']) unset($where['from.notin']);

        $filed = '`id`,`uid`,`subject`,`from`,`from_name`,`flagged`,`seen`,`udate`,`folder_id`,`is_file`,`description`,`lists`.`email_id`,`to_name`';

        $sql = "select %s from `lists` where ".dbWhere($where);

        /************ 2024-09-02 *************/
        // 模糊标题搜索
        $subject = app()->request('subject');
        if($subject){
            $subject = is_array($subject) ? $subject : [$subject];
            foreach ($subject as $sub){
                $sql.=' and `subject` .'.(empty($is_auto_inbox)?' not ':'').'like "%'.addslashes($sub).'%"';
            }
        }
        // 不在此邮箱中的
        $from_not_in_like = app()->request('from_not_in_like');
        if($from_not_in_like){
            $from_not_in_like = is_array($from_not_in_like) ? $from_not_in_like : [$from_not_in_like];
            foreach ($from_not_in_like as $sub){
                $sql.=' and `from` .'.(empty($is_auto_inbox)?' not ':'').'like "%'.addslashes($sub).'%"';
            }
        }
        /************ 2024-09-02 *************/


        // 查询列表数据
        $lists = db()->all(sprintf($sql,$filed)." order by `udate` desc limit {$limit} offset ".(($page-1)*$limit));

        // map
        $lists = array_map(function ($v){
            $v['uuid'] = md5($v['email_id'].'-'.$v['folder_id'].'-'.$v['uid']);
            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;
        },$lists?:[]);

        // 总数
        $total  = db()->cache($this->cache)->count(sprintf($sql,"count(*)"));

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

    }


    /**
     * 统计收件箱的数量
     * @author:dc
     * @time 2024/7/19 10:15
     */
    public function count(){
        $emails = $this->getEmails('id');

        // show_count_filed
        $show_count_filed = app()->requestArr('show_count_filed',['inbox', 'send', 'unseen', 'flagged', 'junk', 'hot_inbox', 'hot_send']);

        $sql = "select count(*) from `lists` where ";

        $where = ['email_id'=>$emails];
        $where['deleted']   =   0; //未删状态
        // 屏蔽哪些邮件
        $where['from.notin'] = $this->from_not_in();
        $where['_'] = "`from` not like 'postmaster@%'";
        if(!$where['from.notin']) unset($where['from.notin']);

        // 发件箱
        if(in_array('hot_send',$show_count_filed)){
            $where['folder_id'] = $this->getFolderId('发件箱');
            // 预热发件箱
            $where['is_hots'] = 1;
            $fCount = db()->cache($this->cache)->count($sql.dbWhere($where));
        }

        // 预热收件箱
        if(in_array('hot_inbox',$show_count_filed)) {
            $where['folder_id'] = $this->getFolderId('收件箱');
            $where['is_hots'] = 1;
            $sCount = db()->cache($this->cache)->count($sql . dbWhere($where));
        }

        if(in_array('send',$show_count_filed)) {
            $where['folder_id'] = $this->getFolderId('发件箱');
            $where['is_hots'] = 0;
            $faCount = db()->cache($this->cache)->count($sql . dbWhere($where));
        }
// 垃圾箱
        if(in_array('junk',$show_count_filed)) {
            $where['folder_id'] = $this->getFolderId('垃圾箱');
            $where['is_hots'] = 0;
            $lajiCount = db()->cache($this->cache)->count($sql . dbWhere($where));
        }
// 收件箱
        if(in_array('inbox',$show_count_filed)) {
            $where['folder_id'] = $this->getFolderId('收件箱');
            $where['is_hots'] = 0;
            $shouCount = db()->cache($this->cache)->count($sql . dbWhere($where));
        }
        // 未读
        if(in_array('unseen',$show_count_filed)) {
            $where['seen'] = 0;
            $where['is_hots'] = 0;
            $seenCount = db()->cache($this->cache)->count($sql . dbWhere($where));
            unset($where['seen']);
        }
        // 星标
        if(in_array('flagged',$show_count_filed)) {
            $where['flagged'] = 1;
            $flaggedCount = db()->cache($this->cache)->count($sql . dbWhere($where));
        }
        $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);

    }



}