人人商城

wechat.class.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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'] = USER_TYPE_COMMON;
  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. );
  71. }
  72. protected function requestApi($url, $post = '') {
  73. $response = ihttp_request($url, $post);
  74. $result = @json_decode($response['content'], true);
  75. if(is_error($response)) {
  76. return error($result['errcode'], "访问公众平台接口失败, 错误详情: {$result['errmsg']}");
  77. }
  78. if(empty($result)) {
  79. return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  80. } elseif(!empty($result['errcode'])) {
  81. return error($result['errcode'], "访问公众平台接口失败, 错误: {$result['errmsg']}");
  82. }
  83. return $result;
  84. }
  85. public function register() {
  86. return true;
  87. }
  88. public function login() {
  89. load()->model('user');
  90. $user = $this->user();
  91. if (is_error($user)) {
  92. return $user;
  93. }
  94. if (is_error($user)) {
  95. return $user;
  96. }
  97. $user_table = table('users');
  98. $user_id = pdo_getcolumn('users', array('openid' => $user['member']['openid']), 'uid');
  99. $user_bind_info = $user_table->userBindInfo($user['member']['openid'], $user['member']['register_type']);
  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. return $user_id;
  109. }
  110. return parent::user_register($user);
  111. }
  112. public function bind() {
  113. global $_W;
  114. $user = $this->user();
  115. $user_table = table('users');
  116. $user_id = pdo_getcolumn('users', array('openid' => $user['member']['openid']), 'uid');
  117. $user_bind_info = $user_table->userBindInfo($user['member']['openid'], $user['member']['register_type']);
  118. if (!empty($user_id) || !empty($user_bind_info)) {
  119. return error(-1, '已被其他用户绑定,请更换账号');
  120. }
  121. 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'])));
  122. return true;
  123. }
  124. public function unbind() {
  125. global $_GPC, $_W;
  126. $user_table = table('users');
  127. $third_type = $_GPC['bind_type'];
  128. $user_table->bindSearchWithUser($_W['uid']);
  129. $user_table->bindSearchWithType($third_type);
  130. $bind_info = $user_table->bindInfo();
  131. if (empty($bind_info)) {
  132. return error(-1, '已经解除绑定');
  133. }
  134. pdo_update('users', array('openid' => ''), array('uid' => $_W['uid']));
  135. pdo_delete('users_bind', array('uid' => $_W['uid'], 'third_type' => $third_type));
  136. return error(0, '成功');
  137. }
  138. }