kefu_b.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. <?php
  2. //echo checkSignature;
  3. /* 2.58 增加@功能 .
  4. 2.11 增加群发 图片
  5. 2.12 增加消息报告 本地缓存
  6. 2.13 ?支持公共号消息
  7. 2.14 ?支持多用户
  8. 2.15 ?支持素材库
  9. CopyRight 2017 www.17cheng.cn All Rights Reserved
  10. */
  11. header('Content-type:text/json');
  12. traceHttp();
  13. ini_set("display_errors", "off");
  14. $stime=microtime(true);
  15. define("TOKEN", "xcxkefu");
  16. $wechatObj = new wechatCallbackapiTest();
  17. if (isset($_GET['echostr'])) { //判断是不是首次验证
  18. $wechatObj->valid();
  19. }else{
  20. $wechatObj->responseMsg();
  21. }
  22. class wechatCallbackapiTest
  23. {
  24. public function valid()
  25. {
  26. $echoStr = $_GET["echostr"];
  27. if($this->checkSignature()){
  28. echo $echoStr;
  29. exit;
  30. }
  31. }
  32. private function checkSignature()
  33. {
  34. $signature = $_GET["signature"];
  35. $timestamp = $_GET["timestamp"];
  36. $nonce = $_GET["nonce"];
  37. $token = TOKEN;
  38. $tmpArr = array($token, $timestamp, $nonce);
  39. sort($tmpArr);
  40. $tmpStr = implode( $tmpArr );
  41. $tmpStr = sha1( $tmpStr );
  42. if( $tmpStr == $signature ){
  43. return true;
  44. }else{
  45. return false;
  46. }
  47. }
  48. public function responseMsg()
  49. {
  50. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //获取数据
  51. //logger(time());
  52. if (!empty($postStr)){
  53. //$postObj =json_decode($postStr);
  54. $gzh=true;
  55. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  56. $fromUsername = trim($postObj->FromUserName); //openid
  57. $toUsername = $postObj->ToUserName;
  58. $tousernamebak=trim($toUsername);
  59. $keyword = trim($postObj->Content);
  60. $sessionFrom=$postObj->SessionFrom;
  61. //logger('sessionFrom:'.$sessionFrom);
  62. // $sessionFrom.="|";
  63. //$userinfo=explode("|",$sessionFrom,-1);
  64. $MsgType = $postObj->MsgType;//消息类型 text event
  65. //$SessionFrom = $postObj->SessionFrom;//消息类型 text event
  66. $time = time();
  67. logger($postStr);
  68. //读取设置json 系统设置
  69. $json = file_get_contents('json/'.$toUsername.'.json');
  70. $arr = json_decode($json,true);
  71. $uniacid=$arr['uniacid'];
  72. $website=$arr['website'];
  73. $json = file_get_contents('json/'.$uniacid.'_keywords.json');//读取key列表
  74. $keys = json_decode($json,true);
  75. $json = file_get_contents('json/'.$uniacid.'_keys.json');//读取数字key列表
  76. $numkeys = json_decode($json,true);
  77. define("APPID", $arr['APPID']);
  78. define("APPSECRET", $arr['APPSECRET']);
  79. $myopenid=trim($arr['openids']);//管理员id
  80. // if(isset($arr['uniacid'])){$uniacid=0; }else{};
  81. //获取access_token 先获取token备份.查询 token是否存在
  82. // $token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
  83. // logger( $token_access_url);
  84. // $res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
  85. // $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
  86. // $Atoken = $result['access_token']; //
  87. $Atoken=getToken(APPID,APPSECRET,$toUsername);//修正token时间问题
  88. //下发信息
  89. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$Atoken;
  90. $urlbak=$url;
  91. if ($MsgType == "image") {//接收图片
  92. $PicUrl = $postObj->PicUrl;
  93. $MediaId = $postObj->MediaId;
  94. $MsgId = $postObj->MsgId;
  95. if ($fromUsername!= $myopenid){ //当是自己用户发来消息时.
  96. $data1=' { "touser":"'.$myopenid.'", "msgtype":"image",
  97. "image": { "media_id":"'.$MediaId.'" }}';
  98. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  99. } else{//管理员下发消息.
  100. $nowid=dujson('json/'.$toUsername.'_now.json');
  101. $nowid=json_decode($nowid, true);
  102. //判断是给谁的图片 .获取到token 并
  103. if (trim($nowid['tousername'])==$toUsername)
  104. {
  105. $data1=' { "touser":"'.$nowid['openid'].'", "msgtype":"image",
  106. "image": { "media_id":"'.$MediaId.'" }}';
  107. $bbb = http_post_data($url, $data1);
  108. }else{
  109. $file = file_get_contents("json/{$nowid['tousername']}_token.json",true);
  110. $result = json_decode($file,true);
  111. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$result['access_token'];
  112. $add="temp/".time().".png";//保存图片时的id
  113. $img = GrabImage($PicUrl,$add);
  114. logger('////--'.$img);
  115. // $type = 'image';
  116. if ($img<>''){//当是图片时 转换成微信图片
  117. $udata= json_decode(uploadFile($add,'image',$result['access_token']),true) ;
  118. $MediaId = $udata['media_id'];
  119. logger('Mid:'.$MediaId);
  120. }
  121. //发来图片 没转换成功时
  122. if ($MediaId==''){
  123. $data1='{ "touser":"'.trim($nowid['openid']).'", "msgtype":"text",
  124. "text": { "content":"[客服发来图片]\n <a href=\"'.$PicUrl.'\">点击查看</a> " } }';
  125. $bbb = http_post_data($url, $data1);
  126. }else{
  127. $data1=' { "touser":"'.$nowid['openid'].'", "msgtype":"image",
  128. "image": { "media_id":"'.$MediaId.'" }}';
  129. $bbb = http_post_data($url, $data1);
  130. }
  131. logger($data1.$bbb);
  132. }
  133. }
  134. exit;
  135. }
  136. if ($MsgType == "link") {//接收图文链接
  137. $PicUrl = $postObj->PicUrl;
  138. $MsgId = $postObj->MsgId;
  139. $Title = $postObj->Title;
  140. $Description = $postObj->Description;
  141. $Url = $postObj->Url;
  142. $baogao=' { "title": "'.$Title.'", "description": "'.$Description.'",
  143. "url": "'.$Url.'", "thumb_url": "" }';
  144. logger($baogao);
  145. if ($fromUsername!= $myopenid){ //当是自己用户发来消息时.
  146. $data1='{ "touser":"'.$myopenid.'", "msgtype":"link",
  147. "link": '.$baogao.' }';
  148. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  149. } else{//管理员下发消息.
  150. $nowid=dujson('json/'.$toUsername.'_now.json');
  151. $nowid=json_decode($nowid, true);
  152. //判断是给谁的图片 .获取到token 并
  153. if (trim($nowid['tousername'])==$toUsername)
  154. {
  155. $data1='{ "touser":"'.$nowid['openid'].'", "msgtype":"link",
  156. "link": '.$baogao.' }';
  157. $bbb = http_post_data($url, $data1);
  158. }else{
  159. $file = file_get_contents("json/{$nowid['tousername']}_token.json",true);
  160. $result = json_decode($file,true);
  161. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$result['access_token'];
  162. $data1='{ "touser":"'.$nowid['openid'].'", "msgtype":"link",
  163. "link": '.$baogao.' }';
  164. $bbb = http_post_data($url, $data1);
  165. logger($data1.$bbb);
  166. }
  167. }
  168. exit;
  169. }
  170. if ($MsgType == "video") {//接收图片
  171. $PicUrl = $postObj->PicUrl;
  172. $MediaId = $postObj->MediaId;
  173. $thumb_media_id = $postObj->ThumbMediaId;
  174. if ($fromUsername!= $myopenid){ //
  175. $data1=' { "touser":"'.$myopenid.'", "msgtype":"video",
  176. "video": { "media_id":"'.$MediaId.'", "thumb_media_id":"VJFReD3DymOpc5slq3FvgQwzNtXQ9hONlafGaxg4P_F5x-1qvj1bgArhYQnrLrRV",
  177. "title":"未命名'.$title.'", "description":"视频说明:'.$MediaId.'" }}';
  178. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  179. logger($data1);
  180. logger($aaa);
  181. } else{
  182. $nowid=dujson('json/'.$toUsername.'_now.json');
  183. $nowopenid=json_decode($nowid, true);
  184. $content=''.$paidui['nickname'].':\n';
  185. // $fromUsername= $myopenid;
  186. $data1=' { "touser":"'.$nowopenid['openid'] .'", "msgtype":"video",
  187. "video": { "media_id":"'.$MediaId.'", "thumb_media_id":"'.$MediaId.'",
  188. "title":"未命名'.$title.', "description":"视频说明:'.$MediaId.'" }}';
  189. $bbb = http_post_data($url, $data1);
  190. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  191. "text": { "content":"[图片ID]\n '.$MediaId.'\n 图片链接:'.$PicUrl.' " } }';
  192. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  193. }
  194. exit;
  195. }
  196. if ($MsgType == "voice") {//接收声音
  197. $PicUrl = $postObj->PicUrl;
  198. $MediaId = $postObj->MediaId;
  199. $MsgId = $postObj->MsgId;
  200. $Recognition = $postObj->Recognition;
  201. if ($fromUsername!= $myopenid){ //接收用户发来的语音
  202. $data1=' { "touser":"'.$myopenid.'", "msgtype":"voice",
  203. "voice": { "media_id":"'.$MediaId.'" }}';
  204. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  205. if($Recognition<>''){
  206. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  207. "text": { "content":"[语音文本]\n '.$Recognition.' " } }';
  208. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  209. }
  210. } else{
  211. $nowid=dujson('json/'.$toUsername.'_now.json');
  212. $nowid=json_decode($nowid, true);
  213. if (trim($nowid['tousername'])==$toUsername)
  214. {
  215. //$nowopenid=json_decode($nowid, true)['openid'];
  216. $data1=' { "touser":"'.$nowid['openid'] .'", "msgtype":"voice",
  217. "voice": { "media_id":"'.$MediaId.'" }}';
  218. $bbb = http_post_data($url, $data1);
  219. }else{
  220. $file = file_get_contents("json/{$nowid['tousername']}_token.json",true);
  221. $result = json_decode($file,true);
  222. $url1 = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$result['access_token'];
  223. $nowopenid=$nowid['openid'];
  224. $data1='{ "touser":"'.trim($nowopenid).'", "msgtype":"text",
  225. "text": { "content":"[语音文本]\n '.$Recognition.' " } }';
  226. $bbb = http_post_data($url1, $data1);
  227. if (json_decode($bbb, true)['errcode']==0){
  228. $sd='已送达';}else{$sd='未送达';}
  229. $data1='{ "touser":"'.trim($fromUsername).'", "msgtype":"text",
  230. "text": { "content":"['.$sd.']\n '.$Recognition.' " } }';
  231. $bbb = http_post_data($url, $data1);
  232. }
  233. ////////////////////////
  234. }
  235. exit;
  236. }
  237. if ($MsgType == "file") {//接收文件 暂时不能支持 .
  238. $FromUserName = $postObj->FromUserName;
  239. $Title = $postObj->Title;
  240. $FileMd5 = $postObj->FileMd5;
  241. if ($fromUsername!= $myopenid){
  242. // $data1=' { "touser":"'.$fromUsername.'", "msgtype":"file",
  243. // "file": { "title": "'.$Title.'", "FileMd5":"'.$FileMd5.'" }}';
  244. $data1='{ "touser":"'.$fromUsername.'", "msgtype":"text",
  245. "text": { "content":"[系统提示]\n由于微信系统限制,我们还不能接收您发来的文件,可以加我们的客服QQ进行传递.回复QQ立即接通QQ链接. " } }';
  246. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  247. logger($aaa);
  248. } else{
  249. $nowid=dujson('json/'.$toUsername.'_now.json');
  250. $nowopenid=json_decode($nowid, true);
  251. $content=''.$paidui['nickname'].':\n';
  252. // $fromUsername= $myopenid;
  253. $data1=' { "touser":"'.$nowopenid['openid'].'", "msgtype":"image",
  254. "image": { "media_id":"'.$MediaId.'" }}';
  255. $bbb = http_post_data($url, $data1);
  256. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  257. "text": { "content":"[图片ID]\n '.$MediaId.'\n 图片链接:'.$PicUrl.' " } }';
  258. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  259. }
  260. exit;
  261. }
  262. if ($MsgType == "miniprogrampage") {//接收转发小程序...
  263. $FromUserName = $postObj->FromUserName;
  264. $Title = $postObj->Title;
  265. $AppId = $postObj->AppId;
  266. $PagePath = $postObj->PagePath;
  267. $ThumbMediaId = $postObj->ThumbMediaId;
  268. if ($fromUsername!= $myopenid){ //发送给管理员
  269. $data1=' { "touser":"'.$myopenid.'", "msgtype":"miniprogrampage",
  270. "miniprogrampage": { "title": "'.$Title.'",
  271. "pagepath": "'.$pagepath.'",
  272. "thumb_media_id": "'.$ThumbMediaId.'" }}';
  273. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  274. // logger($aaa);
  275. } else{
  276. $nowid=dujson('json/'.$toUsername.'_now.json');
  277. $nowopenid=json_decode($nowid, true);
  278. // $fromUsername= $myopenid;
  279. // $data1=' { "touser":"'.$nowopenid['openid'].'", "msgtype":"image",
  280. // "image": { "media_id":"'.$MediaId.'" }}';
  281. $data1=' { "touser":"'.$nowopenid['openid'].'", "msgtype":"miniprogrampage",
  282. "miniprogrampage": { "title": "'.$Title.'",
  283. "pagepath": "'.$pagepath.'",
  284. "thumb_media_id": "'.$ThumbMediaId.'" }}';
  285. $bbb = http_post_data($url, $data1);
  286. }
  287. exit;
  288. }
  289. $Event = $postObj->Event;
  290. if ($Event == "CLICK") {
  291. $EventKey = $postObj->EventKey;
  292. $MsgType='text';
  293. $keyword=trim($EventKey);//修复公众号问题.
  294. logger('event:'.$Event.$MsgType.'key:'.$keyword);
  295. }
  296. if ($Event == "VIEW") {
  297. exit;
  298. }
  299. if ($MsgType == "event") {
  300. $event = $postObj->Event;
  301. //如果传输了seession 就读取然后存储 .
  302. /// if (length($SessionFrom)>3) {$nickname=explode("|",$SessionFrom,-1)[0] ;}
  303. // $nickname=explode("|",$sessionFrom,-1)[0] ;
  304. // logger('nickname0:'.$nickname);
  305. //处理第一次进入的回复内容 用户信息查询. 先查询数据库: 并注册用户.
  306. if ($fromUsername== $myopenid){ //管理员上线通知 排队人数 留言条数 排队的人.
  307. $paiduinumber=count(file('json/'.$toUsername.'_now.json'));
  308. $liuyannumber=count(file('json/'.$toUsername.'_lishi.json'));
  309. $nowid=dujson('json/'.$toUsername.'_now.json');
  310. $nowopenid=json_decode($nowid, true);
  311. $tongzhi='排队人数:'. $paiduinumber.'\n'.'留言条数:\n'. $liuyannumber.' \n当前聊天:\n'.$nowopenid['nickname'];
  312. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  313. "text": { "content":"[管理员上线工作提醒]\n '.$tongzhi.' " } }';
  314. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  315. exit;
  316. }
  317. //下发图片.
  318. // $MediaId='EZMIeOmWpWieTSEPkUNQ-RfhAoGXBgjHrAXnVbfp9HRt6-Kz3Wx7_jdOSGWZW1uK';
  319. // $data1=' { "touser":"'.$fromUsername.'", "msgtype":"image",
  320. // "image": { "media_id":"'.$MediaId.'" }}';
  321. // $bbb = http_post_data($url, $data1);
  322. // exit;
  323. $user= getwxinfo($Atoken,$fromUsername);
  324. if($event=='unsubscribe'){ $eve='取消关注'; exit; };
  325. $userinfo= getuser($uniacid,$fromUsername,$user['nickname'],$website);
  326. logger('nickname1:'.$uniacid.$fromUsername.$userinfo['nickname']);
  327. $send=$keys[$arr['默认']];
  328. $data['title']= str_replace($userinfo['id'],'[id]',$data);
  329. if ($send['link']['title']!=''){
  330. $send['link']['title']=str_replace('id',$userinfo['id'],$send['link']['title']);
  331. $send['link']['title']=str_replace('[name]', trim($userinfo['nickname']),$send['link']['title']);
  332. }
  333. if ($send['miniprogrampage']['title']!=''){
  334. $send['miniprogrampage']['title']=str_replace('id',$userinfo['id'],$send['miniprogrampage']['title']);
  335. $send['miniprogrampage']['title']=str_replace('[name]', trim($userinfo['nickname']),$send['miniprogrampage']['title']);
  336. }
  337. if ($send['text']['content']!=''){
  338. $send['text']['content']=str_replace('[id]',$userinfo['id'],$send['text']['content']);
  339. $send['text']['content']=str_replace('[name]',trim($userinfo['nickname']),$send['text']['content']);
  340. }
  341. $send['touser']=$fromUsername;
  342. $data=json_encode($send,JSON_UNESCAPED_UNICODE);
  343. //替换会员id和名字:
  344. // $data= str_replace($userinfo['id'],'[id]',$data);
  345. // echo str_replace("world","Shanghai","Hello world!");
  346. // json_encode( $arr['关注'],JSON_UNESCAPED_UNICODE);
  347. $aaa = http_post_data($url, $data); //下发首次进入的回复内容.
  348. $zt=json_decode($aaa, true)['errcode'];
  349. logger("关注下发:".$data.'-状态:'. $zt);
  350. // $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  351. //"text": { "content":"[进入提醒测试]\n '.$zt.' " } }';
  352. // $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  353. $caidan=trim($arr['Gjsonid']);//进入通知:
  354. if ($caidan<>''){
  355. $Btoken=getToken($arr['Gappid'],$arr['GAPPSECRET'],$caidan);//修正token时间问题
  356. // logger("通知下发:".$caidan_arr['APPID'].'-状态:'.$caidan_arr['openids']);
  357. $url1 = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$Btoken;
  358. $data1='{ "touser":"'.$arr['Gopenids'].'", "msgtype":"text",
  359. "text": { "content":"[小程序进入提醒]\n '.$userinfo['nickname'].' " } }';
  360. $aaa = http_post_data($url1, $data1); // //通知到公共号
  361. logger($data1.$aaa);
  362. }
  363. $eve='用户点击了按钮';
  364. if($event=='subscribe'){ $eve='新增关注'; };
  365. if($event=='unsubscribe'){ $eve='取消关注'; };
  366. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  367. "text": { "content":"['.$eve.'通知]\n 编号: '.$userinfo['id'].' 昵称: '.$userinfo['nickname'].$eve.'" } }';
  368. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  369. exit;
  370. }
  371. if ($MsgType == "text") {// 如果不是第一次进入
  372. // logger("keyword".$arr[$keyword]);
  373. //如果进入的是数字 判断是否有对应的文字 .
  374. if(is_numeric($keyword)){
  375. $send=$keys[$numkeys[$keyword]];
  376. }
  377. else {
  378. $send=$keys[$keyword]; //$keys['关于我们'];
  379. }
  380. if ( $send<>'' ) { //判断是否是知识库设置字段
  381. // $user= getwxinfo($Atoken,$fromUsername);
  382. // logger('获取微信用户信息:'.$user);
  383. // $userinfo= getuser($uniacid,$fromUsername,$nickname);
  384. $userinfo=getwxinfo($Atoken,$fromUsername);
  385. if ($send['link']['title']!=''){
  386. $send['link']['title']=str_replace('[id]',$userinfo['id'],$send['link']['title']);
  387. $send['link']['title']=str_replace('[name]', trim($userinfo['nickname']),$send['link']['title']);
  388. }
  389. if ($send['miniprogrampage']['title']!=''){
  390. $send['miniprogrampage']['title']=str_replace('[id]',$userinfo['id'],$send['miniprogrampage']['title']);
  391. $send['miniprogrampage']['title']=str_replace('[name]', trim($userinfo['nickname']),$send['miniprogrampage']['title']);
  392. }
  393. if ($send['text']['content']!=''){
  394. $send['text']['content']=str_replace('[id]',$userinfo['id'],$send['text']['content']);
  395. $send['text']['content']=str_replace('[name]',trim($userinfo['nickname']),$send['text']['content']);
  396. $send['text']['content']=changeurl($send['text']['content']);
  397. }
  398. $send['touser']=$fromUsername;
  399. $data=json_encode($send,JSON_UNESCAPED_UNICODE);
  400. $aaa = http_post_data($url, $data);
  401. logger($data);
  402. getuser($uniacid,$fromUsername,$userinfo['nickname'],$website); //保存
  403. $zt=json_decode($aaa, true)['errcode'];
  404. if ($zt!=0){//下发未成功
  405. $data='{ "touser":"'.$fromUsername.'", "msgtype":"text",
  406. "text": { "content":" 系统忙 错误代码: ' . $zt . ' " } }';
  407. $aaa = http_post_data($url, $data);
  408. }
  409. logger("关注下发:".$data.'-状态:'. $zt);
  410. exit;
  411. }else {//文字推送
  412. if ($keyword=='openid'){ //获取openid
  413. $data='{ "touser":"'.$fromUsername.'", "msgtype":"text",
  414. "text": { "content":" 你的openid: ' . $fromUsername . ' " } }';
  415. $aaa = http_post_data($url, $data);
  416. exit;
  417. }
  418. if (substr($keyword,0,6)=='取号'){ //手动设置昵称..
  419. $quhao= edituser($uniacid,$fromUsername,trim(substr($keyword,6)),$website);
  420. $allnumber=count(file('json/'.$toUsername.'_now.json'));
  421. $data='{ "touser":"'.$fromUsername.'", "msgtype":"text",
  422. "text": { "content":" '.substr($keyword,6).$quhao.' 前面有' .$allnumber . '人排队. " } }';
  423. $aaa = http_post_data($url, $data);
  424. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  425. "text": { "content":"[取号通知]\n'.substr($keyword,6).'排队取号,目前共'.$allnumber+1 . ' 人. \n " } }';
  426. $bbb = http_post_data($url, $data1);
  427. exit;
  428. }
  429. if ($arr["其他"]<>""){
  430. //人工客服时 读一次数据库 .
  431. if ($fromUsername!= $myopenid){ //不是管理员时
  432. //先判断用户是否在排队?
  433. $caidan=trim($arr['Gjsonid']);//进入通知:
  434. if ($arr["其他"]=='公众号客服'){
  435. $Btoken=getToken($arr['Gappid'],$arr['GAPPSECRET'],$caidan);//修正token时间问题
  436. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$Btoken;
  437. $myopenid=trim($arr['Gopenids']);
  438. $toUsername= $caidan; //写入新的公众号文件里面.
  439. }
  440. $paidui= ifpaidui('json/'.$toUsername.'_now.json',$fromUsername);
  441. logger($fromUsername.':'.$paidui['shunxu']);
  442. if ($paidui['shunxu']==0)//是新用户 查编号 写入队列.
  443. {
  444. $userinfowx=getwxinfo($Atoken,$fromUsername);
  445. $userinfo= getusernew($uniacid,$fromUsername,$userinfowx['nickname'],$website);//如果是第一个 直接读取缓存
  446. $userinfo['tousername']=$tousernamebak;
  447. $userinfo['uniacid']=$uniacid;
  448. //判断是否是公共号绑定回复----
  449. $new= savejson('json/'.$toUsername.'_now.json', json_encode($userinfo));//写入一个. 改.
  450. $allnumber=count(file('json/'.$toUsername.'_now.json'));
  451. if ($new==0){$new1=$allnumber;} else{$new1=$new;} //新用户 总排队
  452. // $fromUsername= $myopenid;
  453. if($allnumber==1)
  454. {$hui='可直接回复';
  455. $tishi='\n人工客服已经接入.';
  456. //对方正在输入...哈哈
  457. $data8='{ "touser":"'.$fromUsername.'", "command":"Typing"}';
  458. $url8='https://api.weixin.qq.com/cgi-bin/message/custom/typing?access_token='.$Atoken;
  459. $bbb = http_post_data($url8, $data8);
  460. logger($bbb);
  461. //结束输入判断.
  462. } else{
  463. $hui='可用命令 下一个 接通.或\n @'.$userinfo['id'].':私信Ta';
  464. if ($arr["其他"]=='人工+机器人'){ $tishi='\n直接回复接入智能聊天机器人.';};
  465. };
  466. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  467. "text": { "content":"[排队通知]目前共'.$allnumber . ' 人. \n'.$userinfowx['nickname'].' 备注:['.$userinfo['beizhu'].'] :'.$keyword.'\n '.$hui.'" } }';
  468. $bbb = http_post_data($url, $data1); //第一次打招呼通知管理员.
  469. //读取Key 自动导航
  470. $arr= array_keys($numkeys);
  471. $i=0;
  472. foreach ($arr as $arr) {
  473. $daohang.=$arr.':'.changeurl($numkeys[$arr]) .'\n';
  474. $i++;
  475. if ($i>9){break;};
  476. }
  477. //echo $caidan;
  478. $jqr=' 先看看这些能不能帮到您. 直接回复序号即可.\n'.$daohang;
  479. $data='{ "touser":"'.$fromUsername.'", "msgtype":"text",
  480. "text": { "content":"排队中,您是第'.$new1.' 个. '.$jqr.$tishi.' " } }'; //当没有约定好的回复时,这么回复。
  481. $bbb = http_post_data($urlbak, $data); //第一次打招
  482. exit;
  483. }
  484. elseif($paidui['shunxu']==1){ //是第一个 .
  485. $content='['.$paidui['id'].']'.$paidui['nickname'].'_'.$uniacid.':\n';
  486. // $fromUsername= $myopenid;
  487. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  488. "text": { "content":"'. $content. $keyword . ' " } }';
  489. $bbb = http_post_data($url, $data1);
  490. if (json_decode($bbb, true)['errcode']==40001)
  491. {
  492. //发送失败 删掉token 再次获取.
  493. exec("rm -f ".'json/'.$toUsername.'_token.json');
  494. //
  495. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  496. "text": { "content":"重发:'. $content. $keyword . ' " } }';
  497. $bbb = http_post_data($url, $data1);
  498. exit;
  499. }
  500. if (json_decode($bbb, true)['errcode']<>0){
  501. $data2='{ "touser":"'.$fromUsername.'", "msgtype":"text",
  502. "text": { "content":" 客服离开... 消息已存入信箱.'.json_decode($bbb, true)['errcode'].'" } }'; //当没有约定好的回复时,这么回复。
  503. $bbbb = http_post_data($urlbak, $data2);
  504. savejson('json/'.$toUsername.'_lishi.json', '@'.$paidui['id'].'_'.$content.''.$keyword );
  505. $caidan=trim($arr['Gjsonid']);//消息通知到公共号:
  506. if ($caidan<>''){
  507. $Btoken=getToken($arr['Gappid'],$arr['GAPPSECRET'],$caidan);//修正token时间问题
  508. $url1 = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$Btoken;
  509. $data1='{ "touser":"'.$arr['Gopenids'].'", "msgtype":"text",
  510. "text": { "content":"[客服离线消息]错误代码:'.json_decode($bbb, true)['errcode'].'\n '. $content. $keyword .' " } }';
  511. $aaa = http_post_data($url1, $data1); // //通知到公共号
  512. logger($data1.$aaa);
  513. if (json_decode($aaa, true)['errcode']==40001)
  514. {
  515. //发送失败 删掉token 再次获取.
  516. exec("rm -f ".'json/'.$caidan.'_token.json');
  517. $Btoken=getToken($arr['Gappid'],$arr['GAPPSECRET'],$caidan);//修正token时间问题
  518. $url1 = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$Btoken;
  519. $data1='{ "touser":"'.$arr['Gopenids'].'", "msgtype":"text",
  520. "text": { "content":"[客服离线消息]错误代码:'.json_decode($bbb, true)['errcode'].'\n '. $content. $keyword .' " } }';
  521. $aaa = http_post_data($url1, $data1); // //通知到公共号
  522. }
  523. }
  524. }
  525. }else {//不是第一个 判断客服模式
  526. if ($arr["其他"]=='人工+机器人')
  527. { $jqr='先和智能客服聊会. \n智能客服:'.jiqiren($keyword); }
  528. else
  529. { $jqr='轮到您时我们会信息通知.请耐心等待. \n '; }
  530. // $jqr='先和智能客服聊会. \n智能客服:'.jiqiren($keyword);
  531. $data='{ "touser":"'.$fromUsername.'", "msgtype":"text",
  532. "text": { "content":"排队中,您是第'.$paidui['shunxu'].' 个. '.$jqr.' " } }'; //当没有约定好的回复时,这么回复。
  533. $aaa = http_post_data($urlbak, $data); //
  534. logger('下发:'.$data.$aaa);
  535. //通知到公共号
  536. exit;
  537. }
  538. }else{//处理管理员发来的消息
  539. //测试下发图片
  540. if ($keyword=='生成缓存'){//调用生成缓存接口.
  541. //
  542. $huncun= json_decode(alluser($uniacid,$website),true);
  543. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  544. "text": { "content":" [缓存报告:]'.$huncun['openids'].' 数量:'.$huncun['count'].' "" } }';
  545. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  546. exit;
  547. }
  548. //处理群发所有人消息
  549. if (substr($keyword,0,10)=='@所有人'){
  550. //判断有没有缓存.
  551. $huncun= json_decode(alluser($uniacid,$website),true);
  552. //复制一份 "json/".$uniacid."_user.json"
  553. // exec("cp -f {$icon} ".$_base_path.'/WeMall/res/drawable-xhdpi-v4/ic_launcher.png');
  554. $gjz=substr($keyword,10,strlen(trim($keyword))-10);
  555. if (substr ($keyword, -1)=='#'){
  556. $gjz=str_replace('#','|',$gjz);
  557. }
  558. logger('是不是:'.$gjz) ;
  559. $baogao=' {
  560. "title": "代发列表已经准备好!点此群发",
  561. "description": "本次准备发送: '.$huncun['count'].' 请1分钟内点击,过期失效.",
  562. "url": "'.$website.'addons/qidong_app/qunfa.php?uniacid='.$uniacid.'&keyword='.$gjz.'&tousername='.$toUsername.'&t='.time().'",
  563. "thumb_url": "'.$website.'/addons/qidong_app/icon.jpg"
  564. }';
  565. logger($baogao);
  566. $data1='{ "touser":"'.$myopenid.'", "msgtype":"link",
  567. "link": '.$baogao.' }';
  568. $aaa = http_post_data($url, $data1); //
  569. exit;
  570. }///@所有人结束
  571. //增加@某人的功能
  572. if (substr($keyword,0,1)=='@'){
  573. //取openid hiuser
  574. $stri=0;
  575. $stri= stripos($keyword,':');
  576. if ($stri==0){$stri= stripos($keyword,' ') ;}
  577. if ($stri==0){$stri= stripos($keyword,':') ;}
  578. $nid=substr($keyword,1,$stri-1); //@12:=>12
  579. $say=trim(substr($keyword,$stri+1));//发送的内容:
  580. //判断say最后字符是# 则是关键词消息 991#
  581. $liuuser= hiuser($uniacid,$nid,$website);
  582. $result = substr ($say, -1);
  583. if ($result=='#'){
  584. $gjz=substr($say,0,strlen($say)-1);
  585. if(is_numeric($gjz)){
  586. $send=$keys[$numkeys[$gjz]];
  587. }
  588. else {
  589. $send=$keys[$gjz]; //$keys['关于我们'];
  590. }
  591. $send['touser']=$liuuser['openid'];
  592. $nickname=$liuuser['nickname'];
  593. if($nickname==''){$nickname='会员'.$liuuser['id'];}
  594. $send['link']['title']=str_replace('[id]',$liuuser['id'],$send['link']['title']);
  595. $send['link']['title']=str_replace('[name]', trim($nickname),$send['link']['title']);
  596. $data=json_encode($send,JSON_UNESCAPED_UNICODE);
  597. $bbb = http_post_data($url, $data);
  598. logger('单发:'.$data);
  599. }else{
  600. $data2='{ "touser":"'.$liuuser['openid'].'", "msgtype":"text",
  601. "text": { "content":"[管理员@你]:\n'.$say.'" } }'; //当没有约定好的回复时,这么回复。
  602. $bbb = http_post_data($url, $data2);
  603. }
  604. if (json_decode($bbb, true)['errcode']==0)
  605. { $zt='送达'; }else{ $zt='失败,可能已经离线'.json_decode($bbb, true)['errcode']; };
  606. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  607. "text": { "content":"会员'.$nid.'[消息'.$zt.']\n " } }';
  608. $aaa = http_post_data($url, $data1); //减少骚扰 不通知
  609. //发送消息
  610. exit;
  611. }//@某人结束
  612. switch ($keyword)
  613. {
  614. case '下一个':
  615. $delid=deljson('json/'.$toUsername.'_now.json');//删掉一个
  616. //
  617. logger($delid);
  618. $jieshuyu= str_replace('[name]',$delid['nickname'],$arr['结束语']);
  619. $data0='{ "touser":"'.trim($delid['openid']).'", "msgtype":"text",
  620. "text": { "content":" '.$jieshuyu.' " } }';
  621. //$delid=json_decode($delid, true);
  622. logger('1:'.$uniacid.'---'.$delid['uniacid']);
  623. if (trim($delid['uniacid'])==$uniacid)
  624. // If (substr(trim($delid['tousername']),0,1)=='')
  625. { //当是自己公众号的客户时:
  626. $a= edituser($uniacid,$delid['openid'],'',$website);
  627. logger('更新时间:'.$uniacid.$delid['openid'].$a);
  628. $aaa = http_post_data($url, $data0);
  629. logger('结束语1:'.$data0);
  630. }else{
  631. $file = file_get_contents("json/{$delid['tousername']}_token.json",true);
  632. $a= edituser($delid['uniacid'],$delid['openid'],'',$website);
  633. logger('更新时间1:'.$uniacid.$delid['openid'].$a);
  634. $result = json_decode($file,true);
  635. $url1 = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$result['access_token'];
  636. $aaa = http_post_data($url1, $data0);//下发结束语 .
  637. logger('结束语2:'.$data0);
  638. }
  639. //////////////读取下一个//////////////////////////////////////////////
  640. $nowid=dujson('json/'.$toUsername.'_now.json');
  641. $nowid=json_decode($nowid, true);
  642. $data2='{ "touser":"'.trim($nowid['openid']).'", "msgtype":"text",
  643. "text": { "content":"'.$nowid['nickname'].', 你好,久等了,有什么可以帮你的吗? " } }';
  644. // 通知到管理员
  645. If (substr(trim($nowid['openid']),0,1)=='o'){
  646. //$userinfo= getuser($uniacid,$nowid,$nickname);
  647. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  648. "text": { "content":"新用户'.$nowid['nickname'].'接入,准备接待.\n排队人数:'.count(file('json/'.$toUsername.'_now.json')).' " } }';
  649. $bbb = http_post_data($url, $data1);
  650. // exit;
  651. }else{
  652. $data1='{ "touser":"'.$myopenid.'", "msgtype":"text",
  653. "text": { "content":"已经接待完毕,没有用户排队了. " } }';
  654. $bbb = http_post_data($url, $data1);
  655. // exit;
  656. }
  657. if (trim($nowid['tousername'])==null)
  658. { //当是自己公众号的客户时:
  659. $aaa = http_post_data($url, $data2);
  660. logger('欢迎语1 自己公共号:'.$data2);
  661. }
  662. else{
  663. $file = file_get_contents("json/{$nowid['tousername']}_token.json",true);
  664. $result = json_decode($file,true);
  665. $url1 = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$result['access_token'];
  666. $aaa = http_post_data($url1, $data2);
  667. logger('欢迎语1 其他小程序:'.$data2);
  668. }
  669. exit;//解决两次通知问题?2.28
  670. // file_put_contents('json/'.$toUsername.'_all.json'.$delid.PHP_EOL, FILE_APPEND);
  671. break;
  672. case '排队'://排队列表
  673. $paiduis=paiduilist('json/'.$toUsername.'_now.json');
  674. $data1='{ "touser":"'.$fromUsername.'", "msgtype":"text",
  675. "text": { "content":"排队人数:'.count(file('json/'.$toUsername.'_now.json')).'\n 排队列表'.$paiduis.'\n 小提示 发送@ID:内容 可直接给会员私信. " } }';
  676. break;
  677. case '看留言':
  678. $json = file_get_contents('json/'.$toUsername.'_lishi.json');
  679. $data1='{ "touser":"'.$fromUsername.'", "msgtype":"text",
  680. "text": { "content":"本次获取的内容,已清空留言板:\n'.$json.' " } }';
  681. exec("rm -f ".'json/'.$toUsername.'_lishi.json');
  682. break;
  683. case '清零'://清除文件 继续..
  684. exec("rm -f ".'json/'.$toUsername.'_now.json');
  685. exec("rm -f ".'json/'.$toUsername.'_token.json');
  686. exec("rm -f ".'log.xml');
  687. if (!file_exists('json/'.$toUsername.'_now.json')){
  688. $data1='{ "touser":"'.$fromUsername.'", "msgtype":"text",
  689. "text": { "content":" 排队人数清零成功! " } }';
  690. }
  691. else{
  692. $data1='{ "touser":"'.$fromUsername.'", "msgtype":"text",
  693. "text": { "content":" 清零失败,请检查目录权限. " } }';
  694. }
  695. break;
  696. case '公众号':
  697. ///推送公众号给用户
  698. $nowid=dujson('json/'.$toUsername.'_now.json');
  699. $data1=' {
  700. "touser": "'.$nowid.'",
  701. "msgtype": "link",
  702. "link": '.json_encode( $arr['关注'],JSON_UNESCAPED_UNICODE).'}';
  703. //没成功....
  704. break;
  705. default://管理员默认回复第一个 .
  706. $nowid=dujson('json/'.$toUsername.'_now.json');
  707. $nowid=json_decode($nowid, true);
  708. //如果读到的jsonid 和 这里的不同 则是来自其他平台的 .
  709. //logger('读取到了一个文件:'.$nowid['tousername']);
  710. if (trim($nowid['tousername'])==null)
  711. {
  712. //$nowopenid=json_decode($nowid, true)['openid'];
  713. $data1='{ "touser":"'.trim($nowid['openid']).'", "msgtype":"text",
  714. "text": { "content":" '. $keyword . ' " } }';
  715. }else{
  716. $file = file_get_contents("json/{$nowid['tousername']}_token.json",true);
  717. $result = json_decode($file,true);
  718. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$result['access_token'];
  719. $nowopenid=$nowid['openid'];
  720. $data1='{ "touser":"'.trim($nowopenid).'", "msgtype":"text",
  721. "text": { "content":" '. $keyword . ' " } }';
  722. }
  723. }
  724. $bbb = http_post_data($url, $data1);
  725. logger($data1.$bbb) ; }
  726. }
  727. }
  728. }
  729. $aaa = http_post_data($url, $data);
  730. //给管理员推送:
  731. logger($data.$aaa) ;
  732. }else{
  733. echo "";
  734. exit;
  735. }
  736. }
  737. }
  738. function http_post_data($url, $data_string) {
  739. set_time_limit ( 0 );
  740. $curl = curl_init();
  741. curl_setopt($curl, CURLOPT_URL, $url);
  742. curl_setopt($curl, CURLOPT_HEADER, 0);
  743. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  744. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
  745. curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
  746. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  747. 'Content-Type: application/json',
  748. 'Content-Length: ' . strlen($data_string))
  749. );
  750. $data = curl_exec($curl);
  751. curl_close($curl);
  752. return $data;
  753. }
  754. function traceHttp()
  755. {
  756. //logger("\n\nREMOTE_ADDR:".$_SERVER["REMOTE_ADDR"].(strstr($_SERVER["REMOTE_ADDR"],'101.226')? " FROM WeiXin": "Unknown IP"));
  757. // logger("QUERY_STRING:".$_SERVER["QUERY_STRING"]);
  758. }
  759. function logger($log_content)
  760. {
  761. if(isset($_SERVER['HTTP_APPNAME'])){ //SAE
  762. sae_set_display_errors(false);
  763. sae_debug($log_content);
  764. sae_set_display_errors(true);
  765. }else{ //LOCAL
  766. $max_size = 500000;
  767. $log_filename = "log.xml";
  768. if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
  769. file_put_contents($log_filename, date('Y-m-d H:i:s').$log_content."\r\n", FILE_APPEND);
  770. }
  771. }
  772. //查询进入人的相关信息的
  773. function getinfo($open_id,$title,$description,$url){
  774. $link=mysql_connect("localhost","we7000","w7pass000");
  775. if(!$link) echo "没有连接成功!";
  776. mysql_select_db("we7", $link); //选择数据库
  777. //查询userid ,昵称,图片
  778. $q = "SELECT * FROM hjmall_user where (wechat_open_id ='".$open_id."' and store_id=20) "; //SQL查询语句
  779. mysql_query("SET NAMES utf8");
  780. $rs = mysql_query($q); //获取数据集
  781. if(!$rs){die("Valid result!");}
  782. $row = mysql_fetch_array($rs);
  783. $result=' {
  784. "title": "hi!'.$row[10].','.$title.'",
  785. "description": "'.$description.'",
  786. "url": "'.$url.'",
  787. "thumb_url":"'. $row[11].'"
  788. }';
  789. mysql_free_result($rs); //关闭数据集
  790. return $result;
  791. }
  792. //查询进入人的相关信息的
  793. function getinfo_s($open_id){
  794. $link=mysql_connect("localhost","we70000","w7pass000");
  795. if(!$link) echo "没有连接成功!";
  796. mysql_select_db("we7", $link); //选择数据库
  797. //查询userid ,昵称,图片
  798. $q = "SELECT * FROM hjmall_user where (wechat_open_id ='".$open_id."' and store_id=20) "; //SQL查询语句
  799. mysql_query("SET NAMES utf8");
  800. $rs = mysql_query($q); //获取数据集
  801. if(!$rs){die("Valid result!");}
  802. $row = mysql_fetch_array($rs);
  803. $result= $row[10] ;
  804. mysql_free_result($rs); //关闭数据集
  805. return $result;
  806. }
  807. function alluser($ucid,$website){
  808. $token_access_url = "{$website}/app/index.php?i=".$ucid."&c=entry&a=wxapp&do=Getallopenid&m=qidong_app";
  809. $res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
  810. $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP
  811. $data=$result['data'] ;
  812. return json_encode($data) ;
  813. }
  814. function jiqiren($keyword)
  815. {
  816. $token_access_url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=".urlencode($keyword);
  817. // logger( $token_access_url);
  818. $res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
  819. $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
  820. $content = $result['content'];
  821. $content=str_replace("{br}","\n",$content);
  822. return $content;
  823. }
  824. function getwxinfo($token,$openid){
  825. $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$token}&openid={$openid}&lang=zh_CN";
  826. // logger($url);
  827. $access_token_Arr = https_request($url);
  828. return $access_token_Arr;
  829. }
  830. function getToken($appid,$appsecret,$jsonid){
  831. $file = file_get_contents("json/{$jsonid}_token.json",true);
  832. $result = json_decode($file,true);
  833. if (time() > $result['expires']){
  834. $data = array();
  835. $data['access_token'] = getNewToken($appid,$appsecret);
  836. $data['expires']=time()+7200;
  837. $jsonStr = json_encode($data);
  838. $fp = fopen("json/{$jsonid}_token.json", "w");
  839. fwrite($fp, $jsonStr);
  840. fclose($fp);
  841. return $data['access_token'];
  842. }else{
  843. return $result['access_token'];
  844. }
  845. }
  846. function getNewToken($appid,$appsecret){
  847. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
  848. $access_token_Arr = https_request($url);
  849. return $access_token_Arr['access_token'];
  850. }
  851. function https_request ($url){
  852. $ch = curl_init();
  853. curl_setopt($ch, CURLOPT_URL, $url);
  854. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  855. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  856. $out = curl_exec($ch);
  857. curl_close($ch);
  858. return json_decode($out,true);
  859. }
  860. function deljson($path){//读出第一行 删除
  861. $f= fopen($path, "r");
  862. $line = fgets($f);
  863. ob_start();
  864. fpassthru($f);
  865. fclose($f);
  866. file_put_contents($path, ob_get_clean() );
  867. $nowopenid=json_decode($line, true);
  868. return $nowopenid;
  869. }
  870. function dujson($path){//读出第一行
  871. $f= fopen($path, "r");
  872. $line = fgets($f);
  873. return trim($line);
  874. }
  875. function ifpaidui($path,$openid){//查看用户是否在队列
  876. $lines = file($path);
  877. $i=0;
  878. $cf=0;
  879. foreach ($lines as $line) {
  880. $line = rtrim($line);
  881. $i++;
  882. $nowopenid=json_decode($line, true);
  883. if ($openid==$nowopenid['openid']){
  884. $cf=$i;
  885. $findedata = array(
  886. 'shunxu' => $cf,
  887. 'nickname' => $nowopenid['nickname'],
  888. 'openid'=> $nowopenid['openid'],
  889. 'id' => $nowopenid['id']
  890. );
  891. break;//找到后中止.
  892. }
  893. // more statements...
  894. }//如果没找到.
  895. if ($cf==0) {
  896. $data = array(
  897. 'shunxu' => $cf,
  898. 'nickname' => '无',
  899. 'openid'=> '',
  900. 'avartar' => ''
  901. );
  902. return $data;
  903. }
  904. else{
  905. return $findedata;
  906. }
  907. }
  908. function paiduilist($path){//查看用户是否在队列
  909. $lines = file($path);
  910. $i=0;
  911. $cf=0;
  912. foreach ($lines as $line) {
  913. $line = rtrim($line);
  914. $i++;
  915. $nowopenid=json_decode($line, true);
  916. $paidui=$paidui.'\n ID:'.$nowopenid['id'].' 昵称:'.$nowopenid['nickname'];
  917. // more statements...
  918. }//如果没找到.
  919. return $paidui;
  920. }
  921. function savejson($path,$data){ //保存1行
  922. //去除重复
  923. $lines = file($path);
  924. $i=0;
  925. $cf=0;
  926. foreach ($lines as $line) {
  927. $line = rtrim($line);
  928. $i++;
  929. // print("$line\n");
  930. if ($data==$line){
  931. $cf=$i;
  932. break;
  933. }
  934. // more statements...
  935. }
  936. if ($cf==0) {
  937. // echo "newone:\n";
  938. file_put_contents($path, $data.PHP_EOL, FILE_APPEND);
  939. return $cf;
  940. }
  941. else{
  942. return $cf;
  943. }
  944. }
  945. function edituser($ucid,$openid,$nickname,$website) //改名取号
  946. {
  947. $token_access_url = "{$website}/app/index.php?i=".$ucid."&c=entry&a=wxapp&do=edituser&m=qidong_app&openid=".$openid."&nickname=".$nickname;
  948. $res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
  949. $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP
  950. $data=$result['data'];
  951. if ($data==1){$data='取号成功!';}
  952. else{$data='取号失败!';}
  953. return $data ;
  954. }
  955. function getusernew($ucid,$openid,$nickname,$website)
  956. {
  957. $file = file_get_contents("json/{$ucid}_token.json",true);
  958. $result = json_decode($file,true);
  959. if ($openid != $result['openid']){
  960. $data = array();
  961. $token_access_url = "{$website}/app/index.php?i=".$ucid."&c=entry&a=wxapp&do=getuser&m=qidong_app&openid=".$openid."&nickname=".$nickname;
  962. // logger( $token_access_url);
  963. $res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
  964. $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP
  965. $nickname= $result['data']['wxapp'][0]['nickname'];
  966. if (trim($nickname==null)) {
  967. $nickname='会员'.$result['data']['wxapp'][0]['id'];
  968. $isid=1;
  969. }
  970. $data = array(
  971. 'nickname' => $nickname,
  972. 'id' => $result['data']['wxapp'][0]['id'],
  973. 'openid'=> $result['data']['wxapp'][0]['openid'],
  974. 'avartar' => $result['data']['wxapp'][0]['avartar']
  975. );
  976. $jsonStr = json_encode($data);
  977. $fp = fopen("json/{$ucid}_token.json", "w");
  978. fwrite($fp, $jsonStr);
  979. fclose($fp);
  980. return $data;
  981. }else{
  982. return $result;
  983. }
  984. }
  985. function hiuser($ucid,$id,$website){
  986. $token_access_url = "{$website}/app/index.php?i=".$ucid."&c=entry&a=wxapp&do=getopenid&m=qidong_app&id=".$id;
  987. $res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
  988. $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP
  989. $data=$result['data'][0] ;
  990. return $data ;
  991. }
  992. function getuser($ucid,$openid,$nickname,$website)
  993. {
  994. ///先判断缓存文件是否存在
  995. if (file_exists("json/".$ucid."_user.json")){
  996. $json = file_get_contents("json/".$ucid."_user.json");
  997. $userinfos = json_decode($json,true);
  998. $userid=$userinfos[$openid]['id'];
  999. }
  1000. if ($userid==null){
  1001. //获取微信昵称 ..??
  1002. $token_access_url = "{$website}/app/index.php?i=".$ucid."&c=entry&a=wxapp&do=getuser&m=qidong_app&openid=".$openid."&nickname=".$nickname;
  1003. // logger( $token_access_url);
  1004. $res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
  1005. $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP
  1006. $nickname= $result['data']['wxapp'][0]['nickname'];
  1007. if (trim($nickname)==null) {
  1008. $nickname='会员'.$result['data']['wxapp'][0]['id'];
  1009. $isid=1;
  1010. }
  1011. $userid=$result['data']['wxapp'][0]['id'];
  1012. }else
  1013. {
  1014. if (trim($userinfos[$openid]['nickname'])==null) {
  1015. $nickname='会员'.$userinfos[$openid]['id'];
  1016. $isid=1;
  1017. }else{
  1018. $nickname=$userinfos[$openid]['nickname'];
  1019. $userid=''.$userinfos[$openid]['id'];
  1020. }
  1021. }
  1022. //缓存 先去判断文件是否存在 .
  1023. $data = array(
  1024. 'nickname' => $nickname,
  1025. 'id' => $userid,
  1026. 'isid'=> $isid,
  1027. 'avartar' => ''
  1028. );
  1029. return $data ;
  1030. }
  1031. function changeurl($a){
  1032. $bi= strpos($a,'[[');
  1033. if ($bi==0){
  1034. return $a;
  1035. }else{
  1036. $ei= strpos($a,']]');
  1037. $qb=substr($a,0,$bi);
  1038. $eb=substr($a,$ei+2);
  1039. $b=substr($a,$bi+2,$ei-$bi-2);
  1040. $ci=strpos($b,'|');
  1041. $add=substr($b,0,$ci);
  1042. $herf=substr($b,$ci+1,strlen($b)-$ci);
  1043. }
  1044. return $qb."<a href='".$herf."'>".$add."</a>".$eb;
  1045. }
  1046. function GrabImage($url, $filename = "") {
  1047. if ($url == ""):return false;
  1048. endif;
  1049. //如果$url地址为空,直接退出
  1050. if ($filename == "") {
  1051. //如果没有指定新的文件名
  1052. $ext = strrchr($url, ".");
  1053. //得到$url的图片格式
  1054. if ($ext != ".gif" && $ext != ".jpg"):return false;
  1055. endif;
  1056. //如果图片格式不为.gif或者.jpg,直接退出
  1057. $filename = date("dMYHis") . $ext;
  1058. //用天月面时分秒来命名新的文件名
  1059. }
  1060. ob_start();//打开输出
  1061. readfile($url);//输出图片文件
  1062. $img = ob_get_contents();//得到浏览器输出
  1063. ob_end_clean();//清除输出并关闭
  1064. $size = strlen($img);//得到图片大小
  1065. $fp2 = @fopen($filename, "a");
  1066. fwrite($fp2, $img);//向当前目录写入图片文件,并重新命名
  1067. fclose($fp2);
  1068. return $filename;//返回新的文件名
  1069. }
  1070. function uploadFile($file, $type = 'image', $acctoken = '') {
  1071. // $post_data ['type'] = $type; // 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
  1072. $post_data ['media'] = '@'.$file;
  1073. //$post_data ['media'] =$file;
  1074. $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={$acctoken}&type=image";
  1075. $output=http_post_media($url, $post_data);
  1076. return $output;
  1077. }
  1078. function http_post_media($url,$strPOST)
  1079. {
  1080. $oCurl = curl_init ();
  1081. curl_setopt ( $oCurl, CURLOPT_SAFE_UPLOAD, false);
  1082. if (stripos ( $url, "https://" ) !== FALSE) {
  1083. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );
  1084. curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false );
  1085. }
  1086. curl_setopt ( $oCurl, CURLOPT_URL, $url );
  1087. curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );
  1088. curl_setopt ( $oCurl, CURLOPT_POST, true );
  1089. curl_setopt ( $oCurl, CURLOPT_POSTFIELDS, $strPOST );
  1090. $sContent = curl_exec ( $oCurl );
  1091. $aStatus = curl_getinfo ( $oCurl );
  1092. curl_close ( $oCurl );
  1093. if (intval ( $aStatus ["http_code"] ) == 200) {
  1094. return $sContent;
  1095. } else {
  1096. return false;
  1097. }
  1098. }
  1099. ?>