<?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: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use app\user\service\UserService;
use cmf\controller\HomeBaseController;
use think\facade\Validate;

class RegisterController extends HomeBaseController
{

    /**
     * 前台用户注册
     */
    public function index()
    {
        $redirect = $this->request->post("redirect");
        if (empty($redirect)) {
            $redirect = $this->request->server('HTTP_REFERER');
        } else {
            $redirect = base64_decode($redirect);
        }
        session('login_http_referer', $redirect);

        if (cmf_is_user_login()) {
            return redirect($this->request->root() . '/');
        } else {
            return $this->fetch(":register");
        }
    }

    /**
     * 前台用户注册提交
     */
    public function doRegister()
    {
        if ($this->request->isPost()) {
            $rules = [
//                'captcha'  => 'require',
                'code'     => 'require',
                'password' => 'require|min:6|max:32',
            ];

            $isOpenRegistration = cmf_is_open_registration();

            if ($isOpenRegistration) {
                unset($rules['code']);
            }

            $validate = new \think\Validate();
            $validate->rule($rules);
            $validate->message([
                'code.require'     => lang('数字验证码不能为空！'),
                'password.require' => lang('密码不能为空！'),
                'password.max'     => lang('密码不能超过32个字符！'),
                'password.min'     => lang('密码不能小于6个字符！')
            ]);

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

            if (!$isOpenRegistration) {

                $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)) {
                        $this->error(lang('验证码错误！'));
                    }
                }

                $errMsg = cmf_check_verification_code($data['username'], $data['code']);
                if (!empty($errMsg)) {
                    $this->error($errMsg);
                }

            }

            $user['user_pass'] = $data['password'];
            if (Validate::is($data['username'], 'email')) {
                $user['user_email'] = $data['username'];
                $log                = UserService::register($user, 3);
            } else if (cmf_check_mobile($data['username'])) {
                $user['mobile'] = $data['username'];
                $log            = UserService::register($user, 2);
            } else {
                $log = 2;
            }
            $sessionLoginHttpReferer = session('login_http_referer');
            $redirect                = empty($sessionLoginHttpReferer) ? cmf_get_root() . '/' : $sessionLoginHttpReferer;
            hook('user_register', ['user' => $user, 'code' => $log]);
            switch ($log) {
                case 0:
                    $this->success(lang('注册成功！'), $redirect);
                case 1:
                    $this->error(lang('您的账号已注册过！'));
                case 2:
                    $this->error(lang('账号格式错误！'));
                default :
                    $this->error(lang('未受理的请求！'));
            }

        } else {
            $this->error(lang('illegal request'));
        }

    }
}
