Passport.class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Cas\Controller;
  3. use KIF\Cache\Memcached;
  4. use KIF\Core\Config;
  5. use KIF\Core\Request;
  6. use Cas\Dao\UserInfo;
  7. /**
  8. *
  9. * passport 从第三方获取userid
  10. * @author rabin
  11. *
  12. */
  13. class Passport extends Controller{
  14. private $config;
  15. public function __construct() {
  16. $this->config = Config::getInstance()->get('passport');
  17. //$this->objMemcached = new Memcached();
  18. //$this->refer = 'passportRefer';
  19. }
  20. public function doGet() {
  21. $refer = Request::g ( 'referer' );
  22. $cas_uid = Request::g ( 'cas_uid' );
  23. $tokenid = Request::g ( 'tokenid' );
  24. $objUserData = new UserInfo();
  25. $userData = $objUserData -> get($cas_uid);
  26. //$this->objMemcached->set($this->refer, $refer);
  27. $host = $this->config['url'];
  28. $param = $this->config['param'];
  29. if ($param) {
  30. foreach ($param as $k => $v) {
  31. if ($v == '{passport_uid}') {
  32. if (isset($userData['passport_uid']) && $userData['passport_uid']) {
  33. $param[$k] = $userData['passport_uid'];
  34. } else {
  35. $param[$k] = $v = $tokenid;
  36. }
  37. }
  38. if (!$v) {
  39. unset($param[$k]);
  40. }
  41. }
  42. }
  43. //echo $this->config['url'] . "\r\n";
  44. //print_r($param);
  45. $data = $this->httpPost($host, $param);
  46. $data = json_decode($data, true);
  47. /*
  48. $data['data'] = array
  49. (
  50. 'nickName' => 'rabin',
  51. 'avatar' => 'http://echarts.baidu.com/images/logo.png',
  52. 'tokenId' => '111111',
  53. );
  54. */
  55. if (isset($data['data']) && isset($data['data']['tokenId']) && $data['data']['tokenId']) {
  56. $refer = urldecode($refer);
  57. $refer = str_replace('&amp;', '&', $refer);
  58. $param['nickname'] = $data['data']['nickName'];
  59. $param['headimgurl'] = $data['data']['avatar'];
  60. $param['uid'] = $data['data']['tokenId'];
  61. $refer .= '&' . http_build_query($param);
  62. header ( "Location: " . $refer);
  63. } else {
  64. print_r($data);die;
  65. }
  66. }
  67. private function httpGet($url) {
  68. $curl = curl_init();
  69. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  70. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  71. curl_setopt($curl, CURLOPT_URL, $url);
  72. $res = curl_exec($curl);
  73. curl_close($curl);
  74. return $res;
  75. }
  76. private function httpPost($url, $param) {
  77. $curl = curl_init();
  78. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  79. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  80. curl_setopt($curl, CURLOPT_URL, $url);
  81. curl_setopt($curl, CURLOPT_POST, 1);
  82. curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
  83. $res = curl_exec($curl);
  84. curl_close($curl);
  85. return $res;
  86. }
  87. }