tp6微信公众号开发者模式基础消息

官方文档

https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_standard_messages.html

1:结合上篇继续再services目录下的WechatService.php继续封装  

https://www.cnblogs.com/xiaoyantongxue/p/15803334.html                                                                                                                                     


<?php

namespace app\services;

use think\Log;

class WechatService
{

    protected static $_token_key = 'wxdev:wechat:token';

    /*
     * 微信公众号获取access_token
     * */
    public static function getToken()
    {
        $accessToken = cache(self::$_token_key);
        if (!$accessToken) {
            $accessTokenUrl = sprintf(config('wechat.access_token_url'), config('wechat.appid'), config('wechat.appsecret'));
            $accessTokenArr = json_decode(file_get_contents($accessTokenUrl), true);
            if (isset($accessTokenArr['errcode'])) {
//                记录日记
                Log::info($accessTokenArr['errmsg']);
                return false;
            } else {
                $accessToken = $accessTokenArr['access_token'];
                cache(self::$_token_key, $accessToken, $accessTokenArr['expires_in'] - 200);
            }
        }
        return $accessToken;
    }

    /*
     * 基础消息能力-文本消息
     * */
    public static function testReplay($postObj)
    {
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $msgType = $postObj->MsgType;
        $keyword = trim((string)$postObj->Content);
        $time = time();
        if ($keyword == '你好') {
            $replyContent = "欢迎来到我的公众号!";
        } else {
            $replyContent = "厉害了,我的哥!!!!!";
        }
        $sendXml = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                             <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            <FuncFlag>0</FuncFlag>
                            </xml>";
        $sendBackXml = sprintf($sendXml, $fromUsername, $toUsername, $time, $msgType, $replyContent);
        return $sendBackXml;
    }

    /*
  * 基础消息能力-图片消息
  * */
    public static function imageReplay($postObj)
    {
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $time = time();
        $replyContent = "图片picUrl:{$postObj->PicUrl},图片的MediaId:{$postObj->MediaId}";
        $sendXml = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                             <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            </xml>";
        $sendBackXml = sprintf($sendXml, $fromUsername, $toUsername, $time, 'text', $replyContent);
        return $sendBackXml;
    }

    /*
     * 掌握基础消息能力-语音消息
     * */
    public static function voiceReplay($postObj)
    {
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $time = time();
        $replyContent = "语音消息MediaId为:{$postObj->MediaId}'具体内容为:{$postObj->Recognition}";
        $sendXml = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                             <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            </xml>";
        $sendBackXml = sprintf($sendXml, $fromUsername, $toUsername, $time, 'text', $replyContent);
        return $sendBackXml;
    }

    /*
   * 基础消息能力-视频消息
   * */
    public static function videoReplay($postObj)
    {
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $time = time();
        $replyContent = "视频消息MediaId为:{$postObj->MediaId}'体文件下载接口拉取数据:{$postObj->ThumbMediaId}";
        $sendXml = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                             <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            </xml>";
        $sendBackXml = sprintf($sendXml, $fromUsername, $toUsername, $time, 'text', $replyContent);
        return $sendBackXml;
    }

    /*
* -地理位置消息
* */
    public static function locationReplay($postObj)
    {
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $time = time();
        $replyContent = "经度为:{$postObj->Location_Y}'维度:{$postObj->Location_X},具体地址:{$postObj->Label}";
        $sendXml = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                             <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            </xml>";
        $sendBackXml = sprintf($sendXml, $fromUsername, $toUsername, $time, 'text', $replyContent);
        return $sendBackXml;
    }

    /*
* -基础消息能力-链接消息
* */
    public static function linkReplay($postObj)
    {
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $time = time();
        $replyContent = "消息的标题为:{$postObj->Title},描述:{$postObj->Description}";
        $sendXml = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                             <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            </xml>";
        $sendBackXml = sprintf($sendXml, $fromUsername, $toUsername, $time, 'text', $replyContent);
        return $sendBackXml;
    }

}

2:控制器进行调用:

tp6微信公众号开发者模式基础消息

 

 

<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\services\WechatService;
use think\Request;

class Wechat extends BaseController
{
    /*
     * 域名解析首方法
     * */
    public function valid()
    {
        //获取随机字符串
        $echoStr = input("echostr");
        if ($echoStr) {
// 验证接口的有效性,由于接口有效性的验证必定会传递echostr 参数
            if ($this->checkSignature()) {
                echo $echoStr;
                exit;
            }
        } else {
            return $this->responseMsg();
        }
    }

    /*
     * 解析文本、图片、语音.视频、地理、链接
     * */
    private function responseMsg()
    {
        $postStr = file_get_contents('php://input');
        libxml_disable_entity_loader(true);
        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

        switch ($postObj->MsgType) {
//            如果发过来的信息是文本,调用WechatService的testReplay方法
            case 'text':
                $sendBackXml = WechatService::testReplay($postObj); return $sendBackXml; break;
//            如果发过来的信息是图片,调用WechatService的imgReplay方法
            case 'image':
                $sendBackXml = WechatService::imageReplay($postObj); return $sendBackXml; break;
//           如果发过来的信息是语音,调用WechatService的voiceReplay方法
            case 'voice':
                $sendBackXml = WechatService::voiceReplay($postObj); return $sendBackXml; break;

 //           如果发过来的信息是视屏,调用WechatService的videoReplay方法
            case 'video':
                $sendBackXml = WechatService::videoReplay($postObj); return $sendBackXml; break;

  //           如果发过来的信息是地理位置消息,调用WechatService的videoReplay方法
            case 'location':
                $sendBackXml = WechatService::locationReplay($postObj); return $sendBackXml; break;
 //           如果发过来的信息是地理位置消息,调用WechatService的videoReplay方法
            case 'link':
                $sendBackXml = WechatService::linkReplay($postObj); return $sendBackXml; break;

        }



    }


    /*
     *  获取token
     * */
    protected function checkSignature()
    {
// 微信加密签名
        $signature = input("signature");
        $timestamp = input("timestamp");//时间戳
        $nonce = input("nonce");//随机数
        $token = "yanbing"; //token值,必须和你设置的一样
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }


    /*
     * 获取access_token
     * */
    public function getToken()
    {
        $accessToken = WechatService::getToken();
        echo $accessToken;
    }
}

3:效果图:

tp6微信公众号开发者模式基础消息

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

上一篇:快速排序(C++)


下一篇:C++实现简易计算器