Sms.php 3.9 KB

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