1
0

MessageController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 actionSendtopic()
  36. {
  37. $model = new MessageForm();
  38. $model->setScenario('sendtopic');
  39. $model->load(['MessageForm' => Yii::$app->request->post()]);
  40. $data = [];
  41. if ($model->validate()) {
  42. $data = $model->sendMessageTopic();
  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 actionSend()
  58. {
  59. $model = new MessageForm();
  60. $model->setScenario('send');
  61. $model->load(['MessageForm' => Yii::$app->request->post()]);
  62. $data = [];
  63. if ($model->validate()) {
  64. $data = $model->sendMessage();
  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 actionBatchSend()
  80. {
  81. $model = new MessageForm();
  82. $model->setScenario('batch_send');
  83. $model->load(['MessageForm' => Yii::$app->request->post()]);
  84. $data = [];
  85. if ($model->validate()) {
  86. $data = $model->batchSendMessage();
  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. * @return array
  100. */
  101. public function actionList()
  102. {
  103. $model = new MessageForm();
  104. $model->setScenario('message_list');
  105. $model->load(['MessageForm' => Yii::$app->request->get()]);
  106. $data = [];
  107. if ($model->validate()) {
  108. $data = $model->getMessageList();
  109. } else {
  110. $model->handleError();
  111. }
  112. return [
  113. 'code' => 200,
  114. 'message' => Yii::t('error', 200),
  115. 'data' => $data
  116. ];
  117. }
  118. /**
  119. * 消费消息
  120. * @author: libingke
  121. */
  122. public function actionConsume()
  123. {
  124. $model = new MessageForm();
  125. $model->setScenario('consume');
  126. $model->load(['MessageForm' => Yii::$app->request->post()]);
  127. $data = [];
  128. if ($model->validate()) {
  129. $data = $model->consumeMessage();
  130. } else {
  131. $model->handleError();
  132. }
  133. return [
  134. 'code' => 200,
  135. 'message' => Yii::t('error', 200),
  136. 'data' => $data
  137. ];
  138. }
  139. /**
  140. * 手动应答
  141. * @author: libingke
  142. */
  143. public function actionAck()
  144. {
  145. $model = new MessageForm();
  146. $model->setScenario('ack');
  147. $model->load(['MessageForm' => Yii::$app->request->post()]);
  148. $data = [];
  149. if ($model->validate()) {
  150. $data = $model->ackMessage();
  151. } else {
  152. $model->handleError();
  153. }
  154. return [
  155. 'code' => 200,
  156. 'message' => Yii::t('error', 200),
  157. 'data' => $data
  158. ];
  159. }
  160. /**
  161. * 删除消息
  162. * @author: libingke
  163. * @return array
  164. * @throws Exception
  165. */
  166. public function actionDelete()
  167. {
  168. $model = new MessageForm();
  169. $model->setScenario('delete');
  170. $model->load(['MessageForm' => Yii::$app->request->post()]);
  171. $data = [];
  172. if ($model->validate()) {
  173. $data = $model->deleteMessage();
  174. } else {
  175. $model->handleError();
  176. }
  177. return [
  178. 'code' => 200,
  179. 'message' => Yii::t('error', 200),
  180. 'data' => $data
  181. ];
  182. }
  183. /**
  184. * 清空消息
  185. * @author: libingke
  186. * @return array
  187. */
  188. public function actionPurge()
  189. {
  190. $model = new MessageForm();
  191. $model->setScenario('purge');
  192. $model->load(['MessageForm' => Yii::$app->request->post()]);
  193. $data = [];
  194. if ($model->validate()) {
  195. $data = $model->purge();
  196. } else {
  197. $model->handleError();
  198. }
  199. return [
  200. 'code' => 200,
  201. 'message' => Yii::t('error', 200),
  202. 'data' => $data
  203. ];
  204. }
  205. }