shadowfax icon indicating copy to clipboard operation
shadowfax copied to clipboard

easywechat 网页授权问题

Open wilbur-yu opened this issue 4 years ago • 0 comments

返回回来的 code 一直失效或者二次使用, 导致无法获取用户信息.

log

[2020-10-09 10:14:19] production.ERROR: 获取用户信息失败 {"msg":"Authorize Failed: {\"errcode\":40163,\"errmsg\":\"code been used, hints: [ req_id: GEeEWaMre-GRQona ]\"}"}
[2020-10-09 10:14:20] production.ERROR: 获取用户信息失败 {"msg":"Authorize Failed: {\"errcode\":40029,\"errmsg\":\"invalid code, hints: [ req_id: GEeEUqNre-TL.5ya ]\"}"}

middleware.php

		public function handle(Request $request, Closure $next)
		{
			$session = session(self::SESSION_KEY, []);
//			Log::error('user', ['data' => $session]);
			
			if (!$session) {
				/** @var Application $officialAccount */
				$officialAccount = app('wechat.official_account.wwf');
				
				if ($request->has('code')) {
					try {
						$user = $officialAccount->oauth->user();
						
						// 存储到session
						session([self::SESSION_KEY => $user]);
						
						// 重定向到首页
//						return redirect()->to($this->getTargetUrl($request));
						return redirect()->route('module.wwf.home');
					} catch (AuthorizeFailedException $e) {
						Log::error('获取用户信息失败', ['msg' => $e->getMessage()]);
						
						return redirect()->route('module.wwf.home');
					}
					
				}
				
				session()->forget(self::SESSION_KEY);
				
				return $officialAccount->oauth->scopes(['snsapi_userinfo'])
					->redirect($request->fullUrl());
			}
			
			return $next($request);
		}

wilbur-yu avatar Oct 09 '20 02:10 wilbur-yu