<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use cmf\controller\HomeBaseController;
use think\facade\Validate;

class VerificationCodeController extends HomeBaseController
{
    public function send()
    {
        if (!$this->request->isPost()) {
            $this->error(lang('illegal request'));
        }
        $validate = new \think\Validate();
        $validate->rule([
            'username' => 'require',
//            'captcha'  => 'require',
        ]);
        $validate->message([
            'username.require' => lang('请输入手机号或邮箱！'),
            'captcha.require'  => lang('验证码不能为空！'),
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $result = hook_one("check_third_party_captcha");

        if ($result) {
            if (is_string($result)) {
                $this->error($result);
            }
        } else {
            if (empty($data['captcha'])) {
                $this->error(lang('验证码不能为空！'));
            }
            $captchaId = empty($data['captcha_id']) ? '' : $data['captcha_id'];
            if (!cmf_captcha_check($data['captcha'], $captchaId, false)) {
                $this->error(lang('验证码错误！'));
            }

            $registerCaptcha = session('register_captcha');

            session('register_captcha', $data['captcha']);

            if ($registerCaptcha == $data['captcha']) {
                cmf_captcha_check($data['captcha'], $captchaId, true);
                $this->error(lang('请输入新验证码！'));
            }
        }

        $accountType = '';

        if (Validate::is($data['username'], 'email')) {
            $accountType = 'email';
        } else if (cmf_check_mobile($data['username'])) {
            $accountType = 'mobile';
        } else {
            $this->error(lang('请输入正确的手机或者邮箱格式！'));
        }

        if (isset($data['type']) && $data['type'] == 'register') {
            if ($accountType == 'email') {
                $findUserCount = db('user')->where('user_email', $data['username'])->count();
            } else if ($accountType == 'mobile') {
                $findUserCount = db('user')->where('mobile', $data['username'])->count();
            }

            if ($findUserCount > 0) {
                $this->error(lang('您的账号已注册过！'));
            }
        }

        //TODO 限制 每个ip 的发送次数

        $code = cmf_get_verification_code($data['username']);
        if (empty($code)) {
            $this->error(lang('验证码发送过多,请明天再试！'));
        }

        if ($accountType == 'email') {

            $emailTemplate = cmf_get_option('email_template_verification_code');
            if (empty($emailTemplate)) {
                $this->error(lang('未设置邮箱验证码模板！'));
            }
            $user = cmf_get_current_user();
            if ($user === false) {
                $username = $data['username'];
            } else {
                $username = empty($user['user_nickname']) ? $user['user_login'] : $user['user_nickname'];
            }

            $message = htmlspecialchars_decode($emailTemplate['template']);
            $message = $this->view->display($message, ['code' => $code, 'username' => $username]);
            $subject = empty($emailTemplate['subject']) ? lang('数字验证码') : $emailTemplate['subject'];
            $result  = cmf_send_email($data['username'], $subject, $message);

            if (empty($result['error'])) {
                cmf_verification_code_log($data['username'], $code);
                $this->success(lang('验证码已经发送成功！'));
            } else {
                $this->error(lang('邮箱验证码发送失败！'));
            }

        } else if ($accountType == 'mobile') {

            $param  = ['mobile' => $data['username'], 'code' => $code];
            $result = hook_one("send_mobile_verification_code", $param);

            if ($result !== false && !empty($result['error'])) {
                $this->error($result['message']);
            }

            if ($result === false) {
                $this->error(lang('未安装验证码发送插件,请联系管理员！'));
            }

            $expireTime = empty($result['expire_time']) ? 0 : $result['expire_time'];

            cmf_verification_code_log($data['username'], $code, $expireTime);

            if (!empty($result['message'])) {
                $this->success($result['message']);
            } else {
                $this->success(lang('验证码已经发送成功！'));
            }

        }


    }

}
