Sms.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Sms as Smslib;
  5. use app\common\model\User;
  6. use think\Hook;
  7. /**
  8. * 手机短信接口
  9. */
  10. class Sms extends Api
  11. {
  12. protected $noNeedLogin = '*';
  13. protected $noNeedRight = '*';
  14. /**
  15. * 发送验证码
  16. *
  17. * @ApiMethod (POST)
  18. * @param string $mobile 手机号
  19. * @param string $event 事件名称
  20. */
  21. public function send()
  22. {
  23. $mobile = $this->request->post("mobile",'','trim,intval');
  24. $event = $this->request->post("event");
  25. $event = 'default';
  26. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  27. $this->error(__('手机号不正确'));
  28. }
  29. if(!$this->apiLimit(60,1,'60_'.$mobile)){
  30. $this->error('您的手机号发送频繁,请一分钟后再试');
  31. };
  32. if(!$this->apiLimit(3600,10,'3600_'.$mobile)){
  33. $this->error('您的手机号发送频繁,请一小时后再试!');
  34. };
  35. if(!$this->apiLimit(60,10,request()->ip())){
  36. $this->error('当前发送人数过多,请稍后再试');
  37. };
  38. $last = Smslib::get($mobile, $event);
  39. if ($last && time() - $last['createtime'] < 60) {
  40. $this->error(__('发送频繁,请稍后再试'));
  41. }
  42. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  43. if ($ipSendTotal >= 5) {
  44. $this->error(__('发送频繁,请稍后再试'));
  45. }
  46. $userinfo = User::getByMobile($mobile);
  47. if (!$userinfo) {
  48. $this->error('您的手机号未注册');
  49. }
  50. if ($event) {
  51. $userinfo = User::getByMobile($mobile);
  52. if ($event == 'register' && $userinfo) {
  53. //已被注册
  54. $this->error(__('已被注册'));
  55. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  56. //被占用
  57. $this->error(__('已被占用'));
  58. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  59. //未注册
  60. $this->error(__('未注册'));
  61. }
  62. }
  63. /*if (!Hook::get('sms_send')) {
  64. $this->error(__('请在后台插件管理安装短信验证插件'));
  65. }*/
  66. $ret = Smslib::send($mobile, null, $event);
  67. if ($ret) {
  68. $this->success(__('发送成功'));
  69. } else {
  70. $this->error(__('糟糕,发送失败了'));
  71. }
  72. }
  73. /**
  74. * 检测验证码
  75. *
  76. * @ApiMethod (POST)
  77. * @param string $mobile 手机号
  78. * @param string $event 事件名称
  79. * @param string $captcha 验证码
  80. */
  81. public function check()
  82. {
  83. exit;
  84. $mobile = $this->request->post("mobile");
  85. $event = $this->request->post("event");
  86. $event = $event ? $event : 'register';
  87. $captcha = $this->request->post("captcha");
  88. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  89. $this->error(__('手机号不正确'));
  90. }
  91. if ($event) {
  92. $userinfo = User::getByMobile($mobile);
  93. if ($event == 'register' && $userinfo) {
  94. //已被注册
  95. $this->error(__('已被注册'));
  96. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  97. //被占用
  98. $this->error(__('已被占用'));
  99. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  100. //未注册
  101. $this->error(__('未注册'));
  102. }
  103. }
  104. $ret = Smslib::check($mobile, $captcha, $event);
  105. if ($ret) {
  106. $this->success(__('成功'));
  107. } else {
  108. $this->error(__('验证码不正确'));
  109. }
  110. }
  111. }