1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace Jindouyun\Cache;
- use http\Exception;
- use Mall\Framework\Factory;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\ErrorCode;
- class SmsVerification
- {
- private $cache;
- protected $registerSmsCountKey = 'registerSmsCount_';
- protected $key = '';
- protected $userRegisterSmsCodeExpire = 300;
- protected $registerSms = 'registerSms';
- public function __construct()
- {
- $this->key = $this->registerSmsCountKey . date('Y-m-d', time());
- $this->cache = Factory::cache('default');
- }
-
- public function userMobileCodeCache($mobile, $mobileCode)
- {
- if (empty($mobileCode)) {
- return ResultWrapper::fail('要缓存的数据为空', ErrorCode::$paramError);
- }
- $result = $this->cache->set($this->registerSms . '::' . $mobile, $mobileCode, $this->userRegisterSmsCodeExpire);
- if ($result) {
-
- $returnData = $this->cache->zincrby($this->key, 1, $mobile);
- if ($returnData) {
- $this->cache->expire($this->key, 86400);
- }
- return ResultWrapper::success('用户注册手机验证码写入缓存成功');
- } else {
- return ResultWrapper::fail('用户注册手机验证码写入缓存失败', ErrorCode::$redisWriteError);
- }
- }
-
- public function getMobileCode($mobile)
- {
- if (!$mobile) {
- return '';
- }
- $result = Factory::cache('default')->get($this->registerSms . '::' . $mobile);
- if (!$result) {
- return '';
- }
- return $result;
- }
-
- public function getRegisterSmsCount($mobile)
- {
- return $this->cache->zscore($this->key, $mobile);
- }
- }
|