<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>;
// +----------------------------------------------------------------------
namespace Home\Controller;
use OT\DataDictionary;
use Think\Ucpaas; //验证码接口文件
use User\Api\UserApi;
/**
* 前台首页控制器
* 主要获取首页聚合数据
*/
class ApiZhuceController extends HomeController {
//发送验证码
public function fsyzm(){
//判断手机号是否已经注册
$datatel['username']= I('tel');
$dbtel = M('ucenter_member');
$requlttel = $dbtel->where($datatel)->select();
if($requlttel){
$data['zt']='no';
$data['tishi']='该手机号已注册!';
echo I('huidiao')."(".json_encode($data).")";die;
}
$db = M('telyz');
$options['accountsid']='XXXXXXXXXXXXXXXXXXXXXXX';
$options['token']='XXXXXXXXXXXXXXXXXXXXXXXXX';
$ucpass = new Ucpaas($options);
$to = I('tel');
$appId = "XXXXXXXXXXXXXXXXXXXXX";
$templateId = "1434";
$yzm = rand(1000,9999);
$param="test," . $yzm . ",3";
$resulta = $db->where(array("tel"=>$to))->select();
$stime = strtotime(date('Y-m-d')); //取当天凌晨时间濯
$xianzai = time();
$fasongdate = intval($resulta[0]['fasongdate']);
$tiaoshu = intval($resulta[0]['tiaoshu']);
//echo "111---" . $stime ."---222---" . $xianzai ."---333---".$fasongdate.">>";
$zhuangtai = 0;
//判断数据库有没有记录
if($resulta){
$fasongdate = intval($resulta[0]['fasongdate']);
$fasongdate6 = $fasongdate + 60;
$xianzai = time();
$tiaoshu = $resulta[0]['tiaoshu'];
if($fasongdate < $stime){
$zhuangtai = 102;//状态102=今天没有发送验证码。直接发送验证码,并修改数据库记录,条数为1
}elseif($tiaoshu >= 10){
$zhuangtai = 301;//状态301=今天发送条数大于10条,返回JSON no+提示发送条数大于10
}elseif($xianzai < $fasongdate6 ){
$zhuangtai = 302;//状态302=距离上次发送验证码时间小于1分钟。返回JSON no+提示距离上次发送时间小于1分钟。
}else{
$zhuangtai = 103;//状态103=满足条件,直接发送验证码,并修改数据库记录,条数+1
}
}else{
$zhuangtai = 101;//状态101=未找到数据库记录。直接发送验证码,并添加数据库记录,条数为1
}
switch ($zhuangtai)
{
case 101:
//echo "101";
$reqult = $ucpass->templateSMS($appId,$to,$templateId,$param);
$arr= json_decode($reqult,true);
$res = $arr['resp']['respCode'];
if ($res=="000000"){
//发送成功返回JSON
$data['zt']='yes';
echo I('huidiao')."(".json_encode($data).")";
//发送成功后,将验证码、手机号、发送时间、状态添加到数据库。
$datadb['yzm'] = $yzm;
$datadb['tel'] = $to;
$datadb['fasongdate'] = time();
$datadb['zt'] = "1";
$datadb['tiaoshu'] = "1";
$db = $db->data($datadb)->add();die;
}else{
//发送错误返回JSON
$data['zt']='no';
$data['tishi']='错误101';
echo I('huidiao')."(".json_encode($data).")";die;
}
break;
case 102:
//echo "102";
//状态102=今天没有发送验证码。直接发送验证码,并修改数据库记录,条数为1
$reqult = $ucpass->templateSMS($appId,$to,$templateId,$param);
$arr= json_decode($reqult,true);
$res = $arr['resp']['respCode'];
if ($res=="000000"){
//发送成功返回JSON
$data['zt']='yes';
echo I('huidiao')."(".json_encode($data).")";
//发送成功后,将验证码、手机号、发送时间、状态添加到数据库。
$datadb['yzm'] = $yzm;
$datadb['tel'] = $to;
$datadb['fasongdate'] = time();
$datadb['zt'] = "1";
$datadb['tiaoshu'] = "1";
$db->where(array("tel"=>$to))->save($datadb);die;
}else{
//发送错误返回JSON
$data['zt']='no';
$data['tishi']='错误102';
echo I('huidiao')."(".json_encode($data).")";die;
}
break;
case 103:
//echo "103";
//状态103=满足条件,直接发送验证码,并修改数据库记录,条数+1
$reqult = $ucpass->templateSMS($appId,$to,$templateId,$param);
$arr= json_decode($reqult,true);
$res = $arr['resp']['respCode'];
if ($res=="000000"){
//发送成功返回JSON
$data['zt']='yes';
echo I('huidiao')."(".json_encode($data).")";
//发送成功后,将验证码、手机号、发送时间、状态添加到数据库。
$datadb['yzm'] = $yzm;
$datadb['tel'] = $to;
$datadb['fasongdate'] = time();
$datadb['zt'] = "1";
$datadb['tiaoshu'] = $tiaoshu + 1;
$db->where(array("tel"=>$to))->save($datadb);die;
}else{
//发送错误返回JSON
$data['zt']='no';
$data['tishi']='错误103';
echo I('huidiao')."(".json_encode($data).")";die;
}
break;
case 301:
//echo "301";
//状态301=今天发送条数大于10条,返回JSON no+提示发送条数大于10
$data['zt']='no';
$data['tishi']='今天已经发送了10条验证码';
echo I('huidiao')."(".json_encode($data).")";die;
break;
case 302:
//echo "302";
//状态302=距离上次发送验证码时间小于1分钟。返回JSON no+提示距离上次发送时间小于1分钟。
$data['zt']='no';
$data['tishi']='距离上次发送验证码时间小于1分钟';
echo I('huidiao')."(".json_encode($data).")";die;
break;
}
}
}