<?php
// +----------------------------------------------------------------------
// | 奇乐自媒体内容管理系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com
// +----------------------------------------------------------------------
// | 官方论坛：http://bbs.qilecms.com
// +----------------------------------------------------------------------
// | Author:奇乐网络
// +----------------------------------------------------------------------
// | 版权说明：本产品为免费版，使用请保留底部版权
// +----------------------------------------------------------------------
namespace app\user\controller;
class Validate extends Base
{
   public function initialize()
    {
       parent::initialize();
       $this->isLogin();
    }
  public function email()
  {
  	$param = $this->request->param();

    if(!empty($param['token'])){
        $email_auth  =  session('email_auth'); //授权随机数
        $email      =  cookie('ql_email');
    	$token = md5(session('username').$email_auth.$email);
       if($param['token'] == $token){
       	$data['is_email'] =1;
       	$where['uid'] = session('uid');
        model('user')->editData($data,$where);
        $this->redirect('validate/email');
       }
      
    }
  	return $this->fetch();
  }
  public function sendEmail()
  {
  	 $param =$this->request->param();

       $rule =[    
          'email'=>'require|email',
        ];
        $msg= [
         'email.require' =>'邮箱不存在',
         'email.email' =>'邮箱格式错误',
  
        ];
         $error = $this->checkSubmit($param,$rule,$msg);  //调用common 公共控制器中的 checkSubmit 方法
       if($error){
              return error_json($error);
         }
        if(session('email_auth')){
          if(cookie('ql_email')){

            return error_json('请稍后60秒后重新发送','',-1);
          }
        }
     $email = trim($param['email']); //邮箱
     $email_auth = random_text(20); //授权随机数
     session('email_auth',$email_auth);
     cookie('ql_email',$email,60); 
	    $token = md5(session('username').$email_auth.$email);
	    $url = $this->request->root(true).'/user/validate/email?token='.$token;
	    $subject = $this->settings['site']['sitename'].'激活邮件';
	    $body = '<table border="0" cellpadding="0" cellspacing="0" style="width: 600px; border: 1px solid #ddd; border-radius: 3px; color: #555;; font-size: 12px; height: auto; margin: auto; overflow: hidden; text-align: left; word-break: break-all; word-wrap: break-word;"> <tbody style="margin: 0; padding: 0;"> <tr style="background-color: #393D49; height: 60px; margin: 0; padding: 0;"> <td style="margin: 0; padding: 0;"> <div style="color: #5EB576; margin: 0; margin-left: 30px; padding: 0;"><a style="font-size: 14px; margin: 0; padding: 0; color: #5EB576; text-decoration: none;" href="http://fly.layui.com/" target="_blank" rel="noopener">'.$this->settings['site']['sitename'].'</a></div> </td> </tr> <tr style="margin: 0; padding: 0;"> <td style="margin: 0; padding: 30px;"> <p style="line-height: 20px; margin: 0; margin-bottom: 10px; padding: 0;"> Hi，<em style="font-weight: 700;">'.session('username').'</em>，请完成以下操作： </p> <div style=""> <a href="'.$url.'" style="background-color: #009E94; color: #fff; display: inline-block; height: 32px; line-height: 32px; margin: 0 15px 0 0; padding: 0 15px; text-decoration: none;" target="_blank" rel="noopener">立即激活邮箱</a> </div> <p style="line-height: 20px; margin-top: 20px; padding: 10px; background-color: #f2f2f2; font-size: 12px;"> 如果该邮件不是由你本人操作，请勿进行激活！否则你的邮箱将会被他人绑定。 </p> </td> </tr> <tr style="background-color: #fafafa; color: #999; height: 35px; margin: 0; padding: 0; text-align: center;"> <td style="margin: 0; padding: 0;">系统邮件，请勿直接回复。</td> </tr> </tbody> </table>';
     send_email($email,$subject,$body);
  	 return success_json('验证邮件已经发送成功');
  }

  public function mobile()
  {
    if($this->request->isAjax()){
      $param = $this->request->param();
      $smscode = $param['smscode'];
      if(empty($smscode)){
        return error_json('短信验证码不能为空');
      }
      if($smscode != session('smscode')){
        return error_json('短信验证码错误');
      }
         $data['is_mobile'] = 1;
         $where['uid'] = $this->user->uid;
         model('user')->editData($data,$where);
         return success_json('验证成功');
    }


    return $this->fetch();
  }
//发送短信
 public function sendSms()
 {
      $param = $this->request->param();
         if($this->request->isAjax()){
             $mobile = $param['mobile'];
               $rule =[
              'mobile'   =>'require|mobile',
            ];
            $msg= [
             'mobile.require'  =>'请输入手机号',
             'mobile.mobile'   =>'请输入正确的手机号',
            ];
             $error = $this->checkSubmit($param,$rule,$msg);  
            if($error){
                return error_json($error);
             }          
      //判断手机号是否存在
             $where['mobile']  = $param['mobile'];
             $where['is_mobile']  = 1;
             $user = model('user')->infoData($where);
             if(!empty($user)){
               return error_json('该手机号已经存在，请更换其他手机号');
             }

           session('smscode', null);
           $rand =  rand(100000,900000);  //随机数
           $params =['sendcode'=>$rand];  //六位数
             $res = send_sms($mobile,'sms_yzm',$params,"aliyun");
            if($res =='success'){
               session('smscode',$rand);
              
               return success_json('验证码发送成功');
            }else{
              //返回错误代码
               return error_json($res);
            }
       }
 }

}