sq_kefu.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /*
  3. 易企程客服平台
  4. CopyRight 2018 All Rights Reserved
  5. */
  6. define("TOKEN", "eqcxcx");
  7. define("AppID", "wxd6972323f37bfdf0");//Appsecret
  8. define("Appsecret", "e7c30a645edec78aa64d30a92024237b");
  9. define("EncodingAESKey", "53o42sbbzslie4tjbll3ta5zlwe11rrk53o42sbbzsl");
  10. $user_name = trim($_POST['user_name']);
  11. $openid = trim($_POST['openid']);
  12. //下发一条确认消息 .
  13. $ComponentVerifyTicket = file_get_contents('json/VerifyTicket.json');//yaoshi
  14. $Mtoken= getMToken(AppID,Appsecret,$ComponentVerifyTicket,'11111');//拿到自己的密钥
  15. //$this->logger(" M \r\n".$ComponentVerifyTicket);
  16. //$this->logger(" M \r\n".$Mtoken);
  17. $ToUserName=$user_name; // 获取当前公众号的name
  18. $info = json_decode(file_get_contents("jsonX/info_".$ToUserName.".json"),true);//读取设置文件.
  19. $mini=$info['authorizer_info']['MiniProgramInfo'];
  20. if (is_array($mini)) { $xcx= true; }else{ $xcx= false; }
  21. $name=$info['authorizer_info']['nick_name'];//公众号名称
  22. $signature=$info['authorizer_info']['signature'];//公众号名称 signature
  23. $head_img=$info['authorizer_info']['head_img'];//公众号图片
  24. $Cappid=$info['authorization_info']['authorizer_appid'];
  25. $refresh_token=$info['authorization_info']['authorizer_refresh_token'];
  26. $Atoken= getCToken(AppID,$Cappid,$refresh_token,$Mtoken);
  27. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$Atoken;
  28. $aaa=sendmsg($openid,'text','收到本信息,表明您的设置已经成功!可以使用啦','',$url);//给用户发消息.
  29. if (json_decode($aaa, true)['errcode']==0){
  30. //发送成功!设置路径
  31. //
  32. $arr=array(
  33. 'uniacid' => $ToUserName,
  34. 'bakuniacid' => 149,
  35. 'website' => 'http://'.$_SERVER['HTTP_HOST'],
  36. 'openids' => $openid,
  37. '结束语' => '感谢您访问'.$name
  38. );
  39. $res=file_put_contents("json/".$ToUserName.".json", json_encode($arr));
  40. $kefuarr=array($openid=>"");
  41. $kefulistarr=array($openid=>"客服");
  42. $res=file_put_contents("json/".$ToUserName."_kefu.json", json_encode($kefuarr));
  43. $res=file_put_contents("json/".$ToUserName."_kefulist.json", json_encode($kefulistarr));
  44. if(file_exists("json/".$ToUserName."_kefulist.json")){
  45. $bakmsg="设置成功!";
  46. $path="jsonX/zhucelist.json";
  47. $data=array('ToUserName'=>$ToUserName,'name'=>$name,'head_img'=>$head_img);
  48. savejson($path,json_encode($data));
  49. }
  50. else{$bakmsg="文件创建失败,请检查权限!";}
  51. }else{
  52. //openid不对 .提示重新发送.
  53. $bakmsg= "openid不对,请重新设置.";
  54. }
  55. ?>
  56. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  57. <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-cn">
  58. <head>
  59. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  60. <title>智能客服-绑定客服</title>
  61. <style type="text/css">
  62. .center
  63. {
  64. margin-left:auto;
  65. opacity:0.5;
  66. width:70%;
  67. background-color:#b0e0e6;
  68. }
  69. body
  70. {
  71. background-image: url('http://web.woshangtong.com/attachment/images/global/fh0HERD4p8mr8pEsMmM0Gb9hENm9M8.jpg');
  72. background-repeat: repeat-y;
  73. }
  74. .align-center{
  75. position:fixed;left:10%;top:30%;margin-left:width/2;margin-top:height/2;
  76. }
  77. </style>
  78. </head>
  79. <body>
  80. <div class="align-center">
  81. <table >
  82. <tr>
  83. <th>
  84. <img width='200' src='logo.png'>
  85. </th>
  86. <th width=400 >
  87. <div class="center">
  88. <br><?if($bakmsg=="设置成功!"){?>
  89. 常用命令:<br>
  90. 下一个:接通下一个客户<br>
  91. 清零 :清除排队用户<br>
  92. 排队 :查看排队用户<br>
  93. </div>
  94. </th>
  95. <th>
  96. <div class="center">
  97. 高级版功能:<br>
  98. 多客服席位:可设置多个客服同时接待<br>
  99. 自定义回复:可设置多个自动回复内容,支持:文字/图片/图文/小程序<br>
  100. 群发功能:可对有效会话期间的所有用户群发.<br>
  101. 私聊功能:可主动发起对某个用户的聊天<br>
  102. 专属客服支持:客服在线答疑.<br>
  103. 每年仅需300元起,即可立即享有本服务.开通联系QQ:511079021<br>
  104. 本系统正在微擎热卖:<a href='https://s.we7.cc/module-9528.html'>点此购买</a>
  105. </div>
  106. <?}?>
  107. </th>
  108. <th>
  109. <font size=20 color=red><?=$bakmsg?></font>
  110. </th>
  111. </tr>
  112. </table>
  113. </div>
  114. </body>
  115. </html>
  116. <?php
  117. function http_post_data($url, $data_string) {
  118. $ch = curl_init();
  119. curl_setopt($ch, CURLOPT_POST, 1);
  120. curl_setopt($ch, CURLOPT_URL, $url);
  121. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  122. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  123. 'Content-Type: application/json; charset=utf-8',
  124. 'Content-Length: ' . strlen($data_string))
  125. );
  126. ob_start();
  127. curl_exec($ch);
  128. $return_content = ob_get_contents();
  129. //echo $return_content."<br>";
  130. ob_end_clean();
  131. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  132. // return array($return_code, $return_content);
  133. return $return_content;
  134. }
  135. function getMToken($appid,$appsecret,$ComponentVerifyTicket,$jsonid){
  136. $file = file_get_contents("jsonX/{$appid}_token.json",true);
  137. $result = json_decode($file,true);
  138. if (time() > $result['expires']){
  139. $data = array();
  140. $data['component_access_token'] = getNewToken($appid,$appsecret,$ComponentVerifyTicket);
  141. $data['expires']=time()+7200;
  142. $jsonStr = json_encode($data);
  143. $fp = fopen("jsonX/{$appid}_token.json", "w");
  144. fwrite($fp, $jsonStr);
  145. fclose($fp);
  146. return $data['component_access_token'];
  147. }else{
  148. return $result['component_access_token'];
  149. }
  150. }
  151. function getNewToken($appid,$appsecret,$ComponentVerifyTicket){
  152. $data_string='{
  153. "component_appid":"'.$appid.'" ,
  154. "component_appsecret": "'.$appsecret.'",
  155. "component_verify_ticket": "'.$ComponentVerifyTicket.'"
  156. }';
  157. $access_token_Arr = http_post_data("https://api.weixin.qq.com/cgi-bin/component/api_component_token", $data_string);
  158. //echo "<br>---";
  159. $access_token_Arr=json_decode($access_token_Arr,true);
  160. return $access_token_Arr['component_access_token'];
  161. }
  162. function getNewCToken($Mappid,$Cappid,$refresh_token,$Mtoken){
  163. $data_string='{
  164. "component_appid":"'.$Mappid.'",
  165. "authorizer_appid":"'.$Cappid.'",
  166. "authorizer_refresh_token":"'.$refresh_token.'",
  167. }';
  168. $access_token_Arr = http_post_data("https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=$Mtoken", $data_string);
  169. //$access_token_Arr=json_decode($access_token_Arr,true);
  170. return $access_token_Arr;
  171. }
  172. function getCToken($Mappid,$Cappid,$refresh_token,$Mtoken){
  173. $file = file_get_contents("jsonX/{$Cappid}_token.json",true);
  174. $result = json_decode($file,true);
  175. if (time() > $result['expires']){
  176. // $data = array();
  177. $jsonStr = getNewCToken($Mappid,$Cappid,$refresh_token,$Mtoken);
  178. // $jsonStr = json_encode($data);
  179. $fp = fopen("jsonX/{$Cappid}_token.json", "w");
  180. fwrite($fp, $jsonStr);
  181. fclose($fp);
  182. $data=json_decode($jsonStr,true);
  183. return $data['authorizer_access_token'];
  184. }else{
  185. return $result['authorizer_access_token'];
  186. }
  187. }
  188. function sendmsg($openid,$msgtype,$keyword,$postObj,$url){
  189. //消息处理模块.
  190. switch ($msgtype)
  191. {
  192. case 'text':
  193. $data1='{ "touser":"'.$openid.'", "msgtype":"text",
  194. "text": { "content":" '.$keyword.' " } }';
  195. break;
  196. case 'image':
  197. $MediaId = $postObj->MediaId;
  198. $data1=' { "touser":"'.$openid.'", "msgtype":"image",
  199. "image": { "media_id":"'.$MediaId.'" }}';
  200. break;
  201. case 'voice':
  202. $MediaId = $postObj->MediaId;
  203. $Recognition = $postObj->Recognition;
  204. $data1=' { "touser":"'.$openid.'", "msgtype":"voice",
  205. "voice": { "media_id":"'.$MediaId.'" }}';
  206. break;
  207. case 'miniprogrampage':
  208. $data1='';
  209. break;
  210. default:
  211. $data1='';
  212. }
  213. $aaa = http_post_data($url, $data1);
  214. // echo '发送消息:'.$data1.$aaa;
  215. // logger('@@@@@@@@'.$data1.$aaa);
  216. return $aaa;
  217. }
  218. function savejson($path,$data){ //保存1行
  219. //去除重复
  220. $lines = file($path);
  221. $i=0;
  222. $cf=0;
  223. foreach ($lines as $line) {
  224. $line = rtrim($line);
  225. $i++;
  226. // print("$line\n");
  227. if ($data==$line){
  228. $cf=$i;
  229. break;
  230. }
  231. // more statements...
  232. }
  233. if ($cf==0) {
  234. // echo "newone:\n";
  235. file_put_contents($path, $data.PHP_EOL, FILE_APPEND);
  236. return $cf;
  237. }
  238. else{
  239. return $cf;
  240. }
  241. }
  242. ?>