TicketController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Http\Controllers\User;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\User\TicketSave;
  5. use App\Http\Requests\User\TicketWithdraw;
  6. use App\Jobs\SendTelegramJob;
  7. use App\Models\User;
  8. use App\Services\TelegramService;
  9. use App\Services\TicketService;
  10. use App\Utils\Dict;
  11. use Illuminate\Http\Request;
  12. use App\Models\Ticket;
  13. use App\Models\TicketMessage;
  14. use Illuminate\Support\Facades\DB;
  15. class TicketController extends Controller
  16. {
  17. public function fetch(Request $request)
  18. {
  19. if ($request->input('id')) {
  20. $ticket = Ticket::where('id', $request->input('id'))
  21. ->where('user_id', $request->user['id'])
  22. ->first();
  23. if (!$ticket) {
  24. abort(500, __('Ticket does not exist'));
  25. }
  26. $ticket['message'] = TicketMessage::where('ticket_id', $ticket->id)->get();
  27. for ($i = 0; $i < count($ticket['message']); $i++) {
  28. if ($ticket['message'][$i]['user_id'] == $ticket->user_id) {
  29. $ticket['message'][$i]['is_me'] = true;
  30. } else {
  31. $ticket['message'][$i]['is_me'] = false;
  32. }
  33. }
  34. return response([
  35. 'data' => $ticket
  36. ]);
  37. }
  38. $ticket = Ticket::where('user_id', $request->user['id'])
  39. ->orderBy('created_at', 'DESC')
  40. ->get();
  41. return response([
  42. 'data' => $ticket
  43. ]);
  44. }
  45. public function save(TicketSave $request)
  46. {
  47. DB::beginTransaction();
  48. if ((int)Ticket::where('status', 0)->where('user_id', $request->user['id'])->lockForUpdate()->count()) {
  49. abort(500, __('There are other unresolved tickets'));
  50. }
  51. $ticket = Ticket::create(array_merge($request->only([
  52. 'subject',
  53. 'level'
  54. ]), [
  55. 'user_id' => $request->user['id']
  56. ]));
  57. if (!$ticket) {
  58. DB::rollback();
  59. abort(500, __('Failed to open ticket'));
  60. }
  61. $ticketMessage = TicketMessage::create([
  62. 'user_id' => $request->user['id'],
  63. 'ticket_id' => $ticket->id,
  64. 'message' => $request->input('message')
  65. ]);
  66. if (!$ticketMessage) {
  67. DB::rollback();
  68. abort(500, __('Failed to open ticket'));
  69. }
  70. DB::commit();
  71. $this->sendNotify($ticket, $request->input('message'));
  72. return response([
  73. 'data' => true
  74. ]);
  75. }
  76. public function reply(Request $request)
  77. {
  78. if (empty($request->input('id'))) {
  79. abort(500, __('Invalid parameter'));
  80. }
  81. if (empty($request->input('message'))) {
  82. abort(500, __('Message cannot be empty'));
  83. }
  84. $ticket = Ticket::where('id', $request->input('id'))
  85. ->where('user_id', $request->user['id'])
  86. ->first();
  87. if (!$ticket) {
  88. abort(500, __('Ticket does not exist'));
  89. }
  90. if ($ticket->status) {
  91. abort(500, __('The ticket is closed and cannot be replied'));
  92. }
  93. if ($request->user['id'] == $this->getLastMessage($ticket->id)->user_id) {
  94. abort(500, __('Please wait for the technical enginneer to reply'));
  95. }
  96. $ticketService = new TicketService();
  97. if (!$ticketService->reply(
  98. $ticket,
  99. $request->input('message'),
  100. $request->user['id']
  101. )) {
  102. abort(500, __('Ticket reply failed'));
  103. }
  104. $this->sendNotify($ticket, $request->input('message'));
  105. return response([
  106. 'data' => true
  107. ]);
  108. }
  109. public function close(Request $request)
  110. {
  111. if (empty($request->input('id'))) {
  112. abort(500, __('Invalid parameter'));
  113. }
  114. $ticket = Ticket::where('id', $request->input('id'))
  115. ->where('user_id', $request->user['id'])
  116. ->first();
  117. if (!$ticket) {
  118. abort(500, __('Ticket does not exist'));
  119. }
  120. $ticket->status = 1;
  121. if (!$ticket->save()) {
  122. abort(500, __('Close failed'));
  123. }
  124. return response([
  125. 'data' => true
  126. ]);
  127. }
  128. private function getLastMessage($ticketId)
  129. {
  130. return TicketMessage::where('ticket_id', $ticketId)
  131. ->orderBy('id', 'DESC')
  132. ->first();
  133. }
  134. public function withdraw(TicketWithdraw $request)
  135. {
  136. if ((int)config('v2board.withdraw_close_enable', 0)) {
  137. abort(500, 'user.ticket.withdraw.not_support_withdraw');
  138. }
  139. if (!in_array(
  140. $request->input('withdraw_method'),
  141. config(
  142. 'v2board.commission_withdraw_method',
  143. Dict::WITHDRAW_METHOD_WHITELIST_DEFAULT
  144. )
  145. )) {
  146. abort(500, __('Unsupported withdrawal method'));
  147. }
  148. $user = User::find($request->user['id']);
  149. $limit = config('v2board.commission_withdraw_limit', 100);
  150. if ($limit > ($user->commission_balance / 100)) {
  151. abort(500, __('The current required minimum withdrawal commission is :limit', ['limit' => $limit]));
  152. }
  153. DB::beginTransaction();
  154. $subject = __('[Commission Withdrawal Request] This ticket is opened by the system');
  155. $ticket = Ticket::create([
  156. 'subject' => $subject,
  157. 'level' => 2,
  158. 'user_id' => $request->user['id']
  159. ]);
  160. if (!$ticket) {
  161. DB::rollback();
  162. abort(500, __('Failed to open ticket'));
  163. }
  164. $message = sprintf("%s\r\n%s",
  165. __('Withdrawal method') . ":" . $request->input('withdraw_method'),
  166. __('Withdrawal account') . ":" . $request->input('withdraw_account')
  167. );
  168. $ticketMessage = TicketMessage::create([
  169. 'user_id' => $request->user['id'],
  170. 'ticket_id' => $ticket->id,
  171. 'message' => $message
  172. ]);
  173. if (!$ticketMessage) {
  174. DB::rollback();
  175. abort(500, __('Failed to open ticket'));
  176. }
  177. DB::commit();
  178. $this->sendNotify($ticket, $message);
  179. return response([
  180. 'data' => true
  181. ]);
  182. }
  183. private function sendNotify(Ticket $ticket, string $message)
  184. {
  185. $telegramService = new TelegramService();
  186. $telegramService->sendMessageWithAdmin("📮工单提醒 #{$ticket->id}\n———————————————\n主题:\n`{$ticket->subject}`\n内容:\n`{$message}`", true);
  187. }
  188. }