TicketController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Http\Controllers\User;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Ticket;
  5. use Illuminate\Contracts\View\View;
  6. use Illuminate\Http\JsonResponse;
  7. use Illuminate\Http\Request;
  8. class TicketController extends Controller
  9. {
  10. public function index(Request $request): View
  11. { // 工单
  12. return view('user.tickets', [
  13. 'tickets' => auth()->user()->tickets()->latest()->paginate(10)->appends($request->except('page')),
  14. 'responseStats' => Ticket::getAvgFirstResponseData(),
  15. ]);
  16. }
  17. public function store(Request $request): JsonResponse
  18. { // 添加工单
  19. $validatedData = $request->validate([
  20. 'title' => 'required|string|max:255',
  21. 'content' => 'required|string|max:300',
  22. ]);
  23. // 清理内容,防止恶意代码
  24. $title = $validatedData['title'];
  25. $content = substr(str_replace(['atob', 'eval'], '', clean($validatedData['content'])), 0, 300);
  26. $ticket = auth()->user()->tickets()->create(compact('title', 'content'));
  27. if ($ticket) {
  28. // 通知相关管理员
  29. return response()->json([
  30. 'status' => 'success',
  31. 'message' => trans('common.success_item', ['attribute' => trans('common.submit')]),
  32. ]);
  33. }
  34. return response()->json([
  35. 'status' => 'fail',
  36. 'message' => trans('common.failed_item', ['attribute' => trans('common.create')]),
  37. ]);
  38. }
  39. public function edit(Ticket $ticket): View
  40. { // 回复工单
  41. $replyList = $ticket->reply()
  42. ->with('ticket:id,status', 'admin:id,username,qq', 'user:id,username,qq')
  43. ->oldest()
  44. ->get();
  45. return view('user.replyTicket', compact('ticket', 'replyList'));
  46. }
  47. public function reply(Request $request, Ticket $ticket): JsonResponse
  48. {
  49. $validatedData = $request->validate([
  50. 'content' => 'required|string|max:300',
  51. ]);
  52. // 清理内容,防止恶意代码
  53. $content = substr(str_replace(['atob', 'eval'], '', clean($validatedData['content'])), 0, 300);
  54. $reply = $ticket->reply()->create([
  55. 'user_id' => auth()->id(),
  56. 'content' => $content,
  57. ]);
  58. if ($reply) {
  59. return response()->json([
  60. 'status' => 'success',
  61. 'message' => trans('common.success_item', ['attribute' => trans('user.ticket.reply')]),
  62. ]);
  63. }
  64. return response()->json([
  65. 'status' => 'fail',
  66. 'message' => trans('common.failed_item', ['attribute' => trans('user.ticket.reply')]),
  67. ]);
  68. }
  69. public function close(Ticket $ticket): JsonResponse
  70. { // 关闭工单
  71. if ($ticket->close()) {
  72. return response()->json([
  73. 'status' => 'success',
  74. 'message' => trans('common.success_item', ['attribute' => trans('common.close')]),
  75. ]);
  76. }
  77. return response()->json([
  78. 'status' => 'fail',
  79. 'message' => trans('common.failed_item', ['attribute' => trans('common.close')]),
  80. ]);
  81. }
  82. }