Email.php 5.2 KB
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * 邮箱
 * @time 2022/7/29 15:09
 * Class Email
 * @package App\Mail\Models
 */
class Email extends Model {

    // 启用
    const STATUS_ACTIVE =   1;
    // 禁用
    const STATUS_DISABLED =   0;


    /**
     * @param string $v
     * @return mixed|string
     * @time 2022/7/29 17:34
     */
    public function getPasswordAttribute($v)
    {
        return $v ? @base64_decode($v) : '';
    }

    /**
     * 添加邮箱
     * @param int $user_id
     * @param string $email
     * @param string $password
     * @param string $email_name
     * @param array $host
     * @return false|static
     * @time 2022/8/17 11:12
     */
    public static function _add(int $user_id, string $email, string $password='', string $email_name = '',$host = []) {
        // 这里可以用事务提交,允许断层

        // 是否已存在邮箱
        $model =   static::where('email',$email)->first();
        if(!$model){
            // 新增
            $model  =   new static();
            $model->email = $email;
        }

        $password && $model->password = encrypt($password);
        $model->status = self::STATUS_ACTIVE;
        $model->email_name = $email_name;
        if(!empty($host['imap'])){
            $model->imap = $host['imap'];
        }
        if(!empty($host['smtp'])){
            $model->smtp = $host['smtp'];
        }

        if(!$model->save()){
            return false;
        }

        // 是否已绑定
        $userEmail = EmailJoinUser::isBind($user_id,$model->id);
        if(!$userEmail){
            if(!EmailJoinUser::insert([
                'user_id'   =>  $user_id,
                'email_id'  =>  $model->id
            ])){
                return false;
            };
        }

        return $model;
    }


    /**
     * 修改密码
     * @param string $email
     * @param string $password
     * @return bool
     * @time 2022/7/29 17:32
     */
    public static function _changePwd(string $email, string $password):bool {
        $data   =   static::where([
            'email'    =>  $email
        ])
            ->first();

        if($data){
            $data->password =   encrypt($password);
            $data->pwd_error =   0;
            $data->save();
        }
        return false;
    }


    /**
     * 读取列表
     * @return mixed
     * @author:dc
     * @time 2023/2/5 14:27
     */
    public static function _all() {
        $lists = static::where([
                'status'    =>  self::STATUS_ACTIVE,
                'pwd_error'    =>  0,
            ])->get();
        return $lists;
    }



    public static function _getById($user_id){
        return DB::table('email_join_users as eu')
            ->leftJoin('emails as e','eu.email_id','=','e.id')
            ->where([
//                'eu.user_id'=>$user_id,
                'e.status'    =>  self::STATUS_ACTIVE
            ])->get(['e.id'])
            ->toArray();
    }

    /**
     * 读取一条
     * @param string $email
     * @return mixed
     * @author:dc
     * @time 2023/2/4 15:50
     */
    public static function _first(string $email) {
        return static::where([
            'email'    =>  $email
        ])
            ->first();
    }

    /**
     * 根据主键获取数据
     * @param $id
     * @return array
     * @author:dc
     * @time 2022/10/24 11:38
     */
    public static function _firstById($id):array {
        $data   =   static::where('id',$id)->first();

        return $data ? $data->toArray() : [];
    }


    /**
     * 禁用邮箱
     * @param string $email
     * @return bool
     * @time 2022/7/29 16:18
     */
    public static function disabled(string $email):bool{
        $data   =   static::where([
            'email'  =>  $email,
        ])->first();

        if ($data){
            $data->status   =   self::STATUS_DISABLED;
            return $data->save();
        }
        return false;
    }

    /**
     * 启用
     * @param string $email
     * @return bool
     * @time 2022/7/29 16:20
     */
    public static function active(string $email):bool{
        $data   =   static::where([
            'email'  =>  $email,
        ])->first();

        if ($data){
            $data->status   =   self::STATUS_ACTIVE;
            return $data->save();
        }
        return false;
    }


    /**
     * 更新
     * @param $where
     * @param $data
     * @return mixed
     * @time 2022/8/1 15:54
     */
    public static function _update($where,$data){
        return static::where($where)->update($data);
    }

    /**
     * @param $email
     * @return mixed
     * @time 2022/8/17 11:38
     */
    public static function _getIds($email){
        return static::where('email',is_array($email)?'in':'=',$email)->get(['id'])->pluck('id')->toArray();
    }


    /**
     * 读取发布同步任务 的 列表
     * @param int $p 分页
     * @return mixed
     * @author:dc
     * @time 2023/2/6 11:25
     */
    public static function getPushEmailList(int $p){
        $emails =  Email::where(['status'=>1,'pwd_error'=>0])
            ->orderBy('id','asc')
            ->select()
            ->limit($p*100,100)
            ->get();

        return $emails->isEmpty() ? [] :$emails;
    }








}