Sms.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\Env;
  7. use think\Hook;
  8. /**
  9. * 手机短信接口
  10. */
  11. class Sms extends Api
  12. {
  13. protected $noNeedLogin = '*';
  14. protected $noNeedRight = '*';
  15. /**
  16. * 发送验证码
  17. *
  18. * @param string $mobile 手机号
  19. * @param string $event 事件名称
  20. */
  21. public function send()
  22. {
  23. $mobile = $this->request->post("mobile");
  24. $event = $this->request->post("event");
  25. $event = $event ? strtolower($event) : 'register';
  26. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  27. $this->error(__('手机号不正确'));
  28. }
  29. $this->lock($mobile, 3);
  30. /*print_r(Env::get('app.sms_dev', false));
  31. // 开启模拟短信,则禁用短信插件 begin
  32. if (Env::get('app.sms_dev', false)) {
  33. Hook::add('sms_send', function ($params) {
  34. return true;
  35. }, true);
  36. }
  37. // 开启模拟短信,则禁用短信插件 end
  38. */
  39. $last = Smslib::get($mobile, $event);
  40. if ($last && time() - $last['createtime'] < 60) {
  41. $this->error(__('发送频繁'));
  42. }
  43. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  44. if ($ipSendTotal >= 10) {
  45. $this->error(__('发送频繁'));
  46. }
  47. if ($event) {
  48. $userinfo = User::getByMobile($mobile);
  49. if ('login' == $event && !$userinfo) {
  50. // 未注册用户登录即注册
  51. $event = 'register';
  52. }
  53. if ($event == 'register' && $userinfo) {
  54. //已被注册
  55. $this->error(__('手机号已被注册'));
  56. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  57. //被占用
  58. $this->error(__('手机号已被占用'));
  59. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  60. //未注册
  61. $this->error(__('手机号未注册'));
  62. }
  63. }
  64. if (!Hook::get('sms_send')) {
  65. $this->error(__('请在后台插件管理安装短信验证插件'));
  66. }
  67. // 开启短信模拟则短信验证码固定为123456
  68. $code = null;//Env::get('app.sms_dev', false) ? '123456' : null;
  69. $ret = Smslib::send($mobile, $code, $event);
  70. // print_r($code);
  71. if ($ret) {
  72. $this->success(__('发送成功'));
  73. } else {
  74. $this->error(__('发送失败,请检查短信配置是否正确'));
  75. }
  76. }
  77. /**
  78. * 检测验证码
  79. *
  80. * @param string $mobile 手机号
  81. * @param string $event 事件名称
  82. * @param string $captcha 验证码
  83. */
  84. public function check()
  85. {
  86. $mobile = $this->request->post("mobile");
  87. $event = $this->request->post("event");
  88. $event = $event ? $event : 'register';
  89. $captcha = $this->request->post("captcha");
  90. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  91. $this->error(__('手机号不正确'));
  92. }
  93. if ($event) {
  94. $userinfo = User::getByMobile($mobile);
  95. if ($event == 'register' && $userinfo) {
  96. //已被注册
  97. $this->error(__('已被注册'));
  98. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  99. //被占用
  100. $this->error(__('已被占用'));
  101. } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
  102. //未注册
  103. $this->error(__('未注册'));
  104. }
  105. }
  106. $ret = Smslib::check($mobile, $captcha, $event);
  107. if ($ret) {
  108. $this->success(__('成功'));
  109. } else {
  110. $this->error(__('验证码不正确'));
  111. }
  112. }
  113. }