Sms.php 3.2 KB

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