"操作", "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_data_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); } $antiXss = new AntiXSS(); $cleanContent = $antiXss->xss_clean($content); try { $result = TicketReplyService::create( (int) $id, (int) Auth::getUser()->id, $cleanContent, (int) $status, $request ); } catch (\Throwable $exception) { error_log('Admin ticket reply failed: ' . $exception->getMessage()); return $this->echoJson($response, [ 'ret' => 0, 'msg' => $exception instanceof \InvalidArgumentException ? $exception->getMessage() : '工单回复保存失败', ]); } if ($result === null) { return $this->echoJson($response, ['ret' => 0, 'msg' => '工单不存在']); } $ticketMain = $result['root']; try { $user = User::where('id', (int) $ticketMain->userid)->first(); if ($user !== null && trim((string) $user->email) !== '') { Mail::send($user->email, Config::get('appName') . ' - 工单收到回复', 'ticket/replied.tpl', [ 'user' => $user, 'ticket' => $ticketMain, 'ticket_url' => rtrim((string) Config::get('baseUrl'), '/') . '/user/ticket/' . (int) $ticketMain->id . '/view', ], [], 'ticket_replied'); } } catch (\Throwable $exception) { error_log('Ticket user notification failed: ' . $exception->getMessage()); } $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 = (int) ($args['id'] ?? 0); $ticketMain = Ticket::where("id", $id)->where("rootid", 0)->first(); if ($ticketMain === null) { return $response->withStatus(404); } $pageNum = 1; if (isset($request->getQueryParams()["page"])) { $pageNum = max(1, (int) $request->getQueryParams()["page"]); } $ticketset=Ticket::where("id", $id)->orWhere("rootid", "=", $id)->orderBy("datetime", "desc")->paginate(5, ['*'], 'page', $pageNum); $ticketset->setPath('/admin/ticket/'.$id."/view"); $attachmentsAvailable = $this->ticketAttachmentsAvailable(); 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)); $attachments = []; if ($attachmentsAvailable) { try { $attachments = TicketAttachment::where('message_id', (int) $ticket->id)->orderBy('id')->get(); } catch (\Throwable $exception) { // A partially migrated installation must still be able to open a ticket. $attachments = []; } } $ticket->setAttribute('attachments', $attachments); $ticket->setAttribute('has_attachments', count($attachments) > 0); } return $this->view()->assign('ticketset', $ticketset) ->assign('ticket_main', $ticketMain) ->assign('id', $id) ->assign('ticket_attachments_available', $attachmentsAvailable) ->display('admin/ticket/view.tpl'); } public function attachment($request, $response, $args) { if (!$this->ticketAttachmentsAvailable()) { return $response->withStatus(404); } try { $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(); } catch (\Throwable $exception) { return $response->withStatus(404); } return $this->streamTicketAttachment($response, $attachment); } public function ajax($request, $response, $args) { $datatables = new Datatables(new DatatablesHelper()); $connection = (new Ticket())->getConnection(); $schema = $connection->getSchemaBuilder(); $ticketType = $this->ticketColumnExpression($schema, 'ticket_type', "'technical'"); $priority = $this->ticketColumnExpression($schema, 'priority', "'medium'"); $instanceId = $this->ticketColumnExpression($schema, 'instance_id', 'NULL'); $datatables->query('Select ticket.id as op,ticket.id,ticket.datetime,ticket.title,ticket.userid,user.user_name,' . $ticketType . ' as ticket_type,' . $priority . ' as priority,' . $instanceId . ' as instance_id,ticket.status from ticket,user where ticket.userid = user.id and ticket.rootid = 0'); $datatables->edit('op', function ($data) { return '查看'; }); $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()); } private function ticketAttachmentsAvailable(): bool { try { $schema = (new TicketAttachment())->getConnection()->getSchemaBuilder(); if (!$schema->hasTable('ticket_attachments')) { return false; } foreach (['id', 'ticket_id', 'message_id', 'user_id', 'storage_key', 'original_name', 'mime_type', 'bytes', 'sha256', 'created_at'] as $column) { if (!$schema->hasColumn('ticket_attachments', $column)) { return false; } } return true; } catch (\Throwable $exception) { return false; } } private function ticketColumnExpression($schema, string $column, string $fallback): string { try { return $schema->hasColumn('ticket', $column) ? 'ticket.' . $column : $fallback; } catch (\Throwable $exception) { return $fallback; } } }