TicketController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\TicketSave;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Ticket;
  7. use App\Models\TicketMessage;
  8. use App\Utils\Helper;
  9. use Illuminate\Support\Facades\DB;
  10. class TicketController extends Controller
  11. {
  12. public function index (Request $request) {
  13. if ($request->input('id')) {
  14. $ticket = Ticket::where('id', $request->input('id'))
  15. ->where('user_id', $request->session()->get('id'))
  16. ->first();
  17. if (!$ticket) {
  18. abort(500, '工单不存在');
  19. }
  20. $ticket['message'] = TicketMessage::where('ticket_id', $ticket->id)->get();
  21. for ($i = 0; $i < count($ticket['message']); $i++) {
  22. if ($ticket['message'][$i]['user_id'] == $request->session()->get('id')) {
  23. $ticket['message'][$i]['is_me'] = true;
  24. } else {
  25. $ticket['message'][$i]['is_me'] = false;
  26. }
  27. }
  28. return response([
  29. 'data' => $ticket
  30. ]);
  31. }
  32. return response([
  33. 'data' => Ticket::where('user_id', $request->session()->get('id'))
  34. ->orderBy('created_at', 'DESC')
  35. ->get()
  36. ]);
  37. }
  38. public function save (TicketSave $request) {
  39. DB::beginTransaction();
  40. $ticket = Ticket::create(array_merge($request->only([
  41. 'subject',
  42. 'level'
  43. ]), [
  44. 'user_id' => $request->session()->get('id')
  45. ]));
  46. if (!$ticket) {
  47. DB::rollback();
  48. abort(500, '工单创建失败');
  49. }
  50. $ticketMessage = TicketMessage::create([
  51. 'user_id' => $request->session()->get('id'),
  52. 'ticket_id' => $ticket->id,
  53. 'message' => $request->input('message')
  54. ]);
  55. if (!$ticketMessage) {
  56. DB::rollback();
  57. abort(500, '工单创建失败');
  58. }
  59. DB::commit();
  60. return response([
  61. 'data' => true
  62. ]);
  63. }
  64. public function reply (Request $request) {
  65. if (empty($request->input('id'))) {
  66. abort(500, '参数错误');
  67. }
  68. if (empty($request->input('message'))) {
  69. abort(500, '消息不能为空');
  70. }
  71. $ticket = Ticket::where('id', $request->input('id'))
  72. ->where('user_id', $request->session()->get('id'))
  73. ->first();
  74. if (!$ticket) {
  75. abort(500, '工单不存在');
  76. }
  77. if ($request->session()->get('id') == $this->getLastMessage($ticket->id)->user_id) {
  78. abort(500, '请等待技术支持回复');
  79. }
  80. $ticketMessage = TicketMessage::create([
  81. 'user_id' => $request->session()->get('id'),
  82. 'ticket_id' => $ticket->id,
  83. 'message' => $request->input('message')
  84. ]);
  85. if (!$ticketMessage) {
  86. abort(500, '工单回复失败');
  87. }
  88. return response([
  89. 'data' => true
  90. ]);
  91. }
  92. private function getLastMessage ($ticketId) {
  93. return TicketMessage::where('ticket_id', $ticketId)
  94. ->orderBy('id', 'DESC')
  95. ->first();
  96. }
  97. }