TopicForm.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace backend\forms;
  3. use components\Curl;
  4. use components\Exception;
  5. use Yii;
  6. /**
  7. * Class QueueForm
  8. * @package backend\forms
  9. */
  10. class TopicForm extends BaseForm
  11. {
  12. /**
  13. * @var string 名称
  14. */
  15. public $name;
  16. public $name1;
  17. public $name2;
  18. public function rules()
  19. {
  20. return [
  21. //create_queue delete_queue
  22. [['name'], 'required', 'message' => 1100, 'on' => ['create_topic', 'delete_topic']],
  23. [['name1','name2'], 'required', 'message' => 1100, 'on' => ['update_topic']],
  24. ['name', 'string', 'message' => 1101, 'on' => ['create_topic', 'delete_topic']],
  25. ['name', 'filter', 'filter' => 'trim', 'on' => ['create_topic', 'delete_topic']],
  26. ];
  27. }
  28. /**
  29. * [创建队列]
  30. * @author: libingke
  31. * @return array
  32. * @throws Exception
  33. */
  34. public function createTopic()
  35. {
  36. try {
  37. $connect = $this->getConnect();
  38. $channel = $connect->channel();
  39. list($topic,,) = $channel->exchange_declare($this->name, 'topic', false, true, false);
  40. } catch (\Exception $e) {
  41. throw new Exception(1001, $e->getMessage());
  42. }
  43. return $data = ['TopicName' => empty($topic)?$this->name:$topic];
  44. }
  45. /**
  46. * [删除队列]
  47. * @author: libingke
  48. * @return array
  49. * @throws Exception
  50. */
  51. public function deleteTopic()
  52. {
  53. try {
  54. $connect = $this->getConnect();
  55. $channel = $connect->channel();
  56. $channel->exchange_delete($this->name, false, false );
  57. return [
  58. 'name' => $this->name,
  59. 'result' => '删除成功'
  60. ];
  61. } catch (\Exception $e) {
  62. throw new Exception(1001, $e->getMessage());
  63. }
  64. }
  65. /**
  66. * [获取消息列表]
  67. * @author: libingke
  68. */
  69. public function getTopicList()
  70. {
  71. $authStr = Yii::$app->Amqp->user . ':' . Yii::$app->Amqp->pass;
  72. $url = Yii::$app->Amqp->host . ':' . Yii::$app->Amqp->api_port . "/api/exchanges";
  73. $curl = new Curl();
  74. $curl->setOption(CURLOPT_USERPWD, $authStr);
  75. $result = json_decode($curl->get($url), true);
  76. if ($curl->responseCode != 200)
  77. throw new Exception(1002);
  78. if ($curl->errorText)
  79. throw new Exception(1002, $curl->errorText);
  80. if (isset($result['error']) && is_string($result['error']))
  81. throw new Exception(1002, $result['error']);
  82. $rows = [];
  83. foreach ($result as $k => $v) {
  84. if($v['type'] = 'topic'){
  85. $name = $v['name'];
  86. $rows[$name]['name'] = $name;
  87. $rows[$name]['auto_delete'] = $v['auto_delete'];
  88. $rows[$name]['durable'] = $v['durable'];
  89. $rows[$name]['arguments'] = $v['arguments'];
  90. $rows[$name]['type'] = $v['type'];
  91. $rows[$name]['vhost'] = $v['vhost'];
  92. }
  93. }
  94. unset($result);
  95. return ['count' => count($rows), 'rows' => $rows];
  96. }
  97. /**
  98. * [修改队列]
  99. * @author: hanguangxu
  100. * @return array
  101. * @throws Exception
  102. */
  103. public function updateTopic()
  104. {
  105. try {
  106. $connect = $this->getConnect();
  107. $channel = $connect->channel();
  108. $channel->exchange_delete($this->name1, false, false );
  109. list($topic,,) = $channel->exchange_declare($this->name2, 'topic', false, true, false);
  110. } catch (\Exception $e) {
  111. throw new Exception(1001, $e->getMessage());
  112. }
  113. return $data = ['TopicName' => empty($topic)?$this->name2:$topic];
  114. }
  115. }