文档地址消息推送 | 微信开放文档
接收消息和事件 | 微信开放文档
发送客服消息 | 微信开放文档
文章来源:https://www.toymoban.com/news/detail-520273.html
代码参考文章来源地址https://www.toymoban.com/news/detail-520273.html
public function xcxToken(){
$signature = $this->request->request('signature') ? $this->request->request('signature') : '';
$timestamp = $this->request->request('timestamp') ? $this->request->request('timestamp') : '';
$nonce = $this->request->request('nonce') ? $this->request->request('nonce') : '';
$echostr = $this->request->request('echostr') ? $this->request->request('echostr') : '';
if($echostr){
$token = 'xcxToken'; //对应微信公众平台配置的token
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
echo $_GET['echostr'];
exit;
}else{
return false;
}
}else{
$event_message = file_get_contents("php://input");
// $server_document = str_replace("/public","",$_SERVER['DOCUMENT_ROOT']);
// file_put_contents($server_document.'/runtime/xcx_message.txt', PHP_EOL.$event_message.PHP_EOL, FILE_APPEND);
$message = json_decode($event_message,true);
if($message['MsgType'] == 'miniprogrampage'){
$tokenInfo = $this->getXcxAccessToken();
$token = $tokenInfo['access_token'];
$send_url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$token;
$picurl = $this->getUrl('/assets/img/config_init/img_name.jpg');
$user_id = db('user')->where('xcx_openid',$message['FromUserName'])->value('id');//用户id
$url = 'https://www.xxx.com/index/test/index?user_id='.$user_id;
$query_data = [
"touser" => $message['FromUserName'],
"msgtype" => "link",
"link" => [
"title" => '消息标题',//消息标题
"description" => '图文链接消息',//图文链接消息
"url" => $url,//图文链接消息被点击后跳转的链接
"thumb_url" => $picurl,//图文链接消息的图片链接,支持 JPG、PNG 格式,较好的效果为大图 640 X 320,小图 80 X 80
],
];
Http::post($send_url,json_encode($query_data,JSON_UNESCAPED_UNICODE));
}
}
}
public function getUrl($str){
if(strpos($str,'https://') === false && strpos($str,'http://') === false){
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
return $http_type . $_SERVER['HTTP_HOST'] . $str;
}
return $str;
}
public function getXcxAccessToken()
{
$wechat = Config::get('site.wechatapp');
$appid = $wechat['app_id'];
$secret = $wechat['app_secret'];
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$response = Http::get($url);
$ret = (array)json_decode($response, true);
return $ret ? $ret : [];
}
到了这里,关于微信小程序消息推送、接收消息事件、发送客服消息的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!