Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

easywechat 网页授权问题 #50

Open
wilbur-yu opened this issue Oct 9, 2020 · 0 comments
Open

easywechat 网页授权问题 #50

wilbur-yu opened this issue Oct 9, 2020 · 0 comments

Comments

@wilbur-yu
Copy link
Contributor

wilbur-yu commented Oct 9, 2020

返回回来的 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);
		}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant