微信消息类

前一阵子写 微信 相关的项目开发, 脑子一抽,把每一种微信消息编写了一个类


<?php
/**
 * 各种消息类
 * @author 蓝冰
 * @date 2016-04-21
 */

/**
 * 用户通过微信向公众号发送的消息类 基类
 * Class MsgReceive
 */
abstract class MsgReceive{
    //禁止实例化
    private function __construct(SimpleXMLElement $req)
    {
    }

    /**
     * 从GET参数中构造一个消息实例,用于开发和测试
     * @return MsgReceiveImage|MsgReceiveLink|MsgReceiveLocation|MsgReceiveShortVideo|MsgReceiveText|MsgReceiveVideo|MsgReceiveVoice
     */
    final static public function test(){
        $info=(object)$_GET;
        $instance=self::createInstance($info);
        $instance->formal=false;
        return $instance;
    }

    /**
     * 构造一个正式的请求消息实例
     * @return MsgReceiveImage|MsgReceiveLink|MsgReceiveLocation|MsgReceiveShortVideo|MsgReceiveText|MsgReceiveVideo|MsgReceiveVoice
     */
    final static public function formal(){
        $xml= $GLOBALS["HTTP_RAW_POST_DATA"];
        $wxData = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $instance=self::createInstance($wxData->children());
        $instance->formal=true;
        return $instance;
    }

    //以下是消息的常规字段,可参考微信开发文档
    public $ToUserName;
    public $FromUserName;
    public $CreateTime;
    public $MsgType;
    public $MsgId;

    /**
     * 创建一个具体的消息实例
     * @param $req
     * @return MsgReceiveImage|MsgReceiveLink|MsgReceiveLocation|MsgReceiveShortVideo|MsgReceiveText|MsgReceiveVideo|MsgReceiveVoice
     */
    static private function createInstance( $req){
        switch($req->MsgType){
            case 'text':
                $msg= new MsgReceiveText($req);
                break;
            case 'image':
                $msg=new MsgReceiveImage($req);
                break;
            case 'voice':
                $msg=new MsgReceiveVoice($req);
                break;
            case 'video':
                $msg=new MsgReceiveVideo($req);
                break;
            case 'shortvideo':
                $msg=new MsgReceiveShortVideo($req);
                break;
            case 'link':
                $msg=new MsgReceiveLink($req);
                break;
            case 'location':
                $msg=new MsgReceiveLocation($req);
                break;
            case 'event':
                $msg=MsgReceiveEvent::createEventInstance($req);
                break;
            default:
                $msg=new MsgReceiveUnknown($req);
        }

        //给消息中的常规字段赋值
        $msg->MsgType=$req->MsgType;
        $msg->ToUserName=$req->ToUserName;
        $msg->FromUserName=$req->FromUserName;
        $msg->CreateTime=$req->CreateTime;
        $msg->MsgId=$req->MsgId;

        //返回消息子类的实例
        return $msg;
    }

    //是否是正式请求
    public $formal;
}

/**
 * 接收到的文本消息
 * Class MsgReceiveText
 */
class MsgReceiveText extends MsgReceive{
    public $Content;
    protected function __construct( $req)
    {
        $this->Content=$req->Content;
    }
}

/**
 * 接收到的图片消息
 * Class MsgReceiveImage
 */
class MsgReceiveImage extends MsgReceive{
    public $PicUrl,$MediaId;
    protected function __construct( $req)
    {
        $this->PicUrl=$req->PicUrl;
        $this->MediaId=$req->MediaId;
    }
}

/**
 * 接收到的语音消息
 * Class MsgReceiveVoice
 */
class MsgReceiveVoice extends MsgReceive{
    public $MediaId,$Format,$Content;
    protected function __construct( $req)
    {
        $this->MediaId=$req->MediaId;
        $this->Format=$req->Format;
        if(isset($req->Recognition) and $req->Recognition){
            $this->Content=$req->Recognition;
        }
    }
}

/**
 * 接收到的视频消息
 * Class MsgReceiveVideo
 */
class MsgReceiveVideo extends MsgReceive
{
    public $MediaId, $ThumbMediaId;
    protected function __construct( $req)
    {
        $this->MediaId = $req->MediaId;
        $this->ThumbMediaId = $req->ThumbMediaId;
    }
}

/**
 * 接收到的短视频消息
 * Class MsgReceiveShortVideo
 */
