mirror of
https://github.com/v2board/v2board.git
synced 2024-11-10 17:49:11 +08:00
opt: ticket service
This commit is contained in:
parent
36bc93e1f8
commit
f626acc0aa
@ -3,6 +3,7 @@
|
|||||||
namespace App\Http\Controllers\Admin;
|
namespace App\Http\Controllers\Admin;
|
||||||
|
|
||||||
use App\Jobs\SendEmailJob;
|
use App\Jobs\SendEmailJob;
|
||||||
|
use App\Services\TicketService;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\Ticket;
|
use App\Models\Ticket;
|
||||||
@ -63,27 +64,12 @@ class TicketController extends Controller
|
|||||||
if (empty($request->input('message'))) {
|
if (empty($request->input('message'))) {
|
||||||
abort(500, '消息不能为空');
|
abort(500, '消息不能为空');
|
||||||
}
|
}
|
||||||
$ticket = Ticket::where('id', $request->input('id'))
|
$ticketService = new TicketService();
|
||||||
->first();
|
$ticketService->replayByAdmin(
|
||||||
if (!$ticket) {
|
$request->input('id'),
|
||||||
abort(500, '工单不存在');
|
$request->input('message'),
|
||||||
}
|
$request->session()->get('id')
|
||||||
if ($ticket->status) {
|
);
|
||||||
abort(500, '工单已关闭,无法回复');
|
|
||||||
}
|
|
||||||
DB::beginTransaction();
|
|
||||||
$ticketMessage = TicketMessage::create([
|
|
||||||
'user_id' => $request->session()->get('id'),
|
|
||||||
'ticket_id' => $ticket->id,
|
|
||||||
'message' => $request->input('message')
|
|
||||||
]);
|
|
||||||
$ticket->last_reply_user_id = $request->session()->get('id');
|
|
||||||
if (!$ticketMessage || !$ticket->save()) {
|
|
||||||
DB::rollback();
|
|
||||||
abort(500, '工单回复失败');
|
|
||||||
}
|
|
||||||
DB::commit();
|
|
||||||
$this->sendEmailNotify($ticket, $ticketMessage);
|
|
||||||
return response([
|
return response([
|
||||||
'data' => true
|
'data' => true
|
||||||
]);
|
]);
|
||||||
@ -107,24 +93,4 @@ class TicketController extends Controller
|
|||||||
'data' => true
|
'data' => true
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 半小时内不再重复通知
|
|
||||||
private function sendEmailNotify(Ticket $ticket, TicketMessage $ticketMessage)
|
|
||||||
{
|
|
||||||
$user = User::find($ticket->user_id);
|
|
||||||
$cacheKey = 'ticket_sendEmailNotify_' . $ticket->user_id;
|
|
||||||
if (!Cache::get($cacheKey)) {
|
|
||||||
Cache::put($cacheKey, 1, 1800);
|
|
||||||
SendEmailJob::dispatch([
|
|
||||||
'email' => $user->email,
|
|
||||||
'subject' => '您在' . config('v2board.app_name', 'V2Board') . '的工单得到了回复',
|
|
||||||
'template_name' => 'notify',
|
|
||||||
'template_value' => [
|
|
||||||
'name' => config('v2board.app_name', 'V2Board'),
|
|
||||||
'url' => config('v2board.app_url'),
|
|
||||||
'content' => "主题:{$ticket->subject}\r\n回复内容:{$ticketMessage->message}"
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
57
app/Services/TicketService.php
Normal file
57
app/Services/TicketService.php
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Jobs\SendEmailJob;
|
||||||
|
use App\Models\Ticket;
|
||||||
|
use App\Models\TicketMessage;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class TicketService {
|
||||||
|
public function replayByAdmin($ticketId, $message, $userId):void
|
||||||
|
{
|
||||||
|
$ticket = Ticket::where('id', $ticketId)
|
||||||
|
->first();
|
||||||
|
if (!$ticket) {
|
||||||
|
abort(500, '工单不存在');
|
||||||
|
}
|
||||||
|
if ($ticket->status) {
|
||||||
|
abort(500, '工单已关闭,无法回复');
|
||||||
|
}
|
||||||
|
DB::beginTransaction();
|
||||||
|
$ticketMessage = TicketMessage::create([
|
||||||
|
'user_id' => $userId,
|
||||||
|
'ticket_id' => $ticket->id,
|
||||||
|
'message' => $message
|
||||||
|
]);
|
||||||
|
$ticket->last_reply_user_id = $userId;
|
||||||
|
if (!$ticketMessage || !$ticket->save()) {
|
||||||
|
DB::rollback();
|
||||||
|
abort(500, '工单回复失败');
|
||||||
|
}
|
||||||
|
DB::commit();
|
||||||
|
$this->sendEmailNotify($ticket, $ticketMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 半小时内不再重复通知
|
||||||
|
private function sendEmailNotify(Ticket $ticket, TicketMessage $ticketMessage)
|
||||||
|
{
|
||||||
|
$user = User::find($ticket->user_id);
|
||||||
|
$cacheKey = 'ticket_sendEmailNotify_' . $ticket->user_id;
|
||||||
|
if (!Cache::get($cacheKey)) {
|
||||||
|
Cache::put($cacheKey, 1, 1800);
|
||||||
|
SendEmailJob::dispatch([
|
||||||
|
'email' => $user->email,
|
||||||
|
'subject' => '您在' . config('v2board.app_name', 'V2Board') . '的工单得到了回复',
|
||||||
|
'template_name' => 'notify',
|
||||||
|
'template_value' => [
|
||||||
|
'name' => config('v2board.app_name', 'V2Board'),
|
||||||
|
'url' => config('v2board.app_url'),
|
||||||
|
'content' => "主题:{$ticket->subject}\r\n回复内容:{$ticketMessage->message}"
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user