TicketController.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. ]);
  15. }
  16. public function store(Request $request): JsonResponse
  17. { // 添加工单
  18. $validatedData = $request->validate([
  19. 'title' => 'required|string|max:255',
  20. 'content' => 'required|string|max:300',
  21. ]);
  22. // 清理内容,防止恶意代码
  23. $title = $validatedData['title'];
  24. $content = substr(str_replace(['atob', 'eval'], '', clean($validatedData['content'])), 0, 300);
  25. $ticket = auth()->user()->tickets()->create(compact('title', 'content'));
  26. if ($ticket) {
  27. // 通知相关管理员
  28. return response()->json([
  29. 'status' => 'success',
  30. 'message' => trans('common.success_item', ['attribute' => trans('common.submit')]),
  31. ]);
  32. }
  33. return response()->json([
  34. 'status' => 'fail',
  35. 'message' => trans('common.failed_item', ['attribute' => trans('common.create')]),
  36. ]);
  37. }
  38. public function edit(Ticket $ticket): View
  39. { // 回复工单
  40. $replyList = $ticket->reply()
  41. ->with('ticket:id,status', 'admin:id,username,qq', 'user:id,username,qq')
  42. ->oldest()
  43. ->get();
  44. return view('user.replyTicket', compact('ticket', 'replyList'));
  45. }
  46. public function reply(Request $request, Ticket $ticket): JsonResponse
  47. {
  48. $validatedData = $request->validate([
  49. 'content' => 'required|string|max:300',
  50. ]);
  51. // 清理内容,防止恶意代码
  52. $content = substr(str_replace(['atob', 'eval'], '', clean($validatedData['content'])), 0, 300);
  53. $reply = $ticket->reply()->create([
  54. 'user_id' => auth()->id(),
  55. 'content' => $content,
  56. ]);
  57. if ($reply) {
  58. return response()->json([
  59. 'status' => 'success',
  60. 'message' => trans('common.success_item', ['attribute' => trans('user.ticket.reply')]),
  61. ]);
  62. }
  63. return response()->json([
  64. 'status' => 'fail',
  65. 'message' => trans('common.failed_item', ['attribute' => trans('user.ticket.reply')]),
  66. ]);
  67. }
  68. public function close(Ticket $ticket): JsonResponse
  69. { // 关闭工单
  70. if ($ticket->close()) {
  71. return response()->json([
  72. 'status' => 'success',
  73. 'message' => trans('common.success_item', ['attribute' => trans('common.close')]),
  74. ]);
  75. }
  76. return response()->json([
  77. 'status' => 'fail',
  78. 'message' => trans('common.failed_item', ['attribute' => trans('common.close')]),
  79. ]);
  80. }
  81. }