From 66f4127f5c6095dfafff5ae1b1110e96c1856069 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Thu, 18 Apr 2024 17:18:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=B7=A5=E5=8D=95=E5=8A=9F?= =?UTF-8?q?=E8=83=BD(10/10)=20=E5=AE=9E=E7=8E=B0=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=91=98=E4=BE=A7=E5=B7=A5=E5=8D=95=E6=94=AF=E6=8C=81=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20*=E5=90=8E=E7=AB=AF=E5=AE=9E=E7=8E=B0=20*=E6=B8=85?= =?UTF-8?q?=E7=90=86=E6=97=A0=E6=95=88=E7=9A=84feedbackMG=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/AdminController.php | 143 +++++++++++++++++++++++++++++--- 1 file changed, 133 insertions(+), 10 deletions(-) diff --git a/controllers/AdminController.php b/controllers/AdminController.php index 8480260..140d658 100644 --- a/controllers/AdminController.php +++ b/controllers/AdminController.php @@ -10,6 +10,9 @@ use app\models\LoginLogs; use app\models\Share; use app\models\ShareSearch; use app\models\SiteConfig; +use app\models\TicketReplies; +use app\models\Tickets; +use app\models\TicketsSearch; use app\models\User; use app\models\UserSearch; use app\utils\AdminSword; @@ -46,7 +49,7 @@ class AdminController extends Controller 'actions' => ['index', 'system', 'user', 'info', 'user-view', 'user-create', 'user-update', 'user-delete', 'user-totpoff', 'user-pwdreset', 'login-log', 'access-log', 'collection-up-log', 'share-manage', 'share-manage-view', 'share-manage-delete', 'collection-manage', 'collection-manage-view', - 'collection-manage-delete', 'notice-manage', 'feedback-manage', 'sysinfo','get-sysinfo'], + 'collection-manage-delete', 'notice-manage', 'sysinfo', 'get-sysinfo', 'ticket-manage', 'ticket-view', 'ticket-delete', 'ticket-reply'], 'roles' => ['admin'], // only admin can do these ] ], @@ -74,9 +77,12 @@ class AdminController extends Controller 'collection-manage-view' => ['GET'], 'collection-manage-delete' => ['POST'], 'notice-manage' => ['GET'], - 'feedback-manage' => ['GET'], 'sysinfo' => ['GET'], 'get-sysinfo' => ['GET'], + 'ticket-manage' => ['GET'], + 'ticket-view' => ['GET'], + 'ticket-delete' => ['POST'], + 'ticket-reply' => ['POST'], ], ], ] @@ -574,14 +580,6 @@ class AdminController extends Controller return $this->render('notice_manage'); } - /** - * @return string - */ - public function actionFeedbackManage(): string - { - return $this->render('feedback_manage'); - } - /** * @return string */ @@ -609,4 +607,129 @@ class AdminController extends Controller } return Yii::$app->response; } + + /** + * @return string + */ + public function actionTicketManage(): string + { + $searchModel = new TicketsSearch(); + $dataProvider = $searchModel->search($this->request->queryParams); + + return $this->render('ticket_index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + 'pendingTickets' => TicketsSearch::getPendingTicketsCount() + ]); + } + + /** + * @param int $id + * @return string + * @throws NotFoundHttpException + */ + public function actionTicketView(int $id): string + { + //fetch all replies for this ticket + $ticketReplies = $this->findTicketReplies($id); + //json + $json = json_encode($ticketReplies); + return $this->render('ticket_view', [ + 'model' => $this->findTicketModel($id), + 'ticketReplies' => $json, + ]); + } + + /** + * @param int $id + * @return Tickets + * @throws NotFoundHttpException + */ + protected function findTicketModel(int $id): Tickets + { + if (($model = Tickets::findOne(['id' => $id])) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } + + /** + * @param int $ticketId + * @return array + */ + protected function findTicketReplies(int $ticketId): array + { + $ticketReplies = TicketReplies::find() + ->where(['ticket_id' => $ticketId]) + ->orderBy(['created_at' => SORT_ASC]) + ->all(); + + $result = []; + foreach ($ticketReplies as $reply) { + $result[] = $reply->toArray(); + } + + return $result; + } + + /** + * NoNoNo, you can't delete a ticket. Just close it. + * @param int $id 工单id + * @param string $from + * @return Response + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionTicketDelete(int $id, string $from = 'unset'): Response + { + $this->findTicketModel($id)->updateAttributes(['status' => Tickets::STATUS_CLOSED]); + Yii::$app->session->setFlash('info', '工单已关闭'); + if ($from !== 'unset') { + return $this->redirect(['ticket-manage']); + } + return $this->redirect(['ticket-view', 'id' => $id]); + } + + /** + * Ticket reply action + * For user + * @return Response + * @throws NotFoundHttpException + */ + public function actionTicketReply(): Response + { + $request = Yii::$app->request; + if ($request->isPost) { + Yii::$app->response->format = Response::FORMAT_JSON; + + $ticketId = $request->post('ticketId'); + + // check this ticket exists + $ticket = Tickets::findOne(['id' => $ticketId]); + if ($ticket === null) { + return $this->asJson(['status' => 'error', 'message' => 'Invalid ticket']); + } + + // 创建一个新的TicketReplies对象 + $reply = new TicketReplies(); + $reply->ticket_id = $ticketId; + $reply->user_id = Yii::$app->user->id; // 设置为当前用户的ID + $reply->message = $request->post('content'); + $reply->ip = $request->userIP; + $reply->is_admin = 1; // 设置为用户回复 + $reply->created_at = date('Y-m-d H:i:s'); + + if ($reply->save()) { + // 如果保存成功,返回一个成功的响应 + $this->findTicketModel($ticketId)->updateAttributes(['status' => Tickets::STATUS_ADMIN_REPLY]); + return $this->asJson(['status' => 'success']); + } else { + // 如果保存失败,返回一个包含错误信息的响应 + return $this->asJson(['status' => 'error', 'errors' => $reply->errors]); + } + } + + // 如果不是POST请求,返回一个错误响应 + return $this->asJson(['status' => 'error', 'message' => 'Invalid request']); + } }