SmsLogic.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Admin\Http\Logics;
  3. use App\DataApi\Helper\TencentSmsHelper;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\Redis;
  6. use function App\Http\_header;
  7. use function App\Http\error;
  8. use function App\Http\success;
  9. class SmsLogic
  10. {
  11. /**
  12. * 发送验证码
  13. *
  14. * @param [type] $phone
  15. * @return array
  16. */
  17. public static function sendSms($phone)
  18. {
  19. if (Redis::exists('CODE_' . $phone)) return error("该手机号已发送短信,请稍后再试");
  20. $tencentMessage = new TencentSmsHelper();
  21. $code = (string)rand(1111, 9999);
  22. $res = $tencentMessage->send('86' . $phone, '1734411', [$code, '5']);
  23. if (!empty($res['code']) and $res['code'] == 1) {
  24. Redis::setex('CODE_' . $phone, 60 * 5, $code);
  25. return success([], "发送成功,验证码5分钟内有效");
  26. } else {
  27. return error($res['msg']);
  28. }
  29. }
  30. /**
  31. * 验证验证码
  32. *
  33. * @param [type] $phone
  34. * @return bool
  35. */
  36. public static function validSms($phone, $code)
  37. {
  38. return ($code == Redis::get('CODE_' . $phone));
  39. }
  40. }