Sms.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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");
  24. $event = 'default';
  25. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  26. $this->error(__('手机号不正确'));
  27. }
  28. $last = Smslib::get($mobile, $event);
  29. if ($last && time() - $last['createtime'] < 60) {
  30. $this->error(__('发送频繁'));
  31. }
  32. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  33. if ($ipSendTotal >= 5) {
  34. $this->error(__('发送频繁'));
  35. }
  36. $ret = Smslib::send($mobile, null, $event);
  37. if ($ret) {
  38. $this->success(__('发送成功'));
  39. } else {
  40. $this->error(__('发送失败,请检查短信配置是否正确'));
  41. }
  42. }
  43. /**
  44. * 检测验证码
  45. *
  46. * @ApiMethod (POST)
  47. * @param string $mobile 手机号
  48. * @param string $event 事件名称
  49. * @param string $captcha 验证码
  50. */
  51. public function check()
  52. {
  53. $mobile = $this->request->post("mobile");
  54. $event = 'default';
  55. $captcha = $this->request->post("captcha");
  56. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  57. $this->error(__('手机号不正确'));
  58. }
  59. $ret = Smslib::check($mobile, $captcha, $event);
  60. if ($ret) {
  61. $this->success(__('成功'));
  62. } else {
  63. $this->error(__('验证码不正确'));
  64. }
  65. }
  66. }