<?php
/**
 * [UQcms System] Copyright uqcms.com
 * QQ: 2200631718
 * Author: gj.name
 * This is NOT a freeware
*/
 class wchat { private static $instance; public static function init(){ if (is_null(self::$instance)){ self::$instance = new self(); } return self::$instance; } var $time_limit = 3600; function __construct(){ $this->appid = cfg('wchat_appid'); $this->appsecret = cfg('wchat_appsecret'); $this->token = cfg('wchat_token'); $this->encoding = cfg('wchat_encoding'); } function get_code(){ $query_string = $_SERVER['QUERY_STRING']; if($query_string){ $redirect_uri = cfg('site_url').'wap.php?'.$_SERVER['QUERY_STRING']; }else{ $redirect_uri = cfg('site_url').'wap.php'; } $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appid.'&redirect_uri='.urlencode($redirect_uri).'&response_type=code&scope=snsapi_base&state=123456#wechat_redirect'; header("Location:".$url); } function openid($code){ $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->appsecret.'&code='.$code.'&grant_type=authorization_code'; $result = curl::get($url); $con = json_decode($result); if(isset($con->errcode)){ $this->error($con); }else{ return $con->openid; } } public function access_token(){ $token_time = @$_SESSION['connect']['wchat']['lifetime']; $access_token = @$_SESSION['connect']['wchat']['access_token']; if(!empty($access_token) && ((time() - $token_time) < $this->time_limit)){ return $access_token; }else{ $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret; $result = curl::get($url); $con = json_decode($result); if(isset($con->errcode)){ $this->error($con); }else{ $_SESSION['connect']['wchat']['access_token'] = $con->access_token; $_SESSION['connect']['wchat']['lifetime'] = time(); return $con->access_token; } } } function js_config($openid = null){ if(empty($openid)){ $openid = $_SESSION['connect']['wchat']['openid']; } $jsArr['timestamp'] = time(); $jsArr['noncestr'] = get_rand(32); $jsArr['appid'] = cfg('wchat_appid'); $jsArr['openid'] = $openid; $jsArr['signature'] = $this->js_sign($jsArr); return $jsArr; } function js_sign($data){ $param['jsapi_ticket'] = $this->js_ticket(); $param['noncestr'] = $data['noncestr']; $param['timestamp'] = $data['timestamp']; $param['url'] = cfg('current_page_url'); ksort($param); $params = $this->toUrlParams($param); return sha1($params); } function js_ticket(){ $token_time = @$_SESSION['connect']['wchat']['lifetime']; $ticket = @$_SESSION['connect']['wchat']['ticket']; if(!empty($ticket) && ((time() - $token_time) < $this->time_limit)){ return $ticket; }else{ $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this->access_token().'&type=jsapi'; $result = curl::get($url); $con = json_decode($result); if($con->errcode == '0'){ $ticket = $con->ticket; @$_SESSION['connect']['wchat']['ticket'] = $ticket; return $ticket; }else{ $this->error($con); } } } private function toUrlParams($urlObj){ $buff = ""; foreach($urlObj as $k=>$v){ if($k != "sign"){ $buff .= $k . "=" . $v . "&"; } } $buff = trim($buff, "&"); return $buff; } function error($code){ if($code->errcode == '40163'){ $info = '错误代码: '.$code->errcode.'，错误描述: '.$code->errmsg.'<br>处理办法：把当前的IP加入到微信白名单中。'; }elseif($code->errcode == '40164'){ $info = '错误代码: '.$code->errcode.'，错误描述: '.$code->errmsg.'<br>处理办法：把当前的IP加入到微信白名单中。'; }elseif($code->errcode == '40001'){ $info = '错误代码: '.$code->errcode.'，错误描述: '.$code->errmsg.'<br>处理办法：access_token过期，重新获取。'; }else{ $info = '错误代码: '.$code->errcode.'，错误描述: '.$code->errmsg.'<br>'; } error_msg($info); } public function user_info($openid = null){ if(empty($openid)){ $openid = $_SESSION['connect']['wchat']['openid']; } $access_token = $this->access_token(); $url ="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN"; $result = curl::get($url); $con = json_decode($result); if(isset($con->errcode)){ $this->error($con); }else{ return array( 'nick'=>$con->nickname, 'avatar'=>$con->headimgurl, 'sex'=>$con->sex ); } } }