Sms.php 3.4 KB

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