Skip to content

Commit

Permalink
Merge pull request #2 from Neko-X2-Kyarameru/FantajiNeko-patch-1
Browse files Browse the repository at this point in the history
Update TicketController.php
  • Loading branch information
FantajiNeko authored Dec 30, 2024
2 parents 176c73f + c8afdd5 commit 742f76e
Showing 1 changed file with 67 additions and 19 deletions.
86 changes: 67 additions & 19 deletions app/Http/Controllers/User/TicketController.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?php

/*
Author: SakuraUI
Website: https://sakuraui.com
Description: 请勿倒卖,如果你是花钱买的恭喜你,倒霉蛋!
*/
namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Http\Requests\User\TicketSave;
use App\Http\Requests\User\TicketWithdraw;
use App\Jobs\SendTelegramJob;
use App\Models\User;
use App\Models\Plan;
use App\Services\TelegramService;
use App\Services\TicketService;
use App\Utils\Dict;
Expand Down Expand Up @@ -49,7 +54,7 @@ public function fetch(Request $request)
public function save(TicketSave $request)
{
DB::beginTransaction();
if ((int)Ticket::where('status', 0)->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([
Expand All @@ -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
]);
Expand All @@ -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
]);
Expand Down Expand Up @@ -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']);
Expand All @@ -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')
);
Expand All @@ -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;
}
}

0 comments on commit 742f76e

Please sign in to comment.