Sms.php 3.3 KB

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