GoogleCodeService.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. /**
  13. * 谷歌验证码管理-服务类
  14. * Class GoogleCodeService
  15. * @package App\Services
  16. */
  17. class GoogleCodeService extends BaseService
  18. {
  19. // 静态对象
  20. protected static $instance = null;
  21. /**
  22. * 初始化配置
  23. * @return bool
  24. */
  25. public function __construct()
  26. {
  27. $this->config = ConfigService::make()->getConfigByGroup(2);
  28. if (empty($this->config)) {
  29. return false;
  30. }
  31. }
  32. /**
  33. * 静态入口
  34. * @return static|null
  35. */
  36. public static function make()
  37. {
  38. if (!self::$instance) {
  39. self::$instance = (new static());
  40. }
  41. return self::$instance;
  42. }
  43. public function getQrcode($userId)
  44. {
  45. }
  46. /**
  47. * 发送谷歌验证码
  48. * @param $userId
  49. * @param string $scene 场景:默认reg-注册
  50. * @return bool
  51. */
  52. public function sendCode($userId, $scene = 'reg')
  53. {
  54. try {
  55. $cacheKey = "stores:codes:google_{$scene}:{$userId}";
  56. } catch (\Exception $exception) {
  57. $this->error = $exception->getMessage();
  58. return false;
  59. }
  60. }
  61. /**
  62. * 验证手机短信
  63. * @param $mobile
  64. * @param $code
  65. * @param string $scene 场景:默认reg-注册
  66. * @return bool
  67. */
  68. public function check($mobile, $code, $scene = 'reg')
  69. {
  70. $cacheKey = "stores:codes:sms_{$scene}:". substr($mobile, -3, 3) . '_' . md5($mobile);
  71. $codeData = RedisService::get($cacheKey);
  72. $checkCode = isset($codeData['code']) ? $codeData['code'] : 0;
  73. $checkMobile = isset($codeData['mobile']) ? $codeData['mobile'] : '';
  74. if (empty($codeData) || empty($checkCode)) {
  75. $this->error = 2010;
  76. return false;
  77. }
  78. if ($checkMobile != $mobile || $checkCode != $code) {
  79. $this->error = 2006;
  80. return false;
  81. }
  82. return true;
  83. }
  84. }