From c8afdd5ad4da7be1d681567f19a2cd91bdedebc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fantaj=C4=AB=20=C3=97=20Neko?= Date: Mon, 30 Dec 2024 15:11:03 +0800 Subject: [PATCH] Update TicketController.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 工单telegram通知 --- .../Controllers/User/TicketController.php | 86 +++++++++++++++---- 1 file changed, 67 insertions(+), 19 deletions(-) diff --git a/app/Http/Controllers/User/TicketController.php b/app/Http/Controllers/User/TicketController.php index 14373b4f5..52344a5d5 100644 --- a/app/Http/Controllers/User/TicketController.php +++ b/app/Http/Controllers/User/TicketController.php @@ -1,5 +1,9 @@ where('user_id', $request->user['id'])->lockForUpdate()->count()) { + if ((int) Ticket::where('status', 0)->where('user_id', $request->user['id'])->lockForUpdate()->count()) { abort(500, __('There are other unresolved tickets')); } $ticket = Ticket::create(array_merge($request->only([ @@ -72,7 +77,7 @@ public function save(TicketSave $request) abort(500, __('Failed to open ticket')); } DB::commit(); - $this->sendNotify($ticket, $request->input('message')); + $this->sendNotify($ticket, $request->input('message'), $request->user['id']); return response([ 'data' => true ]); @@ -99,14 +104,16 @@ public function reply(Request $request) abort(500, __('Please wait for the technical enginneer to reply')); } $ticketService = new TicketService(); - if (!$ticketService->reply( - $ticket, - $request->input('message'), - $request->user['id'] - )) { + if ( + !$ticketService->reply( + $ticket, + $request->input('message'), + $request->user['id'] + ) + ) { abort(500, __('Ticket reply failed')); } - $this->sendNotify($ticket, $request->input('message')); + $this->sendNotify($ticket, $request->input('message'), $request->user['id']); return response([ 'data' => true ]); @@ -142,16 +149,18 @@ private function getLastMessage($ticketId) public function withdraw(TicketWithdraw $request) { - if ((int)config('v2board.withdraw_close_enable', 0)) { + if ((int) config('v2board.withdraw_close_enable', 0)) { abort(500, 'user.ticket.withdraw.not_support_withdraw'); } - if (!in_array( - $request->input('withdraw_method'), - config( - 'v2board.commission_withdraw_method', - Dict::WITHDRAW_METHOD_WHITELIST_DEFAULT + if ( + !in_array( + $request->input('withdraw_method'), + config( + 'v2board.commission_withdraw_method', + Dict::WITHDRAW_METHOD_WHITELIST_DEFAULT + ) ) - )) { + ) { abort(500, __('Unsupported withdrawal method')); } $user = User::find($request->user['id']); @@ -170,7 +179,8 @@ public function withdraw(TicketWithdraw $request) DB::rollback(); abort(500, __('Failed to open ticket')); } - $message = sprintf("%s\r\n%s", + $message = sprintf( + "%s\r\n%s", __('Withdrawal method') . ":" . $request->input('withdraw_method'), __('Withdrawal account') . ":" . $request->input('withdraw_account') ); @@ -190,9 +200,47 @@ public function withdraw(TicketWithdraw $request) ]); } - private function sendNotify(Ticket $ticket, string $message) + private function sendNotify(Ticket $ticket, string $message, $userid = null) { $telegramService = new TelegramService(); - $telegramService->sendMessageWithAdmin("📮工单提醒 #{$ticket->id}\n———————————————\n主题:\n`{$ticket->subject}`\n内容:\n`{$message}`", true); + if (!empty($userid)) { + $user = User::find($userid); + $transfer_enable = $this->getFlowData($user->transfer_enable); // 总流量 + $remaining_traffic = $this->getFlowData($user->transfer_enable - $user->u - $user->d); // 剩余流量 + $u = $this->getFlowData($user->u); // 上传 + $d = $this->getFlowData($user->d); // 下载 + $expired_at = date("Y-m-d h:m:s", $user->expired_at); // 到期时间 + $ip_address = $_SERVER['REMOTE_ADDR']; // IP地址 + $api_url = "http://ip-api.com/json/{$ip_address}?fields=520191&lang=zh-CN"; + $response = file_get_contents($api_url); + $user_location = json_decode($response, true); + if ($user_location && $user_location['status'] === 'success') { + $location = $user_location['city'] . ", " . $user_location['country']; + } else { + $location = "无法确定用户地址"; + } + $plan = Plan::find($user->plan_id); + if ($plan) { + $planName = $plan->name; + } else { + $planName = '未分配套餐'; // 或者提供一个默认值 + } + $money = $user->balance / 100; + $affmoney = $user->commission_balance / 100; + $telegramService->sendMessageWithAdmin("📮工单提醒 #{$ticket->id}\n———————————————\n邮箱:\n`{$user->email}`\n用户位置:\n`{$location}`\nIP:\n{$ip_address}\n套餐与流量:\n`{$planName} of {$transfer_enable}/{$remaining_traffic}`\n上传/下载:\n`{$u}/{$d}`\n到期时间:\n`{$expired_at}`\n余额/佣金余额:\n`{$money}/{$affmoney}`\n主题:\n`{$ticket->subject}`\n内容:\n`{$message}`", true); + } else { + $telegramService->sendMessageWithAdmin("📮工单提醒 #{$ticket->id}\n———————————————\n主题:\n`{$ticket->subject}`\n内容:\n`{$message}`", true); + } + } + private function getFlowData($b) + { + $g = $b / (1024 * 1024 * 1024); // 转换流量数据 + $m = $b / (1024 * 1024); + if ($g >= 1) { + $text = round($g, 2) . "GB"; + } else { + $text = round($m, 2) . "MB"; + } + return $text; } }