<?php

namespace app\user\controller;

use think\Controller;
use think\Lang;
use think\Validate;

class Register extends Controller
{

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/login.lang.php');
    }

    /**
     * 验证密码
     * @return mixed
     */
    function checkPassword($password)
    {
        if (preg_match('/(?=^.{6,20}$)(?=.*\d)(?=.*[a-z])(?=.*[!@#$%^&*]).*$/', $password)) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 注册
     * @return mixed
     */
    public function index()
    {
        if (request()->isPost()) {

            $user_email = input('post.user_email');
            $user_password = input('post.user_password');
            $confirm_password = input('post.confirm_password');
            $user_phone = input('post.user_phone');
            $captcha = input('post.captcha');
            $ch_box = input('post.ch_box');

            // 协议 校验
            if ($ch_box == false) {
                //验证失败
                $this->error('未勾选协议');
            }

            //验证密码
            if ($user_password != $confirm_password) {
                //验证失败
                $this->error('密码不一致');
            }

            $data = array(
                'user_email' => $user_email,
                'user_password' => $user_password,
                'user_phone' => $user_phone,
                'captcha' => $captcha,
            );

            //验证数据  BEGIN
            $rule = [
                ['user_email', 'require|min:2', '帐号为必填|帐号长度至少为2位'],
                ['user_password', 'require|min:2', '密码为必填|帐号长度至少为6位'],
                ['captcha', 'require|min:3', '验证码为必填|帐号长度至少为3位'],
            ];
            $validate = new Validate($rule);
            $validate_result = $validate->check($data);

            if (!$validate_result) {
                $this->error($validate->getError());
            }
            //验证数据  END
            if (!captcha_check(input('post.captcha'))) {
                //验证失败
                $this->error('验证码错误');
            }

            $condition['user_email'] = $user_email;

            $user_info = db('user')->where($condition)->find();
            if (is_array($user_info) and !empty($user_info)) {
                $this->success('账户已存在');
            } else {
                $user_info = array(
                    'user_email' => $user_email,
                    'user_password' => md5($user_password),
                    'user_phone' => $user_phone,
                    'user_addTime' => TIMESTAMP,
                    'user_loginTime' => TIMESTAMP
                );
                $id = db('user')->insertGetId($user_info);

                //设置 session
                session('user_id', $id);
                session('user_email', $user_info['user_email']);

                return $this->redirect('User/Index/index');
            }
        } else {
            return $this->fetch();
        }
    }


}


?>
