Sms.php 3.5 KB

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