wechat.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. load()->func('communication');
  7. define('Wechat_PLATFORM_API_OAUTH_LOGIN_URL', 'https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect');
  8. define('Wechat_PLATFORM_API_GET_ACCESS_TOKEN', 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code');
  9. define('Wechat_PLATFORM_API_GET_USERINFO', 'https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN');
  10. class Wechat extends OAuth2Client {
  11. private $calback_url;
  12. public function __construct($ak, $sk, $calback_url = '') {
  13. global $_W;
  14. parent::__construct($ak, $sk);
  15. $this->calback_url = $_W['siteroot'] . 'web/index.php';
  16. $this->stateParam['from'] = 'wechat';
  17. }
  18. public function showLoginUrl($calback_url = '') {
  19. $redirect_uri = urlencode($this->calback_url);
  20. $state = $this->stateParam();
  21. return sprintf(Wechat_PLATFORM_API_OAUTH_LOGIN_URL, $this->ak, $redirect_uri, $state);
  22. }
  23. public function getUserInfo($token, $openid) {
  24. if (empty($openid) || empty($token)) {
  25. return error(-1, '参数错误');
  26. }
  27. $user_info_url = sprintf(Wechat_PLATFORM_API_GET_USERINFO, $token, $openid);
  28. $response = $this->requestApi($user_info_url);
  29. return $response;
  30. }
  31. public function getOauthInfo() {
  32. global $_GPC, $_W;
  33. $state = $_GPC['state'];
  34. $code = $_GPC['code'];
  35. if (empty($state) || empty($code)) {
  36. return error(-1, '参数错误');
  37. }
  38. $local_state = $this->stateParam();
  39. if ($state != $local_state) {
  40. return error(-1, '重新登陆');
  41. }
  42. $access_url = sprintf(Wechat_PLATFORM_API_GET_ACCESS_TOKEN, $this->ak, $this->sk, $code, urlencode($this->calback_url));
  43. $response = $this->requestApi($access_url);
  44. return $response;
  45. }
  46. public function user() {
  47. $oauth_info = $this->getOauthInfo();
  48. $openid = $oauth_info['openid'];
  49. $user_info = $this->getUserInfo($oauth_info['access_token'], $openid);
  50. if (is_error($user_info)) {
  51. return $user_info;
  52. }
  53. $user = array();
  54. $profile = array();
  55. $user['username'] = strip_emoji($user_info['nickname']);
  56. $user['password'] = '';
  57. $user['type'] = $this->user_type;
  58. $user['starttime'] = TIMESTAMP;
  59. $user['openid'] = $user_info['openid'];
  60. $user['register_type'] = USER_REGISTER_TYPE_WECHAT;
  61. $profile['avatar'] = $user_info['headimgurl'];
  62. $profile['nickname'] = $user_info['nickname'];
  63. $profile['gender'] = $user_info['sex'];
  64. $profile['resideprovince'] = $user_info['province'];
  65. $profile['residecity'] = $user_info['city'];
  66. $profile['birthyear'] = '';
  67. return array(
  68. 'member' => $user,
  69. 'profile' => $profile,
  70. 'unionid' => empty($user_info['unionid']) ? '' : $user_info['unionid'],
  71. );
  72. }
  73. protected function requestApi($url, $post = '') {
  74. $response = ihttp_request($url, $post);
  75. $result = @json_decode($response['content'], true);
  76. if(is_error($response)) {
  77. return error($result['errcode'], "访问公众平台接口失败, 错误详情: {$result['errmsg']}");
  78. }
  79. if(empty($result)) {
  80. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  81. } elseif(!empty($result['errcode'])) {
  82. return error($result['errcode'], "访问公众平台接口失败, 错误: {$result['errmsg']}");
  83. }
  84. return $result;
  85. }
  86. public function register() {
  87. return true;
  88. }
  89. public function login() {
  90. load()->model('user');
  91. $user = $this->user();
  92. if (is_error($user)) {
  93. return $user;
  94. }
  95. if (is_error($user)) {
  96. return $user;
  97. }
  98. $user_id = pdo_getcolumn('users', array('openid' => $user['member']['openid']), 'uid');
  99. $user_bind_info = table('users_bind')->getByTypeAndBindsign($user['member']['register_type'], $user['member']['openid']);
  100. if (!empty($user_id)) {
  101. return $user_id;
  102. }
  103. if (!empty($user_bind_info)) {
  104. return $user_bind_info['uid'];
  105. }
  106. if (!empty($user_id) && empty($user_bind_info)) {
  107. pdo_insert('users_bind', array('uid' => $user_id, 'bind_sign' => $user['member']['openid'], 'third_type' => $user['member']['register_type'], 'third_nickname' => $user['member']['username']));
  108. if (!empty($user['unionid'])) {
  109. pdo_insert('users_bind', array('uid' => $user_id, 'bind_sign' => $user['unionid'], 'third_type' => USER_REGISTER_TYPE_OPEN_WECHAT, 'third_nickname' => ''));
  110. }
  111. return $user_id;
  112. }
  113. return parent::user_register($user);
  114. }
  115. public function bind() {
  116. global $_W;
  117. $user = $this->user();
  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) || !empty($user_bind_info)) {
  121. return error(-1, '已被其他用户绑定,请更换账号');
  122. }
  123. 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'])));
  124. if (!empty($user['unionid'])) {
  125. pdo_insert('users_bind', array('uid' => $_W['uid'], 'bind_sign' => $user['unionid'], 'third_type' => USER_REGISTER_TYPE_OPEN_WECHAT, 'third_nickname' => ''));
  126. }
  127. return true;
  128. }
  129. public function unbind() {
  130. global $_GPC, $_W;
  131. $third_type = intval($_GPC['bind_type']);
  132. $bind_info = table('users_bind')->getByTypeAndUid($third_type, $_W['uid']);
  133. if (empty($bind_info)) {
  134. return error(-1, '已经解除绑定');
  135. }
  136. pdo_update('users', array('openid' => ''), array('uid' => $_W['uid']));
  137. pdo_delete('users_bind', array('uid' => $_W['uid'], 'third_type' => $third_type));
  138. if ($third_type == USER_REGISTER_TYPE_WECHAT) {
  139. pdo_delete('users_bind', array('uid' => $_W['uid'], 'third_type' => USER_REGISTER_TYPE_OPEN_WECHAT));
  140. }
  141. return error(0, '成功');
  142. }
  143. public function isbind() {
  144. global $_W;
  145. $bind_info = table('users_bind')->getByTypeAndUid(array(USER_REGISTER_TYPE_WECHAT, USER_REGISTER_TYPE_OPEN_WECHAT), $_W['uid']);
  146. return !empty($bind_info['bind_sign']);
  147. }
  148. }