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']], ], ], '
+ = Html::a('创建工单', ['create'], ['class' => 'btn btn-success']) ?> +
+ + + + = GridView::widget([ + 'dataProvider' => $dataProvider, + 'columns' => [ + [ + 'attribute' => 'id', + 'label' => '工单ID', + ], + [ + 'attribute' => 'title', + 'label' => '标题', + 'format' => 'raw', // 使用 raw 格式,这样 Yii2 不会对 value 的返回值进行 HTML 编码 + 'value' => function (Tickets $model) { + return Html::a($model->title, ['view', 'id' => $model->id]); + }, + ], + [ + 'attribute' => 'created_at', + 'label' => '创建时间', + ], + [ + 'attribute' => 'updated_at', + 'label' => '最近更新时间', + 'value' => function (Tickets $model) { + return Yii::$app->formatter->asRelativeTime(new DateTime($model->updated_at, new DateTimeZone('GMT+8'))); + } + ], + [ + 'attribute' => 'status', + 'label' => '状态', + 'format' => 'raw', // 使用 raw 格式,这样 Yii2 不会对 value 的返回值进行 HTML 编码 + 'value' => function (Tickets $model) { + switch ($model->status) { + case Tickets::STATUS_OPEN: + return '工单已开启'; + case Tickets::STATUS_ADMIN_REPLY: + return '管理员已回复'; + case Tickets::STATUS_USER_REPLY: + return '用户已回复'; + case Tickets::STATUS_CLOSED: + return '工单已关闭'; + default: + return '未知状态'; + } + } + ], + [ + 'class' => ActionColumn::class, + 'template' => '{view} {delete}', + 'buttons' => [ + 'view' => function ($url, $model, $key) { + return Html::a('', $url, [ + 'title' => '查看工单', + 'data-pjax' => '0', + ]); + }, + 'delete' => function ($url, $model, $key) { + if ($model->status !== Tickets::STATUS_CLOSED) { + return Html::a('', $url, [ + 'title' => '关闭工单', + 'data-pjax' => '0', + 'data-confirm' => '你确定要关闭这个工单吗?', + 'data-method' => 'post', + ]); + } else { + return Html::tag('i', '', [ + 'class' => 'fa-solid fa-xmark', + 'style' => 'color: gray;', + 'title' => '工单已关闭', + ]); + } + }, + ], + 'urlCreator' => function ($action, Tickets $model, $key, $index, $column) { + return Url::toRoute([$action, 'id' => $model->id]); + }, + ], + ], + ]); ?> + + + ++ = Html::a('关闭工单', ['delete', 'id' => $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +
+ + = DetailView::widget([ + 'model' => $model, + 'attributes' => [ + 'id', + 'user_id', + 'title', + 'description:ntext', + 'status', + 'created_at', + 'updated_at', + 'ip', + ], + ]) ?> + +