作者 邓超

优化同步

<?php
namespace Controller;
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;
use Service\SyncMail;
/**
* @author:dc
* @time 2023/2/13 11:28
* Class Home
* @package Controller
*/
class HomeEs extends Base {
/**
* 邮件列表
* @author:dc
* @time 2023/2/17 14:12
*/
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]);
}
}
// 附件
$attachment = app()->request('attachment',0,'bool_Val');
// 已读/未读
$seen = app()->request('seen',-1,'intval');
// 软删
$deleted = app()->request('deleted',0,'intval');
$query = [
'query'=>[
'bool'=>[
'must'=>[
'terms'=>[
'email_id'=>$this->getEmails('id')
]
]
]
]
];
$folder_id = [];
if (app()->requestArr('folder_id')){
$folder_id = app()->requestArr('folder_id');
}else{
// 目录
$folder = app()->request('folder','收件箱','folderAlias');
if($folder !== true || $folder!=='true'){
$folderList = db()->all(folderSql::all($this->getEmails('id')));
// 文件夹id
if($folderList){
foreach ($folderList as $item){
if(
// 数组文件夹
(is_array($folder) && in_array($item['folder'],$folder))
|| $item['folder'] == $folder
){
$folder_id[] = $item['id'];
}
}
}
}
}
if(!$folder_id){
app()->e('folder_not_fount');
}
$query['query']['bool']['must'][] = ['terms'=>[
'folder_id'=>$folder_id
]];
if($ids) {
$query['query']['bool']['must'][] = ['terms'=>[
'uuid'=>$folder_id
]];
}
if(paramHas('attachment')){ //附件
$query['query']['bool']['must'][] = ['term'=>[
'is_file'=>$attachment ? 1 : 0
]];
}
// 软删
$query['query']['bool']['must'][] = ['term'=>[
'deleted'=>$deleted
]];
// 已读/未读
if(paramHas('seen')){
if(in_array($seen,[0,1])){
$query['query']['bool']['must'][] = ['term'=>[
'seen'=>$seen
]];
}
}
$where['_'] = [];
// 搜索关键字
$keyword = app()->request('keyword','',['htmlspecialchars','addslashes']);
if($keyword){
$query['query']['bool']['must'][] = [
'query_string' => [
'query'=>'"'.addcslashes($keyword,'"').'"',
"default_field"=> "subject"
]
];
}
// 那个发的
$address = app()->request('address');
if($address){
if(is_array($address)){
// 发贱人
if(Verify::sEmail($address['from']??'')){
$query['query']['bool']['must'][] = ['match'=>['from'=>$address['from']]];
}
// 收件人
if(Verify::sEmail($address['to']??'')){
$query['query']['bool']['must'][] = [
'match' => [
'to'=> $address['to'],
]
];
}
}else if(Verify::sEmail($address)){// 收件人/发件人
$query['query']['bool']['must'][] = [
'bool' =>[
'must'=>[
['match'=>['from'=>$address]]
],
'should'=>[
['match'=>['to'=>$address]]
]
]
];
}
}
// from 搜索收件人
if(app()->requestHas('from')){
// 如果是发件箱
if($folder == '发件箱'){
$tos = app()->request('from');
if(!$tos){
// 不让查询数据
$query['query']['bool']['must'][] = ['term'=>['uuid'=>0]];
}else {
$tos = array_map(function ($v){
return '"'.addcslashes($v,'"').'"';
},is_array($tos) ? $tos : [$tos]);
$query['query']['bool']['must'][] = [
'query_string' => [
'query'=> implode(' OR ',$tos),
"default_field"=> "to"
]
];
}
}else{
$from = app()->request('from');
if(!$from){
// 不让查询数据
$query['query']['bool']['must'][] = ['term'=>['uuid'=>0]];
}else{
$query['query']['bool']['must'][] = ['match'=>['from'=>$from]];
}
}
}
// 回复
if (paramHas('answered')){
$query['query']['bool']['must'][] = ['term'=>['answered'=>app()->request('answered',0,'bool_Val')?1:0]];
}
// 这个主要是来筛选 是否是自己发送的
$fromto = app()->request('formorto');
if($fromto=='from'){
$query['query']['bool']['must'][] = [
'query_string' => [
'query'=> implode(' OR ',$this->getEmails('email')),
"default_field"=> "from"
]
];
}elseif ($fromto=='to'){
$query['query']['bool']['must'][] = [
'bool'=>[
'must_not'=>[
[
'query_string' => [
'query'=> implode(' OR ',$this->getEmails('email')),
"default_field"=> "from"
]
]
]
]
];
}
/**
* 不查询哪些发件人的邮件
*/
$form_not_in = app()->request('from_not_in');
if($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){
$query['query']['bool']['must'][] = [
'bool'=>[
'must_not'=>[
[
'query_string' => [
'query'=> implode(' OR ',array_unique($form_not_in)),
"default_field"=> "from"
]
]
]
]
];
}
$lists = es()->search($query, ($page-1)*$limit, $limit,['udate'=>'desc']);
$total = $lists['hits']['total']['value']??0;
$lists = $lists['hits']['hits']??[];
// map
$lists = array_map(function ($v){
$v['uuid'] = get_email_uuid($v['subject'],$v['udate'],$v['from'],$v['to'],$v['size']);
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'] = MailFun::mb_coding($v['to_name'][0]['name']??'');
}
if(is_array($v['to_name'])){
$v['to_name'] = '';
}
// 邮件箱
$v['folder_name'] = db()->cache(86400)->value(folderSql::first($v['folder_id'],'folder'));
return $v;
},$lists);
app()->_json(listsPage($lists,$total,$page,$limit));
}
/**
* 统计
* @throws \Lib\Err
* @author:dc
* @time 2024/10/14 16:20
*/
public function count()
{
$body = [];
$body['query'] = [
'bool'=>[
'must'=>[
['term'=>['deleted'=>0]]
]
]
];
$start_time = app()->request('start_time',0,'intval');
$end_time = app()->request('end_time',0,'intval');
if($start_time){
$body['query']['bool']['must'][] = [
'range' => [
'udate' => [
'gte' => $start_time, // 大于等于开始日期
]
]
];
}
if($end_time){
$body['query']['bool']['must'][] = [
'range' => [
'udate' => [
'lte' => $end_time,
]
]
];
}
// 目录
$folderList = db()->all(folderSql::all($this->getEmails('id')));
$count = function ($body,$folder,$seen=null) use ($folderList){
// 文件夹id
$fids = [];
foreach ($folderList as $item) {
if ($item['folder'] == $folder) {
$fids[] = $item['id'];
}
}
if($fids){
$body['query']['bool']['must'][] = [
'terms' => [
'folder_id' => $fids
]
];
}else{
$body['query']['bool']['must'][] = [
'term' => [
'folder_id' => -1
]
];
}
if($seen!==null){
// 未读
$body['query']['bool']['must'][] = [
'term' => [
'seen' => $seen
]
];
}
return es()->count($body);
};
// 总数
$inbox = $count($body,'收件箱');
if(app()->request('show')=='inbox'){
app()->_json(['inbox'=>$inbox]);
}
$unseen = $count($body,'收件箱',0);
//发件箱
$send = $count($body,'发件箱',0);
app()->_json(['inbox'=>$inbox,'unseen'=>$unseen,'send'=>$send]);
}
}
... ...