/
TicketController.php
241 lines (232 loc) · 8.67 KB
/
TicketController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?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;
use Illuminate\Http\Request;
use App\Models\Ticket;
use App\Models\TicketMessage;
use Illuminate\Support\Facades\DB;
class TicketController extends Controller
{
public function fetch(Request $request)
{
if ($request->input('id')) {
$ticket = Ticket::where('id', $request->input('id'))
->where('user_id', $request->user['id'])
->first();
if (!$ticket) {
abort(500, __('Ticket does not exist'));
}
$ticket['message'] = TicketMessage::where('ticket_id', $ticket->id)->get();
for ($i = 0; $i < count($ticket['message']); $i++) {
if ($ticket['message'][$i]['user_id'] == $ticket->user_id) {
$ticket['message'][$i]['is_me'] = true;
} else {
$ticket['message'][$i]['is_me'] = false;
}
}
return response([
'data' => $ticket
]);
}
$ticket = Ticket::where('user_id', $request->user['id'])
->orderBy('created_at', 'DESC')
->get();
return response([
'data' => $ticket
]);
}
public function save(TicketSave $request)
{
DB::beginTransaction();
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([
'subject',
'level'
]), [
'user_id' => $request->user['id']
]));
if (!$ticket) {
DB::rollback();
abort(500, __('Failed to open ticket'));
}
$ticketMessage = TicketMessage::create([
'user_id' => $request->user['id'],
'ticket_id' => $ticket->id,
'message' => $request->input('message')
]);
if (!$ticketMessage) {
DB::rollback();
abort(500, __('Failed to open ticket'));
}
DB::commit();
$this->sendNotify($ticket, $request->input('message'), $request->user['id']);
return response([
'data' => true
]);
}
public function reply(Request $request)
{
if (empty($request->input('id'))) {
abort(500, __('Invalid parameter'));
}
if (empty($request->input('message'))) {
abort(500, __('Message cannot be empty'));
}
$ticket = Ticket::where('id', $request->input('id'))
->where('user_id', $request->user['id'])
->first();
if (!$ticket) {
abort(500, __('Ticket does not exist'));
}
if ($ticket->status) {
abort(500, __('The ticket is closed and cannot be replied'));
}
if ($request->user['id'] == $this->getLastMessage($ticket->id)->user_id) {
abort(500, __('Please wait for the technical enginneer to reply'));
}
$ticketService = new TicketService();
if (
!$ticketService->reply(
$ticket,
$request->input('message'),
$request->user['id']
)
) {
abort(500, __('Ticket reply failed'));
}
$this->sendNotify($ticket, $request->input('message'), $request->user['id']);
return response([
'data' => true
]);
}
public function close(Request $request)
{
if (empty($request->input('id'))) {
abort(500, __('Invalid parameter'));
}
$ticket = Ticket::where('id', $request->input('id'))
->where('user_id', $request->user['id'])
->first();
if (!$ticket) {
abort(500, __('Ticket does not exist'));
}
$ticket->status = 1;
if (!$ticket->save()) {
abort(500, __('Close failed'));
}
return response([
'data' => true
]);
}
private function getLastMessage($ticketId)
{
return TicketMessage::where('ticket_id', $ticketId)
->orderBy('id', 'DESC')
->first();
}
public function withdraw(TicketWithdraw $request)
{
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
)
)
) {
abort(500, __('Unsupported withdrawal method'));
}
$user = User::find($request->user['id']);
$limit = config('v2board.commission_withdraw_limit', 100);
if ($limit > ($user->commission_balance / 100)) {
abort(500, __('The current required minimum withdrawal commission is :limit', ['limit' => $limit]));
}
DB::beginTransaction();
$subject = __('[Commission Withdrawal Request] This ticket is opened by the system');
$ticket = Ticket::create([
'subject' => $subject,
'level' => 2,
'user_id' => $request->user['id']
]);
if (!$ticket) {
DB::rollback();
abort(500, __('Failed to open ticket'));
}
$message = sprintf(
"%s\r\n%s",
__('Withdrawal method') . ":" . $request->input('withdraw_method'),
__('Withdrawal account') . ":" . $request->input('withdraw_account')
);
$ticketMessage = TicketMessage::create([
'user_id' => $request->user['id'],
'ticket_id' => $ticket->id,
'message' => $message
]);
if (!$ticketMessage) {
DB::rollback();
abort(500, __('Failed to open ticket'));
}
DB::commit();
$this->sendNotify($ticket, $message);
return response([
'data' => true
]);
}
private function sendNotify(Ticket $ticket, string $message, $userid = null)
{
$telegramService = new TelegramService();
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);
$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`{$plan->name} 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;
}
}