Sms.php 3.2 KB

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