作者 邓超

优化同步

  1 +<?php
  2 +
  3 +namespace Controller;
  4 +
  5 +use Lib\Mail\Mail;
  6 +use Lib\Mail\MailFun;
  7 +use Lib\UploadFile;
  8 +use Lib\Verify;
  9 +use Model\bodySql;
  10 +use Model\emailSql;
  11 +use Model\folderSql;
  12 +use Model\listsSql;
  13 +use Model\sendJobsSql;
  14 +use Service\SyncMail;
  15 +
  16 +
  17 +/**
  18 + * @author:dc
  19 + * @time 2023/2/13 11:28
  20 + * Class Home
  21 + * @package Controller
  22 + */
  23 +class HomeEs extends Base {
  24 +
  25 +
  26 + /**
  27 + * 邮件列表
  28 + * @author:dc
  29 + * @time 2023/2/17 14:12
  30 + */
  31 + public function lists(){
  32 +
  33 + // 分页 页数
  34 + $page = app()->request('page',1,'intval');
  35 + $page = $page ? $page : 1;
  36 +
  37 + $limit = app()->request('limit',20,'intval');
  38 + $limit = $limit ? $limit : 1;
  39 +
  40 + // 指定id
  41 + $ids = app()->request('mail_id');
  42 + $ids = is_array($ids) ? $ids : [$ids];
  43 + foreach ($ids as $i=>$d){
  44 + if(!is_numeric($d)){
  45 + unset($ids[$i]);
  46 + }
  47 + }
  48 +
  49 + // 附件
  50 + $attachment = app()->request('attachment',0,'bool_Val');
  51 + // 已读/未读
  52 + $seen = app()->request('seen',-1,'intval');
  53 + // 软删
  54 + $deleted = app()->request('deleted',0,'intval');
  55 +
  56 +
  57 + $query = [
  58 + 'query'=>[
  59 + 'bool'=>[
  60 + 'must'=>[
  61 + 'terms'=>[
  62 + 'email_id'=>$this->getEmails('id')
  63 + ]
  64 + ]
  65 + ]
  66 + ]
  67 + ];
  68 +
  69 + $folder_id = [];
  70 + if (app()->requestArr('folder_id')){
  71 + $folder_id = app()->requestArr('folder_id');
  72 + }else{
  73 + // 目录
  74 + $folder = app()->request('folder','收件箱','folderAlias');
  75 + if($folder !== true || $folder!=='true'){
  76 + $folderList = db()->all(folderSql::all($this->getEmails('id')));
  77 +
  78 + // 文件夹id
  79 + if($folderList){
  80 + foreach ($folderList as $item){
  81 + if(
  82 + // 数组文件夹
  83 + (is_array($folder) && in_array($item['folder'],$folder))
  84 + || $item['folder'] == $folder
  85 + ){
  86 + $folder_id[] = $item['id'];
  87 + }
  88 + }
  89 + }
  90 + }
  91 +
  92 + }
  93 +
  94 + if(!$folder_id){
  95 + app()->e('folder_not_fount');
  96 + }
  97 + $query['query']['bool']['must'][] = ['terms'=>[
  98 + 'folder_id'=>$folder_id
  99 + ]];
  100 +
  101 + if($ids) {
  102 + $query['query']['bool']['must'][] = ['terms'=>[
  103 + 'uuid'=>$folder_id
  104 + ]];
  105 + }
  106 +
  107 + if(paramHas('attachment')){ //附件
  108 + $query['query']['bool']['must'][] = ['term'=>[
  109 + 'is_file'=>$attachment ? 1 : 0
  110 + ]];
  111 + }
  112 + // 软删
  113 + $query['query']['bool']['must'][] = ['term'=>[
  114 + 'deleted'=>$deleted
  115 + ]];
  116 +
  117 + // 已读/未读
  118 + if(paramHas('seen')){
  119 + if(in_array($seen,[0,1])){
  120 + $query['query']['bool']['must'][] = ['term'=>[
  121 + 'seen'=>$seen
  122 + ]];
  123 + }
  124 + }
  125 +
  126 + $where['_'] = [];
  127 + // 搜索关键字
  128 + $keyword = app()->request('keyword','',['htmlspecialchars','addslashes']);
  129 + if($keyword){
  130 + $query['query']['bool']['must'][] = [
  131 + 'query_string' => [
  132 + 'query'=>'"'.addcslashes($keyword,'"').'"',
  133 + "default_field"=> "subject"
  134 + ]
  135 + ];
  136 + }
  137 +
  138 + // 那个发的
  139 + $address = app()->request('address');
  140 + if($address){
  141 + if(is_array($address)){
  142 + // 发贱人
  143 + if(Verify::sEmail($address['from']??'')){
  144 + $query['query']['bool']['must'][] = ['match'=>['from'=>$address['from']]];
  145 + }
  146 + // 收件人
  147 + if(Verify::sEmail($address['to']??'')){
  148 + $query['query']['bool']['must'][] = [
  149 + 'match' => [
  150 + 'to'=> $address['to'],
  151 + ]
  152 + ];
  153 + }
  154 +
  155 + }else if(Verify::sEmail($address)){// 收件人/发件人
  156 + $query['query']['bool']['must'][] = [
  157 + 'bool' =>[
  158 + 'must'=>[
  159 + ['match'=>['from'=>$address]]
  160 + ],
  161 + 'should'=>[
  162 + ['match'=>['to'=>$address]]
  163 + ]
  164 + ]
  165 + ];
  166 + }
  167 + }
  168 + // from 搜索收件人
  169 + if(app()->requestHas('from')){
  170 + // 如果是发件箱
  171 + if($folder == '发件箱'){
  172 + $tos = app()->request('from');
  173 + if(!$tos){
  174 + // 不让查询数据
  175 + $query['query']['bool']['must'][] = ['term'=>['uuid'=>0]];
  176 + }else {
  177 + $tos = array_map(function ($v){
  178 + return '"'.addcslashes($v,'"').'"';
  179 + },is_array($tos) ? $tos : [$tos]);
  180 +
  181 + $query['query']['bool']['must'][] = [
  182 + 'query_string' => [
  183 + 'query'=> implode(' OR ',$tos),
  184 + "default_field"=> "to"
  185 + ]
  186 + ];
  187 + }
  188 + }else{
  189 + $from = app()->request('from');
  190 + if(!$from){
  191 + // 不让查询数据
  192 + $query['query']['bool']['must'][] = ['term'=>['uuid'=>0]];
  193 + }else{
  194 + $query['query']['bool']['must'][] = ['match'=>['from'=>$from]];
  195 + }
  196 + }
  197 +
  198 +
  199 + }
  200 +
  201 +
  202 + // 回复
  203 + if (paramHas('answered')){
  204 + $query['query']['bool']['must'][] = ['term'=>['answered'=>app()->request('answered',0,'bool_Val')?1:0]];
  205 + }
  206 +
  207 + // 这个主要是来筛选 是否是自己发送的
  208 + $fromto = app()->request('formorto');
  209 + if($fromto=='from'){
  210 + $query['query']['bool']['must'][] = [
  211 + 'query_string' => [
  212 + 'query'=> implode(' OR ',$this->getEmails('email')),
  213 + "default_field"=> "from"
  214 + ]
  215 + ];
  216 + }elseif ($fromto=='to'){
  217 + $query['query']['bool']['must'][] = [
  218 + 'bool'=>[
  219 + 'must_not'=>[
  220 + [
  221 + 'query_string' => [
  222 + 'query'=> implode(' OR ',$this->getEmails('email')),
  223 + "default_field"=> "from"
  224 + ]
  225 + ]
  226 + ]
  227 + ]
  228 + ];
  229 + }
  230 +
  231 + /**
  232 + * 不查询哪些发件人的邮件
  233 + */
  234 + $form_not_in = app()->request('from_not_in');
  235 + if($form_not_in){
  236 + $form_not_in = is_array($form_not_in) ? $form_not_in : [$form_not_in];
  237 + $form_not_in = array_filter($form_not_in,function ($v){
  238 + if(is_string($v) && Verify::sEmail($v)){
  239 + return true;
  240 + }
  241 + return false;
  242 + });
  243 + }
  244 + if($form_not_in){
  245 + $query['query']['bool']['must'][] = [
  246 + 'bool'=>[
  247 + 'must_not'=>[
  248 + [
  249 + 'query_string' => [
  250 + 'query'=> implode(' OR ',array_unique($form_not_in)),
  251 + "default_field"=> "from"
  252 + ]
  253 + ]
  254 + ]
  255 + ]
  256 + ];
  257 + }
  258 +
  259 + $lists = es()->search($query, ($page-1)*$limit, $limit,['udate'=>'desc']);
  260 + $total = $lists['hits']['total']['value']??0;
  261 + $lists = $lists['hits']['hits']??[];
  262 +
  263 + // map
  264 + $lists = array_map(function ($v){
  265 + $v['uuid'] = get_email_uuid($v['subject'],$v['udate'],$v['from'],$v['to'],$v['size']);
  266 + if(!empty($v['description'])){
  267 + $v['description'] = @html_entity_decode($v['description'], ENT_COMPAT, 'UTF-8');
  268 + }
  269 + $v['to_name'] = @json_decode($v['to_name'],true);
  270 + $v['to_name'] = $v['to_name']?:[];
  271 + if($v['to_name']){
  272 + if(!empty($v['to_name'][0]['email'])){
  273 + $v['to'] = $v['to_name'][0]['email'];
  274 + }
  275 + $v['to_name'] = MailFun::mb_coding($v['to_name'][0]['name']??'');
  276 + }
  277 + if(is_array($v['to_name'])){
  278 + $v['to_name'] = '';
  279 + }
  280 + // 邮件箱
  281 + $v['folder_name'] = db()->cache(86400)->value(folderSql::first($v['folder_id'],'folder'));
  282 + return $v;
  283 + },$lists);
  284 +
  285 + app()->_json(listsPage($lists,$total,$page,$limit));
  286 +
  287 + }
  288 +
  289 + /**
  290 + * 统计
  291 + * @throws \Lib\Err
  292 + * @author:dc
  293 + * @time 2024/10/14 16:20
  294 + */
  295 + public function count()
  296 + {
  297 +
  298 + $body = [];
  299 + $body['query'] = [
  300 + 'bool'=>[
  301 + 'must'=>[
  302 + ['term'=>['deleted'=>0]]
  303 + ]
  304 + ]
  305 + ];
  306 +
  307 +
  308 + $start_time = app()->request('start_time',0,'intval');
  309 + $end_time = app()->request('end_time',0,'intval');
  310 +
  311 +
  312 + if($start_time){
  313 + $body['query']['bool']['must'][] = [
  314 + 'range' => [
  315 + 'udate' => [
  316 + 'gte' => $start_time, // 大于等于开始日期
  317 + ]
  318 + ]
  319 + ];
  320 + }
  321 + if($end_time){
  322 + $body['query']['bool']['must'][] = [
  323 + 'range' => [
  324 + 'udate' => [
  325 + 'lte' => $end_time,
  326 + ]
  327 + ]
  328 + ];
  329 + }
  330 +
  331 + // 目录
  332 + $folderList = db()->all(folderSql::all($this->getEmails('id')));
  333 +
  334 +
  335 + $count = function ($body,$folder,$seen=null) use ($folderList){
  336 + // 文件夹id
  337 + $fids = [];
  338 + foreach ($folderList as $item) {
  339 + if ($item['folder'] == $folder) {
  340 + $fids[] = $item['id'];
  341 + }
  342 + }
  343 + if($fids){
  344 + $body['query']['bool']['must'][] = [
  345 + 'terms' => [
  346 + 'folder_id' => $fids
  347 + ]
  348 + ];
  349 + }else{
  350 + $body['query']['bool']['must'][] = [
  351 + 'term' => [
  352 + 'folder_id' => -1
  353 + ]
  354 + ];
  355 + }
  356 + if($seen!==null){
  357 + // 未读
  358 + $body['query']['bool']['must'][] = [
  359 + 'term' => [
  360 + 'seen' => $seen
  361 + ]
  362 + ];
  363 + }
  364 +
  365 +
  366 + return es()->count($body);
  367 +
  368 + };
  369 +
  370 +
  371 +
  372 + // 总数
  373 + $inbox = $count($body,'收件箱');
  374 +
  375 + if(app()->request('show')=='inbox'){
  376 + app()->_json(['inbox'=>$inbox]);
  377 + }
  378 +
  379 +
  380 + $unseen = $count($body,'收件箱',0);
  381 +
  382 + //发件箱
  383 + $send = $count($body,'发件箱',0);
  384 +
  385 + app()->_json(['inbox'=>$inbox,'unseen'=>$unseen,'send'=>$send]);
  386 + }
  387 +
  388 +
  389 +
  390 +}
  391 +
  392 +
  393 +
  394 +
  395 +
  396 +
  397 +
  398 +
  399 +
  400 +
  401 +
  402 +
  403 +
  404 +