<?php
// +----------------------------------------------------------------------
// | 贝云cms内容管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.yershop.com All rights reserved.
// +----------------------------------------------------------------------
namespace app\index\controller;
use think\Controller;
use think\Db;

/**
 * 用户控制器
 * 包括用户中心，用户登录及注册
 */
class Mobile extends Home {


	//手机验证码登陆
    public function login($mobile = "",  $code =""){
	
		   if(!api("USER_ALLOW_REGISTER")){
                $this->error("注册已关闭");
             }
			 $mobile=isset($_POST["mobile"])?safe_replace($_POST["mobile"]):"";//过滤
             if(empty($mobile)){
		        $this->error('手机号码不能为空');
	         } 
             if(!preg_match("/1[34578]{1}\d{9}$/",$mobile)){
			  	$this->error("手机号格式不正确！");
			 }	
			
			$send_code=isset($_POST["mobile_code"])?safe_replace($_POST["mobile_code"]):"";//过滤
			/* 检测验证码 */
			if(!$send_code){
				$this->error("验证码不能为空！");
			} 
            if(($mobile!==$_SESSION['mobile'])&&($send_code!==$_SESSION['mobile_code'])){
				$this->error("验证码输入错误！");
			}   
			
			$map["mobile"]=$mobile;
			$map['cotent']=$send_code;
			if(!Db::name("sms")->where($data)->find()){
			       $this->error("手机号或验证码不存在！");              				 
			}
			else{
                 $data["update_time"]=time();	
				 $data["status"]=1;	
                 db('sms')->where($map)->update($data);
			}
			unset($map);
			unset($data);
            //if($send_code!=$decrypt['smscode']){
		     	//error("验证码输入错误！");
			//}
		    
		    $data["mobile"]=$mobile;
		    $user=array();$id=0;
			if(Db::name("User")->where($data)->find()){
				   $update['last_login_time']=time();
				   $update['last_login_ip']=request()->ip();
                   Db::name("User")->where($data)->field('last_login_ip,last_login_time')->update($update);
				   
			      $user=Db::name("User")->where($data)->find();
				  if($user&&$user['id']){
					  $id=$user['id']; 
				  }
                 
			 }	
			else{
			  //注册账号
              $password =safe_replace("123457");//过滤
			  $data["username"]=rand_str();
              $data['password']=encode($password);
			  $data["status"]=1;
         	  $data["reg_time"]=time();
         	  $data["reg_ip"]= request()->ip();
         	  $data['cover_id']=1;
         	  $data["pid"]=input('pid')?input('pid'):0;
              $id=db("user")->insertGetId($data);            
			}
             if($id){
				$user=new \app\common\model\User; 
				$res=$user->send($id); 
				if($res){
					  $url= session("url")?session("url"):url('index/index');
					  $this->success("登录成功",$url);
					
				}else{
					  $url= session("url")?session("url"):url('index/index');
					  $this->error("登录失败",$url);
				}
			  
		  }else{
		       $url= session("url")?session("url"):url('index/index');
			   $this->error("登录失败",$url);
		  }  
     

	
	}
 
     //发送获取校验码


	
	
 public   function Post($curlPost){
	    $url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_NOBODY, true);
		curl_setopt($curl, CURLOPT_POST, true);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
		$return_str = curl_exec($curl);
		curl_close($curl);
		return $return_str;
}

 public function xml_to_array($xml){
		$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
		if(preg_match_all($reg, $xml, $matches)){
			$count = count($matches[0]);
			for($i = 0; $i < $count; $i++){
			$subxml= $matches[2][$i];
			$key = $matches[1][$i];
				if(preg_match( $reg, $subxml )){
					$arr[$key] = $this->xml_to_array( $subxml );
				}else{
					$arr[$key] = $subxml;
				}
			}
		}
		return $arr;
   }
public  function random($length = 6 , $numeric = 0) {
	PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
	if($numeric) {
		$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
	} else {
		$hash = '';
		$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
		$max = strlen($chars) - 1;
		for($i = 0; $i < $length; $i++) {
			$hash .= $chars[mt_rand(0, $max)];
		}
	}
	return $hash;
}

  
 
}
