Login.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Cas\Controller;
  3. use KIF\Cache\Memcached;
  4. use KIF\Core\Config;
  5. use KIF\Core\Request;
  6. /**
  7. *
  8. * Login
  9. * @author rabin
  10. *
  11. */
  12. class Login extends Controller{
  13. private $appId;
  14. private $appSecret;
  15. public function __construct() {
  16. $wechat_cfg = Config::getInstance()->get('wechat_cfg');
  17. $this->appSecret = $wechat_cfg['appSecret'];
  18. $this->appId = $wechat_cfg['appId'];
  19. $this->objMemcached = new Memcached();
  20. $this->refer = 'accessTokenRefer';
  21. }
  22. public function doGet() {
  23. $refer = Request::g ( 'referer' );
  24. $cas_uid = Request::g ( 'cas_uid' );
  25. $this->objMemcached->set($this->refer . '_' . $cas_uid, $refer);
  26. $host = Config::getInstance ()->get ( 'App_Id' );
  27. $callback = urlencode($host . '?c=Login&a=Callback&cas_uid=' . $cas_uid);
  28. $time = time() . rand(0,100);
  29. $url = 'https://open.weixin.qq.com/connect/qrconnect';
  30. $url = 'https://open.weixin.qq.com/connect/oauth2/authorize';
  31. $url .= '?appid='.$this->appId.'&redirect_uri='.$callback.'&response_type=code&scope=snsapi_userinfo&state='.$time.'#wechat_redirect';
  32. //echo $url;die;
  33. //self::redirect($url);
  34. header ( "Location: " . $url);
  35. }
  36. public function doCallback() {
  37. $cas_uid = Request::g ( 'cas_uid' );
  38. $token = false;
  39. /*
  40. $token = $this->objMemcached->get('access_token');
  41. $openid = $this->objMemcached->get('openid');
  42. $data = array();
  43. if ($token && $openid) {
  44. $url = 'https://api.weixin.qq.com/sns/auth?access_token='.$token.'&openid='.$openid;
  45. $result = $this->httpGet($url);
  46. $result = json_decode($result, true);
  47. if ($result['errmsg'] != 'ok') {
  48. $token = false;
  49. $openid = false;
  50. } else {
  51. $data['access_token'] = $token;
  52. $data['openid'] = $openid;
  53. }
  54. }
  55. */
  56. if (!$token) {
  57. $code = Request::g ( 'code' );
  58. $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appId.'&secret='.$this->appSecret.'&code='.$code.'&grant_type=authorization_code';
  59. $data = $this->httpGet($url);
  60. $data = json_decode($data, true);
  61. }
  62. if (isset($data['access_token']) && $data['access_token']) {
  63. //$this->objMemcached->set('access_token', $data['access_token']);
  64. //$this->objMemcached->set('openid', $data['openid']);
  65. $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $data['access_token'] . '&openid=' . $data['openid'] . '&lang=zh_CN';
  66. $data = $this->httpGet($url);
  67. $data = json_decode($data, true);
  68. if (isset($data['nickname']) && $data['nickname']) {
  69. $refer = urldecode($this->objMemcached->get($this->refer . '_' . $cas_uid));
  70. $refer = str_replace('&amp;', '&', $refer);
  71. $param['nickname'] = $data['nickname'];
  72. $param['headimgurl'] = $data['headimgurl'];
  73. $param['uid'] = $data['openid'];
  74. $refer .= '&' . http_build_query($param);
  75. header ( "Location: " . $refer);
  76. } else {
  77. print_r($data);die;
  78. }
  79. } else {
  80. print_r($data);die;
  81. }
  82. }
  83. private function httpGet($url) {
  84. $curl = curl_init();
  85. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  86. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  87. curl_setopt($curl, CURLOPT_URL, $url);
  88. $res = curl_exec($curl);
  89. curl_close($curl);
  90. return $res;
  91. }
  92. }