CodeController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /*
  3. * 验证码
  4. */
  5. namespace api\controllers;
  6. use app\common\components\Emailer;
  7. use app\common\components\Sms;
  8. use app\models\EmailCert;
  9. use app\models\MobileCert;
  10. use app\modules\ucenter\models\User;
  11. use app\common\controllers\AController;
  12. use Yii;
  13. class CodeController extends AController
  14. {
  15. public function init()
  16. {
  17. parent::init();
  18. }
  19. //发送验证码
  20. public function actionSendverifycode()
  21. {
  22. $patternEmail = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/';
  23. $patternMobile = '/^1[3456789]{1}\d{9}$/';
  24. $to = $this->get['to'];
  25. $action = $this->get['action'];
  26. $captcha = $this->get['captcha'];
  27. if(preg_match($patternEmail,$to)) $toType = 'email';
  28. if(preg_match($patternMobile,$to)) $toType='mobile';
  29. if($to&&$action)
  30. {
  31. if(empty($toType))
  32. {
  33. $msgdata = ['error' => 1,'msg' => '账号类型错误','data'=>array(),'code'=>'200'];
  34. return $msgdata;
  35. }
  36. if($this->smsconfig['open_imgvalid']&&!empty($captcha))
  37. {
  38. if(!$this->validateCaptcha($captcha))
  39. {
  40. $msgdata = ['error' => 1,'msg' => '校验失败','data'=>array(),'code'=>'200'];
  41. return $msgdata;
  42. }
  43. }
  44. //如果是找回密码,判断用户是否存在
  45. if($action=='forgetpwd')
  46. {
  47. $user = User::find()->where("mobile='".$to."' or email='".$to."'")->one();
  48. if(empty($user))
  49. {
  50. $msgdata = ['error' => 1,'msg' => '用户不存在','data'=>array(),'code'=>'200'];
  51. return $msgdata;
  52. }
  53. }
  54. if($toType=='email')
  55. {
  56. $exist = EmailCert::find()->where("email='".$to."' and request_from = ".REQUEST_FROM)->orderBy(['id'=>SORT_DESC])->one();
  57. }
  58. else if($toType=='mobile')
  59. {
  60. $exist = MobileCert::find()->where("mobile='".$to."' and request_from = ".REQUEST_FROM)->orderBy(['id'=>SORT_DESC])->one();
  61. }
  62. if($exist&&TIMESTAMP-$exist->sent_time<$this->smsconfig['certTimeOut'])
  63. {
  64. $msgdata = ['error' => 1,'msg' => '发送失败(两次发送时间间隔太短)','data'=>array(),'code'=>'200'];
  65. }
  66. else
  67. {
  68. $code = rand(100000,999999);
  69. if($toType=='email')
  70. {
  71. $mailer = new Emailer();
  72. $result = $mailer->send($action,$to,array('code'=>$code));
  73. }
  74. else if($toType=='mobile')
  75. {
  76. $sms = new Sms();
  77. $sms->init();
  78. $result = $sms->send($action,$to,array('code'=>$code));
  79. }
  80. if($result)
  81. {
  82. $msgdata = ['error' => 0,'msg' => '发送成功','data'=>var_export($result,true),'code'=>'200'];
  83. }
  84. else
  85. {
  86. $msgdata = ['error' => 1,'msg' =>'发送失败','data'=>array(),'code'=>'200'];
  87. }
  88. }
  89. }
  90. else
  91. {
  92. $msgdata = ['error' => 1,'msg' => '系统错误','data'=>array(),'code'=>'200'];
  93. }
  94. return $msgdata;
  95. }
  96. //校验验证码
  97. public function actionCheckverifycode()
  98. {
  99. extract($this->post);
  100. if($user_name&&$code)
  101. {
  102. $result = $this->checkCode($user_name,$code);
  103. if(!empty($result))
  104. {
  105. return $result;
  106. }
  107. $msgdata = ['error' => 0,'msg' => '校验成功','data'=>array('hash'=>sys_auth($user_name)),'code'=>'200'];
  108. }
  109. else
  110. {
  111. $msgdata = ['error' => 1,'msg' => '系统错误','data'=>[],'code'=>'200'];
  112. }
  113. return $msgdata;
  114. }
  115. }