TicketController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Components\Helpers;
  4. use App\Components\PushNotification;
  5. use App\Http\Controllers\Controller;
  6. use App\Mail\closeTicket;
  7. use App\Mail\replyTicket;
  8. use App\Models\Ticket;
  9. use App\Models\TicketReply;
  10. use App\Models\User;
  11. use Auth;
  12. use Illuminate\Http\JsonResponse;
  13. use Illuminate\Http\Request;
  14. use Mail;
  15. use Response;
  16. /**
  17. * 工单控制器
  18. *
  19. * Class TicketController
  20. *
  21. * @package App\Http\Controllers\Controller
  22. */
  23. class TicketController extends Controller {
  24. protected static $systemConfig;
  25. public function __construct() {
  26. self::$systemConfig = Helpers::systemConfig();
  27. }
  28. // 工单列表
  29. public function ticketList(Request $request): \Illuminate\Http\Response {
  30. $email = $request->input('email');
  31. $query = Ticket::query()->whereIn('admin_id', [0, Auth::id()]);
  32. if(isset($email)){
  33. $query->whereHas('user', static function($q) use ($email) {
  34. $q->where('email', 'like', '%'.$email.'%');
  35. });
  36. }
  37. $view['ticketList'] = $query->latest()->paginate(10)->appends($request->except('page'));
  38. return Response::view('admin.ticket.ticketList', $view);
  39. }
  40. // 创建工单
  41. public function createTicket(Request $request): ?JsonResponse {
  42. $id = $request->input('id');
  43. $email = $request->input('email');
  44. $title = $request->input('title');
  45. $content = $request->input('content');
  46. $user = User::find($id)?: User::query()->whereEmail($email)->first();
  47. if(!$user){
  48. return Response::json(['status' => 'fail', 'message' => '用户不存在']);
  49. }
  50. if($user == Auth::user()){
  51. return Response::json(['status' => 'fail', 'message' => '不能对自己发起工单']);
  52. }
  53. if(empty($title) || empty($content)){
  54. return Response::json(['status' => 'fail', 'message' => '请输入标题和内容']);
  55. }
  56. $obj = new Ticket();
  57. $obj->user_id = $user->id;
  58. $obj->admin_id = Auth::id();
  59. $obj->title = $title;
  60. $obj->content = $content;
  61. $obj->status = 0;
  62. $obj->save();
  63. if($obj->id){
  64. return Response::json(['status' => 'success', 'message' => '工单创建成功']);
  65. }
  66. return Response::json(['status' => 'fail', 'message' => '工单创建失败']);
  67. }
  68. // 回复工单
  69. public function replyTicket(Request $request) {
  70. $id = $request->input('id');
  71. if($request->isMethod('POST')){
  72. $content = clean($request->input('content'));
  73. $content = str_replace(["atob", "eval"], "", $content);
  74. $content = substr($content, 0, 300);
  75. $obj = new TicketReply();
  76. $obj->ticket_id = $id;
  77. $obj->admin_id = Auth::id();
  78. $obj->content = $content;
  79. $obj->save();
  80. if($obj->id){
  81. // 将工单置为已回复
  82. $ticket = Ticket::query()->with(['user'])->whereId($id)->firstOrFail();
  83. Ticket::query()->whereId($id)->update(['status' => 1]);
  84. $title = "工单回复提醒";
  85. $content = "标题:".$ticket->title."<br>管理员回复:".$content;
  86. // 发通知邮件
  87. if(!Auth::getUser()->is_admin){
  88. if(self::$systemConfig['webmaster_email']){
  89. $logId = Helpers::addNotificationLog($title, $content, 1,
  90. self::$systemConfig['webmaster_email']);
  91. Mail::to(self::$systemConfig['webmaster_email'])->send(new replyTicket($logId, $title,
  92. $content));
  93. }
  94. // 推送通知管理员
  95. PushNotification::send($title, $content);
  96. }else{
  97. $logId = Helpers::addNotificationLog($title, $content, 1, $ticket->user->email);
  98. Mail::to($ticket->user->email)->send(new replyTicket($logId, $title, $content));
  99. }
  100. return Response::json(['status' => 'success', 'message' => '回复成功']);
  101. }
  102. return Response::json(['status' => 'fail', 'message' => '回复失败']);
  103. }
  104. $view['ticket'] = Ticket::find($id);
  105. $view['replyList'] = TicketReply::query()->whereTicketId($id)->oldest()->get();
  106. return Response::view('admin.ticket.replyTicket', $view);
  107. }
  108. // 关闭工单
  109. public function closeTicket(Request $request): JsonResponse {
  110. $id = $request->input('id');
  111. $ticket = Ticket::query()->with(['user'])->whereId($id)->first();
  112. if(!$ticket){
  113. return Response::json(['status' => 'fail', 'message' => '关闭失败']);
  114. }
  115. $ret = Ticket::query()->whereId($id)->update(['status' => 2]);
  116. if(!$ret){
  117. return Response::json(['status' => 'fail', 'message' => '关闭失败']);
  118. }
  119. $title = "工单关闭提醒";
  120. $content = "工单【".$ticket->title."】已关闭";
  121. // 发邮件通知用户
  122. $logId = Helpers::addNotificationLog($title, $content, 1, $ticket->user->email);
  123. Mail::to($ticket->user->email)->send(new closeTicket($logId, $title, $content));
  124. return Response::json(['status' => 'success', 'message' => '关闭成功']);
  125. }
  126. }