123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- /*
- * 验证码
- */
- namespace api\controllers;
- use app\common\components\Emailer;
- use app\common\components\Sms;
- use app\models\EmailCert;
- use app\models\MobileCert;
- use app\modules\ucenter\models\User;
- use app\common\controllers\AController;
- use Yii;
- class CodeController extends AController
- {
- public function init()
- {
- parent::init();
- }
- //发送验证码
- public function actionSendverifycode()
- {
- $patternEmail = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/';
- $patternMobile = '/^1[3456789]{1}\d{9}$/';
- $to = $this->get['to'];
- $action = $this->get['action'];
- $captcha = $this->get['captcha'];
- if(preg_match($patternEmail,$to)) $toType = 'email';
- if(preg_match($patternMobile,$to)) $toType='mobile';
- if($to&&$action)
- {
- if(empty($toType))
- {
- $msgdata = ['error' => 1,'msg' => '账号类型错误','data'=>array(),'code'=>'200'];
- return $msgdata;
- }
- if($this->smsconfig['open_imgvalid']&&!empty($captcha))
- {
- if(!$this->validateCaptcha($captcha))
- {
- $msgdata = ['error' => 1,'msg' => '校验失败','data'=>array(),'code'=>'200'];
- return $msgdata;
- }
- }
- //如果是找回密码,判断用户是否存在
- if($action=='forgetpwd')
- {
- $user = User::find()->where("mobile='".$to."' or email='".$to."'")->one();
- if(empty($user))
- {
- $msgdata = ['error' => 1,'msg' => '用户不存在','data'=>array(),'code'=>'200'];
- return $msgdata;
- }
- }
- if($toType=='email')
- {
- $exist = EmailCert::find()->where("email='".$to."' and request_from = ".REQUEST_FROM)->orderBy(['id'=>SORT_DESC])->one();
- }
- else if($toType=='mobile')
- {
- $exist = MobileCert::find()->where("mobile='".$to."' and request_from = ".REQUEST_FROM)->orderBy(['id'=>SORT_DESC])->one();
- }
- if($exist&&TIMESTAMP-$exist->sent_time<$this->smsconfig['certTimeOut'])
- {
- $msgdata = ['error' => 1,'msg' => '发送失败(两次发送时间间隔太短)','data'=>array(),'code'=>'200'];
- }
- else
- {
- $code = rand(100000,999999);
- if($toType=='email')
- {
- $mailer = new Emailer();
- $result = $mailer->send($action,$to,array('code'=>$code));
- }
- else if($toType=='mobile')
- {
- $sms = new Sms();
- $sms->init();
- $result = $sms->send($action,$to,array('code'=>$code));
- }
- if($result)
- {
- $msgdata = ['error' => 0,'msg' => '发送成功','data'=>var_export($result,true),'code'=>'200'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' =>'发送失败','data'=>array(),'code'=>'200'];
- }
- }
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '系统错误','data'=>array(),'code'=>'200'];
- }
- return $msgdata;
- }
- //校验验证码
- public function actionCheckverifycode()
- {
- extract($this->post);
- if($user_name&&$code)
- {
- $result = $this->checkCode($user_name,$code);
- if(!empty($result))
- {
- return $result;
- }
- $msgdata = ['error' => 0,'msg' => '校验成功','data'=>array('hash'=>sys_auth($user_name)),'code'=>'200'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '系统错误','data'=>[],'code'=>'200'];
- }
- return $msgdata;
- }
- }
|