From 386bb0832793beefff092964715cfb9c56d85a8f Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Tue, 16 Apr 2024 16:46:56 +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(1/10)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/TicketsController.php | 162 ++++++++++++++++++++++++++++++ models/TicketReplies.php | 82 +++++++++++++++ models/Tickets.php | 91 +++++++++++++++++ models/TicketsSearch.php | 90 +++++++++++++++++ views/layouts/admin_main.php | 4 +- views/layouts/main.php | 1 + views/tickets/_form.php | 25 +++++ views/tickets/_search.php | 44 ++++++++ views/tickets/create.php | 20 ++++ views/tickets/index.php | 110 ++++++++++++++++++++ views/tickets/update.php | 21 ++++ views/tickets/view.php | 43 ++++++++ 12 files changed, 691 insertions(+), 2 deletions(-) create mode 100644 controllers/TicketsController.php create mode 100644 models/TicketReplies.php create mode 100644 models/Tickets.php create mode 100644 models/TicketsSearch.php create mode 100644 views/tickets/_form.php create mode 100644 views/tickets/_search.php create mode 100644 views/tickets/create.php create mode 100644 views/tickets/index.php create mode 100644 views/tickets/update.php create mode 100644 views/tickets/view.php diff --git a/controllers/TicketsController.php b/controllers/TicketsController.php new file mode 100644 index 0000000..3a23a82 --- /dev/null +++ b/controllers/TicketsController.php @@ -0,0 +1,162 @@ + [ + 'class' => AccessControl::class, + 'rules' => [ + [ + 'allow' => true, + 'actions' => ['index', 'view', 'create', 'update', 'delete'], + 'roles' => ['user'], // only user can do these + ] + ], + ], + 'verbs' => [ + 'class' => VerbFilter::class, + 'actions' => [ + 'index' => ['GET'], + 'view' => ['GET'], + 'create' => ['GET', 'POST'], + 'update' => ['GET', 'POST'], + 'delete' => ['POST'], + ], + ], + ] + ); + } + + /** + * Lists all Tickets models. + * + * @return string + */ + public function actionIndex(): string + { + $searchModel = new TicketsSearch(); + $dataProvider = $searchModel->search($this->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Tickets model. + * @param int $id 工单id + * @return string + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionView(int $id): string + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Tickets model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return string|Response + */ + public function actionCreate(): Response|string + { + $model = new Tickets(); + + if ($this->request->isPost) { + if ($model->load($this->request->post())) { + // add properties that are not in the form + $model->user_id = Yii::$app->user->id; + $model->status = Tickets::STATUS_OPEN; + $model->ip = $this->request->userIP; + $model->created_at = date('Y-m-d H:i:s'); + $model->updated_at = date('Y-m-d H:i:s'); + + if($model->save()){ + return $this->redirect(['view', 'id' => $model->id]); + } + } + } + + return $this->render('create', [ + 'model' => $model, + ]); + } + + /** + * Updates an existing Tickets model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param int $id 工单id + * @return string|Response + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionUpdate(int $id): Response|string + { + $model = $this->findModel($id); + + if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('update', [ + 'model' => $model, + ]); + } + + /** + * Deletes an existing Tickets model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param int $id 工单id + * @return Response + * @throws NotFoundHttpException if the model cannot be found + * @throws \Throwable + * @throws StaleObjectException + */ + public function actionDelete(int $id): Response + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the Tickets model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param int $id 工单id + * @return Tickets the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel(int $id): Tickets + { + if (($model = Tickets::findOne(['id' => $id])) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } +} diff --git a/models/TicketReplies.php b/models/TicketReplies.php new file mode 100644 index 0000000..d039f71 --- /dev/null +++ b/models/TicketReplies.php @@ -0,0 +1,82 @@ + 150], + [['ticket_id'], 'exist', 'skipOnError' => true, 'targetClass' => Tickets::class, 'targetAttribute' => ['ticket_id' => 'id']], + [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels(): array + { + return [ + 'id' => '工单消息id', + 'ticket_id' => '归属的工单id', + 'user_id' => '消息发送者id', + 'message' => '消息内容', + 'created_at' => '发送时间', + 'ip' => 'ip地址', + ]; + } + + /** + * Gets query for [[Ticket]]. + * + * @return ActiveQuery + */ + public function getTicket(): ActiveQuery + { + return $this->hasOne(Tickets::class, ['id' => 'ticket_id']); + } + + /** + * Gets query for [[User]]. + * + * @return ActiveQuery + */ + public function getUser(): ActiveQuery + { + return $this->hasOne(User::class, ['id' => 'user_id']); + } +} diff --git a/models/Tickets.php b/models/Tickets.php new file mode 100644 index 0000000..df88786 --- /dev/null +++ b/models/Tickets.php @@ -0,0 +1,91 @@ + 50], + [['ip'], 'string', 'max' => 150], + [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels(): array + { + return [ + 'id' => '工单id', + 'user_id' => '发起工单的用户id', + 'title' => '工单标题', + 'description' => '工单内容', + 'status' => '工单状态', + 'created_at' => '工单创建时间', + 'updated_at' => '工单更新时间', + 'ip' => 'ip地址', + ]; + } + + /** + * Gets query for [[TicketReplies]]. + * + * @return ActiveQuery + */ + public function getTicketReplies(): ActiveQuery + { + return $this->hasMany(TicketReplies::class, ['ticket_id' => 'id']); + } + + /** + * Gets query for [[User]]. + * + * @return ActiveQuery + */ + public function getUser(): ActiveQuery + { + return $this->hasOne(User::class, ['id' => 'user_id']); + } +} diff --git a/models/TicketsSearch.php b/models/TicketsSearch.php new file mode 100644 index 0000000..715d1ed --- /dev/null +++ b/models/TicketsSearch.php @@ -0,0 +1,90 @@ +user->can('admin')) { + $dataProvider = new ActiveDataProvider([ + 'query' => $query, + ]); + }else{ + $dataProvider = new ActiveDataProvider([ + 'query' => $query->where(['user_id' => Yii::$app->user->id]), + ]); + } + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + // grid filtering conditions + // if can user + if(Yii::$app->user->can('admin')) { + $query->andFilterWhere([ + 'id' => $this->id, + 'user_id' => $this->user_id, + 'status' => $this->status, + 'created_at' => $this->created_at, + 'updated_at' => $this->updated_at, + ]); + } else{ + $query->andFilterWhere([ + 'user_id' => $this->user_id, + 'status' => $this->status, + 'created_at' => $this->created_at, + 'updated_at' => $this->updated_at, + ]); + } + $query->andFilterWhere(['like', 'title', $this->title]) + ->andFilterWhere(['like', 'description', $this->description]) + ->andFilterWhere(['like', 'ip', $this->ip]); + + return $dataProvider; + } +} diff --git a/views/layouts/admin_main.php b/views/layouts/admin_main.php index 0e69e46..e116259 100644 --- a/views/layouts/admin_main.php +++ b/views/layouts/admin_main.php @@ -78,7 +78,7 @@ $this->registerCssFile('@web/css/fuckyou-navpadding.css'); ['label' => '文件分享管理', 'url' => ['/admin/share-manage']], ['label' => '文件收集管理', 'url' => ['/admin/collection-manage']], ['label' => '站点公告管理', 'url' => ['/admin/notice-manage']], // 未完工 - ['label' => '用户反馈管理', 'url' => ['/admin/feedback-manage']], // 未完工 + ['label' => '工单支持管理', 'url' => ['/admin/ticket-manage']], // 未完工 ], ], ['label' => '日志', 'items' => [ @@ -90,7 +90,7 @@ $this->registerCssFile('@web/css/fuckyou-navpadding.css'); ['label' => '设置', 'items' => [ ['label' => '个人设置', 'url' => ['/admin/info']], ['label' => '系统设置', 'url' => ['/admin/system']], - ['label' => '系统信息', 'url' => ['/admin/sysinfo']], // 未完工 + ['label' => '系统信息', 'url' => ['/admin/sysinfo']], ], ], '