2019-12-12 19:47:32 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2020-05-10 19:04:16 +08:00
|
|
|
use App\Jobs\SendEmailJob;
|
2019-12-12 19:47:32 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Ticket;
|
2020-05-10 21:20:51 +08:00
|
|
|
use App\Models\User;
|
2019-12-12 19:47:32 +08:00
|
|
|
use App\Models\TicketMessage;
|
2020-05-10 19:04:16 +08:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2019-12-13 17:07:46 +08:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2019-12-12 19:47:32 +08:00
|
|
|
|
|
|
|
class TicketController extends Controller
|
|
|
|
{
|
2020-01-11 13:36:52 +08:00
|
|
|
public function fetch(Request $request)
|
|
|
|
{
|
2019-12-13 16:00:04 +08:00
|
|
|
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++) {
|
2019-12-13 22:30:45 +08:00
|
|
|
if ($ticket['message'][$i]['user_id'] !== $ticket->user_id) {
|
2019-12-13 16:00:04 +08:00
|
|
|
$ticket['message'][$i]['is_me'] = true;
|
|
|
|
} else {
|
|
|
|
$ticket['message'][$i]['is_me'] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response([
|
|
|
|
'data' => $ticket
|
|
|
|
]);
|
|
|
|
}
|
2020-04-26 12:24:26 +08:00
|
|
|
$current = $request->input('current') ? $request->input('current') : 1;
|
|
|
|
$pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
|
|
|
|
$model = Ticket::orderBy('created_at', 'DESC');
|
2020-04-26 19:32:42 +08:00
|
|
|
if ($request->input('status') !== NULL) {
|
|
|
|
$model->where('status', $request->input('status'));
|
|
|
|
}
|
2020-04-26 12:24:26 +08:00
|
|
|
$total = $model->count();
|
|
|
|
$res = $model->forPage($current, $pageSize)
|
2019-12-13 17:13:10 +08:00
|
|
|
->get();
|
2020-04-26 12:24:26 +08:00
|
|
|
for ($i = 0; $i < count($res); $i++) {
|
|
|
|
if ($res[$i]['last_reply_user_id'] == $request->session()->get('id')) {
|
|
|
|
$res[$i]['reply_status'] = 0;
|
2019-12-13 17:13:10 +08:00
|
|
|
} else {
|
2020-04-26 12:24:26 +08:00
|
|
|
$res[$i]['reply_status'] = 1;
|
2019-12-13 17:13:10 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-13 16:00:04 +08:00
|
|
|
return response([
|
2020-04-26 12:24:26 +08:00
|
|
|
'data' => $res,
|
|
|
|
'total' => $total
|
2019-12-13 16:00:04 +08:00
|
|
|
]);
|
2019-12-12 19:47:32 +08:00
|
|
|
}
|
2019-12-13 16:02:19 +08:00
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public function reply(Request $request)
|
|
|
|
{
|
2019-12-13 16:02:19 +08:00
|
|
|
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, '工单不存在');
|
|
|
|
}
|
2019-12-13 17:42:21 +08:00
|
|
|
if ($ticket->status) {
|
|
|
|
abort(500, '工单已关闭,无法回复');
|
|
|
|
}
|
2019-12-13 17:07:46 +08:00
|
|
|
DB::beginTransaction();
|
2019-12-13 16:02:19 +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();
|
2019-12-13 16:02:19 +08:00
|
|
|
abort(500, '工单回复失败');
|
|
|
|
}
|
2019-12-13 17:07:46 +08:00
|
|
|
DB::commit();
|
2020-05-10 19:04:16 +08:00
|
|
|
$this->sendEmailNotify($ticket, $ticketMessage);
|
2019-12-13 16:02:19 +08:00
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
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'))) {
|
|
|
|
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
|
|
|
|
]);
|
|
|
|
}
|
2020-05-10 19:04:16 +08:00
|
|
|
|
|
|
|
// 半小时内不再重复通知
|
|
|
|
private function sendEmailNotify(Ticket $ticket, TicketMessage $ticketMessage)
|
|
|
|
{
|
|
|
|
$user = User::find($ticket->user_id);
|
2020-05-10 21:20:51 +08:00
|
|
|
$cacheKey = 'ticket_sendEmailNotify_' . $ticket->user_id;
|
2020-05-10 19:04:16 +08:00
|
|
|
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}"
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 19:47:32 +08:00
|
|
|
}
|