qq.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->func('communication');
  8. define('QQ_PLATFORM_API_OAUTH_LOGIN_URL', 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=%s&redirect_uri=%s&state=%s');
  9. define('QQ_PLATFORM_API_GET_ACCESS_TOKEN', 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=%s&client_secret=%s&code=%s&redirect_uri=%s');
  10. define('QQ_PLATFORM_API_GET_OPENID', 'https://graph.qq.com/oauth2.0/me?access_token=%s');
  11. define('QQ_PLATFORM_API_GET_USERINFO', 'https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s');
  12. class Qq extends OAuth2Client {
  13. private $calback_url;
  14. public function __construct($ak, $sk, $calback_url = '') {
  15. global $_W;
  16. parent::__construct($ak, $sk);
  17. $this->calback_url = $_W['siteroot'] . 'web/index.php';
  18. $this->stateParam['from'] = 'qq';
  19. }
  20. public function showLoginUrl($calback_url = '') {
  21. $state = $this->stateParam();
  22. return sprintf(QQ_PLATFORM_API_OAUTH_LOGIN_URL, $this->ak, $this->calback_url, $state);
  23. }
  24. public function getAccessToken($state, $code) {
  25. if (empty($state) || empty($code)) {
  26. return error(-1, '参数错误');
  27. }
  28. $local_state = $this->stateParam();
  29. if ($state != $local_state) {
  30. return error(-1, '重新登陆');
  31. }
  32. $access_url = sprintf(QQ_PLATFORM_API_GET_ACCESS_TOKEN, $this->ak, $this->sk, $code, urlencode($this->calback_url));
  33. $response = ihttp_get($access_url);
  34. if (strexists($response['content'], 'callback') !== false){
  35. return error(-1, $response['content']);
  36. }
  37. parse_str($response['content'], $result);
  38. return $result;
  39. }
  40. public function getOpenid($token) {
  41. if (empty($token)) {
  42. return error(-1, '参数错误');
  43. }
  44. $openid_url = sprintf(QQ_PLATFORM_API_GET_OPENID, $token);
  45. $response = ihttp_get($openid_url);
  46. if (strexists($response['content'], "callback") !== false) {
  47. $lpos = strpos($response['content'], "(");
  48. $rpos = strrpos($response['content'], ")");
  49. $content = substr($response['content'], $lpos + 1, $rpos - $lpos -1);
  50. }
  51. $result = json_decode($content, true);
  52. if (isset($result->error)) {
  53. return error(-1, $result['content']);
  54. }
  55. return $result;
  56. }
  57. public function getUserInfo($token, $openid) {
  58. if (empty($openid) || empty($token)) {
  59. return error(-1, '参数错误');
  60. }
  61. $openid_url = sprintf(QQ_PLATFORM_API_GET_USERINFO, $token, $this->ak, $openid);
  62. $response = ihttp_get($openid_url);
  63. $user_info = json_decode($response['content'], true);
  64. if ($user_info['ret'] != 0) {
  65. return error(-1, $user_info['ret'] . ',' . $user_info['msg']);
  66. }
  67. return $user_info;
  68. }
  69. public function getOauthInfo() {
  70. global $_GPC;
  71. $getAccessToken = $this->getAccessToken($_GPC['state'], $_GPC['code']);
  72. if (is_error($getAccessToken)) {
  73. return error($getAccessToken['errno'], $getAccessToken['message']);
  74. }
  75. $oauth['access_token'] = $getAccessToken['access_token'];
  76. $getOpenId = $this->getOpenid($oauth['access_token']);
  77. if (is_error($getOpenId['openid'])) {
  78. return error($getOpenId['errno'], $getOpenId['message']);
  79. }
  80. $oauth['openid'] = $getOpenId['openid'];
  81. return $oauth;
  82. }
  83. public function user() {
  84. $oauth_info = $this->getOauthInfo();
  85. $openid = $oauth_info['openid'];
  86. $user_info = $this->getUserInfo($oauth_info['access_token'], $openid);
  87. if (is_error($user_info)) {
  88. return $user_info;
  89. }
  90. $user = array();
  91. $profile = array();
  92. $user['username'] = strip_emoji($user_info['nickname']);
  93. $user['password'] = '';
  94. $user['type'] = $this->user_type;
  95. $user['starttime'] = TIMESTAMP;
  96. $user['openid'] = $openid;
  97. $user['register_type'] = USER_REGISTER_TYPE_QQ;
  98. $profile['avatar'] = $user_info['figureurl_qq_1'];
  99. $profile['nickname'] = strip_emoji($user_info['nickname']);
  100. $profile['gender'] = $user_info['gender'] == '女' ? 0 : 1;
  101. $profile['resideprovince'] = $user_info['province'];
  102. $profile['residecity'] = $user_info['city'];
  103. $profile['birthyear'] = $user_info['year'];
  104. return array(
  105. 'member' => $user,
  106. 'profile' => $profile
  107. );
  108. }
  109. public function register() {
  110. return true;
  111. }
  112. public function login() {
  113. load()->model('user');
  114. $user = $this->user();
  115. if (is_error($user)) {
  116. return $user;
  117. }
  118. $user_id = pdo_getcolumn('users', array('openid' => $user['member']['openid']), 'uid');
  119. $user_bind_info = table('users_bind')->getByTypeAndBindsign($user['member']['register_type'], $user['member']['openid']);
  120. if (!empty($user_id)) {
  121. return $user_id;
  122. }
  123. if (!empty($user_bind_info)) {
  124. return $user_bind_info['uid'];
  125. }
  126. if (!empty($user_id) && empty($user_bind_info)) {
  127. pdo_insert('users_bind', array('uid' => $user_id, 'bind_sign' => $user['member']['openid'], 'third_type' => $user['member']['register_type'], 'third_nickname' => $user['member']['username']));
  128. return $user_id;
  129. }
  130. return parent::user_register($user);
  131. }
  132. public function bind() {
  133. global $_W;
  134. $user = $this->user();
  135. $user_id = pdo_getcolumn('users', array('openid' => $user['member']['openid']), 'uid');
  136. $user_bind_info = table('users_bind')->getByTypeAndBindsign($user['member']['register_type'], $user['member']['openid']);
  137. if (!empty($user_id) || !empty($user_bind_info)) {
  138. return error(-1, '已被其他用户绑定,请更换账号');
  139. }
  140. pdo_insert('users_bind', array('uid' => $_W['uid'], 'bind_sign' => $user['member']['openid'], 'third_type' => $user['member']['register_type'], 'third_nickname' => strip_emoji($user['profile']['nickname'])));
  141. return true;
  142. }
  143. public function unbind() {
  144. global $_GPC, $_W;
  145. $third_type = intval($_GPC['bind_type']);
  146. $bind_info = table('users_bind')->getByTypeAndUid($third_type, $_W['uid']);
  147. if (empty($bind_info)) {
  148. return error(-1, '已经解除绑定');
  149. }
  150. pdo_update('users', array('openid' => ''), array('uid' => $_W['uid']));
  151. pdo_delete('users_bind', array('uid' => $_W['uid'], 'third_type' => $third_type));
  152. return error(0, '成功');
  153. }
  154. public function isbind() {
  155. global $_W;
  156. $bind_info = table('users_bind')->getByTypeAndUid(USER_REGISTER_TYPE_QQ, $_W['uid']);
  157. return !empty($bind_info['bind_sign']);
  158. }
  159. }