<?php
		Session_start(); 
		if(!$_SESSION["check"] || $_SESSION["check"]!=intval(Request::get('check'))){
			$arr['msg']='error';
			Response::json($arr);
		}
		$domain = $_SERVER['HTTP_HOST'];
		$agency = new Model('user_agency');	
		$agency->find(array('domain'=>$domain));
		if($agency->message_td==2){//阿里云
			require_once DOXCX_FRAME_PATH . "/fun/aliyun-dysms/sendSms.php";
			$alsms = new Model('user_agency_sms');
			$alsms->find(["uid"=>$agency->id]);
			$accessKeyId = $alsms->accessKeyId;
			$accessKeySecret = $alsms->accessKeySecret;
			$PhoneNumbers = Request::get('tel');
			$SignName = $alsms->signName;
			$tpls = json_decode($alsms->templateCodes,true);
			$TemplateCode = $tpls['template_register_code'];
			$code = rand(100000,999999);
			$TemplateParam = ["code" =>$code];	
			$resp = sendAlSms($accessKeyId,$accessKeySecret,$PhoneNumbers,$SignName,$TemplateCode,$TemplateParam);
			$smsRecord = new Model('agency_sms_send_record');
			if($resp->Code=='OK'){
				//插入发送短信记录表
				$smsRecord->uid = $agency->id;
				$smsRecord->content = "尊敬的客户您好，您的短信验证码是:".$code;
				$smsRecord->tel = $PhoneNumbers;
				$smsRecord->ctime = time();
				$smsRecord->status = 1;
				$smsRecord->return_code = 'OK';
				$smsRecord->save();
				Cache::set('sms_code', $code, 300);
				$arr['msg'] = 'ok';
				Response::json($arr);
			}else{
				//插入发送短信记录表
				$smsRecord->uid = $agency->id;
				$smsRecord->content = "尊敬的客户您好，您的短信验证码是:".$code;
				$smsRecord->tel = $PhoneNumbers;
				$smsRecord->ctime = time();
				$smsRecord->status = 2;
				$smsRecord->return_code = $resp->Code;
				$smsRecord->save();
				$arr['msg'] = $resp->Message;
				Response::json($arr);
			}
		}else{
			$message_sign=$agency->message_sign;
			$message_id=$agency->message_id;
			$message_pwd=$agency->message_pwd;
		
			$code=rand(100000,999999);
			$content = "尊敬的客户您好，您的短信验证码是:".$code."【".$message_sign."】";   
			$url="http://service.winic.org:8009/sys_port/gateway/index.asp?";
			$data = "id=%s&pwd=%s&to=%s&Content=%s&time=";
			$id = urlencode(iconv("utf-8","gb2312","$message_id"));
			$pwd =$message_pwd;
			$to =Request::get('tel'); 
			$contents = urlencode(iconv("UTF-8","GB2312",$content));
			$rdata = sprintf($data, $id, $pwd, $to, $contents);
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_POST,1);
			curl_setopt($ch, CURLOPT_POSTFIELDS,$rdata);
			curl_setopt($ch, CURLOPT_URL,$url);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			curl_setopt($ch, CURLOPT_TIMEOUT,2);
			$result = curl_exec($ch);
			curl_close($ch);
			// $return=substr($result,0,3);
			$returnArray = explode("/", $result);
			$return_code=$returnArray[0];
			$smsRecord=new Model('agency_sms_send_record');
			if($return_code=='000'){
				//插入发送短信记录表
				$smsRecord->uid=$agency->id;
				$smsRecord->content=$content;
				$smsRecord->tel=$to;
				$smsRecord->ctime=time();
				$smsRecord->status=1;
				$smsRecord->return_code=$return_code;
				$smsRecord->save();
				Cache::set('sms_code', $code, 300);
				$arr['msg']='ok';
				Response::json($arr);
			}else{
				//插入发送短信记录表
				$smsRecord->uid=$agency->id;
				$smsRecord->content=$content;
				$smsRecord->tel=$to;
				$smsRecord->ctime=time();
				$smsRecord->status=2;
				$smsRecord->return_code=$return_code;
				$smsRecord->save();
				$arr['msg']=$return_code;
				Response::json($arr);
			}
		}
