Login.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. $this->objMemcached->set($this->refer, $refer);
  25. $host = Config::getInstance ()->get ( 'App_Id' );
  26. $callback = urlencode($host . '?c=Login&a=Callback');
  27. $time = time() . rand(0,100);
  28. $url = 'https://open.weixin.qq.com/connect/qrconnect';
  29. $url = 'https://open.weixin.qq.com/connect/oauth2/authorize';
  30. $url .= '?appid='.$this->appId.'&redirect_uri='.$callback.'&response_type=code&scope=snsapi_userinfo&state='.$time.'#wechat_redirect';
  31. //echo $url;die;
  32. //self::redirect($url);
  33. header ( "Location: " . $url);
  34. }
  35. public function doCallback() {
  36. $token = $this->objMemcached->get('access_token');
  37. $openid = $this->objMemcached->get('openid');
  38. $data = array();
  39. if ($token && $openid) {
  40. $url = 'https://api.weixin.qq.com/sns/auth?access_token='.$token.'&openid='.$openid;
  41. $result = $this->httpGet($url);
  42. $result = json_decode($result, true);
  43. if ($result['errmsg'] != 'ok') {
  44. $token = false;
  45. $openid = false;
  46. } else {
  47. $data['access_token'] = $token;
  48. $data['openid'] = $openid;
  49. }
  50. }
  51. if (!$token) {
  52. $code = Request::g ( 'code' );
  53. $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appId.'&secret='.$this->appSecret.'&code='.$code.'&grant_type=authorization_code';
  54. $data = $this->httpGet($url);
  55. $data = json_decode($data, true);
  56. }
  57. if (isset($data['access_token']) && $data['access_token']) {
  58. $this->objMemcached->set('access_token', $data['access_token']);
  59. $this->objMemcached->set('openid', $data['openid']);
  60. $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $data['access_token'] . '&openid=' . $data['openid'] . '&lang=zh_CN';
  61. $data = $this->httpGet($url);
  62. $data = json_decode($data, true);
  63. if (isset($data['nickname']) && $data['nickname']) {
  64. $refer = urldecode($this->objMemcached->get($this->refer));
  65. $refer = str_replace('&amp;', '&', $refer);
  66. $param['nickname'] = $data['nickname'];
  67. $param['headimgurl'] = $data['headimgurl'];
  68. $param['uid'] = $data['openid'];
  69. $refer .= '&' . http_build_query($param);
  70. header ( "Location: " . $refer);
  71. } else {
  72. print_r($data);die;
  73. }
  74. } else {
  75. print_r($data);die;
  76. }
  77. }
  78. private function httpGet($url) {
  79. $curl = curl_init();
  80. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  81. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  82. curl_setopt($curl, CURLOPT_URL, $url);
  83. $res = curl_exec($curl);
  84. curl_close($curl);
  85. return $res;
  86. }
  87. }