TicketController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers\User;
  4. use App\Controllers\UserController;
  5. use App\Models\Ticket;
  6. use App\Models\User;
  7. use App\Utils\Tools;
  8. use Psr\Http\Message\ResponseInterface;
  9. use Slim\Http\Request;
  10. use Slim\Http\Response;
  11. use voku\helper\AntiXSS;
  12. /**
  13. * TicketController
  14. */
  15. final class TicketController extends UserController
  16. {
  17. /**
  18. * @param array $args
  19. */
  20. public function ticket(Request $request, Response $response, array $args): ?ResponseInterface
  21. {
  22. if ($_ENV['enable_ticket'] !== true) {
  23. return null;
  24. }
  25. $pageNum = $request->getQueryParams()['page'] ?? 1;
  26. $tickets = Ticket::where('userid', $this->user->id)->where('rootid', 0)->orderBy('datetime', 'desc')->paginate(15, ['*'], 'page', $pageNum);
  27. $tickets->setPath('/user/ticket');
  28. if ($request->getParam('json') === 1) {
  29. return $response->withJson([
  30. 'ret' => 1,
  31. 'tickets' => $tickets,
  32. ]);
  33. }
  34. $render = Tools::paginateRender($tickets);
  35. return $response->write(
  36. $this->view()
  37. ->assign('tickets', $tickets)
  38. ->assign('render', $render)
  39. ->display('user/ticket.tpl')
  40. );
  41. }
  42. /**
  43. * @param array $args
  44. */
  45. public function ticketCreate(Request $request, Response $response, array $args): ResponseInterface
  46. {
  47. return $response->write(
  48. $this->view()
  49. ->display('user/ticket_create.tpl')
  50. );
  51. }
  52. /**
  53. * @param array $args
  54. */
  55. public function ticketAdd(Request $request, Response $response, array $args): ResponseInterface
  56. {
  57. $title = $request->getParam('title');
  58. $content = $request->getParam('content');
  59. $markdown = $request->getParam('markdown');
  60. if ($title === '' || $content === '') {
  61. return $response->withJson([
  62. 'ret' => 0,
  63. 'msg' => '非法输入',
  64. ]);
  65. }
  66. if (strpos($content, 'admin') !== false || strpos($content, 'user') !== false) {
  67. return $response->withJson([
  68. 'ret' => 0,
  69. 'msg' => '请求中有不当词语',
  70. ]);
  71. }
  72. $ticket = new Ticket();
  73. $antiXss = new AntiXSS();
  74. $ticket->title = $antiXss->xss_clean($title);
  75. $ticket->content = $antiXss->xss_clean($content);
  76. $ticket->rootid = 0;
  77. $ticket->userid = $this->user->id;
  78. $ticket->datetime = time();
  79. $ticket->save();
  80. if ($_ENV['mail_ticket'] === true && $markdown !== '') {
  81. $adminUser = User::where('is_admin', 1)->get();
  82. foreach ($adminUser as $user) {
  83. $user->sendMail(
  84. $_ENV['appName'] . '-新工单被开启',
  85. 'news/warn.tpl',
  86. [
  87. 'text' => '管理员,有人开启了新的工单,请您及时处理。',
  88. ],
  89. []
  90. );
  91. }
  92. }
  93. if ($_ENV['useScFtqq'] === true && $markdown !== '') {
  94. $ScFtqq_SCKEY = $_ENV['ScFtqq_SCKEY'];
  95. $postdata = http_build_query([
  96. 'text' => $_ENV['appName'] . '-新工单被开启',
  97. 'desp' => $markdown,
  98. ]);
  99. $opts = [
  100. 'http' => [
  101. 'method' => 'POST',
  102. 'header' => 'Content-type: application/x-www-form-urlencoded',
  103. 'content' => $postdata,
  104. ],
  105. ];
  106. $context = stream_context_create($opts);
  107. file_get_contents('https://sctapi.ftqq.com/' . $ScFtqq_SCKEY . '.send', false, $context);
  108. }
  109. return $response->withJson([
  110. 'ret' => 1,
  111. 'msg' => '提交成功',
  112. ]);
  113. }
  114. /**
  115. * @param array $args
  116. */
  117. public function ticketUpdate(Request $request, Response $response, array $args): ResponseInterface
  118. {
  119. $id = $args['id'];
  120. $content = $request->getParam('content');
  121. $status = $request->getParam('status');
  122. $markdown = $request->getParam('markdown');
  123. if ($content === '' || $status === '') {
  124. return $response->withJson([
  125. 'ret' => 0,
  126. 'msg' => '非法输入',
  127. ]);
  128. }
  129. if (strpos($content, 'admin') !== false || strpos($content, 'user') !== false) {
  130. return $response->withJson([
  131. 'ret' => 0,
  132. 'msg' => '请求中有不当词语',
  133. ]);
  134. }
  135. $ticket_main = Ticket::where('id', $id)->where('userid', $this->user->id)->where('rootid', 0)->first();
  136. if ($ticket_main === null) {
  137. return $response->withStatus(302)->withHeader('Location', '/user/ticket');
  138. }
  139. if ($status === 1 && $ticket_main->status !== $status) {
  140. if ($_ENV['mail_ticket'] === true && $markdown !== '') {
  141. $adminUser = User::where('is_admin', '=', '1')->get();
  142. foreach ($adminUser as $user) {
  143. $user->sendMail(
  144. $_ENV['appName'] . '-工单被重新开启',
  145. 'news/warn.tpl',
  146. [
  147. 'text' => '管理员,有人重新开启了<a href="' . $_ENV['baseUrl'] . '/admin/ticket/' . $ticket_main->id . '/view">工单</a>,请您及时处理。',
  148. ],
  149. []
  150. );
  151. }
  152. }
  153. if ($_ENV['useScFtqq'] === true && $markdown !== '') {
  154. $ScFtqq_SCKEY = $_ENV['ScFtqq_SCKEY'];
  155. $postdata = http_build_query([
  156. 'text' => $_ENV['appName'] . '-工单被重新开启',
  157. 'desp' => $markdown,
  158. ]);
  159. $opts = [
  160. 'http' => [
  161. 'method' => 'POST',
  162. 'header' => 'Content-type: application/x-www-form-urlencoded',
  163. 'content' => $postdata,
  164. ],
  165. ];
  166. $context = stream_context_create($opts);
  167. file_get_contents('https://sctapi.ftqq.com/' . $ScFtqq_SCKEY . '.send', false, $context);
  168. }
  169. } else {
  170. if ($_ENV['mail_ticket'] === true && $markdown !== '') {
  171. $adminUser = User::where('is_admin', 1)->get();
  172. foreach ($adminUser as $user) {
  173. $user->sendMail(
  174. $_ENV['appName'] . '-工单被回复',
  175. 'news/warn.tpl',
  176. [
  177. 'text' => '管理员,有人回复了<a href="' . $_ENV['baseUrl'] . '/admin/ticket/' . $ticket_main->id . '/view">工单</a>,请您及时处理。',
  178. ],
  179. []
  180. );
  181. }
  182. }
  183. if ($_ENV['useScFtqq'] === true && $markdown !== '') {
  184. $ScFtqq_SCKEY = $_ENV['ScFtqq_SCKEY'];
  185. $postdata = http_build_query([
  186. 'text' => $_ENV['appName'] . '-工单被回复',
  187. 'desp' => $markdown,
  188. ]);
  189. $opts = [
  190. 'http' => [
  191. 'method' => 'POST',
  192. 'header' => 'Content-type: application/x-www-form-urlencoded',
  193. 'content' => $postdata,
  194. ],
  195. ];
  196. $context = stream_context_create($opts);
  197. file_get_contents('https://sctapi.ftqq.com/' . $ScFtqq_SCKEY . '.send', false, $context);
  198. }
  199. }
  200. $antiXss = new AntiXSS();
  201. $ticket = new Ticket();
  202. $ticket->title = $antiXss->xss_clean($ticket_main->title);
  203. $ticket->content = $antiXss->xss_clean($content);
  204. $ticket->rootid = $ticket_main->id;
  205. $ticket->userid = $this->user->id;
  206. $ticket->datetime = time();
  207. $ticket_main->status = $status;
  208. $ticket_main->save();
  209. $ticket->save();
  210. return $response->withJson([
  211. 'ret' => 1,
  212. 'msg' => '提交成功',
  213. ]);
  214. }
  215. /**
  216. * @param array $args
  217. */
  218. public function ticketView(Request $request, Response $response, array $args): ResponseInterface
  219. {
  220. $id = $args['id'];
  221. $ticket_main = Ticket::where('id', '=', $id)->where('userid', $this->user->id)->where('rootid', '=', 0)->first();
  222. if ($ticket_main === null) {
  223. if ($request->getParam('json') === 1) {
  224. return $response->withJson([
  225. 'ret' => 0,
  226. 'msg' => '这不是你的工单!',
  227. ]);
  228. }
  229. return $response->withStatus(302)->withHeader('Location', '/user/ticket');
  230. }
  231. $pageNum = $request->getQueryParams()['page'] ?? 1;
  232. $ticketset = Ticket::where('id', $id)->orWhere('rootid', '=', $id)->orderBy('datetime', 'desc')->paginate(5, ['*'], 'page', $pageNum);
  233. $ticketset->setPath('/user/ticket/' . $id . '/view');
  234. if ($request->getParam('json') === 1) {
  235. foreach ($ticketset as $set) {
  236. $set->username = $set->user()->user_name;
  237. $set->datetime = $set->datetime();
  238. }
  239. return $response->withJson([
  240. 'ret' => 1,
  241. 'tickets' => $ticketset,
  242. ]);
  243. }
  244. $render = Tools::paginateRender($ticketset);
  245. return $response->write(
  246. $this->view()
  247. ->assign('ticketset', $ticketset)
  248. ->assign('id', $id)
  249. ->assign('render', $render)
  250. ->display('user/ticket_view.tpl')
  251. );
  252. }
  253. }