MessageController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace backend\controllers;
  3. use components\Exception;
  4. use backend\forms\MessageForm;
  5. use yii\helpers\ArrayHelper;
  6. use Yii;
  7. class MessageController extends BaseController
  8. {
  9. /**
  10. * behaviors
  11. * @return array
  12. */
  13. public function behaviors()
  14. {
  15. return ArrayHelper::merge(parent::behaviors(), [
  16. 'verbs' => [
  17. 'class' => \yii\filters\VerbFilter::className(),
  18. 'actions' => [
  19. 'send' => ['POST'],
  20. 'batch-send' => ['POST'],
  21. 'list' => ['GET'],
  22. 'purge' => ['POST'],
  23. 'consume' => ['POST'],
  24. 'delete' => ['POST'],
  25. 'ack' => ['POST'],
  26. ],
  27. ],
  28. ]);
  29. }
  30. /**
  31. * 发送消息
  32. * @author: libingke
  33. * @return array
  34. */
  35. public function actionSend()
  36. {
  37. $model = new MessageForm();
  38. $model->setScenario('send');
  39. $model->load(['MessageForm' => Yii::$app->request->post()]);
  40. $data = [];
  41. if ($model->validate()) {
  42. $data = $model->sendMessage();
  43. } else {
  44. $model->handleError();//处理验证失败
  45. }
  46. return [
  47. 'code' => 200,
  48. 'message' => Yii::t('error', 200),
  49. 'data' => $data
  50. ];
  51. }
  52. /**
  53. * 批量发送消息
  54. * @author: libingke
  55. * @return array
  56. */
  57. public function actionBatchSend()
  58. {
  59. $model = new MessageForm();
  60. $model->setScenario('batch_send');
  61. $model->load(['MessageForm' => Yii::$app->request->post()]);
  62. $data = [];
  63. if ($model->validate()) {
  64. $data = $model->batchSendMessage();
  65. } else {
  66. $model->handleError();
  67. }
  68. return [
  69. 'code' => 200,
  70. 'message' => Yii::t('error', 200),
  71. 'data' => $data
  72. ];
  73. }
  74. /**
  75. * 获取队列消息列表
  76. * @author: libingke
  77. * @return array
  78. */
  79. public function actionList()
  80. {
  81. $model = new MessageForm();
  82. $model->setScenario('message_list');
  83. $model->load(['MessageForm' => Yii::$app->request->get()]);
  84. $data = [];
  85. if ($model->validate()) {
  86. $data = $model->getMessageList();
  87. } else {
  88. $model->handleError();
  89. }
  90. return [
  91. 'code' => 200,
  92. 'message' => Yii::t('error', 200),
  93. 'data' => $data
  94. ];
  95. }
  96. /**
  97. * 消费消息
  98. * @author: libingke
  99. */
  100. public function actionConsume()
  101. {
  102. $model = new MessageForm();
  103. $model->setScenario('consume');
  104. $model->load(['MessageForm' => Yii::$app->request->post()]);
  105. $data = [];
  106. if ($model->validate()) {
  107. $data = $model->consumeMessage();
  108. } else {
  109. $model->handleError();
  110. }
  111. return [
  112. 'code' => 200,
  113. 'message' => Yii::t('error', 200),
  114. 'data' => $data
  115. ];
  116. }
  117. /**
  118. * 手动应答
  119. * @author: libingke
  120. */
  121. public function actionAck()
  122. {
  123. $model = new MessageForm();
  124. $model->setScenario('ack');
  125. $model->load(['MessageForm' => Yii::$app->request->post()]);
  126. $data = [];
  127. if ($model->validate()) {
  128. $data = $model->ackMessage();
  129. } else {
  130. $model->handleError();
  131. }
  132. return [
  133. 'code' => 200,
  134. 'message' => Yii::t('error', 200),
  135. 'data' => $data
  136. ];
  137. }
  138. /**
  139. * 删除消息
  140. * @author: libingke
  141. * @return array
  142. * @throws Exception
  143. */
  144. public function actionDelete()
  145. {
  146. $model = new MessageForm();
  147. $model->setScenario('delete');
  148. $model->load(['MessageForm' => Yii::$app->request->post()]);
  149. $data = [];
  150. if ($model->validate()) {
  151. $data = $model->deleteMessage();
  152. } else {
  153. $model->handleError();
  154. }
  155. return [
  156. 'code' => 200,
  157. 'message' => Yii::t('error', 200),
  158. 'data' => $data
  159. ];
  160. }
  161. /**
  162. * 清空消息
  163. * @author: libingke
  164. * @return array
  165. */
  166. public function actionPurge()
  167. {
  168. $model = new MessageForm();
  169. $model->setScenario('purge');
  170. $model->load(['MessageForm' => Yii::$app->request->post()]);
  171. $data = [];
  172. if ($model->validate()) {
  173. $data = $model->purge();
  174. } else {
  175. $model->handleError();
  176. }
  177. return [
  178. 'code' => 200,
  179. 'message' => Yii::t('error', 200),
  180. 'data' => $data
  181. ];
  182. }
  183. }