900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 微信小程序消息推送 接收消息事件 发送客服消息

微信小程序消息推送 接收消息事件 发送客服消息

时间:2024-04-28 03:17:38

相关推荐

微信小程序消息推送 接收消息事件 发送客服消息

文档地址消息推送 | 微信开放文档

接收消息和事件 | 微信开放文档

发送客服消息 | 微信开放文档

代码参考

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./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 = '/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./cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;$response = Http::get($url);$ret = (array)json_decode($response, true);return $ret ? $ret : [];}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。