diff --git a/app/controller/wechat/Wechat.php b/app/controller/wechat/Wechat.php index 52e51ca..0a8ec3b 100644 --- a/app/controller/wechat/Wechat.php +++ b/app/controller/wechat/Wechat.php @@ -4,13 +4,14 @@ namespace app\controller\wechat; use app\model\WechatPucode; -use app\model\WechatSms; use app\model\WechatUser; use app\service\user\SmsService; use app\service\webService\FeeService; +use app\util\AuthCodeUtil; use app\validate\WechatUser as WechatUserValidate; use app\service\wechat\WechatService; use app\util\ReturnCode; +use think\cache\driver\Redis; use think\exception\ValidateException; use think\facade\Db; use think\Request; @@ -44,28 +45,20 @@ class Wechat extends Base { $param = $request->param(); - $wechat_user_id = $request->wechat_user_id; - try { validate(WechatUserValidate::class)->scene('smsCode')->check($param); - $create_time = WechatSms::getValidUser($wechat_user_id); - if ($create_time) { - $time = time() - $create_time; - throw new \Exception('请在' . $time . '秒后再次发送'); - } + $AuthCodeUtil = new AuthCodeUtil(new Redis()); - $code = WechatSms::getCode($wechat_user_id); - $addResult = WechatSms::addSmsCode($request->wechat_user_id,$code); - if (!$addResult) { - throw new \Exception('发送失败'); - } + $AuthCodeUtil->setRate($param['mobile']); + + $code = $AuthCodeUtil->setCode($param['mobile']); $SmsService = new SmsService(); $SmsService->sendSms($param['mobile'],$SmsService->smsCodeTemplate($code)); - return $this->buildSuccess(); + return $this->buildSuccess([],'短信已发送'); } catch (\Exception $e) { return $this->buildFailed(ReturnCode::INVALID,$e->getMessage()); } @@ -88,7 +81,7 @@ class Wechat extends Base //$defaultSnsCode = '439764'; try { validate(WechatUserValidate::class)->scene('savePuCode')->check($param); - $res = WechatSms::getValidCode($param['snsCode'],$wechat_user_id); + $res = (new AuthCodeUtil(new Redis()))->verifyCode($param['mobile'],$param['snsCode']); if (!$res) { throw new \Exception('短信验证码错误'); } diff --git a/app/model/WechatSms.php b/app/model/WechatSms.php deleted file mode 100644 index 46303d8..0000000 --- a/app/model/WechatSms.php +++ /dev/null @@ -1,60 +0,0 @@ -=', time() - (10*60)], - ]; - - return (new self())->where($where)->value('wechat_user_id'); - } - - - public static function getValidUser($wechat_user_id) - { - $where = [ - ['wechat_user_id', '=', $wechat_user_id], - ['create_time', '>=', time() - 60], - ]; - - return (new self())->where($where)->value('create_time'); - } - - public static function addSmsCode($wechat_user_id,$code): bool - { - - $data = [ - 'wechat_user_id' => $wechat_user_id, - 'code' => $code, - 'create_time' => time() - ]; - - return (new self())->save($data); - } - - -} \ No newline at end of file diff --git a/app/util/AuthCodeUtil.php b/app/util/AuthCodeUtil.php new file mode 100644 index 0000000..351f889 --- /dev/null +++ b/app/util/AuthCodeUtil.php @@ -0,0 +1,75 @@ +cache = $cache; + } + + //生成code保存到redis,并返回code + public function setCode($mobile) + { + $key = "Auth:" . $mobile; + $code = $this->newAuthCode(); + $this->cache->set($key, $code, $this->ttl); + return $code; + } + + /** + * 校对验证码 + * @param $mobile + * @param $code + * @return bool + */ + public function verifyCode($mobile, $code): bool + { + $key = "Auth:" . $mobile; + $cacheCode = $this->cache->get($key); + if ($cacheCode == $code) { + return true; + } else { + return false; + } + } + + /** + * + * @return int + */ + public function newAuthCode(): int + { + return rand(100000, 999999); + } + + /** + * @param $mobile + * @return bool + * @throws FuncException + */ + public function setRate($mobile): bool + { + $key = "Auth:" . $mobile; + $cacheCode = $this->cache->get($key); + + if ($cacheCode) { + $ttl = Cache::store('redis')->ttl($key); + if (($this->ttl - $ttl) < 60) { + throw new FuncException('请超过60秒之后再发送短信'); + } + } + + return true; + } +} \ No newline at end of file