123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace weixin;
- class Wx
- {
- private $Appid;
- private $Appsecret;
- protected $access_token;
- public function __construct()
- {
- $this->Appid =sysconfig('wchat.wxsub_appid');
- $this->Appsecret =sysconfig('wchat.wxsub_secret');
- $this->getAccessToken();
-
- }
-
-
- public function getAccessToken()
- {
- $this->access_token=cache('client_credential_token');
- if(empty($this->access_token)){
- $tokenData = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->Appid.'&secret='.$this->Appsecret);
- $tokenData = json_decode($tokenData,true);
- if(isset($tokenData['access_token'])){
- $this->access_token = $tokenData['access_token'];
- cache('client_credential_token',$this->access_token,5400);
- }
- }
- return $this->access_token;
- }
-
-
- public function getUnionId($openid){
- $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
- $result = file_get_contents($url);
- $result = json_decode($result,true);
- if(false===isset($result['errcode']) && $result['subscribe']!=0){
- return $result['unionid'];
- }else{
- return "";
- }
- }
-
-
- public function GetUserinfo(string $openid='')
- {
- $result=file_get_contents('https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$this->access_token.'&openid='.$openid.'&lang=zh_CN');
- return json_decode($result,true);
- }
-
-
- public function CreateQrcode($uid)
- {
- $rwm_file = runtime_path().'rwm'.DS.base64_encode('generalize_'.$uid).'.jpg';
- $json='{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$uid.'}}}';
- $result=Wxcommon::postXmlCurl($json,'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$this->access_token);
- WLog('wx_create_qrcode',$result);
- $result=json_decode($result,true);
- $result=file_get_contents('https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$result['ticket']);
- file_put_contents($rwm_file,$result);
- return true;
- }
-
-
- public function JsapiTicket()
- {
- $result=file_get_contents('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this->access_token.'&type=jsapi');
- $result=json_decode($result,true);
- return $result['ticket'];
- }
-
-
- public function GetSign($url)
- {
- $data['noncestr']=GetRandStr(32);
- $data['jsapi_ticket']=$this->JsapiTicket();
- $data['timestamp']=time();
- $data['url']=$url;
- $data['sign']=Wxcommon::MakeSign_SHA1($data);
- $data['appid']=$this->Appid;
- return $data;
- }
-
-
- public function WxMenu($jsonmenu)
- {
- $URL="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->access_token;
- return Wxcommon::postXmlCurl($jsonmenu,$URL);
- }
-
-
- public function WxEvent($access_check=0)
- {
- $token = "o0Bfh608Q8XFn4SK9jYYIh";
- $postStr = file_get_contents("php://input");
-
- if($access_check==1){
- return input('echostr');
- }
-
- $result = \XmlToArray($postStr);
-
- if($result['MsgType']=='event'){
- if($result['Event']=='subscribe'){
-
- WLog('WxEvent',json_encode($result));
- $openid = $result['FromUserName'];
- $unionid= $this->getUnionId($openid);
-
- }else if($result['Event']=='unsubscribe'){
-
- WLog('WxEvent',json_encode($result));
-
- }else if($result['Event']=='CLICK'){
-
-
- }else if($result['Event']=='SCAN'){
-
-
- }
- }elseif($result['MsgType']=='text'){
- $to = $result['FromUserName'];
- $from = $result['ToUserName'];
- $text = $this->Config['wxreplay'];
- return "<xml>
- <ToUserName><![CDATA[$to]]></ToUserName>
- <FromUserName><![CDATA[$from]]></FromUserName>
- <CreateTime>{time()}</CreateTime>
- <MsgType><![CDATA[text]]></MsgType>
- <Content><![CDATA[$text]]></Content>
- </xml>";
- }
- return 0;
- }
-
-
- static public function checkSignature(string $signature,string $timestamp,string $nonce,string $token)
- {
- $tmpArr = array($token,$timestamp,$nonce);
- sort($tmpArr,SORT_STRING);
- $tmpStr = implode($tmpArr);
- $tmpStr = sha1($tmpStr);
- if($tmpStr == $signature){
- return true;
- }else{
- return false;
- }
- }
- }
- ?>
|