Message.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. include_once(__DIR__."/../../utils/error.inc.php");
  3. include_once(__DIR__."/../../utils/Utils.class.php");
  4. class Message
  5. {
  6. public $sendToAll = false; // bool, 是否全员发送, 即文档所谓 @all
  7. public $touser = array(); // string array
  8. public $toparty = array(); // uint array
  9. public $totag = array(); // uint array
  10. public $agentid = null; // uint
  11. public $safe = null; // uint, 表示是否是保密消息,0表示否,1表示是,默认0
  12. public $messageContent = null; // xxxMessageContent
  13. public function CheckMessageSendArgs()
  14. {
  15. if (count($this->touser) > 1000) throw new QyApiError("touser should be no more than 1000");
  16. if (count($this->toparty) > 100) throw new QyApiError("toparty should be no more than 100");
  17. if (count($this->totag) > 100) throw new QyApiError("toparty should be no more than 100");
  18. if (is_null($this->messageContent)) throw new QyApiError("messageContent is empty");
  19. $this->messageContent->CheckMessageSendArgs();
  20. }
  21. public function Message2Array()
  22. {
  23. $args = array();
  24. if (true == $this->sendToAll) {
  25. Utils::setIfNotNull("@all", "touser", $args);
  26. } else {
  27. //
  28. $touser_string = null;
  29. foreach($this->touser as $item) {
  30. $touser_string = $touser_string . $item . "|";
  31. }
  32. Utils::setIfNotNull($touser_string, "touser", $args);
  33. //
  34. $toparty_string = null;
  35. foreach($this->toparty as $item) {
  36. $toparty_string = $toparty_string . $item . "|";
  37. }
  38. Utils::setIfNotNull($toparty_string, "toparty", $args);
  39. //
  40. $totag_string = null;
  41. foreach($this->totag as $item) {
  42. $totag_string = $totag_string . $item . "|";
  43. }
  44. Utils::setIfNotNull($totag_string, "totag", $args);
  45. }
  46. Utils::setIfNotNull($this->agentid, "agentid", $args);
  47. Utils::setIfNotNull($this->safe, "safe", $args);
  48. $this->messageContent->MessageContent2Array($args);
  49. return $args;
  50. }
  51. }
  52. // --------------------- 各种类型的 MessageContent -----------------------------
  53. //
  54. class TextMessageContent
  55. {
  56. public $msgtype = "text";
  57. private $content = null; // string
  58. public function __construct($content=null)
  59. {
  60. $this->content = $content;
  61. }
  62. public function CheckMessageSendArgs()
  63. {
  64. $len = strlen($this->content);
  65. if ($len == 0 || $len > 2048) {
  66. throw new QyApiError("invalid content length " . $len);
  67. }
  68. }
  69. public function MessageContent2Array(&$arr)
  70. {
  71. Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
  72. $contentArr = array("content" => $this->content);
  73. Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
  74. }
  75. }
  76. class ImageMessageContent
  77. {
  78. public $msgtype = "image";
  79. private $media_id = null; // string
  80. public function __construct($media_id=null)
  81. {
  82. $this->media_id = $media_id;
  83. }
  84. public function CheckMessageSendArgs()
  85. {
  86. Utils::checkNotEmptyStr($this->media_id, "media_id");
  87. }
  88. public function MessageContent2Array(&$arr)
  89. {
  90. Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
  91. $contentArr = array("media_id" => $this->media_id);
  92. Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
  93. }
  94. }
  95. class VoiceMessageContent
  96. {
  97. public $msgtype = "voice";
  98. private $media_id = null; // string
  99. public function __construct($media_id=null)
  100. {
  101. $this->media_id = $media_id;
  102. }
  103. public function CheckMessageSendArgs()
  104. {
  105. Utils::checkNotEmptyStr($this->media_id, "media_id");
  106. }
  107. public function MessageContent2Array(&$arr)
  108. {
  109. Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
  110. $contentArr = array("media_id" => $this->media_id);
  111. Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
  112. }
  113. }
  114. class VideoMessageContent
  115. {
  116. public $msgtype = "video";
  117. public $media_id = null; // string
  118. public $title = null; // string
  119. public $description = null; // string
  120. public function __construct($media_id=null, $title=null, $description=null)
  121. {
  122. $this->media_id = $media_id;
  123. $this->title = $title;
  124. $this->description = $description;
  125. }
  126. public function CheckMessageSendArgs()
  127. {
  128. Utils::checkNotEmptyStr($this->media_id, "media_id");
  129. }
  130. public function MessageContent2Array(&$arr)
  131. {
  132. Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
  133. $contentArr = array();
  134. {
  135. Utils::setIfNotNull($this->media_id, "media_id", $contentArr);
  136. Utils::setIfNotNull($this->title, "title", $contentArr);
  137. Utils::setIfNotNull($this->description, "description", $contentArr);
  138. }
  139. Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
  140. }
  141. }
  142. class FileMessageContent
  143. {
  144. public $msgtype = "file";
  145. public $media_id = null; // string
  146. public function __construct($media_id=null)
  147. {
  148. $this->media_id = $media_id;
  149. }
  150. public function CheckMessageSendArgs()
  151. {
  152. Utils::checkNotEmptyStr($this->media_id, "media_id");
  153. }
  154. public function MessageContent2Array(&$arr)
  155. {
  156. Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
  157. $contentArr = array();
  158. {
  159. Utils::setIfNotNull($this->media_id, "media_id", $contentArr);
  160. }
  161. Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
  162. }
  163. }
  164. class TextCardMessageContent
  165. {
  166. public $msgtype = "textcard";
  167. public $title = null; // string
  168. public $description = null; // string
  169. public $url = null; // string
  170. public $btntxt = null; // string
  171. public function __construct($title=null, $description=null, $url=null, $btntxt=null)
  172. {
  173. $this->title = $title;
  174. $this->description = $description;
  175. $this->url = $url;
  176. $this->btntxt = $btntxt;
  177. }
  178. public function CheckMessageSendArgs()
  179. {
  180. Utils::checkNotEmptyStr($this->title, "title");
  181. Utils::checkNotEmptyStr($this->description, "description");
  182. Utils::checkNotEmptyStr($this->url, "url");
  183. }
  184. public function MessageContent2Array(&$arr)
  185. {
  186. Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
  187. $contentArr = array();
  188. {
  189. Utils::setIfNotNull($this->title, "title", $contentArr);
  190. Utils::setIfNotNull($this->description, "description", $contentArr);
  191. Utils::setIfNotNull($this->url, "url", $contentArr);
  192. Utils::setIfNotNull($this->btntxt, "btntxt", $contentArr);
  193. }
  194. Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
  195. }
  196. }
  197. class NewsArticle {
  198. public $title = null; // string
  199. public $description = null; // string
  200. public $url = null; // string
  201. public $picurl = null; // string
  202. public $btntxt = null; // string
  203. public function __construct($title=null, $description=null, $url=null, $picurl=null, $btntxt=null)
  204. {
  205. $this->title = $title;
  206. $this->description = $description;
  207. $this->url = $url;
  208. $this->picurl = $picurl;
  209. $this->btntxt = $btntxt;
  210. }
  211. public function CheckMessageSendArgs()
  212. {
  213. Utils::checkNotEmptyStr($this->title, "title");
  214. Utils::checkNotEmptyStr($this->url, "url");
  215. }
  216. public function Article2Array()
  217. {
  218. $args = array();
  219. Utils::setIfNotNull($this->title, "title", $args);
  220. Utils::setIfNotNull($this->description, "description", $args);
  221. Utils::setIfNotNull($this->url, "url", $args);
  222. Utils::setIfNotNull($this->picurl, "picurl", $args);
  223. Utils::setIfNotNull($this->btntxt, "btntxt", $args);
  224. return $args;
  225. }
  226. }
  227. class NewsMessageContent
  228. {
  229. public $msgtype = "news";
  230. public $articles = array(); // NewsArticle array
  231. public function __construct($articles)
  232. {
  233. $this->articles = $articles;
  234. }
  235. public function CheckMessageSendArgs()
  236. {
  237. $size = count($this->articles);
  238. if ($size < 1 || $size > 8) throw QyApiError("1~8 articles should be given");
  239. foreach($this->articles as $item) {
  240. $item->CheckMessageSendArgs();
  241. }
  242. }
  243. public function MessageContent2Array(&$arr)
  244. {
  245. Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
  246. $articleList = array();
  247. foreach($this->articles as $item) {
  248. $articleList[] = $item->Article2Array();
  249. }
  250. $arr[$this->msgtype]["articles"] = $articleList;
  251. }
  252. }
  253. class MpNewsArticle {
  254. public $title = null; // string
  255. public $thumb_media_id = null; // string
  256. public $author = null; // string
  257. public $content_source_url = null; // string
  258. public $content = null; // string
  259. public $digest = null; // string
  260. public function __construct(
  261. $title=null,
  262. $thumb_media_id=null,
  263. $author=null,
  264. $content_source_url=null,
  265. $content=null,
  266. $digest=null)
  267. {
  268. $this->title = $title;
  269. $this->thumb_media_id = $thumb_media_id;
  270. $this->author = $author;
  271. $this->content_source_url = $content_source_url;
  272. $this->content = $content;
  273. $this->digest = $digest;
  274. }
  275. public function CheckMessageSendArgs()
  276. {
  277. Utils::checkNotEmptyStr($this->title, "title");
  278. Utils::checkNotEmptyStr($this->thumb_media_id, "thumb_media_id");
  279. Utils::checkNotEmptyStr($this->content, "content");
  280. }
  281. public function Article2Array()
  282. {
  283. $args = array();
  284. Utils::setIfNotNull($this->title, "title", $args);
  285. Utils::setIfNotNull($this->thumb_media_id , "thumb_media_id", $args);
  286. Utils::setIfNotNull($this->author, "author", $args);
  287. Utils::setIfNotNull($this->content_source_url, "content_source_url", $args);
  288. Utils::setIfNotNull($this->content, "content", $args);
  289. Utils::setIfNotNull($this->digest, "digest", $args);
  290. return $args;
  291. }
  292. }
  293. class MpNewsMessageContent
  294. {
  295. public $msgtype = "mpnews";
  296. public $articles = array(); // MpNewsArticle array
  297. public function __construct($articles)
  298. {
  299. $this->articles = $articles;
  300. }
  301. public function CheckMessageSendArgs()
  302. {
  303. $size = count($this->articles);
  304. if ($size < 1 || $size > 8) throw QyApiError("1~8 articles should be given");
  305. foreach($this->articles as $item) {
  306. $item->CheckMessageSendArgs();
  307. }
  308. }
  309. public function MessageContent2Array(&$arr)
  310. {
  311. Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
  312. $articleList = array();
  313. foreach($this->articles as $item) {
  314. $articleList[] = $item->Article2Array();
  315. }
  316. $arr[$this->msgtype]["articles"] = $articleList;
  317. }
  318. }