|
@@ -0,0 +1,78 @@
|
|
|
+<?php
|
|
|
+namespace Cas\Controller;
|
|
|
+
|
|
|
+use KIF\Core\Config;
|
|
|
+
|
|
|
+
|
|
|
+ *
|
|
|
+ * Login
|
|
|
+ * @author rabin
|
|
|
+ *
|
|
|
+ */
|
|
|
+class Login extends Controller{
|
|
|
+
|
|
|
+ private $appId;
|
|
|
+ private $appSecret;
|
|
|
+
|
|
|
+ public function __construct() {
|
|
|
+ $wechat_cfg = Config::getInstance()->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' );
|
|
|
+
|
|
|
+ $this->objMemcached->set($this->refer, $refer);
|
|
|
+
|
|
|
+ $host = Config::getInstance ()->get ( 'App_Id' );
|
|
|
+ $callback = urlencode($host . '?c=Login&a=Callback');
|
|
|
+
|
|
|
+ $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_login&state='.$time.'#wechat_redirect';
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ header ( "Location: " . $url);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function doCallback() {
|
|
|
+ $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']) {
|
|
|
+ $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);
|
|
|
+
|
|
|
+ $refer = $this->objMemcached->get($this->refer);
|
|
|
+
|
|
|
+ $param = $data;
|
|
|
+ $param['uid'] = $data['openid'];
|
|
|
+ $refer .= '&' . http_build_url($param);
|
|
|
+
|
|
|
+ header ( "Location: " . $refer);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+}
|