Wx.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace weixin;
  3. /**
  4. * @title : 微信公众号基础接口
  5. * @desc :
  6. * @Author : Rock
  7. * @Date : 2023-04-06 19:06:59
  8. */
  9. class Wx
  10. {
  11. private $Appid;
  12. private $Appsecret;
  13. protected $access_token;
  14. public function __construct()
  15. {
  16. $this->Appid =sysconfig('wchat.wxsub_appid');// 公众号APPID
  17. $this->Appsecret =sysconfig('wchat.wxsub_secret');// 公众号SECRET
  18. $this->getAccessToken();
  19. }
  20. /**
  21. * @title: 获取AccessToken凭据
  22. * @desc: 描述
  23. * @return {*}
  24. * @author: Rock
  25. * @method: POST
  26. * @Date: 2023-04-06 19:10:42
  27. */
  28. public function getAccessToken()
  29. {
  30. $this->access_token=cache('client_credential_token');
  31. if(empty($this->access_token)){
  32. $tokenData = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->Appid.'&secret='.$this->Appsecret);
  33. $tokenData = json_decode($tokenData,true);
  34. if(isset($tokenData['access_token'])){
  35. $this->access_token = $tokenData['access_token'];
  36. cache('client_credential_token',$this->access_token,5400);
  37. }
  38. }
  39. return $this->access_token;
  40. }
  41. /**
  42. * @title: 获取unionid
  43. * @desc: 获取公众号openid关联的unionid
  44. * @param {string} {openid} {} {公众号openid}
  45. * @return {*}
  46. * @author: Rock
  47. * @method: POST
  48. * @Date: 2023-04-08 08:57:39
  49. */
  50. public function getUnionId($openid){
  51. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN";
  52. $result = file_get_contents($url);
  53. $result = json_decode($result,true);
  54. if(false===isset($result['errcode']) && $result['subscribe']!=0){
  55. return $result['unionid'];
  56. }else{
  57. return "";
  58. }
  59. }
  60. /**
  61. * @title: 获得微信用户基本信息
  62. * @desc: 描述
  63. * @param {string} {openid} {} {公众号用户的OPENID}
  64. * @return {*}
  65. * @author: Rock
  66. * @method: POST
  67. * @Date: 2023-04-06 19:11:33
  68. */
  69. public function GetUserinfo(string $openid='')
  70. {
  71. $result=file_get_contents('https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$this->access_token.'&openid='.$openid.'&lang=zh_CN');
  72. return json_decode($result,true);
  73. }
  74. /**
  75. * @title: 创建唯一带参数的公众号二维码
  76. * @desc: 描述
  77. * @param {int} {uid} {} {用户ID}
  78. * @return {*}
  79. * @author: Rock
  80. * @method: POST
  81. * @Date: 2023-04-06 19:12:28
  82. */
  83. public function CreateQrcode($uid)
  84. {
  85. $rwm_file = runtime_path().'rwm'.DS.base64_encode('generalize_'.$uid).'.jpg';
  86. $json='{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$uid.'}}}';
  87. $result=Wxcommon::postXmlCurl($json,'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$this->access_token);
  88. WLog('wx_create_qrcode',$result);
  89. $result=json_decode($result,true);
  90. $result=file_get_contents('https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$result['ticket']);
  91. file_put_contents($rwm_file,$result);
  92. return true;
  93. }
  94. /**
  95. * @title: 获得JSSDK票据数据
  96. * @desc: 描述
  97. * @return {*}
  98. * @author: Rock
  99. * @method: POST
  100. * @Date: 2023-04-06 19:27:56
  101. */
  102. public function JsapiTicket()
  103. {
  104. $result=file_get_contents('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this->access_token.'&type=jsapi');
  105. $result=json_decode($result,true);
  106. return $result['ticket'];
  107. }
  108. /**
  109. * @title: 获得分享页面签名参数
  110. * @desc: 描述
  111. * @param {*} $url
  112. * @return {*}
  113. * @author: Rock
  114. * @method: POST
  115. * @Date: 2023-04-06 19:28:07
  116. */
  117. public function GetSign($url)
  118. {
  119. $data['noncestr']=GetRandStr(32);
  120. $data['jsapi_ticket']=$this->JsapiTicket();
  121. $data['timestamp']=time();
  122. $data['url']=$url;
  123. $data['sign']=Wxcommon::MakeSign_SHA1($data);
  124. $data['appid']=$this->Appid;
  125. return $data;
  126. }
  127. /**
  128. * @title: 创建公众号菜单
  129. * @desc: 描述
  130. * @param {*} {jsonmenu} {} {菜单数据}
  131. * @return {*}
  132. * @author: Rock
  133. * @method: POST
  134. * @Date: 2023-04-06 19:28:32
  135. * @example
  136. $jsonmenu = '{
  137. "button":[
  138. {
  139. "type":"view",
  140. "name":"预约服务",
  141. "url":"http://sjjd.idaorui.com/"
  142. }
  143. ]
  144. }';
  145. */
  146. public function WxMenu($jsonmenu)
  147. {
  148. $URL="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->access_token;
  149. return Wxcommon::postXmlCurl($jsonmenu,$URL);
  150. }
  151. /**
  152. * @title: 微信公众号服务器事件处理
  153. * @desc: 描述
  154. * @param {*} {access_check} {} {接入验证}
  155. * @return {*}
  156. * @author: Rock
  157. * @method: POST
  158. * @Date: 2023-04-06 19:34:39
  159. */
  160. public function WxEvent($access_check=0)
  161. {
  162. $token = "o0Bfh608Q8XFn4SK9jYYIh";
  163. $postStr = file_get_contents("php://input");
  164. //接入验证
  165. if($access_check==1){
  166. return input('echostr');
  167. }
  168. //回调数据写入日志
  169. $result = \XmlToArray($postStr);
  170. //接收事件
  171. if($result['MsgType']=='event'){
  172. if($result['Event']=='subscribe'){
  173. //关注事件
  174. WLog('WxEvent',json_encode($result));
  175. $openid = $result['FromUserName'];
  176. $unionid= $this->getUnionId($openid);
  177. }else if($result['Event']=='unsubscribe'){
  178. //取关事件
  179. WLog('WxEvent',json_encode($result));
  180. }else if($result['Event']=='CLICK'){
  181. //点击事件
  182. }else if($result['Event']=='SCAN'){
  183. //扫码事件
  184. }
  185. }elseif($result['MsgType']=='text'){
  186. $to = $result['FromUserName'];
  187. $from = $result['ToUserName'];
  188. $text = $this->Config['wxreplay'];
  189. return "<xml>
  190. <ToUserName><![CDATA[$to]]></ToUserName>
  191. <FromUserName><![CDATA[$from]]></FromUserName>
  192. <CreateTime>{time()}</CreateTime>
  193. <MsgType><![CDATA[text]]></MsgType>
  194. <Content><![CDATA[$text]]></Content>
  195. </xml>";
  196. }
  197. return 0;
  198. }
  199. /**
  200. * @title: 验证访问合法性
  201. * @desc: 描述
  202. * @param {string} {signature} {} {加密签名}
  203. * @param {string} {timestamp} {} {时间戳}
  204. * @param {string} {nonce} {} {随机数}
  205. * @param {string} {echostr} {} {随机字符串}
  206. * @return {*}
  207. * @author: Rock
  208. * @method: POST
  209. * @Date: 2023-04-07 16:21:16
  210. */
  211. static public function checkSignature(string $signature,string $timestamp,string $nonce,string $token)
  212. {
  213. $tmpArr = array($token,$timestamp,$nonce);
  214. sort($tmpArr,SORT_STRING);
  215. $tmpStr = implode($tmpArr);
  216. $tmpStr = sha1($tmpStr);
  217. if($tmpStr == $signature){
  218. return true;
  219. }else{
  220. return false;
  221. }
  222. }
  223. }
  224. ?>