123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace backend\controllers;
- use components\Exception;
- use backend\forms\MessageForm;
- use yii\helpers\ArrayHelper;
- use Yii;
- class MessageController extends BaseController
- {
- /**
- * behaviors
- * @return array
- */
- public function behaviors()
- {
- return ArrayHelper::merge(parent::behaviors(), [
- 'verbs' => [
- 'class' => \yii\filters\VerbFilter::className(),
- 'actions' => [
- 'send' => ['POST'],
- 'batch-send' => ['POST'],
- 'list' => ['GET'],
- 'purge' => ['POST'],
- 'consume' => ['POST'],
- 'delete' => ['POST'],
- 'ack' => ['POST'],
- ],
- ],
- ]);
- }
- /**
- * 发送消息
- * @author: libingke
- * @return array
- */
- public function actionSend()
- {
- $model = new MessageForm();
- $model->setScenario('send');
- $model->load(['MessageForm' => Yii::$app->request->post()]);
- $data = [];
- if ($model->validate()) {
- $data = $model->sendMessage();
- } else {
- $model->handleError();//处理验证失败
- }
- return [
- 'code' => 200,
- 'message' => Yii::t('error', 200),
- 'data' => $data
- ];
- }
- /**
- * 批量发送消息
- * @author: libingke
- * @return array
- */
- public function actionBatchSend()
- {
- $model = new MessageForm();
- $model->setScenario('batch_send');
- $model->load(['MessageForm' => Yii::$app->request->post()]);
- $data = [];
- if ($model->validate()) {
- $data = $model->batchSendMessage();
- } else {
- $model->handleError();
- }
- return [
- 'code' => 200,
- 'message' => Yii::t('error', 200),
- 'data' => $data
- ];
- }
- /**
- * 获取队列消息列表
- * @author: libingke
- * @return array
- */
- public function actionList()
- {
- $model = new MessageForm();
- $model->setScenario('message_list');
- $model->load(['MessageForm' => Yii::$app->request->get()]);
- $data = [];
- if ($model->validate()) {
- $data = $model->getMessageList();
- } else {
- $model->handleError();
- }
- return [
- 'code' => 200,
- 'message' => Yii::t('error', 200),
- 'data' => $data
- ];
- }
- /**
- * 消费消息
- * @author: libingke
- */
- public function actionConsume()
- {
- $model = new MessageForm();
- $model->setScenario('consume');
- $model->load(['MessageForm' => Yii::$app->request->post()]);
- $data = [];
- if ($model->validate()) {
- $data = $model->consumeMessage();
- } else {
- $model->handleError();
- }
- return [
- 'code' => 200,
- 'message' => Yii::t('error', 200),
- 'data' => $data
- ];
- }
- /**
- * 手动应答
- * @author: libingke
- */
- public function actionAck()
- {
- $model = new MessageForm();
- $model->setScenario('ack');
- $model->load(['MessageForm' => Yii::$app->request->post()]);
- $data = [];
- if ($model->validate()) {
- $data = $model->ackMessage();
- } else {
- $model->handleError();
- }
- return [
- 'code' => 200,
- 'message' => Yii::t('error', 200),
- 'data' => $data
- ];
- }
- /**
- * 删除消息
- * @author: libingke
- * @return array
- * @throws Exception
- */
- public function actionDelete()
- {
- $model = new MessageForm();
- $model->setScenario('delete');
- $model->load(['MessageForm' => Yii::$app->request->post()]);
- $data = [];
- if ($model->validate()) {
- $data = $model->deleteMessage();
- } else {
- $model->handleError();
- }
- return [
- 'code' => 200,
- 'message' => Yii::t('error', 200),
- 'data' => $data
- ];
- }
- /**
- * 清空消息
- * @author: libingke
- * @return array
- */
- public function actionPurge()
- {
- $model = new MessageForm();
- $model->setScenario('purge');
- $model->load(['MessageForm' => Yii::$app->request->post()]);
- $data = [];
- if ($model->validate()) {
- $data = $model->purge();
- } else {
- $model->handleError();
- }
- return [
- 'code' => 200,
- 'message' => Yii::t('error', 200),
- 'data' => $data
- ];
- }
- }
|