Sms.php 3.2 KB

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