hot_mail.php 4.3 KB
<?php

use Model\listsSql;


/**
 * 只有黑格 在使用此业务
 * 预热 邮件 数据处理
 * 主要功能:
 *  在 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->db = db();
        $this->start();
    }

    /**
     * shopk那边的预热邮箱
     * @var array
     */
    private $shopkHotEmail = [];

    /**
     * @var \Lib\Db|\Lib\DbPool
     */
    private $db;


    /**
     * @author:dc
     * @time 2024/7/18 14:04
     */
    private function start(){
        _echo('启动预热邮件处理 '.getmypid());

        $fob = $this->getFobHotEmail();

        // 把黑格自己的也算进去
        $this->shopkHotEmail = array_merge($this->getHotEmail('e'),$this->getHotEmail('w'),$fob);

        $this->shopkHotEmail = array_map('strtolower',$this->shopkHotEmail);

        $this->shopkHotEmail = array_flip($this->shopkHotEmail);

        $this->shopkHotEmail['job@aicc-email.com'] = 1;

        foreach ($this->shopkHotEmail as $e=>$i){
            try {
                if(!$this->db->count("select count(*) from `hot_mail` where ".dbWhere(['email'=>$e]))){
                    $this->db->throw()->insert('hot_mail',['email'=>$e],false);
                }

            }catch (Throwable $e){}
        }


        $maxId = $this->db->value("select `id` from `lists` order by `id` desc limit 1");
        for ($i=0;$i<ceil($maxId/1000);$i++){
            $this->run(rand($i*1000,($i+1)*1000));
        }

        echo '已执行完成'.PHP_EOL;
    }


    private $folder = [];

    private function run($id){
        $list = $this->db->all(\Model\listsSql::first(dbWhere(['id'=>$id,'is_hots'=>0]),'`id`,`from`,`to`,`folder_id`'));
        foreach ($list as $item){
            if(empty($this->folder[$item['folder_id']])){
                $this->folder[$item['folder_id']] = folderAlias($this->db->value(\Model\folderSql::first($item['folder_id'],'folder')));
            }

            // 是否是发件箱
            if($this->folder[$item['folder_id']] == '发件箱'){
                $w = ['email' => explode(',',$item['to'])];
            }else{
                $w = ['email' =>$item['from']];
            }
            // 是否在 预热邮箱中
            if($this->db->count('select count(*) from `hot_mail` where '.dbWhere($w))){

                $ret = $this->db->update(listsSql::$table,['is_hots'=>1],dbWhere(['id'=>$item['id']]));
                echo date('d H:i:s').' ==》 '.$item['id'].':'.$ret."\n";
            }
        }
    }


    /**
     * 获取预热邮箱
     * shopk的
     * @return array
     * @author:dc
     * @time 2024/7/18 13:58
     */
    private function getHotEmail($t='e'):array {
        $url['e'] = 'https://oa.shopk.com/api/mail/preheat';
        $url['w'] = 'https://oa.shopk.com/api/mail/write?iswrite=1';
        $data = @file_get_contents($url[$t]);

        if($data){
            $data = @json_decode($data,1);
            if($data && isset($data['data']) && is_array($data['data'])){
                // 全部转小写
                return array_map("strtolower",$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')));
        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();
        return [];
    }



}


swoole_set_process_name('hot-email-run-man');

$pm = new Swoole\Process\Manager();

$pm->add(function (){

    swoole_set_process_name('hot-email-run');

    include_once "../vendor/autoload.php";

    new HotMail();
    // 执行完了暂停5分钟在执行
    sleep(300);
});

$pm->start();