123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace weixin;
- /**
- * @title : 微信公众号基础接口
- * @desc :
- * @Author : Rock
- * @Date : 2023-04-06 19:06:59
- */
- class Wx
- {
- private $Appid;
- private $Appsecret;
- protected $access_token;
- public function __construct()
- {
- $this->Appid =sysconfig('wchat.wxsub_appid');// 公众号APPID
- $this->Appsecret =sysconfig('wchat.wxsub_secret');// 公众号SECRET
- $this->getAccessToken();
-
- }
- /**
- * @title: 获取AccessToken凭据
- * @desc: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 19:10:42
- */
- 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;
- }
- /**
- * @title: 获取unionid
- * @desc: 获取公众号openid关联的unionid
- * @param {string} {openid} {} {公众号openid}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-08 08:57:39
- */
- 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 "";
- }
- }
- /**
- * @title: 获得微信用户基本信息
- * @desc: 描述
- * @param {string} {openid} {} {公众号用户的OPENID}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 19:11:33
- */
- 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);
- }
- /**
- * @title: 创建唯一带参数的公众号二维码
- * @desc: 描述
- * @param {int} {uid} {} {用户ID}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 19:12:28
- */
- 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;
- }
- /**
- * @title: 获得JSSDK票据数据
- * @desc: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 19:27:56
- */
- 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'];
- }
- /**
- * @title: 获得分享页面签名参数
- * @desc: 描述
- * @param {*} $url
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 19:28:07
- */
- 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;
- }
- /**
- * @title: 创建公众号菜单
- * @desc: 描述
- * @param {*} {jsonmenu} {} {菜单数据}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 19:28:32
- * @example
- $jsonmenu = '{
- "button":[
- {
- "type":"view",
- "name":"预约服务",
- "url":"http://sjjd.idaorui.com/"
- }
- ]
- }';
-
- */
- public function WxMenu($jsonmenu)
- {
- $URL="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->access_token;
- return Wxcommon::postXmlCurl($jsonmenu,$URL);
- }
- /**
- * @title: 微信公众号服务器事件处理
- * @desc: 描述
- * @param {*} {access_check} {} {接入验证}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 19:34:39
- */
- 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;
- }
- /**
- * @title: 验证访问合法性
- * @desc: 描述
- * @param {string} {signature} {} {加密签名}
- * @param {string} {timestamp} {} {时间戳}
- * @param {string} {nonce} {} {随机数}
- * @param {string} {echostr} {} {随机字符串}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-07 16:21:16
- */
- 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;
- }
- }
- }
- ?>
|