2019-12-12 19:47:32 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
2019-12-13 16:36:20 +08:00
|
|
|
use App\Models\User;
|
2019-12-12 19:47:32 +08:00
|
|
|
use App\Models\Ticket;
|
|
|
|
use App\Models\TicketMessage;
|
|
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
|
|
|
|
class TicketController extends Controller
|
|
|
|
{
|
|
|
|
public function index (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++) {
|
|
|
|
if ($ticket['message'][$i]['user_id'] == $request->session()->get('id')) {
|
|
|
|
$ticket['message'][$i]['is_me'] = true;
|
|
|
|
} else {
|
|
|
|
$ticket['message'][$i]['is_me'] = false;
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 16:38:10 +08:00
|
|
|
$ticket['user'] = User::select([
|
2019-12-13 16:37:12 +08:00
|
|
|
'email'
|
2019-12-13 16:38:10 +08:00
|
|
|
])->find($ticket->user_id);
|
2019-12-13 16:40:54 +08:00
|
|
|
$ticket['avatar_url'] = 'https://cdn.v2ex.com/gravatar/' . md5($ticket['user']->email) . '?s=64&d=identicon';
|
2019-12-13 16:00:04 +08:00
|
|
|
return response([
|
|
|
|
'data' => $ticket
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
return response([
|
|
|
|
'data' => Ticket::orderBy('created_at', 'DESC')
|
|
|
|
->get()
|
|
|
|
]);
|
2019-12-12 19:47:32 +08:00
|
|
|
}
|
2019-12-13 16:02:19 +08:00
|
|
|
|
|
|
|
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, '工单不存在');
|
|
|
|
}
|
|
|
|
$ticketMessage = TicketMessage::create([
|
|
|
|
'user_id' => $request->session()->get('id'),
|
|
|
|
'ticket_id' => $ticket->id,
|
|
|
|
'message' => $request->input('message')
|
|
|
|
]);
|
|
|
|
if (!$ticketMessage) {
|
|
|
|
abort(500, '工单回复失败');
|
|
|
|
}
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2019-12-12 19:47:32 +08:00
|
|
|
}
|