view() ->assign('telegram_enabled', TicketTelegramNotifier::enabled()) ->assign('telegram_configured', TicketTelegramNotifier::configured()) ->assign('telegram_bound', $status['bound']) ->assign('telegram_admin_id', $status['admin_id']) ->display('admin/telegram/index.tpl'); } public function bind($request, $response, $args) { if (!$this->canManageBinding()) { return $response->withJson(['ret' => 0, 'msg' => '你没有权限'], 403); } try { $binding = TicketTelegramNotifier::createBinding((int) Auth::getUser()->id); return $response->withJson(['ret' => 1] + $binding); } catch (\Throwable $exception) { return $response->withJson(['ret' => 0, 'msg' => '无法生成绑定码,请检查 Telegram 配置与数据库'], 422); } } public function unbind($request, $response, $args) { if (!$this->canManageBinding()) { return $response->withJson(['ret' => 0, 'msg' => '你没有权限'], 403); } TicketTelegramNotifier::unbind(); return $response->withJson(['ret' => 1, 'msg' => '已解绑工单通知']); } public function webhook($request, $response, $args) { if (!$this->canManageBinding()) { return $response->withJson(['ret' => 0, 'msg' => '你没有权限'], 403); } try { TicketTelegramNotifier::registerWebhook(); return $response->withJson(['ret' => 1, 'msg' => 'Webhook 注册成功']); } catch (\RuntimeException $exception) { return $response->withJson(['ret' => 0, 'msg' => $exception->getMessage()], 422); } } private function canManageBinding(): bool { // The routes also apply Admin + SuperAdmin/CSRF middleware. Recheck the // current account here so direct invocations cannot use a downgraded ID. $user = Auth::getUser(); return is_object($user) && !empty($user->is_admin) && (int) ($user->enable ?? 0) === 1 && AdminPermission::isSuperAdmin(); } }