| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- <?php
- include_once(__DIR__."/../../utils/error.inc.php");
- include_once(__DIR__."/../../utils/Utils.class.php");
- class Message
- {
- public $sendToAll = false; // bool, 是否全员发送, 即文档所谓 @all
- public $touser = array(); // string array
- public $toparty = array(); // uint array
- public $totag = array(); // uint array
- public $agentid = null; // uint
- public $safe = null; // uint, 表示是否是保密消息,0表示否,1表示是,默认0
- public $messageContent = null; // xxxMessageContent
- public function CheckMessageSendArgs()
- {
- if (count($this->touser) > 1000) throw new QyApiError("touser should be no more than 1000");
- if (count($this->toparty) > 100) throw new QyApiError("toparty should be no more than 100");
- if (count($this->totag) > 100) throw new QyApiError("toparty should be no more than 100");
- if (is_null($this->messageContent)) throw new QyApiError("messageContent is empty");
- $this->messageContent->CheckMessageSendArgs();
- }
- public function Message2Array()
- {
- $args = array();
- if (true == $this->sendToAll) {
- Utils::setIfNotNull("@all", "touser", $args);
- } else {
- //
- $touser_string = null;
- foreach($this->touser as $item) {
- $touser_string = $touser_string . $item . "|";
- }
- Utils::setIfNotNull($touser_string, "touser", $args);
- //
- $toparty_string = null;
- foreach($this->toparty as $item) {
- $toparty_string = $toparty_string . $item . "|";
- }
- Utils::setIfNotNull($toparty_string, "toparty", $args);
- //
- $totag_string = null;
- foreach($this->totag as $item) {
- $totag_string = $totag_string . $item . "|";
- }
- Utils::setIfNotNull($totag_string, "totag", $args);
- }
- Utils::setIfNotNull($this->agentid, "agentid", $args);
- Utils::setIfNotNull($this->safe, "safe", $args);
- $this->messageContent->MessageContent2Array($args);
- return $args;
- }
- }
- // --------------------- 各种类型的 MessageContent -----------------------------
- //
- class TextMessageContent
- {
- public $msgtype = "text";
- private $content = null; // string
- public function __construct($content=null)
- {
- $this->content = $content;
- }
- public function CheckMessageSendArgs()
- {
- $len = strlen($this->content);
- if ($len == 0 || $len > 2048) {
- throw new QyApiError("invalid content length " . $len);
- }
- }
- public function MessageContent2Array(&$arr)
- {
- Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
- $contentArr = array("content" => $this->content);
- Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
- }
- }
- class ImageMessageContent
- {
- public $msgtype = "image";
- private $media_id = null; // string
- public function __construct($media_id=null)
- {
- $this->media_id = $media_id;
- }
- public function CheckMessageSendArgs()
- {
- Utils::checkNotEmptyStr($this->media_id, "media_id");
- }
- public function MessageContent2Array(&$arr)
- {
- Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
- $contentArr = array("media_id" => $this->media_id);
- Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
- }
- }
- class VoiceMessageContent
- {
- public $msgtype = "voice";
- private $media_id = null; // string
- public function __construct($media_id=null)
- {
- $this->media_id = $media_id;
- }
- public function CheckMessageSendArgs()
- {
- Utils::checkNotEmptyStr($this->media_id, "media_id");
- }
- public function MessageContent2Array(&$arr)
- {
- Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
- $contentArr = array("media_id" => $this->media_id);
- Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
- }
- }
- class VideoMessageContent
- {
- public $msgtype = "video";
- public $media_id = null; // string
- public $title = null; // string
- public $description = null; // string
- public function __construct($media_id=null, $title=null, $description=null)
- {
- $this->media_id = $media_id;
- $this->title = $title;
- $this->description = $description;
- }
- public function CheckMessageSendArgs()
- {
- Utils::checkNotEmptyStr($this->media_id, "media_id");
- }
- public function MessageContent2Array(&$arr)
- {
- Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
- $contentArr = array();
- {
- Utils::setIfNotNull($this->media_id, "media_id", $contentArr);
- Utils::setIfNotNull($this->title, "title", $contentArr);
- Utils::setIfNotNull($this->description, "description", $contentArr);
- }
- Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
- }
- }
- class FileMessageContent
- {
- public $msgtype = "file";
- public $media_id = null; // string
- public function __construct($media_id=null)
- {
- $this->media_id = $media_id;
- }
- public function CheckMessageSendArgs()
- {
- Utils::checkNotEmptyStr($this->media_id, "media_id");
- }
- public function MessageContent2Array(&$arr)
- {
- Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
- $contentArr = array();
- {
- Utils::setIfNotNull($this->media_id, "media_id", $contentArr);
- }
- Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
- }
- }
- class TextCardMessageContent
- {
- public $msgtype = "textcard";
- public $title = null; // string
- public $description = null; // string
- public $url = null; // string
- public $btntxt = null; // string
- public function __construct($title=null, $description=null, $url=null, $btntxt=null)
- {
- $this->title = $title;
- $this->description = $description;
- $this->url = $url;
- $this->btntxt = $btntxt;
- }
- public function CheckMessageSendArgs()
- {
- Utils::checkNotEmptyStr($this->title, "title");
- Utils::checkNotEmptyStr($this->description, "description");
- Utils::checkNotEmptyStr($this->url, "url");
- }
- public function MessageContent2Array(&$arr)
- {
- Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
- $contentArr = array();
- {
- Utils::setIfNotNull($this->title, "title", $contentArr);
- Utils::setIfNotNull($this->description, "description", $contentArr);
- Utils::setIfNotNull($this->url, "url", $contentArr);
- Utils::setIfNotNull($this->btntxt, "btntxt", $contentArr);
- }
- Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
- }
- }
- class NewsArticle {
- public $title = null; // string
- public $description = null; // string
- public $url = null; // string
- public $picurl = null; // string
- public $btntxt = null; // string
- public function __construct($title=null, $description=null, $url=null, $picurl=null, $btntxt=null)
- {
- $this->title = $title;
- $this->description = $description;
- $this->url = $url;
- $this->picurl = $picurl;
- $this->btntxt = $btntxt;
- }
- public function CheckMessageSendArgs()
- {
- Utils::checkNotEmptyStr($this->title, "title");
- Utils::checkNotEmptyStr($this->url, "url");
- }
- public function Article2Array()
- {
- $args = array();
- Utils::setIfNotNull($this->title, "title", $args);
- Utils::setIfNotNull($this->description, "description", $args);
- Utils::setIfNotNull($this->url, "url", $args);
- Utils::setIfNotNull($this->picurl, "picurl", $args);
- Utils::setIfNotNull($this->btntxt, "btntxt", $args);
- return $args;
- }
- }
- class NewsMessageContent
- {
- public $msgtype = "news";
- public $articles = array(); // NewsArticle array
- public function __construct($articles)
- {
- $this->articles = $articles;
- }
- public function CheckMessageSendArgs()
- {
- $size = count($this->articles);
- if ($size < 1 || $size > 8) throw QyApiError("1~8 articles should be given");
- foreach($this->articles as $item) {
- $item->CheckMessageSendArgs();
- }
- }
- public function MessageContent2Array(&$arr)
- {
- Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
- $articleList = array();
- foreach($this->articles as $item) {
- $articleList[] = $item->Article2Array();
- }
- $arr[$this->msgtype]["articles"] = $articleList;
- }
- }
- class MpNewsArticle {
- public $title = null; // string
- public $thumb_media_id = null; // string
- public $author = null; // string
- public $content_source_url = null; // string
- public $content = null; // string
- public $digest = null; // string
- public function __construct(
- $title=null,
- $thumb_media_id=null,
- $author=null,
- $content_source_url=null,
- $content=null,
- $digest=null)
- {
- $this->title = $title;
- $this->thumb_media_id = $thumb_media_id;
- $this->author = $author;
- $this->content_source_url = $content_source_url;
- $this->content = $content;
- $this->digest = $digest;
- }
- public function CheckMessageSendArgs()
- {
- Utils::checkNotEmptyStr($this->title, "title");
- Utils::checkNotEmptyStr($this->thumb_media_id, "thumb_media_id");
- Utils::checkNotEmptyStr($this->content, "content");
- }
- public function Article2Array()
- {
- $args = array();
- Utils::setIfNotNull($this->title, "title", $args);
- Utils::setIfNotNull($this->thumb_media_id , "thumb_media_id", $args);
- Utils::setIfNotNull($this->author, "author", $args);
- Utils::setIfNotNull($this->content_source_url, "content_source_url", $args);
- Utils::setIfNotNull($this->content, "content", $args);
- Utils::setIfNotNull($this->digest, "digest", $args);
- return $args;
- }
- }
- class MpNewsMessageContent
- {
- public $msgtype = "mpnews";
- public $articles = array(); // MpNewsArticle array
- public function __construct($articles)
- {
- $this->articles = $articles;
- }
- public function CheckMessageSendArgs()
- {
- $size = count($this->articles);
- if ($size < 1 || $size > 8) throw QyApiError("1~8 articles should be given");
- foreach($this->articles as $item) {
- $item->CheckMessageSendArgs();
- }
- }
- public function MessageContent2Array(&$arr)
- {
- Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
- $articleList = array();
- foreach($this->articles as $item) {
- $articleList[] = $item->Article2Array();
- }
- $arr[$this->msgtype]["articles"] = $articleList;
- }
- }
|