class MsgReceiveShortVideo extends MsgReceive{
    public $MediaId, $ThumbMediaId;
    protected function __construct( $req)
    {
        $this->MediaId = $req->MediaId;
        $this->ThumbMediaId = $req->ThumbMediaId;
    }
}

/**
 * 接收到的位置信息
 * Class MsgReceiveLocation
 */
class MsgReceiveLocation extends MsgReceive
{
    public $Location_X, $Location_Y, $Scale, $Label;
    protected function __construct($req)
    {
        $this->Location_X = $req->Location_X;
        $this->Location_Y = $req->Location_Y;
        $this->Scale = $req->Scale;
        $this->Label = $req->Label;
    }
}

/**
 * 接收到的链接 消息
 * Class MsgReceiveLink
 */
class MsgReceiveLink extends  MsgReceive
{
    public $Title, $Description, $Url;
    protected function __construct( $req)
    {
        $this->Title=$req->Title;
        $this->Description=$req->Description;
        $this->Url=$req->Url;
    }
}

/**
 * 接收到的未知消息
 * Class MsgReceiveUnknown
 */
class MsgReceiveUnknown extends MsgReceive{
    public $Content;
    protected function __construct( $req)
    {
        $this->Content='无法识别的消息类型:'.$req->MsgType;
    }
}

/**
 * 接收到的事件消息
 * Class MsgReceiveEvent
 */
class MsgReceiveEvent extends MsgReceive{
    /**
     * 根据 消息类型,创建一个具体的事件消息实例
     * @param $req
     * @return MsgReceiveEventClick|MsgReceiveEventLocation|MsgReceiveEventScan|MsgReceiveEventScanSubscribe|MsgReceiveEventSubscribe|MsgReceiveEventUnSubscribe|MsgReceiveEventView
     */
    static protected function createEventInstance( $req){
        switch($req->Event){
            case 'subscribe': //关注事件
                if(isset($req->EventKey) and $req->EventKey){
                    $msg=new MsgReceiveEventScanSubscribe($req);
                }else{
                    $msg=new MsgReceiveEventSubscribe($req);
                }
                break;
            case 'unsubscribe': //取消关注事件
                $msg=new MsgReceiveEventUnSubscribe($req);
                break;
            case 'CLICK': //点击菜单
                $msg=new MsgReceiveEventClick($req);
                break;
            case 'SCAN': //扫码事件
                $msg=new MsgReceiveEventScan($req);
                break;
            case 'LOCATION': //位置事件
                $msg=new MsgReceiveEventLocation($req);
                break;
            case 'VIEW': //查看菜单事件
                $msg=new MsgReceiveEventView($req);
                break;
            default:
                $msg=new MsgReceiveEventUnknown($req);
        }

        $msg->Event=$req->Event;
        return $msg;
    }

    //事件类型
    public $Event;
}

/**
 * 关注事件消息
 * Class MsgReceiveEventSubscribe
 */
class MsgReceiveEventSubscribe extends MsgReceiveEvent{
    protected function __construct( $req)
    {
    }
}

/**
 * 取消关注事件消息
 * Class MsgReceiveEventUnSubscribe
 */
class MsgReceiveEventUnSubscribe extends MsgReceiveEvent{
    protected function __construct( $req)
    {
    }
}

/**
 * 扫码关注事件消息
 * Class MsgReceiveEventScanSubscribe
 */
class MsgReceiveEventScanSubscribe extends MsgReceiveEvent{
    public $EventKey,$Ticket;
    public $qrcode;
    protected function __construct( $req)
    {
        $this->EventKey=$req->EventKey;
        $this->Ticket=$req->Ticket;
        $this->qrcode=substr($this->EventKey,8);
    }
}

/**
 * 扫码事件消息
 * Class MsgReceiveEventScan
 */
class MsgReceiveEventScan extends MsgReceiveEvent{
    public $EventKey,$Ticket;
    protected function __construct( $req)
    {
        $this->EventKey=$req->EventKey;
        $this->Ticket=$req->Ticket;
    }
}

/**
 * 位置事件消息
 * Class MsgReceiveEventLocation
 */
class MsgReceiveEventLocation extends MsgReceiveEvent{
    public $Latitude,$Longitude,$Precision;
    protected function __construct( $req)
    {
        $this->Latitude=$req->Latitude;
        $this->Longitude=$req->Longitude;
        $this->Precision=$req->Precision;
    }
}

