TicketController.php 6.5 KB

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