From 0942b89b26dfe873b4cb532bdaf25f091f43865a Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Wed, 17 Apr 2024 16:15:06 +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(3/10)=20*=E5=90=8E=E7=AB=AF=E9=83=A8=E5=88=86=20*?= =?UTF-8?q?=E5=B7=A5=E5=8D=95=E5=86=85=E5=AE=B9=E4=B8=8A=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E9=97=AE=E9=A2=98(XSS)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/TicketsController.php | 25 +++++++++++++++++++++++-- models/TicketReplies.php | 20 ++++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/controllers/TicketsController.php b/controllers/TicketsController.php index 3a23a82..40b0808 100644 --- a/controllers/TicketsController.php +++ b/controllers/TicketsController.php @@ -2,6 +2,7 @@ namespace app\controllers; +use app\models\TicketReplies; use app\models\Tickets; use app\models\TicketsSearch; use Yii; @@ -10,7 +11,6 @@ use yii\filters\AccessControl; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; -use yii\web\Request; use yii\web\Response; /** @@ -74,11 +74,31 @@ class TicketsController extends Controller */ public function actionView(int $id): string { + //fetch all replies for this ticket + $ticketReplies = $this->findTicketReplies($id); + //json + $json = json_encode($ticketReplies); return $this->render('view', [ 'model' => $this->findModel($id), + 'ticketReplies' => $json, ]); } + 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; + } + /** * Creates a new Tickets model. * If creation is successful, the browser will be redirected to the 'view' page. @@ -97,7 +117,8 @@ class TicketsController extends Controller $model->created_at = date('Y-m-d H:i:s'); $model->updated_at = date('Y-m-d H:i:s'); - if($model->save()){ + if ($model->save()) { + Yii::$app->session->setFlash('success', '工单创建成功'); return $this->redirect(['view', 'id' => $model->id]); } } diff --git a/models/TicketReplies.php b/models/TicketReplies.php index d039f71..d9c2a9b 100644 --- a/models/TicketReplies.php +++ b/models/TicketReplies.php @@ -15,6 +15,7 @@ use yii\db\ActiveRecord; * @property string $message 消息内容 * @property string $created_at 发送时间 * @property string $ip ip地址 + * @property int $is_admin 是否是管理员回复 * * @property Tickets $ticket * @property User $user @@ -35,8 +36,8 @@ class TicketReplies extends ActiveRecord public function rules(): array { return [ - [['ticket_id', 'user_id', 'message', 'ip'], 'required'], - [['ticket_id', 'user_id'], 'integer'], + [['ticket_id', 'user_id', 'message', 'ip', 'is_admin'], 'required'], + [['ticket_id', 'user_id', 'is_admin'], 'integer'], [['message'], 'string'], [['created_at'], 'safe'], [['ip'], 'string', 'max' => 150], @@ -57,6 +58,7 @@ class TicketReplies extends ActiveRecord 'message' => '消息内容', 'created_at' => '发送时间', 'ip' => 'ip地址', + 'is_admin' => '是否是管理员回复' ]; } @@ -79,4 +81,18 @@ class TicketReplies extends ActiveRecord { return $this->hasOne(User::class, ['id' => 'user_id']); } + + public function toArray(array $fields = [], array $expand = [], $recursive = true): array + { + + return [ + 'id' => $this->id, + 'ticket_id' => $this->ticket_id, + 'name' => ($this->is_admin === 1) ? $this->user->username : '您', + 'message' => $this->message, + 'created_at' => $this->created_at, + 'ip' => $this->ip, + 'is_admin' => $this->is_admin, + ]; + } }