redirect_url.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <meta charset="utf-8">
  2. <?php
  3. //redirect_url?auth_code=xxx&expires_in=600
  4. define("TOKEN", "eqcxcx");
  5. define("AppID", "wxd6972323f37bfdf0");//Appsecret
  6. define("Appsecret", "e7c30a645edec78aa64d30a92024237b");
  7. define("EncodingAESKey", "53o42sbbzslie4tjbll3ta5zlwe11rrk53o42sbbzsl");
  8. $auth_code= $_GET['auth_code'];
  9. $ComponentVerifyTicket = file_get_contents('json/VerifyTicket.json');
  10. $token= getToken(AppID,Appsecret,$ComponentVerifyTicket,'11111');
  11. $data='{
  12. "component_appid":"'.AppID.'" ,
  13. "authorization_code": "'.$auth_code.'"
  14. }';
  15. //使用授权码换取公众号或小程序的接口调用凭据和授权信息
  16. $url="https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=$token";
  17. //print_r($url);
  18. //echo "<br>";
  19. $authorization=http_post_data($url,$data);
  20. $auth=json_decode($authorization,true);
  21. // print_r($authorization);
  22. $authapp=$auth['authorization_info']['authorizer_appid'];//拿到被授权公众号的APPid
  23. //获取公众号信息
  24. $url1="https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=$token";
  25. $data='{
  26. "component_appid":"'.AppID.'" ,
  27. "authorizer_appid": "'.$authapp.'"
  28. }';
  29. // echo "<br>";
  30. $authorizer_info=http_post_data($url1,$data);
  31. // print_r($authorizer_info);
  32. $info=json_decode($authorizer_info,true);
  33. $name=$info['authorizer_info']['nick_name'];//公众号名称
  34. if(trim($name)==''){
  35. print_r("超时,请返回重新授权.");
  36. exec("rm -f ".'jsonX/'.$AppID.'_token.json');
  37. exit;
  38. }
  39. $head_img=$info['authorizer_info']['head_img'];//公众号图片
  40. // echo "<img src='$head_img'>";
  41. $user_name=$info['authorizer_info']['user_name'];//公众号图片
  42. //MiniProgramInfo
  43. $mini=$info['authorizer_info']['MiniProgramInfo'];//
  44. $renzheng=trim($info['authorizer_info']['verify_type_info']);
  45. // print_r("qx:".json_encode($info['authorization_info']['func_info'][0]));
  46. $quanxian=trim($info['authorization_info']['func_info'][0]['funcscope_category']['id']);
  47. // echo '<br> 权限:'.$quanxian;
  48. if (($quanxian==1) or ($quanxian==19)){
  49. // echo "已经获得消息权限.";
  50. }else{
  51. // echo "没获得消息权限.";
  52. }
  53. // echo "<br>认证状态:".$renzheng;
  54. if ($renzheng==0){
  55. $rz='已认证,';
  56. }else{
  57. $rz='未认证,不支持接入';
  58. }
  59. if(is_array($mini)){
  60. $leixing= "小程序";
  61. $huifu="进入小程序客服消息界面";
  62. }else{
  63. $leixing= "公众号";
  64. $huifu="进入公众号界面,切换到发文字模式";
  65. }
  66. //
  67. // print_r("<br>".$authorizer_info);
  68. if ($authapp<>''){
  69. $res=file_put_contents("jsonX/auth_".$authapp.".json", $authorization);
  70. $res=file_put_contents("jsonX/info_".$user_name.".json", $authorizer_info);
  71. $zhuangtai= "授权成功";
  72. }else{
  73. $zhuangtai= "授权失败";
  74. }
  75. $zhungtai= "您正在接入 $leixing : <font size=6 color=white>$name</font> ".$zhuangtai.$rz;
  76. if (file_exists("jsonX/info_".$user_name.".json")){
  77. $zhungtai.="对接生成!";
  78. }else{
  79. $zhungtai.="对接文件未生成,请重写对接!";
  80. }
  81. //判断有没有 后台对接文件
  82. if (file_exists("json/".$user_name.".json")){
  83. // $ok=true;
  84. $zhungtai.="<br>您已经配置相关对接文件.如需重新配置,请联系本站管理员.";
  85. }else{
  86. $ok=true;
  87. $zhungtai.="<br>现在开始设置管理员,请".$huifu.",发送<font size=10 color=red>openid</font> 把收到的回复内容,填入下面的空格";
  88. }
  89. ?>
  90. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  91. <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-cn">
  92. <head>
  93. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  94. <title>智能客服-开通中</title>
  95. <style type="text/css">
  96. body
  97. {
  98. background-image: url('http://web.woshangtong.com/attachment/images/global/fh0HERD4p8mr8pEsMmM0Gb9hENm9M8.jpg');
  99. background-repeat: repeat-y;
  100. }
  101. .align-center{
  102. position:fixed;left:30%;top:30%;margin-left:width/2;margin-top:height/2;
  103. .input_control{{
  104. width:360px;
  105. margin:20px auto;
  106. }
  107. input[type="text"],#btn1,#btn2{
  108. box-sizing: border-box;
  109. text-align:center;
  110. font-size:1.4em;
  111. height:2.7em;
  112. border-radius:4px;
  113. border:1px solid #c8cccf;
  114. color:#6a6f77;
  115. -web-kit-appearance:none;
  116. -moz-appearance: none;
  117. display:block;
  118. outline:0;
  119. padding:0 1em;
  120. text-decoration:none;
  121. width:100%;
  122. }
  123. input[type="text"]:focus{
  124. border:1px solid #ff7496;
  125. }
  126. }
  127. </style>
  128. </head>
  129. <body>
  130. <div class="align-center">
  131. <table >
  132. <tr>
  133. <th>
  134. <img width='200' src='<?=$head_img?>'>
  135. </th>
  136. </tr>
  137. <tr>
  138. <th>
  139. <br>
  140. </th>
  141. </tr>
  142. <tr>
  143. <th>
  144. <?=$zhungtai?>
  145. </th>
  146. </tr>
  147. <?if($ok){?>
  148. <tr>
  149. <th>
  150. <form action="sq_kefu.php" method="post" >
  151. <input type="hidden" name="user_name" value="<?=$user_name?>" >
  152. <input style="padding:8px 6px;" type="text" name="openid" size="35" value="" >
  153. <input style="padding:8px 6px;" name="submit" type="submit" value="授权" onclick="return confirm('请确认openid输入正确,确定后不能更改');return false;" >
  154. </form>
  155. </th>
  156. <?}?>
  157. </tr>
  158. </table>
  159. </div>
  160. </body>
  161. </html>
  162. <?php
  163. // echo "<br>";
  164. // echo $_GET['expires_in'];
  165. traceHttp();
  166. function traceHttp()
  167. {
  168. logger("\n\nREMOTE_ADDR:".$_SERVER["REMOTE_ADDR"].(strstr($_SERVER["REMOTE_ADDR"],'101.226')? " FROM WeiXin": "Unknown IP"));
  169. logger("QUERY_STRING:".$_SERVER["QUERY_STRING"]);
  170. }
  171. function logger($log_content)
  172. {
  173. if(isset($_SERVER['HTTP_APPNAME'])){ //SAE
  174. sae_set_display_errors(false);
  175. sae_debug($log_content);
  176. sae_set_display_errors(true);
  177. }else{ //LOCAL
  178. $max_size = 500000;
  179. $log_filename = "log_wx.xml";
  180. if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
  181. file_put_contents($log_filename, date('Y-m-d H:i:s').$log_content."\r\n", FILE_APPEND);
  182. }
  183. }
  184. function http_post_data($url, $data_string) {
  185. $ch = curl_init();
  186. curl_setopt($ch, CURLOPT_POST, 1);
  187. curl_setopt($ch, CURLOPT_URL, $url);
  188. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  189. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  190. 'Content-Type: application/json; charset=utf-8',
  191. 'Content-Length: ' . strlen($data_string))
  192. );
  193. ob_start();
  194. curl_exec($ch);
  195. $return_content = ob_get_contents();
  196. //echo $return_content."<br>";
  197. ob_end_clean();
  198. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  199. // return array($return_code, $return_content);
  200. return $return_content;
  201. }
  202. function getToken($appid,$appsecret,$ComponentVerifyTicket,$jsonid){
  203. $file = file_get_contents("jsonX/{$appid}_token.json",true);
  204. $result = json_decode($file,true);
  205. if (time() > $result['expires']){
  206. $data = array();
  207. $data['component_access_token'] = getNewToken($appid,$appsecret,$ComponentVerifyTicket);
  208. $data['expires']=time()+6200;
  209. $jsonStr = json_encode($data);
  210. $fp = fopen("jsonX/{$appid}_token.json", "w");
  211. fwrite($fp, $jsonStr);
  212. fclose($fp);
  213. return $data['component_access_token'];
  214. }else{
  215. return $result['component_access_token'];
  216. }
  217. }
  218. function getNewToken($appid,$appsecret,$ComponentVerifyTicket){
  219. $data_string='{
  220. "component_appid":"'.$appid.'" ,
  221. "component_appsecret": "'.$appsecret.'",
  222. "component_verify_ticket": "'.$ComponentVerifyTicket.'"
  223. }';
  224. $access_token_Arr =http_post_data("https://api.weixin.qq.com/cgi-bin/component/api_component_token", $data_string);
  225. //echo "<br>---";
  226. $access_token_Arr=json_decode($access_token_Arr,true);
  227. return $access_token_Arr['component_access_token'];
  228. }
  229. ?>