/**
 * 点击事件消息
 * Class MsgReceiveEventClick
 */
class MsgReceiveEventClick extends MsgReceiveEvent{
    public $EventKey;
    protected function __construct( $req)
    {
        $this->EventKey=$req->EventKey;
    }
}

/**
 * 查看菜单事件消息
 * Class MsgReceiveEventView
 */
class MsgReceiveEventView extends MsgReceiveEvent{
    public $EventKey;
    protected function __construct( $req)
    {
        $this->EventKey=$req->EventKey;
    }
}

/**
 * 未知事件消息
 * Class MsgReceiveEventUnknown
 */
class MsgReceiveEventUnknown extends MsgReceiveEvent{
    public $Content;
    protected function __construct( $req)
    {
        $this->Content='无法识别的事件消息类型:'.$req->Event;
    }
}

/**
 * 用来回复的消息基类
 * Class MsgReply
 */
abstract class MsgReply{
    //消息中的常规字段,请参考 微信 开发文档
    public $ToUserName;
    public $FromUserName;
    public $CreateTime;
    public $MsgType;

    //禁止实例化
    protected function __construct()
    {
        $this->CreateTime=time();
    }

    /**
     * 说明本消息是用来回复谁的
     * @param MsgReceive $msgReceive 请求的消息
     */
    public function replyFrom(MsgReceive $msgReceive){
        $this->ToUserName=$msgReceive->FromUserName;
        $this->FromUserName=$msgReceive->ToUserName;
    }

    /**
     * 为XML附加 消息的外轮廓
     * @param $xml
     * @return string
     */
    protected function bound($xml){
        return
            '<xml>'.
                cdata('ToUserName',$this->ToUserName).
                cdata('FromUserName',$this->FromUserName).
                '<CreateTime>'.time().'</CreateTime>'.
                cdata('MsgType',$this->MsgType).
                $xml.
            '</xml>';
    }

    /**
     * 每个回复的消息必须 实现 的方法,生成消息的XML
     * @return mixed
     */
    abstract public function toXML();
}

/**
 * 回复的文本消息
 * Class MsgReplyText
 */
class MsgReplyText extends MsgReply{
    public $Content;
    public function __construct($Content)
    {
        parent::__construct();
        $this->MsgType='text';
        $this->Content=$Content;
    }
    public function toXML()
    {
        $xml=cdata('Content',$this->Content);
        return self::bound($xml);
    }
}

/**
 * 回复的图片消息
 * Class MsgReplyImage
 */
class MsgReplyImage extends MsgReply{
    public $MediaId;
    public function __construct($MediaId)
    {
        parent::__construct();
        $this->MsgType='image';
        $this->MediaId=$MediaId;
    }
    public function toXML()
    {
        $xml='<Ima'.'ge>'.cdata('MediaId',$this->MediaId).'</Image>';
        return self::bound($xml);
    }
}

/**
 * 回复的语音消息
 * Class MsgReplyVoice
 */
class MsgReplyVoice extends MsgReply{
    public $MediaId;
    public function __construct($MediaId)
    {
        parent::__construct();
        $this->MsgType='voice';
        $this->MediaId=$MediaId;
    }
    public function toXML()
    {
        $xml='<Voice>'.cdata('MediaId',$this->MediaId).'</Voice>';
        return self::bound($xml);
    }
}

/**
 * 回复的视频消息
 * Class MsgReplyVideo
 */
class MsgReplyVideo extends MsgReply{
    public $MediaId,$Title,$Description;
    public function __construct($MediaId,$Title,$Description)
    {
        parent::__construct();
        $this->MsgType='video';
        $this->MediaId=$MediaId;
        $this->Title=$Title;
        $this->Description=$Description;
    }
    public function toXML()
    {
        $xml='<Video>'.
                cdata('MediaId',$this->MediaId).
                cdata('Title',$this->Title).
                cdata('Description',$this->Description).
            '</Video>';
        return self::bound($xml);
    }
}

/**
 * 回复的音乐消息
 * Class MsgReplyMusic
 */
class MsgReplyMusic extends MsgReply{
    public $Title,$Description,$MusicUrl,$HQMusicUrl,$ThumbMediaId;
    public function __construct($Title,$Description,$MusicUrl,$HQMusicUrl,$ThumbMediaId)
    {
        parent::__construct();
        $this->MsgType='music';
        $this->Title=$Title;
        $this->Description=$Description;
        $this->MusicUrl=$MusicUrl;
        $this->HQMusicUrl=$HQMusicUrl;
        $this->ThumbMediaId=$ThumbMediaId;
    }
    public function toXML()
    {
        $xml='<Music>'.
            cdata('Title',$this->Title).
            cdata('Description',$this->Description).
            cdata('MusicUrl',$this->MusicUrl).
            cdata('HQMusicUrl',$this->HQMusicUrl).
            cdata('ThumbMediaId',$this->ThumbMediaId).
            '</Music>';
        return self::bound($xml);
    }
}

/**
 * 回复的图文消息中的消息项
 * Class MsgReplyNewsItem
 */
class MsgReplyNewsItem{
    public $Title,$Description,$PicUrl,$Url;
    public function __construct($Title,$Description,$PicUrl,$Url)
    {
        $this->Title=$Title;
        $this->Description=$Description;
        $this->PicUrl=$PicUrl;
        $this->Url=$Url;
    }
    public function toXML()
    {
        $xml='<item>'.cdatas([
                'Title'=>$this->Title,
                'Description'=>$this->Description,
                'PicUrl'=>$this->PicUrl,
                'Url'=>$this->Url
            ]).
            '</item>';
        return $xml;
    }
}

/**
 * 回复的图文消息
 * Class MsgReplyNews
 */
class MsgReplyNews extends MsgReply{

    public $Articles;
    public function __construct(array $Articles=array())
    {
        parent::__construct();
        $this->MsgType='news';
        $this->Articles=$Articles;
    }

    /**
     * 增加一条图文消息项
     * @param MsgReplyNewsItem $item
     */
    public function add(MsgReplyNewsItem $item){
        $this->Articles[]=$item;
    }
    public function toXML()
    {
        if(count($this->Articles)>10){
            throw new Exception('图文格式的回复消息最多只允许10项内容');
        }

        $xml='';
        foreach($this->Articles as $article){
            /* @var $article MsgReplyNewsItem */
            $xml.=$article->toXML();
        }

        $xml='<ArticleCount>'.count($this->Articles).'</ArticleCount>'.
        $xml='<Articles>'.$xml.'</Articles>';
        return self::bound($xml);
    }
}

/**
 * 用来主动向用户发送的消息基类
 * Class MsgSend
 */
abstract class MsgSend{
    public $msgtype;
    public $touser;
    /**
     * 为消息结果XML附加 外轮廓
     * @param array $info
     * @return string
     */
    protected function bound(array $info){
        return json_encode(array(
            'touser'=>$this->touser,
            'msgtype'=>$this->msgtype,
            $this->msgtype=>$info
        ),JSON_UNESCAPED_UNICODE);
    }

    /**
     * 每个子类必须实现 的生成JSON串的方法
     * @return string
     */
    abstract public function toJson();
}

/**
 * 用来发送的文本消息
 * Class MsgSendText
 */
class MsgSendText extends MsgSend{
    public $content;
    public function __construct($touser,$content)
    {
        $this->touser=$touser;
        $this->msgtype='text';
        $this->content=$content;
    }
    public function toJson(){
        return parent::bound(array('content'=>$this->content));
    }
}

/**
 * 用来发送的图片消息
 * Class MsgSendImage
 */
class MsgSendImage extends MsgSend{
    public $media_id;
    public function __construct($touser,$media_id)
    {
        $this->touser=$touser;
        $this->msgtype='image';
        $this->media_id=$media_id;
    }
    public function toJson(){
        return parent::bound(array('media_id'=>$this->media_id));
    }
}

/**
 * 用来发送的语音消息
 * Class MsgSendVoice
 */
class MsgSendVoice extends MsgSend{
    public $media_id;
    public function __construct($touser,$media_id)
    {
        $this->touser=$touser;
        $this->msgtype='voice';
        $this->media_id=$media_id;
    }
    public function toJson(){
        return parent::bound(array('media_id'=>$this->media_id));
    }
}

/**
 * 用来发送的视频消息
 * Class MsgSendVideo
 */
