get('wechat_cfg'); $this->appSecret = $wechat_cfg['appSecret']; $this->appId = $wechat_cfg['appId']; $this->objMemcached = new Memcached(); $this->refer = 'accessTokenRefer'; } public function doGet() { $refer = Request::g ( 'referer' ); $cas_uid = Request::g ( 'cas_uid' ); $this->objMemcached->set($this->refer . '_' . $cas_uid, $refer); $host = Config::getInstance ()->get ( 'App_Id' ); $callback = urlencode($host . '?c=Login&a=Callback&cas_uid=' . $cas_uid); $time = time() . rand(0,100); $url = 'https://open.weixin.qq.com/connect/qrconnect'; $url = 'https://open.weixin.qq.com/connect/oauth2/authorize'; $url .= '?appid='.$this->appId.'&redirect_uri='.$callback.'&response_type=code&scope=snsapi_userinfo&state='.$time.'#wechat_redirect'; //echo $url;die; //self::redirect($url); header ( "Location: " . $url); } public function doCallback() { $cas_uid = Request::g ( 'cas_uid' ); $token = false; /* $token = $this->objMemcached->get('access_token'); $openid = $this->objMemcached->get('openid'); $data = array(); if ($token && $openid) { $url = 'https://api.weixin.qq.com/sns/auth?access_token='.$token.'&openid='.$openid; $result = $this->httpGet($url); $result = json_decode($result, true); if ($result['errmsg'] != 'ok') { $token = false; $openid = false; } else { $data['access_token'] = $token; $data['openid'] = $openid; } } */ if (!$token) { $code = Request::g ( 'code' ); $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appId.'&secret='.$this->appSecret.'&code='.$code.'&grant_type=authorization_code'; $data = $this->httpGet($url); $data = json_decode($data, true); } if (isset($data['access_token']) && $data['access_token']) { //$this->objMemcached->set('access_token', $data['access_token']); //$this->objMemcached->set('openid', $data['openid']); $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $data['access_token'] . '&openid=' . $data['openid'] . '&lang=zh_CN'; $data = $this->httpGet($url); $data = json_decode($data, true); if (isset($data['nickname']) && $data['nickname']) { $refer = urldecode($this->objMemcached->get($this->refer . '_' . $cas_uid)); $refer = str_replace('&', '&', $refer); $param['nickname'] = $data['nickname']; $param['headimgurl'] = $data['headimgurl']; $param['uid'] = $data['openid']; $refer .= '&' . http_build_query($param); header ( "Location: " . $refer); } else { print_r($data);die; } } else { print_r($data);die; } } private function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } }