Sms.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 = $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,10,request()->ip())){
  30. $this->error('当前发送人数过多,请稍后再试');
  31. };
  32. if(!$this->apiLimit(3600,10,$mobile)){
  33. $this->error('您的手机号发送频繁,请稍后再试');
  34. };
  35. $last = Smslib::get($mobile, $event);
  36. if ($last && time() - $last['createtime'] < 60) {
  37. $this->error(__('发送频繁,请稍后再试'));
  38. }
  39. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  40. if ($ipSendTotal >= 5) {
  41. $this->error(__('发送频繁,请稍后再试'));
  42. }
  43. if ($event) {
  44. $userinfo = User::getByMobile($mobile);
  45. if ($event == 'register' && $userinfo) {
  46. //已被注册
  47. $this->error(__('已被注册'));
  48. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  49. //被占用
  50. $this->error(__('已被占用'));
  51. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  52. //未注册
  53. $this->error(__('未注册'));
  54. }
  55. }
  56. /*if (!Hook::get('sms_send')) {
  57. $this->error(__('请在后台插件管理安装短信验证插件'));
  58. }*/
  59. $ret = Smslib::send($mobile, null, $event);
  60. if ($ret) {
  61. $this->success(__('发送成功'));
  62. } else {
  63. $this->error(__('糟糕,发送失败了'));
  64. }
  65. }
  66. /**
  67. * 检测验证码
  68. *
  69. * @ApiMethod (POST)
  70. * @param string $mobile 手机号
  71. * @param string $event 事件名称
  72. * @param string $captcha 验证码
  73. */
  74. public function check()
  75. {
  76. $mobile = $this->request->post("mobile");
  77. $event = $this->request->post("event");
  78. $event = $event ? $event : 'register';
  79. $captcha = $this->request->post("captcha");
  80. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  81. $this->error(__('手机号不正确'));
  82. }
  83. if ($event) {
  84. $userinfo = User::getByMobile($mobile);
  85. if ($event == 'register' && $userinfo) {
  86. //已被注册
  87. $this->error(__('已被注册'));
  88. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  89. //被占用
  90. $this->error(__('已被占用'));
  91. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  92. //未注册
  93. $this->error(__('未注册'));
  94. }
  95. }
  96. $ret = Smslib::check($mobile, $captcha, $event);
  97. if ($ret) {
  98. $this->success(__('成功'));
  99. } else {
  100. $this->error(__('验证码不正确'));
  101. }
  102. }
  103. }