MessageInterface.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace components\components\messageQueue;
  3. use Yii;
  4. use yii\base\Component;
  5. /**
  6. * MessageInterface
  7. * 消息队列 接口
  8. * -----------------
  9. * @author Verdient。
  10. * @version 1.0.0
  11. */
  12. class MessageInterface extends Component {
  13. /**
  14. * @var public String $key
  15. * 签名密钥
  16. * -----------------------
  17. * @method Config
  18. * @author Verdient。
  19. */
  20. public $key;
  21. /**
  22. * @var public $protocol
  23. * 访问协议
  24. * ---------------------
  25. * @method Config
  26. * @author Verdient。
  27. */
  28. public $protocol = '';
  29. /**
  30. * @var public $host
  31. * 域名
  32. * -----------------
  33. * @method Config
  34. * @author Verdient。
  35. */
  36. public $host = '';
  37. /**
  38. * @var public $port
  39. * 端口
  40. * -----------------
  41. * @method Config
  42. * @author Verdient。
  43. */
  44. public $port = '';
  45. /**
  46. * @var public $_urlPrefix
  47. * URL 路径前缀
  48. * -----------------------
  49. * @author Verdient。
  50. */
  51. protected $_urlPrefix;
  52. /**
  53. * init()
  54. * 初始化
  55. * ------
  56. * @param String $publicKey 公钥
  57. * -----------------------------
  58. * @return Array / false
  59. * @author Verdient。
  60. */
  61. public function init(){
  62. parent::init();
  63. if(!$this->protocol){
  64. $this->protocol = Yii::$app->request->isSecureConnection ? 'https' : 'http';
  65. }
  66. if(!$this->host){
  67. $this->host = explode(':', $_SERVER['HTTP_HOST'])[0];
  68. }
  69. $this->_urlPrefix = $this->protocol . '://' . $this->host . ($this->port ? (($this->protocol == 'https' && $this->port == 443) || ($this->protocol == 'http' && $this->port == 80) ? '' : (':' . $this->port)) : '');
  70. }
  71. /**
  72. * createQueue(String $name)
  73. * 创建队列
  74. * -------------------------
  75. * @param String $name 队列名称
  76. * ---------------------------
  77. * @return Array
  78. * @author Verdient。
  79. */
  80. public function createQueue($name){
  81. return $this->_formatResponse($this->_send('post', '/queue/create', ['name' => $name]));
  82. }
  83. /**
  84. * queueList()
  85. * 队列列表
  86. * -----------
  87. * @return Array
  88. * @author Verdient。
  89. */
  90. public function queueList(){
  91. return $this->_formatResponse($this->_send('get', '/queue/list'));
  92. }
  93. /**
  94. * deleteQueue(String $name)
  95. * 删除队列
  96. * -------------------------
  97. * @param String $name 队列名称
  98. * ---------------------------
  99. * @return Array
  100. * @author Verdient。
  101. */
  102. public function deleteQueue($name){
  103. return $this->_formatResponse($this->_send('post', '/queue/delete', ['name' => $name]));
  104. }
  105. /**
  106. * sendMessage(String $name, Array $message)
  107. * 发送消息
  108. * -----------------------------------------
  109. * @param String $name 队列名称
  110. * @param Array $message 消息
  111. * ---------------------------
  112. * @return Array
  113. * @author Verdient。
  114. */
  115. public function sendMessage($name, Array $message){
  116. return $this->_formatResponse($this->_send('post', '/message/send', ['queue' => $name, 'message' => $message]));
  117. }
  118. /**
  119. * batchSendMessage(String $name, Array $message)
  120. * 批量发送消息
  121. * ----------------------------------------------
  122. * @param String $name 队列名称
  123. * @param Array $message 消息
  124. * ---------------------------
  125. * @return Array
  126. * @author Verdient。
  127. */
  128. public function batchSendMessage($name, Array $message){
  129. return $this->_formatResponse($this->_send('post', '/message/batch-send', ['queue' => $name, 'message' => $message]));
  130. }
  131. /**
  132. * messageList(String $name)
  133. * 消息列表
  134. * -------------------------
  135. * @param String $name 队列名称
  136. * ---------------------------
  137. * @return Array
  138. * @author Verdient。
  139. */
  140. public function messageList($name){
  141. return $this->_formatResponse($this->_send('get', '/message/list', [], ['name' => $name]));
  142. }
  143. /**
  144. * flushMessage(String $name)
  145. * 清空消息
  146. * --------------------------
  147. * @param String $name 队列名称
  148. * ---------------------------
  149. * @return Array
  150. * @author Verdient。
  151. */
  152. public function flushMessage($name){
  153. return $this->_formatResponse($this->_send('post', '/message/purge', ['name' => $name]));
  154. }
  155. /**
  156. * consumeMessage(String $name, Integer $count)
  157. * 消费消息
  158. * --------------------------------------------
  159. * @param String $name 队列名称
  160. * @param Integer $count 消费数量
  161. * ------------------------------
  162. * @return Array
  163. * @author Verdient。
  164. */
  165. public function consumeMessage($name, $count){
  166. return $this->_formatResponse($this->_send('post', '/message/purge', ['name' => $name, 'count' => $count]));
  167. }
  168. /**
  169. * deleteMessage(String $name, Array $ids)
  170. * 删除消息
  171. * ---------------------------------------
  172. * @param String $name 队列名称
  173. * @param Array $ids 消息编号集合
  174. * -------------------------------
  175. * @return Array
  176. * @author Verdient。
  177. */
  178. public function deleteMessage($name, Array $ids){
  179. return $this->_formatResponse($this->_send('post', '/message/delete', ['name' => $name, 'mids' => $ids]));
  180. }
  181. /**
  182. * ackMessage(String $name, Array $ids)
  183. * 应答消息
  184. * ------------------------------------
  185. * @param String $name 队列名称
  186. * @param Array $ids 消息编号集合
  187. * -----------------------------
  188. * @return Array
  189. * @author Verdient。
  190. */
  191. public function ackMessage($name, Array $ids){
  192. return $this->_formatResponse($this->_send('post', '/message/ack', ['name' => $name, 'mids' => $ids]));
  193. }
  194. /**
  195. * messageStatus(String $name, String $id)
  196. * 消息状态
  197. * ---------------------------------------
  198. * @param String $name 队列名称
  199. * @param String $id 消息编号
  200. * ----------------------------
  201. * @author Verdient。
  202. */
  203. public function messageStatus($name, $id){
  204. return $this->_formatResponse($this->_send('get', '/query/message-status', [], ['queue' => $name, 'mid' => $id]));
  205. }
  206. /**
  207. * _send(String $method, String $url[, Array $data = []])
  208. * 发送数据
  209. * ------------------------------------------------------
  210. * @param String $method 访问方法
  211. * @param String $url URL路径
  212. * @param Array $data 发送的数据
  213. * ------------------------------
  214. * @return Array
  215. * @author Verdient。
  216. */
  217. protected function _send($method, $url, Array $data = [], Array $query = []){
  218. $method = strtolower($method);
  219. if(!empty($query)){
  220. $url .= '?' . http_build_query($query);
  221. }
  222. $url = $this->_urlPrefix . $url;
  223. $signature = new Signature();
  224. $signature->key = $this->key;
  225. $signature->verb = $method;
  226. if($method == 'post'){
  227. if(!empty($data)){
  228. Yii::$app->cUrl->setData($data, 'JSON');
  229. }
  230. $signature->content = $data;
  231. }
  232. Yii::$app->cUrl->setHeader([
  233. 'Authorization' => 'FzmMQ client_test:' . $signature->signature,
  234. 'Verb' => $signature->verb,
  235. 'Date' => $signature->date,
  236. 'Content-Md5' => $signature->contentMd5,
  237. 'Signature-Method' => $signature->signatureMethod,
  238. 'Signature-Version' => $signature->signatureVersion
  239. ]);
  240. $options = Yii::$app->cUrl->getOptions();
  241. $response = Yii::$app->cUrl->$method($url, 'JSON');
  242. Yii::trace(['url' => $url, 'cUrl' => $options, 'response' => $response], __METHOD__);
  243. return $response;
  244. }
  245. /**
  246. * _formatResponse(Mixed $response)
  247. * 格式化响应
  248. * --------------------------------
  249. * @param Mixed $response 响应内容
  250. * ------------------------------
  251. * @return Array
  252. * @author Verdient。
  253. */
  254. protected function _formatResponse($response){
  255. $flag = false;
  256. if(is_array($response) && isset($response['code']) && $response['code'] == 200){
  257. $flag = true;
  258. }
  259. return $flag ? [
  260. 'result' => $flag,
  261. 'data' => isset($response['data']) ? $response['data'] : null,
  262. 'code' => $response['code']
  263. ] : [
  264. 'result' => $flag,
  265. 'message' => isset($response['message']) ? $response['message'] : 'Unknown Error',
  266. 'code' => isset($response['code']) ? $response['code'] : 500
  267. ];
  268. }
  269. }