<?php /** * 网站进行微信OAuth登陆 * 该页面可通过Redirect方式进行访问,或者直接在需要的地方include_once */ chdir(dirname(__FILE__)); //把工作目录切换到文件所在目录 include_once dirname(__FILE__) . '/__config__.php'; // state为交互时双方都会带着的get参数,用于做一些逻辑判断,如果没指定,则默认一个 if (!$state) { $state = "fromydwx"; } $redirect = YDWX_SITE_URL . 'ydwx/webauth.php'; if (!@$_GET['code'] && !@$_GET['state']) { ob_clean(); header("Location: https://open.weixin.qq.com/connect/qrconnect?appid=" . YDWX_WEIXIN_WEB_APP_ID . "&redirect_uri={$redirect}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect"); die; } if (!@$_GET['code'] && @$_GET['state']) { YDWXHook::do_hook(YDWXHook::AUTH_CANCEL); die; } $http = new YDHttp(); $info = json_decode($http->get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . YDWX_WEIXIN_WEB_APP_ID . "&secret=" . YDWX_WEIXIN_WEB_APP_SECRET . "&code=" . $_GET['code'] . "&grant_type=authorization_code"), true); if (!@$info['openid']) { YDWXHook::do_hook(YDWXHook::AUTH_FAIL, YDWXAuthFailResponse::errMsg($info['errmsg'], $info['errcode'])); die; } YDWXHook::do_hook(YDWXHook::AUTH_WEB_SUCCESS, ydwx_sns_userinfo($info['access_token'], $info['openid'], $_GET['state']));
<?php /** * 公众号授权第三方平台托管流程 */ chdir(dirname(__FILE__)); include_once dirname(__FILE__) . '/__config__.php'; $auth_code = @$_GET["auth_code"]; if (!$auth_code) { YDWXHook::do_hook(YDWXHook::AUTH_CANCEL); die; } try { $auth_info = ydwx_agent_query_auth($auth_code); } catch (\Exception $e) { YDWXHook::do_hook(YDWXHook::AUTH_FAIL, YDWXAuthFailResponse::errMsg($e->getMessage())); die; } YDWXHook::do_hook(YDWXHook::AUTH_AGENT_SUCCESS, array($auth_info, ydwx_agent_get_auth_account($auth_info->authorizer_appid)));
YDWXHook::do_hook(YDWXHook::AUTH_FAIL, YDWXAuthFailResponse::errMsg($info['errmsg'], $info['errcode'])); die; } if ($isAgent) { $access_token = YDWXHook::do_hook(YDWXHook::GET_HOST_ACCESS_TOKEN, $appid); } else { $access_token = YDWXHook::do_hook(YDWXHook::GET_ACCESS_TOKEN); } if ($access_token) { try { $user = ydwx_user_info($access_token, $info['openid']); } catch (\Exception $e) { $user = new YDWXSubscribeUser(); $user->openid = $info['openid']; } } else { $user = new YDWXSubscribeUser(); $user->openid = $info['openid']; } $user->appid = $appid; $user->state = $_GET['state']; YDWXHook::do_hook(YDWXHook::AUTH_INAPP_SUCCESS, $user); die; } //企业号返回的是code,可直接获取用户的信息TODO 是否企业号也会托管,那这里是不是该拿托管的企业号token $access_token = YDWXHook::do_hook(YDWXHook::GET_ACCESS_TOKEN); if ($access_token) { YDWXHook::do_hook(YDWXHook::AUTH_CROP_SUCCESS, ydwx_crop_user_info($access_token, $_GET['code'], $_GET['state'])); } else { YDWXHook::do_hook(YDWXHook::AUTH_FAIL, YDWXAuthFailResponse::errMsg("未取得access token")); }