function callback($ctx) { $jump = htmlspecialchars(trim($_GET['jump'])); self::validate_url($jump); if (!$this->appid || !$this->secret) { _redirect($jump); } $code = urlencode(htmlspecialchars(trim($_GET['code']))); if (!$code) { _redirect($jump); } $wx_url = 'https://api.weixin.qq.com/sns/oauth2/access_token'; $wx_url = "{$wx_url}?appid={$this->appid}&secret={$this->secret}&code={$code}&grant_type=authorization_code"; $resp = Http::get($wx_url); $ret = @json_decode($resp, true); if (is_array($ret) && $ret['openid']) { $connect = WxConnect::get_by('wx_openid', $ret['openid']); if ($connect) { Logger::info("wx_openid[{$ret['openid']}] oauth login, uid: {$connect->user_id}"); $profile = Profile::get($connect->user_id); if ($profile && $profile->status != Profile::STATUS_LOCK) { UC::force_login($profile); } } else { // 兼容 /weixin/bind, 因为它依赖 session 中的 openid, 所以这里设置 session_start(); $_SESSION['wx_openid'] = $ret['openid']; } } else { Logger::info("weixin oauth, code: {$code}, resp: {$resp}, " . Http::$error); } _redirect($jump); }
function index($ctx) { $jump = htmlspecialchars(trim($_GET['jump'])); $host = Html::host(); if (!preg_match("/http(s)?:\\/\\/[^\\/]*{$host}\\//", $jump)) { $jump = ''; } // 验证 token $token = htmlspecialchars(trim($_GET['token'])); if (strlen($token) == 32) { $sess = WxTmpLogin::get_session($token); if ($sess) { WxTmpLogin::del_session($token); } } if (!$sess) { #if($token && !$_SESSION['wx_openid']){ # _throw("链接已经过期, 请重新获取微信消息!", 200); #} _redirect($jump); } session_start(); $_SESSION['wx_openid'] = $sess['openid']; $connect = WxConnect::get_by('wx_openid', $sess['openid']); if (!$connect) { setcookie(WxTmpLogin::COOKIE_KEY_AUTO_BIND_WX, 1, time() + 3600 * 24, '/'); Logger::info("not connected wx_openid: {$sess['openid']}"); UC::logout(); } else { $uid = $connect->user_id; $profile = Profile::get($uid); setcookie('ltz_wx_binded', 1, time() + 3600 * 24 * 365, "/"); // 已经绑定了,直接删除该cookie if (isset($_COOKIE[WxTmpLogin::COOKIE_KEY_AUTO_BIND_WX])) { setcookie(WxTmpLogin::COOKIE_KEY_AUTO_BIND_WX, '', time() - 1, '/'); } Logger::info("wx_openid[{$sess['openid']}] login, uid: {$uid}, {$profile->name}"); UC::force_login($profile); } _redirect($jump); }