支持工单功能(1/10)

This commit is contained in:
Chenx221 2024-04-16 16:46:56 +08:00
parent 7b435f4093
commit 386bb08327
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
12 changed files with 691 additions and 2 deletions

View File

@ -0,0 +1,162 @@
<?php
namespace app\controllers;
use app\models\Tickets;
use app\models\TicketsSearch;
use Yii;
use yii\db\StaleObjectException;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\Request;
use yii\web\Response;
/**
* TicketsController implements the CRUD actions for Tickets model.
*/
class TicketsController extends Controller
{
/**
* @inheritDoc
*/
public function behaviors(): array
{
return array_merge(
parent::behaviors(),
[
'access' => [
'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.');
}
}

82
models/TicketReplies.php Normal file
View File

@ -0,0 +1,82 @@
<?php
namespace app\models;
use Yii;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
/**
* This is the model class for table "ticket_replies".
*
* @property int $id 工单消息id
* @property int $ticket_id 归属的工单id
* @property int $user_id 消息发送者id
* @property string $message 消息内容
* @property string $created_at 发送时间
* @property string $ip ip地址
*
* @property Tickets $ticket
* @property User $user
*/
class TicketReplies extends ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName(): string
{
return 'ticket_replies';
}
/**
* {@inheritdoc}
*/
public function rules(): array
{
return [
[['ticket_id', 'user_id', 'message', 'ip'], 'required'],
[['ticket_id', 'user_id'], 'integer'],
[['message'], 'string'],
[['created_at'], 'safe'],
[['ip'], 'string', 'max' => 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']);
}
}

91
models/Tickets.php Normal file
View File

@ -0,0 +1,91 @@
<?php
namespace app\models;
use Yii;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
/**
* This is the model class for table "tickets".
*
* @property int $id 工单id
* @property int $user_id 发起工单的用户id
* @property string $title 工单标题
* @property string $description 工单内容
* @property int $status 工单状态 // 0:工单已开启 1:管理员已回复 2:用户已回复 3:工单已关闭
* @property string $created_at 工单创建时间
* @property string $updated_at 工单更新时间
* @property string $ip ip地址
*
* @property TicketReplies[] $ticketReplies
* @property User $user
*/
class Tickets extends ActiveRecord
{
const STATUS_OPEN = 0;
const STATUS_ADMIN_REPLY = 1;
const STATUS_USER_REPLY = 2;
const STATUS_CLOSED = 3;
/**
* {@inheritdoc}
*/
public static function tableName(): string
{
return 'tickets';
}
/**
* {@inheritdoc}
*/
public function rules(): array
{
return [
[['user_id', 'title', 'description', 'status','ip'], 'required'],
[['user_id', 'status'], 'integer'],
[['description'], 'string'],
[['created_at', 'updated_at'], 'safe'],
[['title'], 'string', 'max' => 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']);
}
}

90
models/TicketsSearch.php Normal file
View File

@ -0,0 +1,90 @@
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Tickets;
/**
* TicketsSearch represents the model behind the search form of `app\models\Tickets`.
*/
class TicketsSearch extends Tickets
{
/**
* {@inheritdoc}
*/
public function rules(): array
{
return [
[['id', 'user_id', 'status'], 'integer'],
[['title', 'description', 'created_at', 'updated_at','ip'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios(): array
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params): ActiveDataProvider
{
$query = Tickets::find();
// add conditions that should always apply here
$dataProvider = null;
if(Yii::$app->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;
}
}

View File

@ -78,7 +78,7 @@ $this->registerCssFile('@web/css/fuckyou-navpadding.css');
['label' => '文件分享管理', 'url' => ['/admin/share-manage']], ['label' => '文件分享管理', 'url' => ['/admin/share-manage']],
['label' => '文件收集管理', 'url' => ['/admin/collection-manage']], ['label' => '文件收集管理', 'url' => ['/admin/collection-manage']],
['label' => '站点公告管理', 'url' => ['/admin/notice-manage']], // 未完工 ['label' => '站点公告管理', 'url' => ['/admin/notice-manage']], // 未完工
['label' => '用户反馈管理', 'url' => ['/admin/feedback-manage']], // 未完工 ['label' => '工单支持管理', 'url' => ['/admin/ticket-manage']], // 未完工
], ],
], ],
['label' => '日志', 'items' => [ ['label' => '日志', 'items' => [
@ -90,7 +90,7 @@ $this->registerCssFile('@web/css/fuckyou-navpadding.css');
['label' => '设置', 'items' => [ ['label' => '设置', 'items' => [
['label' => '个人设置', 'url' => ['/admin/info']], ['label' => '个人设置', 'url' => ['/admin/info']],
['label' => '系统设置', 'url' => ['/admin/system']], ['label' => '系统设置', 'url' => ['/admin/system']],
['label' => '系统信息', 'url' => ['/admin/sysinfo']], // 未完工 ['label' => '系统信息', 'url' => ['/admin/sysinfo']],
], ],
], ],
'<li class="nav-item">' '<li class="nav-item">'

View File

@ -76,6 +76,7 @@ $darkMode = Yii::$app->user->isGuest ? 0 : Yii::$app->user->identity->dark_mode;
['label' => '分享管理', 'url' => ['/share/index']], ['label' => '分享管理', 'url' => ['/share/index']],
['label' => '文件收集', 'url' => ['/collection/index']], ['label' => '文件收集', 'url' => ['/collection/index']],
['label' => '个人设置', 'url' => ['/user/info']], ['label' => '个人设置', 'url' => ['/user/info']],
['label' => '工单支持', 'url' => ['/tickets/index']],
Yii::$app->user->isGuest Yii::$app->user->isGuest
? ['label' => '登录', 'url' => ['/user/login']] ? ['label' => '登录', 'url' => ['/user/login']]
: '<li class="nav-item">' : '<li class="nav-item">'

25
views/tickets/_form.php Normal file
View File

@ -0,0 +1,25 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/** @var yii\web\View $this */
/** @var app\models\Tickets $model */
/** @var yii\widgets\ActiveForm $form */
?>
<div class="tickets-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>
<div class="form-group">
<?= Html::submitButton('创建', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

44
views/tickets/_search.php Normal file
View File

@ -0,0 +1,44 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/** @var yii\web\View $this */
/** @var app\models\TicketsSearch $model */
/** @var yii\widgets\ActiveForm $form */
?>
<div class="tickets-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
'options' => [
'data-pjax' => 1
],
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'user_id') ?>
<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'description') ?>
<?= $form->field($model, 'status') ?>
<?php // echo $form->field($model, 'created_at') ?>
<?php // echo $form->field($model, 'updated_at') ?>
<?php // echo $form->field($model, 'ip') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-outline-secondary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

20
views/tickets/create.php Normal file
View File

@ -0,0 +1,20 @@
<?php
use yii\helpers\Html;
/** @var yii\web\View $this */
/** @var app\models\Tickets $model */
$this->title = '创建工单';
$this->params['breadcrumbs'][] = ['label' => '工单支持', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="tickets-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

110
views/tickets/index.php Normal file
View File

@ -0,0 +1,110 @@
<?php
use app\assets\FontAwesomeAsset;
use app\models\Tickets;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\grid\ActionColumn;
use yii\grid\GridView;
use yii\widgets\Pjax;
/** @var yii\web\View $this */
/** @var app\models\TicketsSearch $searchModel */
/** @var yii\data\ActiveDataProvider $dataProvider */
$this->title = '工单支持';
$this->params['breadcrumbs'][] = $this->title;
FontAwesomeAsset::register($this);
?>
<div class="tickets-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('创建工单', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php Pjax::begin(); ?>
<?= 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 '<span class="badge rounded-pill bg-primary">工单已开启</span>';
case Tickets::STATUS_ADMIN_REPLY:
return '<span class="badge rounded-pill bg-info">管理员已回复</span>';
case Tickets::STATUS_USER_REPLY:
return '<span class="badge rounded-pill bg-secondary">用户已回复</span>';
case Tickets::STATUS_CLOSED:
return '<span class="badge rounded-pill bg-success">工单已关闭</span>';
default:
return '<span class="badge rounded-pill bg-danger">未知状态</span>';
}
}
],
[
'class' => ActionColumn::class,
'template' => '{view} {delete}',
'buttons' => [
'view' => function ($url, $model, $key) {
return Html::a('<i class="fa-solid fa-eye"></i>', $url, [
'title' => '查看工单',
'data-pjax' => '0',
]);
},
'delete' => function ($url, $model, $key) {
if ($model->status !== Tickets::STATUS_CLOSED) {
return Html::a('<i class="fa-solid fa-xmark"></i>', $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]);
},
],
],
]); ?>
<?php Pjax::end(); ?>
</div>

21
views/tickets/update.php Normal file
View File

@ -0,0 +1,21 @@
<?php
use yii\helpers\Html;
/** @var yii\web\View $this */
/** @var app\models\Tickets $model */
$this->title = 'Update Tickets: ' . $model->title;
$this->params['breadcrumbs'][] = ['label' => 'Tickets', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->title, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Update';
?>
<div class="tickets-update">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

43
views/tickets/view.php Normal file
View File

@ -0,0 +1,43 @@
<?php
use yii\helpers\Html;
use yii\web\YiiAsset;
use yii\widgets\DetailView;
/** @var yii\web\View $this */
/** @var app\models\Tickets $model */
$this->title = '工单: '.$model->title;
$this->params['breadcrumbs'][] = ['label' => '工单支持', 'url' => ['index']];
$this->params['breadcrumbs'][] = '工单ID '.$model->id;
YiiAsset::register($this);
?>
<div class="tickets-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('关闭工单', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'user_id',
'title',
'description:ntext',
'status',
'created_at',
'updated_at',
'ip',
],
]) ?>
</div>