TelCodeController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * 返回短信接口
  4. */
  5. namespace api\modules\v1\controllers;
  6. use api\models\Common;
  7. use common\library\Sms;
  8. use yii\rest\ActiveController;
  9. use common\library\Apireturn;
  10. use yii;
  11. class TelCodeController extends ActiveController
  12. {
  13. public $modelClass = 'common\models';
  14. const TYPE_REGISTER =1;//注册
  15. const TYPE_BIND = 3 ;//用户绑定手机号
  16. const TYPE_FIND=2;//忘记密码
  17. const TYPE_LOGIN = 4; //登录
  18. const TIME_LIMIT = 60;//限制X秒发送一条
  19. const TIME_VALID = 300;//有效时间
  20. /**
  21. * @return array
  22. */
  23. public function actionGet()
  24. {
  25. $post = Yii::$app->request->post();
  26. if(empty($post['sign'])||strtolower($post['sign'])!=md5('building'.$post['tel']))
  27. {
  28. return Apireturn::sent(0,'验证未通过',200);
  29. }
  30. if(empty($post['tel'])||empty($post['type']))
  31. {
  32. return Apireturn::sent(0,'参数错误',200);
  33. }
  34. if(!Common::is_tel($post['tel']))
  35. {
  36. return Apireturn::sent(0,'请输入正确的手机号',200);
  37. }
  38. $username = $post['tel'];
  39. $type = $post['type'];
  40. switch ($type)
  41. {
  42. case self::TYPE_REGISTER://注册
  43. $count = \common\models\User::find()->where(array('username'=>$username))->count();
  44. if($count>0)
  45. {
  46. return Apireturn::sent(0,'手机号已注册',200);
  47. }
  48. $result = $this->isSent($username,$type);//判断能否发送
  49. if($result['code']==1){
  50. $result = $this->sent($username,$type);
  51. }
  52. break;
  53. case self::TYPE_BIND ://绑定手机号
  54. $count = \common\models\User::find()->where(array('tel'=>$username))->count();
  55. if($count>0)
  56. {
  57. return Apireturn::sent(0,'手机号已绑定',200);
  58. }
  59. $result = $this->isSent($username,$type);//判断能否发送
  60. if($result['code']==1){
  61. $result = $this->sent($username,$type);
  62. }
  63. break;
  64. default :
  65. break;
  66. }
  67. return $result;
  68. }
  69. //判断是否能发送
  70. public function isSent($username,$type)
  71. {
  72. $key = $username."_".$type;
  73. $cache = Yii::$app->cache->get($key);
  74. if(empty($cache))
  75. {
  76. return Apireturn::sent(1);
  77. }
  78. if(($cache['c_time']+self::TIME_LIMIT)>time())
  79. {
  80. return Apireturn::sent(0,'发送短信太频繁',200);
  81. }
  82. return Apireturn::sent(1);
  83. }
  84. //发送短信
  85. public function sent($username,$type)
  86. {
  87. $code = rand(1000,9999);
  88. $message = "【".Yii::$app->params['sitetitle']."】";
  89. switch ($type)
  90. {
  91. case self::TYPE_REGISTER://注册
  92. $message.="您的注册验证码为:".$code;
  93. break;
  94. case self::TYPE_FIND ://找回密码
  95. $message.="您的验证码为:".$code;
  96. break;
  97. case self::TYPE_BIND://绑定手机号
  98. $message.="您的验证码为:".$code;
  99. break;
  100. }
  101. $sms = new Sms();
  102. $result =$sms->SendMessage($username,$message);
  103. if($result)
  104. {
  105. $key = $username."_".$type;
  106. Yii::$app->cache->set($key,array('username'=>$username,'c_time'=>time(),'code'=>$code,'valid_time'=>time()+self::TIME_VALID),3600);
  107. return Apireturn::sent(1,'发送短信成功',200);
  108. }else{
  109. return Apireturn::sent(0,'发送短信失败',200);
  110. }
  111. }
  112. /**判断验证是否正确
  113. * @param $username 手机号
  114. * @param $type 类型
  115. * @param $code 验证码
  116. */
  117. static public function existCode($username,$type,$code)
  118. {
  119. $key = $username."_".$type;
  120. $cache = Yii::$app->cache->get($key);
  121. if(empty($cache))
  122. {
  123. return false;
  124. }
  125. if($cache['code']==$code)
  126. {
  127. return true;
  128. }
  129. return false;
  130. }
  131. static public function clearCode($username,$type)
  132. {
  133. $key = $username."_".$type;
  134. Yii::$app->cache->set($key,null);
  135. }
  136. }