<?php
// +----------------------------------------------------------------------
// | 奇乐自媒体内容管理系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com
// +----------------------------------------------------------------------
// | 官方论坛：http://bbs.qilecms.com
// +----------------------------------------------------------------------
// | Author:奇乐网络
// +----------------------------------------------------------------------
// | 版权说明：本产品为免费版，使用请保留底部版权
// +----------------------------------------------------------------------
namespace app\user\controller;
//用户注册
class Register extends Base{

    public function index() {

     // 注册页面
       if($this->request->isAjax()){
           $param = $this->request->param();
          
         if($param['type']  == 'account'){
             return $this->accountRegister($param);
          
          }else{
              return error_json('抱歉，请求方式错误');
          }

       }else{
       	 return $this->fetch();
       }
       
    }
    protected function accountRegister($param)
{
    //用户名
           $rule =[
              'username'   =>'require|min:3|max:15',
              'password'   =>'require|min:6',
              'verify_code'=>'require',
            ];
            $msg= [
             'username.require' =>'请输入用户名',
             'username.min'     =>'用户名至少3个字符',
             'username.max'     =>'用户名最多15个字符',
             'password.require' =>'请输入登陆密码',
             'password.min'      => '密码至少6位数',
             'verify_code.require' =>'请输入验证码',
            
            ];
             $error = $this->checkSubmit($param,$rule,$msg);  
            if($error){
                return error_json($error);
             } 


              
           //判断验证码是否正确
            if(!captcha_check($param['verify_code'])){
            return error_json('验证码错误');
            };
           //检测用户是否为纯数字
           if(preg_match("/^(?!(\d+)$)[a-zA-Z0-9_\x{4e00}-\x{9fa5}]{3,15}$/u",$param['username'])){
                   
       //匹配用户名
                      $where['username']  = $param['username'];
                      $data['username']   = $param['username'];

            }else{
                     return error_json('用户名仅支持中英文、数字和下划线');
            }
          
            //过滤非法用户名
            $keywords_ban = $this->settings['site']['register_keywords_ban'];
            if(!empty($keywords_ban)){
               $keywords_ban_data = explode('|',$keywords_ban); 
               if(is_array($keywords_ban_data)){
                  if(in_array($param['username'],$keywords_ban_data )){
                     return error_json('当前用户名禁止注册，请更换其他用户名');
                   }
               }     
          
            }
  


            $count = model('user')->where($where)->count();
            if(!empty($count)){
                return error_json('抱歉，该用户名已经注册，请更换其他账号');
             } 
            
     

                      
             $data['password']  = $param['password'];
             $passsalt          = random_text(8);
   
             $data['nickname']  = $data['username']; //昵称
             $data['password']  = password_key($data['username'],$param['password'],$passsalt);
             $data['passsalt']  = $passsalt;
             $paysalt           = random_text(8);
             $data['payword']   = payword_key($data['username'],$param['password'],$paysalt);
             $data['paysalt']   = $paysalt;
             $data['login_count'] = 1;
             $data['login_time']  = time();
             $data['login_ip']    = get_client_ip();

             $data['reg_time']    = time();
             $data['reg_ip']      = get_client_ip();

             $user = model('user')->addData($data);
             session('uid',$user->id);
             session('username',$user->username);

             return success_json('恭喜您，注册的成功');

}




}