Xboard/app/Http/Controllers/V1/User/TicketController.php

190 lines
6.8 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
namespace App\Http\Controllers\V1\User;
2023-12-04 07:40:49 -05:00
use App\Exceptions\ApiException;
2023-11-17 01:44:01 -05:00
use App\Http\Controllers\Controller;
use App\Http\Requests\User\TicketSave;
use App\Http\Requests\User\TicketWithdraw;
use App\Models\Ticket;
use App\Models\TicketMessage;
use App\Models\User;
use App\Services\TelegramService;
use App\Services\TicketService;
use App\Utils\Dict;
use Illuminate\Http\Request;
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) {
return $this->fail([400, __('Ticket does not exist')]);
2023-11-17 01:44:01 -05:00
}
$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 $this->success($ticket);
2023-11-17 01:44:01 -05:00
}
$ticket = Ticket::where('user_id', $request->user['id'])
->orderBy('created_at', 'DESC')
->get();
return $this->success($ticket);
2023-11-17 01:44:01 -05:00
}
public function save(TicketSave $request)
{
try{
DB::beginTransaction();
if ((int)Ticket::where('status', 0)->where('user_id', $request->user['id'])->lockForUpdate()->count()) {
return $this->fail([400, __('There are other unresolved tickets')]);
}
$ticket = Ticket::create(array_merge($request->only([
'subject',
'level'
]), [
'user_id' => $request->user['id']
]));
if (!$ticket) {
return $this->fail([400, __('Failed to open ticket')]);
}
$ticketMessage = TicketMessage::create([
'user_id' => $request->user['id'],
'ticket_id' => $ticket->id,
'message' => $request->input('message')
]);
if (!$ticketMessage) {
return $this->fail([400, __('Failed to open ticket')]);
}
DB::commit();
}catch(\Exception $e){
DB::rollBack();
throw $e;
2023-11-17 01:44:01 -05:00
}
$this->sendNotify($ticket, $request->input('message'));
return $this->success(true);
2023-11-17 01:44:01 -05:00
}
public function reply(Request $request)
{
if (empty($request->input('id'))) {
return $this->fail([400, __('Invalid parameter')]);
2023-11-17 01:44:01 -05:00
}
if (empty($request->input('message'))) {
return $this->fail([400, __('Message cannot be empty')]);
2023-11-17 01:44:01 -05:00
}
$ticket = Ticket::where('id', $request->input('id'))
->where('user_id', $request->user['id'])
->first();
if (!$ticket) {
return $this->fail([400, __('Ticket does not exist')]);
2023-11-17 01:44:01 -05:00
}
if ($ticket->status) {
return $this->fail([400, __('The ticket is closed and cannot be replied')]);
2023-11-17 01:44:01 -05:00
}
if ($request->user['id'] == $this->getLastMessage($ticket->id)->user_id) {
return $this->fail([400, __('Please wait for the technical enginneer to reply')]);
2023-11-17 01:44:01 -05:00
}
$ticketService = new TicketService();
if (!$ticketService->reply(
$ticket,
$request->input('message'),
$request->user['id']
)) {
return $this->fail([400, __('Ticket reply failed')]);
2023-11-17 01:44:01 -05:00
}
$this->sendNotify($ticket, $request->input('message'));
return $this->success(true);
2023-11-17 01:44:01 -05:00
}
public function close(Request $request)
{
if (empty($request->input('id'))) {
return $this->fail([422, __('Invalid parameter')]);
2023-11-17 01:44:01 -05:00
}
$ticket = Ticket::where('id', $request->input('id'))
->where('user_id', $request->user['id'])
->first();
if (!$ticket) {
return $this->fail([400, __('Ticket does not exist')]);
2023-11-17 01:44:01 -05:00
}
$ticket->status = 1;
if (!$ticket->save()) {
return $this->fail([500, __('Close failed')]);
2023-11-17 01:44:01 -05:00
}
return $this->success(true);
2023-11-17 01:44:01 -05:00
}
private function getLastMessage($ticketId)
{
return TicketMessage::where('ticket_id', $ticketId)
->orderBy('id', 'DESC')
->first();
}
public function withdraw(TicketWithdraw $request)
{
if ((int)admin_setting('withdraw_close_enable', 0)) {
return $this->fail([400, 'Unsupported withdraw']);
2023-11-17 01:44:01 -05:00
}
if (!in_array(
$request->input('withdraw_method'),
admin_setting('commission_withdraw_method',Dict::WITHDRAW_METHOD_WHITELIST_DEFAULT)
)) {
return $this->fail([422, __('Unsupported withdrawal method')]);
2023-11-17 01:44:01 -05:00
}
$user = User::find($request->user['id']);
$limit = admin_setting('commission_withdraw_limit', 100);
if ($limit > ($user->commission_balance / 100)) {
return $this->fail([422, __('The current required minimum withdrawal commission is :limit', ['limit' => $limit])]);
2023-11-17 01:44:01 -05:00
}
try{
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) {
return $this->fail([400, __('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) {
return $this->fail([400, __('Failed to open ticket')]);
}
DB::commit();
}catch(\Exception $e){
DB::rollBack();
throw $e;
2023-11-17 01:44:01 -05:00
}
$this->sendNotify($ticket, $message);
return $this->success(true);
2023-11-17 01:44:01 -05:00
}
private function sendNotify(Ticket $ticket, string $message)
{
$telegramService = new TelegramService();
$telegramService->sendMessageWithAdmin("📮工单提醒 #{$ticket->id}\n———————————————\n主题:\n`{$ticket->subject}`\n内容:\n`{$message}`", true);
}
}