2019-12-12 19:47:32 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
2020-01-29 16:08:50 +08:00
|
|
|
|
namespace App\Http\Controllers\User;
|
2019-12-12 19:47:32 +08:00
|
|
|
|
|
2020-01-29 16:08:50 +08:00
|
|
|
|
use App\Http\Controllers\Controller;
|
2020-01-29 16:22:39 +08:00
|
|
|
|
use App\Http\Requests\User\TicketSave;
|
2020-05-10 18:38:02 +08:00
|
|
|
|
use App\Http\Requests\User\TicketWithdraw;
|
2020-05-25 15:48:12 +08:00
|
|
|
|
use App\Jobs\SendTelegramJob;
|
|
|
|
|
use App\Models\User;
|
2020-07-31 15:22:27 +08:00
|
|
|
|
use App\Services\TelegramService;
|
2021-01-08 00:33:11 +08:00
|
|
|
|
use App\Utils\Dict;
|
2019-12-12 19:47:32 +08:00
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use App\Models\Ticket;
|
|
|
|
|
use App\Models\TicketMessage;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
|
|
class TicketController extends Controller
|
|
|
|
|
{
|
2020-01-11 13:36:52 +08:00
|
|
|
|
public function fetch(Request $request)
|
|
|
|
|
{
|
2019-12-12 20:08:20 +08:00
|
|
|
|
if ($request->input('id')) {
|
|
|
|
|
$ticket = Ticket::where('id', $request->input('id'))
|
|
|
|
|
->where('user_id', $request->session()->get('id'))
|
|
|
|
|
->first();
|
|
|
|
|
if (!$ticket) {
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Ticket does not exist'));
|
2019-12-12 20:08:20 +08:00
|
|
|
|
}
|
|
|
|
|
$ticket['message'] = TicketMessage::where('ticket_id', $ticket->id)->get();
|
2019-12-12 20:27:24 +08:00
|
|
|
|
for ($i = 0; $i < count($ticket['message']); $i++) {
|
2019-12-13 22:30:45 +08:00
|
|
|
|
if ($ticket['message'][$i]['user_id'] == $ticket->user_id) {
|
2019-12-12 20:26:56 +08:00
|
|
|
|
$ticket['message'][$i]['is_me'] = true;
|
|
|
|
|
} else {
|
|
|
|
|
$ticket['message'][$i]['is_me'] = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-12 20:08:20 +08:00
|
|
|
|
return response([
|
|
|
|
|
'data' => $ticket
|
|
|
|
|
]);
|
|
|
|
|
}
|
2019-12-13 17:13:10 +08:00
|
|
|
|
$ticket = Ticket::where('user_id', $request->session()->get('id'))
|
|
|
|
|
->orderBy('created_at', 'DESC')
|
|
|
|
|
->get();
|
|
|
|
|
for ($i = 0; $i < count($ticket); $i++) {
|
2019-12-13 17:16:53 +08:00
|
|
|
|
if ($ticket[$i]['last_reply_user_id'] == $request->session()->get('id')) {
|
2019-12-13 17:13:10 +08:00
|
|
|
|
$ticket[$i]['reply_status'] = 0;
|
|
|
|
|
} else {
|
|
|
|
|
$ticket[$i]['reply_status'] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-12 19:47:32 +08:00
|
|
|
|
return response([
|
2019-12-13 17:13:10 +08:00
|
|
|
|
'data' => $ticket
|
2019-12-12 19:47:32 +08:00
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
|
public function save(TicketSave $request)
|
|
|
|
|
{
|
2019-12-12 19:47:32 +08:00
|
|
|
|
DB::beginTransaction();
|
2020-04-07 01:19:52 +08:00
|
|
|
|
if ((int)Ticket::where('status', 0)->where('user_id', $request->session()->get('id'))->count()) {
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('There are other unresolved tickets'));
|
2020-04-07 00:40:52 +08:00
|
|
|
|
}
|
2019-12-12 19:47:32 +08:00
|
|
|
|
$ticket = Ticket::create(array_merge($request->only([
|
|
|
|
|
'subject',
|
|
|
|
|
'level'
|
|
|
|
|
]), [
|
2019-12-13 17:07:46 +08:00
|
|
|
|
'user_id' => $request->session()->get('id'),
|
|
|
|
|
'last_reply_user_id' => $request->session()->get('id')
|
2019-12-12 19:47:32 +08:00
|
|
|
|
]));
|
|
|
|
|
if (!$ticket) {
|
|
|
|
|
DB::rollback();
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Failed to open ticket'));
|
2019-12-12 19:47:32 +08:00
|
|
|
|
}
|
|
|
|
|
$ticketMessage = TicketMessage::create([
|
|
|
|
|
'user_id' => $request->session()->get('id'),
|
|
|
|
|
'ticket_id' => $ticket->id,
|
|
|
|
|
'message' => $request->input('message')
|
|
|
|
|
]);
|
|
|
|
|
if (!$ticketMessage) {
|
|
|
|
|
DB::rollback();
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Failed to open ticket'));
|
2019-12-12 19:47:32 +08:00
|
|
|
|
}
|
|
|
|
|
DB::commit();
|
2020-05-25 15:48:12 +08:00
|
|
|
|
$this->sendNotify($ticket, $ticketMessage);
|
2019-12-12 19:47:32 +08:00
|
|
|
|
return response([
|
|
|
|
|
'data' => true
|
|
|
|
|
]);
|
|
|
|
|
}
|
2019-12-12 20:33:14 +08:00
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
|
public function reply(Request $request)
|
|
|
|
|
{
|
2019-12-13 15:16:44 +08:00
|
|
|
|
if (empty($request->input('id'))) {
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Invalid parameter'));
|
2019-12-12 20:33:14 +08:00
|
|
|
|
}
|
2019-12-13 15:16:44 +08:00
|
|
|
|
if (empty($request->input('message'))) {
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Message cannot be empty'));
|
2019-12-13 15:16:44 +08:00
|
|
|
|
}
|
2019-12-12 20:33:14 +08:00
|
|
|
|
$ticket = Ticket::where('id', $request->input('id'))
|
|
|
|
|
->where('user_id', $request->session()->get('id'))
|
|
|
|
|
->first();
|
|
|
|
|
if (!$ticket) {
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Ticket does not exist'));
|
2019-12-12 20:33:14 +08:00
|
|
|
|
}
|
2019-12-13 17:37:51 +08:00
|
|
|
|
if ($ticket->status) {
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('The ticket is closed and cannot be replied'));
|
2019-12-13 17:37:51 +08:00
|
|
|
|
}
|
2019-12-13 16:17:01 +08:00
|
|
|
|
if ($request->session()->get('id') == $this->getLastMessage($ticket->id)->user_id) {
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Please wait for the technical enginneer to reply'));
|
2019-12-13 16:16:09 +08:00
|
|
|
|
}
|
2019-12-13 17:07:46 +08:00
|
|
|
|
DB::beginTransaction();
|
2019-12-12 20:33:14 +08:00
|
|
|
|
$ticketMessage = TicketMessage::create([
|
|
|
|
|
'user_id' => $request->session()->get('id'),
|
|
|
|
|
'ticket_id' => $ticket->id,
|
|
|
|
|
'message' => $request->input('message')
|
|
|
|
|
]);
|
2019-12-13 17:07:46 +08:00
|
|
|
|
$ticket->last_reply_user_id = $request->session()->get('id');
|
|
|
|
|
if (!$ticketMessage || !$ticket->save()) {
|
|
|
|
|
DB::rollback();
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Ticket reply failed'));
|
2019-12-12 20:33:14 +08:00
|
|
|
|
}
|
2019-12-13 17:07:46 +08:00
|
|
|
|
DB::commit();
|
2020-05-25 15:48:12 +08:00
|
|
|
|
$this->sendNotify($ticket, $ticketMessage);
|
2019-12-12 20:33:14 +08:00
|
|
|
|
return response([
|
|
|
|
|
'data' => true
|
|
|
|
|
]);
|
|
|
|
|
}
|
2019-12-13 16:16:09 +08:00
|
|
|
|
|
2019-12-13 17:29:55 +08:00
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
|
public function close(Request $request)
|
|
|
|
|
{
|
2019-12-13 17:29:55 +08:00
|
|
|
|
if (empty($request->input('id'))) {
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Invalid parameter'));
|
2019-12-13 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
$ticket = Ticket::where('id', $request->input('id'))
|
|
|
|
|
->where('user_id', $request->session()->get('id'))
|
|
|
|
|
->first();
|
|
|
|
|
if (!$ticket) {
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Ticket does not exist'));
|
2019-12-13 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
$ticket->status = 1;
|
|
|
|
|
if (!$ticket->save()) {
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Close failed'));
|
2019-12-13 17:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
return response([
|
|
|
|
|
'data' => true
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
|
private function getLastMessage($ticketId)
|
|
|
|
|
{
|
2019-12-13 16:16:09 +08:00
|
|
|
|
return TicketMessage::where('ticket_id', $ticketId)
|
|
|
|
|
->orderBy('id', 'DESC')
|
|
|
|
|
->first();
|
|
|
|
|
}
|
2020-05-10 18:38:02 +08:00
|
|
|
|
|
|
|
|
|
public function withdraw(TicketWithdraw $request)
|
|
|
|
|
{
|
2021-02-03 18:17:32 +08:00
|
|
|
|
if ((int)config('v2board.withdraw_close_enable', 0)) {
|
|
|
|
|
abort(500, 'user.ticket.withdraw.not_support_withdraw');
|
|
|
|
|
}
|
2021-01-08 00:33:11 +08:00
|
|
|
|
if (!in_array(
|
|
|
|
|
$request->input('withdraw_method'),
|
|
|
|
|
config(
|
|
|
|
|
'v2board.commission_withdraw_method',
|
|
|
|
|
Dict::WITHDRAW_METHOD_WHITELIST_DEFAULT
|
|
|
|
|
)
|
|
|
|
|
)) {
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Unsupported withdrawal method'));
|
2021-01-08 00:33:11 +08:00
|
|
|
|
}
|
2020-10-29 00:37:37 +08:00
|
|
|
|
$user = User::find($request->session()->get('id'));
|
|
|
|
|
$limit = config('v2board.commission_withdraw_limit', 100);
|
2020-10-29 01:00:22 +08:00
|
|
|
|
if ($limit > ($user->commission_balance / 100)) {
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('The current required minimum withdrawal commission is', ['limit' => $limit]));
|
2020-10-29 00:37:37 +08:00
|
|
|
|
}
|
2020-05-10 18:38:02 +08:00
|
|
|
|
DB::beginTransaction();
|
2021-06-12 00:56:39 +08:00
|
|
|
|
$subject = __('[Commission Withdrawal Request] This ticket is opened by the system');
|
2020-05-10 18:38:02 +08:00
|
|
|
|
$ticket = Ticket::create([
|
|
|
|
|
'subject' => $subject,
|
|
|
|
|
'level' => 2,
|
|
|
|
|
'user_id' => $request->session()->get('id'),
|
|
|
|
|
'last_reply_user_id' => $request->session()->get('id')
|
|
|
|
|
]);
|
|
|
|
|
if (!$ticket) {
|
|
|
|
|
DB::rollback();
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Failed to open ticket'));
|
2020-05-10 18:38:02 +08:00
|
|
|
|
}
|
2021-06-12 00:56:39 +08:00
|
|
|
|
$message = sprintf("%s\r\n%s",
|
|
|
|
|
__('Withdrawal method') . ":" . $request->input('withdraw_method'),
|
|
|
|
|
__('Withdrawal account') . ":" . $request->input('withdraw_account')
|
|
|
|
|
);
|
2020-05-10 18:38:02 +08:00
|
|
|
|
$ticketMessage = TicketMessage::create([
|
|
|
|
|
'user_id' => $request->session()->get('id'),
|
|
|
|
|
'ticket_id' => $ticket->id,
|
|
|
|
|
'message' => $message
|
|
|
|
|
]);
|
|
|
|
|
if (!$ticketMessage) {
|
|
|
|
|
DB::rollback();
|
2021-06-12 00:56:39 +08:00
|
|
|
|
abort(500, __('Failed to open ticket'));
|
2020-05-10 18:38:02 +08:00
|
|
|
|
}
|
|
|
|
|
DB::commit();
|
2020-08-02 15:07:28 +08:00
|
|
|
|
$this->sendNotify($ticket, $ticketMessage);
|
2020-05-10 18:38:02 +08:00
|
|
|
|
return response([
|
|
|
|
|
'data' => true
|
|
|
|
|
]);
|
|
|
|
|
}
|
2020-08-02 15:07:28 +08:00
|
|
|
|
|
|
|
|
|
private function sendNotify(Ticket $ticket, TicketMessage $ticketMessage)
|
|
|
|
|
{
|
|
|
|
|
$telegramService = new TelegramService();
|
2020-09-20 16:41:48 +08:00
|
|
|
|
$telegramService->sendMessageWithAdmin("📮工单提醒 #{$ticket->id}\n———————————————\n主题:\n`{$ticket->subject}`\n内容:\n`{$ticketMessage->message}`", true);
|
2020-08-02 15:07:28 +08:00
|
|
|
|
}
|
2019-12-12 19:47:32 +08:00
|
|
|
|
}
|