<?php

namespace Option\Lib;

use Dever;

class Email
{
    # 修改邮箱
    public function act_api()
    {
        //$signature = Dever::input('signature');
        $signature = $_GET['signature'];
        if (!$signature) {
            Dever::alert('您的验证信息已失效，请重新发送验证邮件');
        }
        $url = Dever::url();
        if (strstr($url, 'uat')) {
            $host = 'http://qiquan.uat.churenyiliao.com/';
        } else {
            $host = 'http://qiquan.churenyiliao.com/';
        }

        $signature = Dever::decode($signature);
        $temp = explode('|||', $signature);
        $code = $temp[2];

        $state = Dever::load('passport/reg')->code($code);
        if (!$state) {
            //Dever::alert('您的验证信息已失效，请重新发送验证邮件');
        }

        $check = Dever::db('option/account')->find(array('email' => $temp[1]));

        if (!$check) {
            Dever::location($host . 'pages/app/checkemail/checkemail/?email='.$temp[1].'&status=2');
            Dever::alert('您的验证信息已失效，请重新发送验证邮件');
        }

        $where['where_id'] = $temp[0];
        //$where['email'] = $temp[1];
        $where['is_email'] = 1;

        Dever::db('option/account')->update($where);

        Dever::location($host . 'pages/app/checkemail/checkemail/?email='.$temp[1].'&status=1');
        echo '您的邮件：'.$temp[1].'，已经验证成功。请登录期权管理系统查询。';die;
    }

    public function renzheng()
    {
        $email = Dever::input('email');
        $username = Dever::input('username');
        $code = Dever::input('code');
        $aid = Dever::input('aid');
        $email = base64_decode($email);
        $config = Dever::config('base', 'project')->email['option'];
        $title = $config['title'];
        $content = $config['content'];

        $signature = Dever::encode($aid . '|||' . $email . '|||' . $code);

        $link = Dever::url($config['link'] . '?signature=' . $signature);
        $date = date('Y-m-d H:i:s');
        $content = str_replace('{link}', $link, $content);
        $content = str_replace('{username}', $username, $content);
        $content = str_replace('{date}', $date, $content);
        $this->send($email, $username, $title, $content);
    }

    public function send($email, $username, $title, $content)
    {
        $config = Dever::config('base', 'project')->email;

        Dever::apply('src/PHPMailer', 'email');
        Dever::apply('src/Exception', 'email');
        Dever::apply('src/SMTP', 'email');
        $mail = new \PHPMailer\PHPMailer\PHPMailer();

        $mail->isSMTP();
        //$mail->SMTPDebug = 2;
        $mail->CharSet = 'UTF-8';
        $mail->Host = $config['smtp'];
        $mail->Port = 465;
        $mail->SMTPSecure = 'ssl';
        $mail->SMTPAuth = true;
        $mail->Username = $config['username'];
        $mail->Password = $config['password'];
        $mail->setFrom($config['from'][0], '期权管理系统');
        $mail->addAddress($email, $username);
        $mail->Subject = "=?utf-8?B?" . base64_encode($title) . "?=";
        $mail->Body = $content;
        $mail->isHTML(true);
        //$mail->addAttachment($file);
        if (!$mail->send()) {
            Dever::alert("Mailer Error: " . $mail->ErrorInfo);
        } else {
            return 'ok';
        }
    }
}
