sKey; } public function setSKey(null $sKey): void { $this->sKey = $sKey; } public function getsCode(): null { return $this->sCode; } public function setsCode(null $sCode): void { $this->sCode = $sCode; } public function getSPhoneArea(): null { return $this->sPhoneArea; } public function setSPhoneArea(null $sPhoneArea): void { $this->sPhoneArea = $sPhoneArea; } public function getSPhone(): null { return $this->sPhone; } public function setSPhone(null $sPhone): void { $this->sPhone = $sPhone; } public function getIType(): null { return $this->iType; } /** * @throws AppException */ public function setIType(null $iType): void { if(!in_array($iType,VrCode::TOPIC)) throw new AppException('invalid sms type'); $this->iType = $iType; } public function getIUid(): null { return $this->iUid; } public function setIUid(null $iUid): void { $this->iUid = $iUid; } function sendSmsToPhone(): bool { return true; } /** * @throws AppException */ function sendCode(): void { $this->sKey = $this->genKey(); $this->sCode = $this->genCode(); if(!$this->sendSmsToPhone()) throw new AppException('send sms failed'); $this->setCodeToCache(); } /** * @throws AppException */ function genKey(): false|string { if(!in_array($this->iType,VrCode::TOPIC_PREFIX)) throw new AppException('invalid sms type'); if($this->iType == VrCode::TOPIC_REGISTER) { return VrCode::TOPIC_PREFIX[VrCode::TOPIC_REGISTER] . md5($this->sPhoneArea . $this->sPhone); }else { return VrCode::TOPIC_PREFIX[VrCode::TOPIC_REGISTER] . $this->iUid; } } function genCode(): string { return (string)Tools::getRandNum(); } function setCodeToCache() { return Redis::set($this->sKey,$this->sCode,VrCode::REDIS_CACHE_EXPIRE); } /** * @throws AppException */ function getCodeFromCache() { return Redis::get($this->genKey()); } function checkCode($sReqCode): bool { $sCode = $this->getCodeFromCache(); if(empty($sCode)) return false; if($sCode != $sReqCode) return false; return true; } }