Login.php 3.1 KB
<?php

namespace Controller;

use Lib\Imap\ImapConfig;
use Lib\Imap\ImapPool;
use Lib\Mail\MailFun;
use Lib\Verify;
use Model\emailSql;
use Service\MailProxy;
use Service\SyncMail;


/**
 * 登录
 * @author:dc
 * @time 2023/2/18 16:49
 * Class Login
 * @package Controller
 */
class Login {


    /**
     * 登录操作
     * @return int[]
     * @author:dc
     * @time 2023/2/13 11:50
     */
    public function login(){
        //        $mail,$password,$imap,$smtp
        $formData = app()->request(['email','password','imap','smtp']);

        if(empty($formData['email']) || !Verify::sEmail($formData['email'])){
            app()->e('email_verify_error');
        }

        if(empty($formData['password'])){
            app()->e('password_verify_error');
        }

        if(empty($formData['imap'])){
            app()->e('imap_verify_error');
        }

        if(empty($formData['smtp'])){
            app()->e('smtp_verify_error');
        }

        $proxy = (new MailProxy($formData['email'],$formData['imap'],$formData['smtp']));

        $mailClient = ImapPool::get(
            (new ImapConfig())
                ->setPassword($formData['password'])
                ->setHost($proxy->getImapProxy())
            ->setEmail($formData['email'])
        );

        $login = $mailClient->login();
        if(!$login->isOk()){
            app()->e($login->getMessage()?'登录失败:'.$login->getMessage():'IMAP:服务器连接失败');
        }



        // 验证smtp登录
        if(!MailFun::smtpLoginTest($proxy->getSmtpProxy(),$formData['email'],$formData['password'])){
            app()->e('email_smtp_server_error');
        }


        // 是否存在
        $id = db()->value(emailSql::hasEmail($formData['email']));

        $data = [
            'password'  =>  base64_encode($formData['password']),
            'imap'  =>  $proxy->getImapProxy(),
            'smtp'  =>  $proxy->getSmtpProxy(),
            'status'    =>  1,
            'pwd_error'    =>  0
        ];
        $proxy->proxySuccess();// 标记代理成功

        if($id){
            // 修改
            $ret = db()->update(emailSql::$table,$data,dbWhere(['id'=>$id]));
        }else{
            // 新增
            $data['email'] = $formData['email'];
            $ret = db()->insert(emailSql::$table,$data);

            if($ret){
//                // 增加邮件数量
//                redis()->incr('email_total');
                // 立即处理 同步
                redis()->lPush('sync_email_lists', $ret);
            }

        }


        if($ret){
            // 同步一次文件夹
            try{
                (new SyncMail($formData['email'],$mailClient))->sync(false);
            }catch (\Throwable $e){}

            $ret = db()->first(
                emailSql::first(
                    $formData['email'],
                    '`id`,`email`,`imap`,`smtp`,`status`,`created_at`'
                ));

            $ret = array_merge($ret,app()->request(['imap','smtp']));

            app()->_json($ret);
        }

        app()->e('login_error');


    }







}