作者 邓超

预热 逻辑

<?php
include_once "../vendor/autoload.php";
/**
* 只有黑格 在使用此业务
* 预热 邮件 数据处理
* 主要功能:
* 在 https://fob.ai.cc/api/mail/preheat 拉取 预热邮件
* 在 https://oa.shopk.com/api/mail/preheat 拉取 预热邮件
* 在 lists表中找到这些邮箱发出的邮件
* 然后在 hot_mail表中记录起来
* @author:dc
* @time 2024/7/18 13:50
* Class HotMail
*/
class HotMail {
public function __construct(){$this->start();}
/**
* shopk那边的预热邮箱
* @var array
*/
private $shopkHotEmail = [];
/**
* @author:dc
* @time 2024/7/18 14:04
*/
private function start(){
_echo('启动预热邮件处理 '.getmypid());
$this->shopkHotEmail = $this->getHotEmail();
if($this->shopkHotEmail){
array_map([$this,'moveMail'],$this->getFobHotEmail());
}
}
/**
* 邮件检查及移动
* @param string $email 检查的邮箱
* @author:dc
* @time 2024/7/18 14:06
*/
private function moveMail(string $email){
// 读取邮箱在表里的id
$email_id = db()->value(sprintf("select `id` from `%s` where `email` = '%s' limit 1",\Model\emailSql::$table, $email));
if($email_id){
$this->findFolder($email_id,'收件箱','s');
$this->findFolder($email_id,'发件箱','f');
}
return true;
}
/**
* 查找文件夹
* @param int $email_id 邮箱id
* @param string $folder 那个文件夹
* @author:dc
* @time 2024/7/18 15:10
*/
private function findFolder(int $email_id, string $folder,string $type){
// 查询 文件夹
$folders = db()->all(\Model\folderSql::all($email_id,['id','folder']));
foreach ($folders as $f){
if(folderAlias($f['folder']) == $folder){
// 最后记录的id
$last_id = db()->value('select max(`lists_id`) from `fob_hot_mail` where `email_id` = '.$email_id.' limit 1');
$this->findList(new Data($email_id,$f['id'],$type,$last_id));
}
}
return true;
}
/**
* 查找 邮件
* @author:dc
* @time 2024/7/18 15:11
*/
private function findList(Data $data, int $p = 1){
$lists = db()->all(
sprintf(
"select `id`,`folder_id`,`email_id`,`from` from `lists` where `id` > %d and `email_id` = %d and `folder_id` = %d and (select count(*) from `fob_hot_mail` where `lists`.`id` = `fob_hot_mail`.`lists_id`) > 0 limit 1000 offset ".(($p-1)*1000),
$data->last_id,
$data->email_id,
$data->folder_id
)
);
if($lists){
// 处理数据
foreach ($lists as $list){
$list['folder'] = $data->type;
$this->insertData($list);
}
$lists = null;
$list = null;
// 再次查找
$this->findList($data,$p + 1);
}
return true;
}
/**
* 插入数据
* @param array $data
* @author:dc
* @time 2024/7/18 15:25
*/
private function insertData(array $data){
// 是预热 邮箱发来的邮件
if(in_array($data['from'],$this->shopkHotEmail)){
// 不存在 数据
if(db()->count("select count(*) from `fob_hot_mail` where `lists_id` = ".$data['id']) == 0){
db()->insert('fob_hot_mail',[
'lists_id' => $data['id'],
'email_id' => $data['email_id'],
'folder_id' => $data['folder_id'],
'folder' => $data['folder']
],false);
}
}
return true;
}
/**
* 获取预热邮箱
* shopk的
* @return array
* @author:dc
* @time 2024/7/18 13:58
*/
private function getHotEmail():array {
$data = @file_get_contents('https://oa.shopk.com/api/mail/preheat');
if($data){
$data = @json_decode($data,1);
if($data && isset($data['data']) && is_array($data['data'])){
return $data['data'];
}
}
logs('shopk 获取预热邮箱错误:'.print_r($data,1),'get_hot_oa_email.error.log')->write();
return [];
}
/**
* fob黑格的预热邮箱
* @author:dc
* @time 2024/7/18 14:21
*/
private function getFobHotEmail(){
$data = @file_get_contents('https://fob.ai.cc/api/mail/preheat/'.md5('aicc.'.date('ymdh')));
print_r($data);
if($data){
$data = @json_decode($data,1);
if($data && isset($data['data']) && is_array($data['data'])){
return $data['data'];
}
}
logs('fob 获取预热邮箱错误:'.print_r($data,1),'get_hot_fob_email.error.log')->write();
}
}
class Data {
public int $email_id = 0;
public int $folder_id = 0;
public string $type = '';
public int $last_id = 0;
public function __construct(int $email_id,int $folder_id, string $type,int $last_id)
{
$this->email_id = $email_id;
$this->folder_id = $folder_id;
$this->type = $type;
$this->last_id = $last_id;
}
}
new HotMail();
... ...
<?php
namespace Controller\fob_ai;
use Controller\Base;
use Model\folderSql;
/**
* 黑格 fob 那边专用 业务
* 为定制逻辑
* @author:dc
* @time 2024/7/18 11:40
* Class MailList
* @package Controller\fob_ai
*/
class MailList extends Base {
/**
* 邮件列表
* @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]);
}
}
// 附件
$attachment = app()->request('attachment',0,'bool_Val');
$deleted = 0;
$where = ['email_id'=>$this->getEmails('id')];
// 目录
$folder = app()->request('folder','收件箱');
// 只允许查询这里文件夹
if(in_array($folder,['收件箱','发件箱','垃圾箱','星标邮件','预热收件箱','预热发件箱']))
// 查询 文件夹
$folderList = db()->all(folderSql::all($where['email_id']));
$folder_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');
}
//目录
$where['folder_id'] = $folder_id;
if($ids) $where['id'] = $ids;
if(paramHas('attachment')){
$where['is_file'] = $attachment ? 1 : 0; //附件
}
// 软删
$where['deleted'] = $deleted;
// 已读/未读
if(paramHas('seen')){
if(in_array($seen,[0,1])){
$where['seen'] = $seen;
}
}
$where['_'] = [];
// 搜索关键字
$keyword = app()->request('keyword','',['htmlspecialchars','addslashes']);
if($keyword){
$where['_'][] = '`subject` like "%'.$keyword.'%"';
}
// 那个发的
$address = app()->request('address');
if($address){
if(is_array($address)){
// 发贱人
if(Verify::sEmail($address['from']??'')){
$where['from'] = $address['from'];
}
// 收件人
if(Verify::sEmail($address['to']??'')){
$where['_'][] = '`to_name` like "%'.$address.'%"';
}
}else if(Verify::sEmail($address)){
// 收件人/发件人
$where['_'][] = '(`from` = "'.$address.'" or `to_name` like "%'.$address.'%")';
}
}
// from 搜索收件人
if(app()->requestHas('from')){
// 如果是发件箱
if($folder == '发件箱'){
$where['to'] = app()->request('from');
if(!$where['to']){
// 不让查询数据
$where['id'] = 0;
}
}else{
$where['from'] = app()->request('from');
if(!$where['from']){
// 不让查询数据
$where['id'] = 0;
}
}
}
// 回复
if (paramHas('answered')){
$where['answered'] = app()->request('answered',0,'bool_Val')?1:0;
}
// 这个主要是来筛选 是否是自己发送的
$fromto = app()->request('formorto');
if($fromto=='from'){
$where['from'] = $this->getEmails('email');
}elseif ($fromto=='to'){
$where['from.notin'] = $this->getEmails('email');
}
/**
* 不查询哪些发件人的邮件
*/
$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){
if(isset($where['from.notin'])){
$where['from.notin'] = array_merge($where['from.notin'],$form_not_in);
}else{
$where['from.notin'] = $form_not_in;
}
}
}
if(!empty($where['from.notin'])){
$where['from.notin'] = array_unique($where['from.notin']);
}
$lists = db()->all(
listsSql::lists(
dbWhere($where),
$page,
$limit
)
);
// 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'] = $v['to_name'][0]['name']??'';
}
if(is_array($v['to_name'])){
$v['to_name'] = '';
}
return $v;
},$lists);
// 总数
$total = db()->count(
listsSql::listCount(dbWhere($where))
);
app()->_json(listsPage($lists,$total,$page,$limit));
}
}
... ...
... ... @@ -62,7 +62,7 @@ function logs($message,$filename=null){
}
// $a = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT,4);
// $message .= print_r($a,1);
\Lib\Log::append($message, $filename);
return \Lib\Log::append($message, $filename);
}
... ...
... ... @@ -16,12 +16,12 @@ class Log {
/**
* @var array
*/
public array $filename = [];
protected array $filename = [];
/**
* @var array
*/
public array $message = [];
protected array $message = [];
/**
* @var self
... ... @@ -39,8 +39,10 @@ class Log {
/**
* 追加日志内容
* @param string $message
* @param null $filename
* @return Log
* @author:dc
* @time 2023/3/14 10:45
* @time 2024/7/18 14:12
*/
public static function append(string $message, $filename = null){
... ... @@ -49,7 +51,7 @@ class Log {
}
self::getInstance()->message[$filename ? md5($filename) : 'default'][] = date('Y-m-d H:i:s ').$message;
return self::getInstance();
}
... ... @@ -84,7 +86,7 @@ class Log {
/**
* @return array
*/
public function getFilename($key=null): string
protected function getFilename($key=null): string
{
return $this->filename[$key]??LOG_PATH.'/'.date('Y-m-d').'.error.log';
}
... ...