MessageController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace backend\controllers;
  3. use common\logic\MQMessage\Message;
  4. use Yii;
  5. class MessageController extends BaseController
  6. {
  7. /**
  8. * 发送消息 (接受多条)
  9. * @author: libingke
  10. */
  11. public function actionSend()
  12. {
  13. //oauth 验证 todo
  14. //获取接收参数 利用model验证或者判断
  15. $params = Yii::$app->request->post();
  16. $post = [
  17. 'message' => 'message 1',
  18. 'queue' => 'task_queue',
  19. ];
  20. try {
  21. $model = new Message();
  22. $data = $model->send($post['message'], $post['queue']);
  23. $result = ['code' => 200, 'message' => Yii::t('common', 'OK'), 'data' => $data];
  24. } catch (\common\logic\MQMessage\Exception $e) {
  25. $result = ['code' => $e->getCode(), 'message' => $e->getMessage(), 'data' => []];
  26. }
  27. return $result;
  28. }
  29. /**
  30. * 发送消息 (接受多条)
  31. * @author: libingke
  32. */
  33. public function actionBatchSend()
  34. {
  35. //$params = Yii::$app->request->post();
  36. $post = [
  37. 'messages' => ['message 1', 'message 2'],
  38. 'queue' => 'task_queue',
  39. ];
  40. try {
  41. $model = new Message();
  42. $data = $model->batchSend($post['messages'], $post['queue']);
  43. $result = ['code' => 200, 'message' => Yii::t('common', 'OK'), 'data' => $data];
  44. } catch (\common\logic\MQMessage\Exception $e) {
  45. $result = ['code' => $e->getCode(), 'message' => $e->getMessage(), 'data' => []];
  46. }
  47. return $result;
  48. }
  49. /**
  50. * 消费消息
  51. * @author: libingke
  52. */
  53. public function actionReceive()
  54. {
  55. //$params = Yii::$app->request->post();
  56. $post = [
  57. 'message' => 'message 1',
  58. 'queue' => 'task_queue',
  59. ];
  60. try {
  61. $model = new Message();
  62. $data = $model->receive($post['queue'], $post['message']);
  63. $result = ['code' => 200, 'message' => Yii::t('common', 'OK'), 'data' => $data];
  64. } catch (\common\logic\MQMessage\Exception $e) {
  65. $result = ['code' => $e->getCode(), 'message' => $e->getMessage(), 'data' => []];
  66. }
  67. return $result;
  68. }
  69. /**
  70. * 批量消费消息
  71. * @author: libingke
  72. */
  73. public function actionBatchReceive()
  74. {
  75. //$params = Yii::$app->request->post();
  76. $post = [
  77. 'messages' => ['message 1', 'message 2'],
  78. 'queue' => 'task_queue',
  79. ];
  80. try {
  81. $model = new Message();
  82. $data = $model->batchReceive($post['queue'], $post['messages']);
  83. $result = ['code' => 200, 'message' => Yii::t('common', 'OK'), 'data' => $data];
  84. } catch (\common\logic\MQMessage\Exception $e) {
  85. $result = ['code' => $e->getCode(), 'message' => $e->getMessage(), 'data' => []];
  86. }
  87. return $result;
  88. }
  89. /**
  90. * 删除消息
  91. * @author: libingke
  92. */
  93. public function actionDelete()
  94. {
  95. }
  96. /**
  97. * 批量删除消息
  98. * @author: libingke
  99. */
  100. public function actionBatchDelete()
  101. {
  102. }
  103. }