class MsgSendVideo extends MsgSend{
    public $media_id,$thumb_media_id,$title,$description;
    public function __construct($touser,$media_id,$thumb_media_id,$title,$description)
    {
        $this->touser=$touser;
        $this->msgtype='video';
        $this->media_id=$media_id;
        $this->thumb_media_id=$thumb_media_id;
        $this->title=$title;
        $this->description=$description;
    }
    public function toJson(){
        return parent::bound(array(
            'media_id'=>$this->media_id,
            'thumb_media_id'=>$this->thumb_media_id,
            'title'=>$this->title,
            'description'=>$this->description
        ));
    }
}

/**
 * 用来发送的音乐消息
 * Class MsgSendMusic
 */
class MsgSendMusic extends MsgSend{
    public $title,$description,$musicurl,$hqmusicurl,$thumb_media_id;
    public function __construct($touser,$title,$description,$musicurl,$hqmusicurl,$thumb_media_id)
    {
        $this->touser=$touser;
        $this->msgtype='video';
        $this->title=$title;
        $this->description=$description;
        $this->musicurl=$musicurl;
        $this->hqmusicurl=$hqmusicurl;
        $this->thumb_media_id=$thumb_media_id;
    }
    public function toJson(){
        return parent::bound(array(
            'title'=>$this->title,
            'description'=>$this->description,
            'musicurl'=>$this->musicurl,
            'hqmusicurl'=>$this->hqmusicurl,
            'thumb_media_id'=>$this->thumb_media_id,
        ));
    }
}

/**
 * 用来发送的图文消息中的消息项
 * Class MsgSendNewsItem
 */
class MsgSendNewsItem{
    public $title,$description,$url,$picurl;
    public function __construct($title,$description,$url,$picurl)
    {
        $this->title=$title;
        $this->description=$description;
        $this->url=$url;
        $this->picurl=$picurl;
    }
    public function toArray(){
        return array(
            'title'=>$this->title,
            'description'=>$this->description,
            'url'=>$this->url,
            'picurl'=>$this->picurl
        );
    }
}

/**
 * 用来发送的图文消息
 * Class MsgSendNews
 */
class MsgSendNews extends MsgSend{
    public $articles;
    public function __construct($touser,array $articles=array())
    {
        $this->touser=$touser;
        $this->msgtype='news';
        $this->articles=$articles;
    }
    public function add(MsgSendNewsItem $item){
        $this->articles[]=$item;
    }
    public function toJson(){
        if(count($this->articles)>8){
            throw new Exception('图文格式的发送消息最多允许8条内容');
        }
        $array=[];
        foreach($this->articles as $article){
            /* @var $article MsgSendNewsItem */
            $array[]=$article->toArray();
        }
        return parent::bound(array(
            'articles'=>$array
        ));
    }
}

/**
 * 用来发送的多图文消息
 * Class MsgSendMpNews
 */
class MsgSendMpNews extends MsgSend{
    public $media_id;
    public function __construct($touser,$media_id)
    {
        $this->touser=$touser;
        $this->msgtype='mpnews';
        $this->media_id=$media_id;
    }
    public function toJson(){
        return parent::bound(array(
            'media_id'=>$this->media_id
        ));
    }
}

/**
 * 用来发送的卡券消息
 * Class MsgSendWxCard
 */
class MsgSendWxCard extends MsgSend{
    public $card_id,$card_ext;
    public function __construct($touser,$card_id,$card_ext)
    {
        $this->touser=$touser;
        $this->msgtype='wxcard';
        $this->card_id=$card_id;
        $this->card_ext=$card_ext;
    }
    public function toJson(){
        return parent::bound(array(
            'card_id'=>$this->card_id,
            'card_ext'=>$this->card_ext
        ));
    }
}

/**
 * 永久图文素材,最多8段限制
 */
class MaterialNews{
    private $data;
    public function add($title,$thumb_media_id,$author,$digest,$show_cover_pic,$content,$content_source_url)
    {
        $this->data[]=array(
            'title'=>$title,
            'thumb_media_id'=>$thumb_media_id,
            'author'=>$author,
            'digest'=>$digest,
            'show_cover_pic'=>intval($show_cover_pic),
            'content'=>$content,
            'content_source_url'=>$content_source_url
        );
    }

    public function toJSON(){
        if(count($this->data)>8){
            throw new Exception('永久图文素材最多允许8项内容');
        }
        return json_encode(array('articles'=>$this->data));
    }
}


//以上36个类,只能证明我得了中二病而且忘记吃药了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值