google2fa = Api::user()->google2fa; if (blank($this->google2fa)) { throw new Exception('必须先初始化密钥'); } } /** * Notes : 获取密钥 * * @Date : 2022/12/1 14:31 * @Author : * @param Request $request * @return JsonResponse * @throws Exception */ public function index(Request $request): JsonResponse { $this->needInitialize(); return $this->success($this->google2fa->google2fa_secret); } /** * Notes : 获取密钥二维码地址 * * @Date : 2022/12/1 14:31 * @Author : * @return JsonResponse * @throws Exception */ public function qrCodeUrl(): JsonResponse { $this->needInitialize(); return $this->success($this->google2fa->getQrCodeUrl()); } /** * Notes : 开启两步验证 * * @Date : 2022/12/1 15:42 * @Author : * @return JsonResponse * @throws Exception */ public function open(): JsonResponse { $this->needInitialize(); if ($this->google2fa->open()) { return $this->success('两步验证开启成功'); } else { return $this->failed('开启失败'); } } /** * Notes : 关闭两步验证 * * @Date : 2022/12/1 15:43 * @Author : */ public function close(Request $request) { $this->needInitialize(); $verify = $request->verify; if (strlen($verify) != 6) { return $this->failed('请输入动态口令'); } if (! $this->google2fa->verify($verify)) { return $this->failed('动态口令不正确'); } if ($this->google2fa->close()) { return $this->success('更新成功'); } else { return $this->failed('更新失败'); } } /** * Notes : 更新密钥 * * @Date : 2022/12/1 15:29 * @Author : * @param Request $request * @return JsonResponse * @throws Exception */ public function update(Request $request): JsonResponse { // 短信验证码 $verify = $request->verify; $this->needInitialize(); if ($this->google2fa->upgrade()) { return $this->success('更新成功'); } else { return $this->failed('更新失败'); } } }