You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
224 lines
8.9 KiB
PHP
224 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Models\Ticket;
|
|
use App\Models\TicketAttachment;
|
|
use App\Models\User;
|
|
|
|
use voku\helper\AntiXSS;
|
|
use App\Services\Auth;
|
|
use App\Services\AdminDataMasker;
|
|
use App\Services\AdminPermission;
|
|
use App\Services\TicketTelegramConfig;
|
|
use App\Services\TicketTelegramNotifier;
|
|
use App\Services\TicketAttachmentService;
|
|
use App\Services\AdminCsrf;
|
|
|
|
use App\Services\Mail;
|
|
use App\Services\Config;
|
|
|
|
use App\Utils\SafeDatatables as Datatables;
|
|
use App\Utils\DatatablesHelper;
|
|
|
|
use App\Controllers\AdminController;
|
|
|
|
class TicketController extends AdminController
|
|
{
|
|
public function index($request, $response, $args)
|
|
{
|
|
$table_config['total_column'] = array("op" => "操作", "id" => "ID",
|
|
"datetime" => "时间", "title" => "标题", "userid" => "用户ID",
|
|
"user_name" => "用户名", "status" => "状态");
|
|
$table_config['default_show_column'] = array("op", "id",
|
|
"datetime", "title", "userid", "user_name", "status");
|
|
$table_config['ajax_url'] = 'ticket/ajax';
|
|
$view = $this->view()->assign('table_config', $table_config);
|
|
if (AdminPermission::isSuperAdmin()) {
|
|
$telegram = $this->telegramViewState();
|
|
$view
|
|
->assign('telegram_config', $telegram['config'])
|
|
->assign('telegram_enabled', $telegram['config']['enabled'])
|
|
->assign('telegram_configured', $telegram['config']['configured'])
|
|
->assign('telegram_bound', $telegram['binding']['bound'])
|
|
->assign('telegram_admin_id', $telegram['binding']['admin_id']);
|
|
}
|
|
return $view->display('admin/ticket/index.tpl');
|
|
}
|
|
|
|
private function telegramViewState(): array
|
|
{
|
|
$binding = ['bound' => false, 'admin_id' => null];
|
|
$bindingError = '';
|
|
try {
|
|
$binding = TicketTelegramNotifier::bindingStatus();
|
|
} catch (\Throwable $exception) {
|
|
$bindingError = 'Telegram 绑定状态读取失败,请检查数据库日志';
|
|
}
|
|
|
|
try {
|
|
$config = TicketTelegramConfig::publicValues();
|
|
} catch (\Throwable $exception) {
|
|
$config = [
|
|
'enabled' => false,
|
|
'webhook_base_url' => '',
|
|
'webhook_url' => '',
|
|
'bot_token_configured' => false,
|
|
'webhook_secret_configured' => false,
|
|
'configured' => false,
|
|
'configuration_error' => 'Telegram 配置读取失败,请检查数据库与 cloud_payload_key',
|
|
];
|
|
}
|
|
if ($bindingError !== '' && ($config['configuration_error'] ?? '') === '') {
|
|
$config['configuration_error'] = $bindingError;
|
|
}
|
|
|
|
return ['config' => $config, 'binding' => $binding];
|
|
}
|
|
|
|
|
|
|
|
public function update($request, $response, $args)
|
|
{
|
|
$id = $args['id'];
|
|
$content = $request->getParam('content');
|
|
$status = $request->getParam('status');
|
|
|
|
if (AdminPermission::isSuperAdmin()) {
|
|
if (!AdminCsrf::verify($request)) {
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '请求已过期,请刷新页面后重试']);
|
|
}
|
|
if (!is_string($content) || trim($content) === '' || !in_array((string) $status, ['0', '1'], true)) {
|
|
$res['ret'] = 0;
|
|
$res['msg'] = "请填全";
|
|
return $this->echoJson($response, $res);
|
|
}
|
|
|
|
$ticket_main=Ticket::where("id", "=", $id)->where("rootid", "=", 0)->first();
|
|
if ($ticket_main === null) {
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => '工单不存在']);
|
|
}
|
|
|
|
$previousStatus = (int) $ticket_main->status;
|
|
$antiXss = new AntiXSS();
|
|
|
|
$ticket=new Ticket();
|
|
$ticket->title=$antiXss->xss_clean($ticket_main->title);
|
|
$ticket->content=$antiXss->xss_clean($content);
|
|
$ticket->rootid=$ticket_main->id;
|
|
$ticket->userid=Auth::getUser()->id;
|
|
$ticket->datetime=time();
|
|
$ticket->status=(int) $status;
|
|
$ticket->ticket_type=(string) $ticket_main->ticket_type;
|
|
$ticket->priority=(string) $ticket_main->priority;
|
|
$ticket->instance_id=$ticket_main->instance_id;
|
|
$ticket_main->status=(int) $status;
|
|
|
|
try {
|
|
$ticket_main->getConnection()->transaction(function () use ($ticket_main, $ticket) {
|
|
if (!$ticket_main->save() || !$ticket->save()) {
|
|
throw new \RuntimeException('工单回复保存失败');
|
|
}
|
|
});
|
|
TicketAttachmentService::store($request, (int) $ticket_main->id, (int) $ticket->id, (int) Auth::getUser()->id);
|
|
} catch (\Throwable $exception) {
|
|
try {
|
|
$ticket_main->status = $previousStatus;
|
|
$ticket_main->save();
|
|
$ticket->delete();
|
|
} catch (\Throwable $rollbackException) {
|
|
error_log('Admin ticket reply rollback failed: ' . $rollbackException->getMessage());
|
|
}
|
|
return $this->echoJson($response, ['ret' => 0, 'msg' => $exception->getMessage() ?: '工单回复保存失败']);
|
|
}
|
|
|
|
$user = User::where('id', (int) $ticket_main->userid)->first();
|
|
if ($user !== null && trim((string) $user->email) !== '') {
|
|
Mail::send($user->email, Config::get('appName') . ' - 工单收到回复', 'ticket/replied.tpl', [
|
|
'user' => $user,
|
|
'ticket' => $ticket_main,
|
|
'ticket_url' => rtrim((string) Config::get('baseUrl'), '/') . '/user/ticket/' . (int) $ticket_main->id . '/view',
|
|
], [], 'ticket_replied');
|
|
}
|
|
|
|
$res['ret'] = 1;
|
|
$res['msg'] = "提交成功";
|
|
return $this->echoJson($response, $res);
|
|
|
|
} else {
|
|
$res['ret'] = 0;
|
|
$res['msg'] = "你没有提交工单权限";
|
|
return $this->echoJson($response, $res);
|
|
}
|
|
}
|
|
|
|
public function show($request, $response, $args)
|
|
{
|
|
$id = $args['id'];
|
|
|
|
$pageNum = 1;
|
|
if (isset($request->getQueryParams()["page"])) {
|
|
$pageNum = $request->getQueryParams()["page"];
|
|
}
|
|
|
|
|
|
$ticketset=Ticket::where("id", $id)->orWhere("rootid", "=", $id)->orderBy("datetime", "desc")->paginate(5, ['*'], 'page', $pageNum);
|
|
$ticketset->setPath('/admin/ticket/'.$id."/view");
|
|
foreach ($ticketset as $ticket) {
|
|
$author = User::find((int) $ticket->userid);
|
|
$isAdmin = $author != null && (int) $author->is_admin === 1;
|
|
$ticket->setAttribute('display_author_is_admin', $isAdmin);
|
|
$ticket->setAttribute(
|
|
'display_author_name',
|
|
$author == null
|
|
? '用户已删除'
|
|
: ($isAdmin ? '管理员' : AdminDataMasker::account($author->user_name))
|
|
);
|
|
$ticket->setAttribute('display_content', AdminDataMasker::sensitiveText($ticket->content));
|
|
$ticket->setAttribute('attachments', TicketAttachment::where('message_id', (int) $ticket->id)->orderBy('id')->get());
|
|
}
|
|
|
|
$ticketMain = Ticket::where('id', (int) $id)->where('rootid', 0)->first();
|
|
return $this->view()->assign('ticketset', $ticketset)
|
|
->assign('ticket_main', $ticketMain)
|
|
->assign("id", $id)->display('admin/ticket/view.tpl');
|
|
}
|
|
|
|
public function attachment($request, $response, $args)
|
|
{
|
|
$ticket = Ticket::where('id', (int) ($args['id'] ?? 0))->where('rootid', 0)->first();
|
|
$attachment = TicketAttachment::where('id', (int) ($args['attachment_id'] ?? 0))
|
|
->where('ticket_id', $ticket ? (int) $ticket->id : -1)->first();
|
|
return $this->streamTicketAttachment($response, $attachment);
|
|
}
|
|
|
|
public function ajax($request, $response, $args)
|
|
{
|
|
$datatables = new Datatables(new DatatablesHelper());
|
|
$datatables->query('Select ticket.id as op,ticket.id,ticket.datetime,ticket.title,ticket.userid,user.user_name,ticket.ticket_type,ticket.priority,ticket.instance_id,ticket.status from ticket,user where ticket.userid = user.id and ticket.rootid = 0');
|
|
|
|
$datatables->edit('op', function ($data) {
|
|
return '<a class="btn btn-brand" href="/admin/ticket/'.$data['id'].'/view">查看</a>';
|
|
});
|
|
|
|
$datatables->edit('datetime', function ($data) {
|
|
return date('Y-m-d H:i:s', $data['datetime']);
|
|
});
|
|
|
|
$datatables->edit('user_name', function ($data) {
|
|
return AdminDataMasker::account($data['user_name']);
|
|
});
|
|
|
|
$datatables->edit('title', function ($data) {
|
|
return AdminDataMasker::sensitiveText($data['title']);
|
|
});
|
|
|
|
$datatables->edit('status', function ($data) {
|
|
return $data['status'] == 1 ? '开启' : '关闭';
|
|
});
|
|
|
|
$body = $response->getBody();
|
|
$body->write($datatables->generate());
|
|
}
|
|
}
|