mirror of
https://github.com/v2board/v2board.git
synced 2024-11-14 07:29:13 +08:00
131 lines
4.3 KiB
PHP
131 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Jobs\SendEmailJob;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Ticket;
|
|
use App\Models\User;
|
|
use App\Models\TicketMessage;
|
|
use Illuminate\Support\Facades\Cache;
|
|
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'))
|
|
->first();
|
|
if (!$ticket) {
|
|
abort(500, '工单不存在');
|
|
}
|
|
$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
|
|
]);
|
|
}
|
|
$current = $request->input('current') ? $request->input('current') : 1;
|
|
$pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
|
|
$model = Ticket::orderBy('created_at', 'DESC');
|
|
if ($request->input('status') !== NULL) {
|
|
$model->where('status', $request->input('status'));
|
|
}
|
|
$total = $model->count();
|
|
$res = $model->forPage($current, $pageSize)
|
|
->get();
|
|
for ($i = 0; $i < count($res); $i++) {
|
|
if ($res[$i]['last_reply_user_id'] == $request->session()->get('id')) {
|
|
$res[$i]['reply_status'] = 0;
|
|
} else {
|
|
$res[$i]['reply_status'] = 1;
|
|
}
|
|
}
|
|
return response([
|
|
'data' => $res,
|
|
'total' => $total
|
|
]);
|
|
}
|
|
|
|
public function reply(Request $request)
|
|
{
|
|
if (empty($request->input('id'))) {
|
|
abort(500, '参数错误');
|
|
}
|
|
if (empty($request->input('message'))) {
|
|
abort(500, '消息不能为空');
|
|
}
|
|
$ticket = Ticket::where('id', $request->input('id'))
|
|
->first();
|
|
if (!$ticket) {
|
|
abort(500, '工单不存在');
|
|
}
|
|
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([
|
|
'data' => true
|
|
]);
|
|
}
|
|
|
|
public function close(Request $request)
|
|
{
|
|
if (empty($request->input('id'))) {
|
|
abort(500, '参数错误');
|
|
}
|
|
$ticket = Ticket::where('id', $request->input('id'))
|
|
->first();
|
|
if (!$ticket) {
|
|
abort(500, '工单不存在');
|
|
}
|
|
$ticket->status = 1;
|
|
if (!$ticket->save()) {
|
|
abort(500, '关闭失败');
|
|
}
|
|
return response([
|
|
'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}"
|
|
]
|
|
]);
|
|
}
|
|
}
|
|
}
|