1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| <?php
namespace addons\shopro\controller;
use addons\shopro\library\Wechat as WechatLibrary; use think\Request;
class WechatKefu extends Base { protected $noNeedLogin = ['*']; protected $noNeedRight = ['*']; protected $app = null;
protected $Token = null; protected $EncodingAESKey = null; protected $get_accessToken_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential'; protected $send_message_url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=';
public function __construct(Request $request = null) { parent::__construct($request);
$this->Token = config('site.wechatKefuToken'); $this->EncodingAESKey = config('site.wechatKefuEncodingAESKey');
$wechat = new WechatLibrary('wxMiniProgram'); $this->app = $wechat->getApp(); }
public function index(){ if (isset($_GET['echostr'])) $this->valid(); else $this->responseMsg(); }
private function valid(){ $echoStr = $_GET["echostr"]; if($this->checkSignature()){ header('content-type:text'); echo $echoStr; exit; }else{ exit('fail! '.$echoStr.'+++'.$this->Token); } }
private function checkSignature(){ $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"];
$tmpArr = array($this->Token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr );
if($tmpStr == $signature) return true; else return false; }
private function responseMsg(){ $postStr = file_get_contents('php://input');
if(empty($postStr) && !is_string($postStr)){ echo ""; exit; }
$postArr =json_decode($postStr,true);
$fromUsername =$postArr['FromUserName']; $toUserName =$postArr['ToUserName']; if(!empty($postArr['MsgType']) &&$postArr['MsgType'] =='text'){ $textTpl =array( "ToUserName"=>$fromUsername, "FromUserName"=>$toUserName, "CreateTime"=>$_SERVER['REQUEST_TIME'], "MsgType"=>"transfer_customer_service", );
exit(json_encode($textTpl)); }elseif(!empty($postArr['MsgType']) &&$postArr['MsgType'] =='image'){ $textTpl =array( "ToUserName"=>$fromUsername, "FromUserName"=>$toUserName, "CreateTime"=>$_SERVER['REQUEST_TIME'], "MsgType"=>"transfer_customer_service", ); exit(json_encode($textTpl)); }elseif($postArr['MsgType'] =='event' &&$postArr['Event']=='user_enter_tempsession'){ $content ='您好,有什么能帮助你?'; $data=array( "touser"=>$fromUsername, "msgtype"=>"text", "text"=>array("content"=>$content) ); $json =json_encode($data,JSON_UNESCAPED_UNICODE);
$this->requestAPI($json); }else exit('Error,未知类型会话');
}
private function requestAPI($json){ $access_token = $this->get_accessToken(); $this->send_message_url = $this->send_message_url.$access_token;
$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $this->send_message_url); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($json)) curl_setopt($curl, CURLOPT_POSTFIELDS,$json);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); if (curl_errno($curl)) echo 'Errno'.curl_error($curl);
curl_close($curl);
if($output == 0) { echo 'success'; exit; } }
private function get_accessToken(){ $url = $this->get_accessToken_url.'&appid='.$this->app->config['app_id'].'&secret='.$this->app->config['secret']; @$weixin = file_get_contents($url); @$jsondecode = json_decode($weixin); @$array = get_object_vars($jsondecode); $token = $array['access_token']; return $token; }
}
|