Send.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\hander\HelpHander;
  4. use think\Controller;
  5. use think\Db;
  6. use think\Exception;
  7. use tools\Qxsms;
  8. class Send extends Controller
  9. {
  10. public function sms(){
  11. $mobile = input('mobile','','trim');
  12. if(!$mobile){
  13. HelpHander::error('请输入手机号');
  14. }
  15. if(!check_mobile($mobile)){
  16. HelpHander::error('手机号格式不正确');
  17. }
  18. $user = Db::name('user')->where('phone',$mobile)->find();
  19. if(!$user){
  20. HelpHander::error('该手机号未注册');
  21. }
  22. $max = config('app.max_send_sms')?config('app.max_send_sms'):0;
  23. $today = strtotime(date('Y-m-d'));
  24. $curTime = time();
  25. $nums = Db::name('sms_record')->where('phone',$mobile)->where('add_time','>=',$today)->count();
  26. if($nums > $max || $max == 0){
  27. HelpHander::error('短信发送次数已到限制条数');
  28. }
  29. // 60s内不能重复发送
  30. $eTime = $curTime - 60;
  31. $info = Db::name('sms_record')
  32. ->where('phone',$mobile)
  33. ->where('add_time','>=',$eTime)
  34. ->find();
  35. if($info){
  36. HelpHander::error('60秒内只能发送一次短信');
  37. }
  38. Db::startTrans();
  39. try{
  40. $code = model('SmsRecord')->add($mobile);
  41. if(!$code){
  42. \exception('短信发送失败');
  43. }
  44. $qxsms = new Qxsms(config('app.sms_config'));
  45. $content = "【基建中心】您的手机验证码为:".$code.",有限期10分钟。";
  46. $res = $qxsms->send($mobile,$content);
  47. if(!$res){
  48. \exception('短信发送失败');
  49. }
  50. Db::commit();
  51. }catch (Exception $e){
  52. Db::rollback();
  53. HelpHander::error('短信发送失败');
  54. }
  55. HelpHander::success([],'短信发送成功');
  56. }
  57. }