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

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

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

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

代码参考

    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 : [];
    }

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木子李0531

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值