request->post(); if(empty($post['sign'])||strtolower($post['sign'])!=md5('building'.$post['tel'])) { return Apireturn::sent(0,'验证未通过',200); } if(empty($post['tel'])||empty($post['type'])) { return Apireturn::sent(0,'参数错误',200); } if(!Common::is_tel($post['tel'])) { return Apireturn::sent(0,'请输入正确的手机号',200); } $username = $post['tel']; $type = $post['type']; switch ($type) { case self::TYPE_REGISTER://注册 $count = \common\models\User::find()->where(array('username'=>$username))->count(); if($count>0) { return Apireturn::sent(0,'手机号已注册',200); } $result = $this->isSent($username,$type);//判断能否发送 if($result['code']==1){ $result = $this->sent($username,$type); } break; case self::TYPE_BIND ://绑定手机号 $count = \common\models\User::find()->where(array('tel'=>$username))->count(); if($count>0) { return Apireturn::sent(0,'手机号已绑定',200); } $result = $this->isSent($username,$type);//判断能否发送 if($result['code']==1){ $result = $this->sent($username,$type); } break; default : break; } return $result; } //判断是否能发送 public function isSent($username,$type) { $key = $username."_".$type; $cache = Yii::$app->cache->get($key); if(empty($cache)) { return Apireturn::sent(1); } if(($cache['c_time']+self::TIME_LIMIT)>time()) { return Apireturn::sent(0,'发送短信太频繁',200); } return Apireturn::sent(1); } //发送短信 public function sent($username,$type) { $code = rand(1000,9999); $message = "【".Yii::$app->params['sitetitle']."】"; switch ($type) { case self::TYPE_REGISTER://注册 $message.="您的注册验证码为:".$code; break; case self::TYPE_FIND ://找回密码 $message.="您的验证码为:".$code; break; case self::TYPE_BIND://绑定手机号 $message.="您的验证码为:".$code; break; } $sms = new Sms(); $result =$sms->SendMessage($username,$message); if($result) { $key = $username."_".$type; Yii::$app->cache->set($key,array('username'=>$username,'c_time'=>time(),'code'=>$code,'valid_time'=>time()+self::TIME_VALID),3600); return Apireturn::sent(1,'发送短信成功',200); }else{ return Apireturn::sent(0,'发送短信失败',200); } } /**判断验证是否正确 * @param $username 手机号 * @param $type 类型 * @param $code 验证码 */ static public function existCode($username,$type,$code) { $key = $username."_".$type; $cache = Yii::$app->cache->get($key); if(empty($cache)) { return false; } if($cache['code']==$code) { return true; } return false; } static public function clearCode($username,$type) { $key = $username."_".$type; Yii::$app->cache->set($key,null); } }