123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- /**
- * 返回短信接口
- */
- namespace api\modules\v1\controllers;
- use api\models\Common;
- use common\library\Sms;
- use yii\rest\ActiveController;
- use common\library\Apireturn;
- use yii;
- class TelCodeController extends ActiveController
- {
- public $modelClass = 'common\models';
- const TYPE_REGISTER =1;//注册
- const TYPE_BIND = 3 ;//用户绑定手机号
- const TYPE_FIND=2;//忘记密码
- const TYPE_LOGIN = 4; //登录
- const TIME_LIMIT = 60;//限制X秒发送一条
- const TIME_VALID = 300;//有效时间
- /**
- * @return array
- */
- public function actionGet()
- {
- $post = Yii::$app->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);
- }
- }
|