<?php
/** 
 * UQ云商：	www.uqcms.com  
 * 联系QQ：	2200631718 
 * 可免费商用，需要留UQ云商链接作为交换，只首页底部留取即可，不影响使用。
 */

 class wchat { private static $uq0; public static function init($uq1){ if (is_null(self::$uq0)){ self::$uq0 = new self($uq1); } return self::$uq0; } function __construct($uq1) { $this->appid = $uq1['appid']; $this->appsecret = $uq1['appsecret']; $this->token = $uq1['token']; $this->encoding = $uq1['encoding']; $this->time_limit = 3600; } function get_code() { $uq2 = cfg['site_domain'].cfg['site_string']; $uq3 = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appid.'&redirect_uri='.urlencode($uq2).'&response_type=code&scope=snsapi_base&state=123456#wechat_redirect'; header("Location:".$uq3); } function openid($uq4 = null) { if(!empty($uq4)){ $uq5 = curl::get('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->appsecret.'&code='.$uq4.'&grant_type=authorization_code'); $uq6 = json_decode($uq5); if(isset($uq6->errcode)){ $this->error($uq6); }else{ $_SESSION['connect']['wchat']['openid'] = $uq6->openid; return $uq6->openid; } }else{ return $_SESSION['connect']['wchat']['openid']; } } public function access_token() { $uq7 = 'temp/wchat/wchat_access_token.php'; if(file_exists(UQCMS_PATH.$uq7) && (time()-filemtime(UQCMS_PATH.$uq7) < '3600')){ return include UQCMS_PATH.$uq7; }else{ $uq3 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret; $uq5 = curl::get($uq3); $uq6 = json_decode($uq5); if(isset($uq6->errcode)){ $this->error($uq6); }else{ file::mk_dir($uq7, true); file::write_file($uq7, "<?php \n\r return '".$uq6->access_token."'; "); return $uq6->access_token; } } } function js_config() { $uq8['timestamp'] = time(); $uq8['noncestr'] = get_rand(32); $uq8['appid'] = $this->appid; $uq8['openid'] = $this->openid(); $uq8['signature'] = $this->js_sign($uq8); return $uq8; } function js_sign($uq9) { $uq10['jsapi_ticket'] = $this->js_ticket(); $uq10['noncestr'] = $uq9['noncestr']; $uq10['timestamp'] = $uq9['timestamp']; $uq10['url'] = cfg['current_page_url']; ksort($uq10); $uq11 = $this->toUrlParams($uq10); return sha1($uq11); } function js_ticket() { $uq7 = 'temp/wchat/wchat_js_ticket.php'; if(file_exists(UQCMS_PATH.$uq7) && (time()-filemtime(UQCMS_PATH.$uq7) < '3600')){ return include UQCMS_PATH.$uq7; }else{ $uq3 = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this->access_token().'&type=jsapi'; $uq5 = curl::get($uq3); $uq6 = json_decode($uq5); if($uq6->errcode == '0'){ file::mk_dir($uq7, true); file::write_file($uq7, "<?php \n\r return '".$uq6->ticket."'; "); return $uq6->ticket; }else{ $this->error($uq6); } } } private function toUrlParams($uq12) { $uq13 = ""; foreach($uq12 as $uq14=>$uq15){ if($uq14 != "sign"){ $uq13 .= $uq14 . "=" . $uq15 . "&"; } } $uq13 = trim($uq13, "&"); return $uq13; } function error($uq4) { if($uq4->errcode == '40163'){ $uq16 = '错误代码: '.$uq4->errcode.'，错误描述: '.$uq4->errmsg.'<br>处理办法：把当前的IP加入到微信白名单中。'; }elseif($uq4->errcode == '40164'){ $uq16 = '错误代码: '.$uq4->errcode.'，错误描述: '.$uq4->errmsg.'<br>处理办法：把当前的IP加入到微信白名单中。'; }elseif($uq4->errcode == '40001'){ $uq16 = '错误代码: '.$uq4->errcode.'，错误描述: '.$uq4->errmsg.'<br>处理办法：access_token过期，重新获取。'; }else{ $uq16 = '错误代码: '.$uq4->errcode.'，错误描述: '.$uq4->errmsg.'<br>'; } error_msg($uq16); } public function user_info() { $uq3 ="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token()."&openid=".$this->openid()."&lang=zh_CN"; $uq5 = curl::get($uq3); $uq6 = json_decode($uq5); if(isset($uq6->errcode)){ $this->error($uq6); }else{ return [ 'nick'=>$uq6->nickname, 'avatar'=>$uq6->headimgurl ]; } } } 