Browse Source

refactor: improve ticket system code quality

M1Screw 2 years ago
parent
commit
5bd99c4c72

+ 3 - 3
resources/views/tabler/admin/ticket/view.tpl

@@ -60,14 +60,14 @@
                                 <div class="row">
                                     <div class="col">
                                         <div>
-                                            {nl2br($comment['comment'])}
+                                            {nl2br($comment->comment)}
                                         </div>
-                                        <div class="text-muted my-1">{$comment['commenter_name']} 回复于 {Tools::toDateTime($comment['datetime'])}
+                                        <div class="text-muted my-1">{$comment->commenter_name} 回复于 {$comment->datetime}
                                         </div>
                                     </div>
                                     <div class="col-auto">
                                         <div>
-                                            # {$comment['comment_id'] + 1}
+                                            # {$comment->comment_id + 1}
                                         </div>
                                     </div>
                                 </div>

+ 3 - 3
resources/views/tabler/user/ticket/view.tpl

@@ -85,13 +85,13 @@
                                     <div class="row">
                                         <div class="col">
                                             <div>
-                                                {nl2br($comment['comment'])}
+                                                {nl2br($comment->comment)}
                                             </div>
-                                            <div class="text-muted my-1">{$comment['commenter_name']} 回复于 {Tools::toDateTime($comment['datetime'])}</div>
+                                            <div class="text-muted my-1">{$comment->commenter_name} 回复于 {$comment->datetime}</div>
                                         </div>
                                         <div class="col-auto">
                                             <div>
-                                                # {$comment['comment_id'] + 1}
+                                                # {$comment->comment_id + 1}
                                             </div>
                                         </div>
                                     </div>

+ 25 - 14
src/Controllers/Admin/TicketController.php

@@ -12,7 +12,6 @@ use Exception;
 use Psr\Http\Message\ResponseInterface;
 use Slim\Http\Response;
 use Slim\Http\ServerRequest;
-use voku\helper\AntiXSS;
 use function array_merge;
 use function count;
 use function json_decode;
@@ -51,32 +50,33 @@ final class TicketController extends BaseController
     /**
      * 后台更新工单内容
      */
-    public function update(ServerRequest $request, Response $response, array $args)
+    public function update(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
     {
         $id = $args['id'];
-        $comment = $request->getParam('comment');
+        $comment = $request->getParam('comment') ?? '';
 
         if ($comment === '') {
             return $response->withJson([
                 'ret' => 0,
-                'msg' => '非法输入',
+                'msg' => '工单回复不能为空',
             ]);
         }
 
         $ticket = Ticket::where('id', $id)->first();
 
         if ($ticket === null) {
-            return $response->withStatus(302)->withHeader('Location', '/admin/ticket');
+            return $response->withJson([
+                'ret' => 0,
+                'msg' => '工单不存在',
+            ]);
         }
 
-        $antiXss = new AntiXSS();
-
         $content_old = json_decode($ticket->content, true);
         $content_new = [
             [
                 'comment_id' => $content_old[count($content_old) - 1]['comment_id'] + 1,
                 'commenter_name' => 'Admin',
-                'comment' => $antiXss->xss_clean($comment),
+                'comment' => $comment,
                 'datetime' => time(),
             ],
         ];
@@ -110,17 +110,21 @@ final class TicketController extends BaseController
     {
         $id = $args['id'];
         $ticket = Ticket::where('id', '=', $id)->first();
-        $comments = json_decode($ticket->content, true);
 
         if ($ticket === null) {
-            return $response->withStatus(302)->withHeader('Location', '/user/ticket');
+            return $response->withStatus(302)->withHeader('Location', '/admin/ticket');
+        }
+
+        $comments = json_decode($ticket->content);
+
+        foreach ($comments as $comment) {
+            $comment->datetime = Tools::toDateTime((int) $comment->datetime);
         }
 
         return $response->write(
             $this->view()
                 ->assign('ticket', $ticket)
                 ->assign('comments', $comments)
-                ->registerClass('Tools', Tools::class)
                 ->fetch('admin/ticket/view.tpl')
         );
     }
@@ -133,10 +137,17 @@ final class TicketController extends BaseController
         $id = $args['id'];
         $ticket = Ticket::where('id', '=', $id)->first();
 
+        if ($ticket === null) {
+            return $response->withJson([
+                'ret' => 0,
+                'msg' => '工单不存在',
+            ]);
+        }
+
         if ($ticket->status === 'closed') {
             return $response->withJson([
                 'ret' => 0,
-                'msg' => '工单已关闭',
+                'msg' => '操作失败,工单已关闭',
             ]);
         }
 
@@ -186,8 +197,8 @@ final class TicketController extends BaseController
             <button type="button" class="btn btn-orange" id="close-ticket" 
             onclick="closeTicket(' . $ticket->id . ')">关闭</button>
             <a class="btn btn-blue" href="/admin/ticket/' . $ticket->id . '/view">查看</a>';
-            $ticket->status = Tools::getTicketStatus($ticket);
-            $ticket->type = Tools::getTicketType($ticket);
+            $ticket->status = $ticket->status();
+            $ticket->type = $ticket->type();
             $ticket->datetime = Tools::toDateTime((int) $ticket->datetime);
         }
 

+ 25 - 35
src/Controllers/User/TicketController.php

@@ -31,24 +31,17 @@ final class TicketController extends BaseController
     public function ticket(ServerRequest $request, Response $response, array $args): ?ResponseInterface
     {
         if (! Setting::obtain('enable_ticket')) {
-            return null;
+            return $response->withStatus(302)->withHeader('Location', '/user');
         }
 
         $tickets = Ticket::where('userid', $this->user->id)->orderBy('datetime', 'desc')->get();
 
         foreach ($tickets as $ticket) {
-            $ticket->status = Tools::getTicketStatus($ticket);
-            $ticket->type = Tools::getTicketType($ticket);
+            $ticket->status = $ticket->status();
+            $ticket->type = $ticket->type();
             $ticket->datetime = Tools::toDateTime((int) $ticket->datetime);
         }
 
-        if ($request->getParam('json') === 1) {
-            return $response->withJson([
-                'ret' => 1,
-                'tickets' => $tickets,
-            ]);
-        }
-
         return $response->write(
             $this->view()
                 ->assign('tickets', $tickets)
@@ -58,13 +51,14 @@ final class TicketController extends BaseController
 
     public function ticketAdd(ServerRequest $request, Response $response, array $args): ResponseInterface
     {
-        $title = $request->getParam('title');
-        $comment = $request->getParam('comment');
-        $type = $request->getParam('type');
-        if ($title === '' || $comment === '') {
+        $title = $request->getParam('title') ?? '';
+        $comment = $request->getParam('comment') ?? '';
+        $type = $request->getParam('type') ?? '';
+
+        if ($title === '' || $comment === '' || $type === '') {
             return $response->withJson([
                 'ret' => 0,
-                'msg' => '非法输入',
+                'msg' => '工单内容不能为空',
             ]);
         }
 
@@ -101,6 +95,7 @@ final class TicketController extends BaseController
                 );
             }
         }
+
         if ($_ENV['useScFtqq'] === true) {
             $ScFtqq_SCKEY = $_ENV['ScFtqq_SCKEY'];
             $postdata = http_build_query([
@@ -127,19 +122,22 @@ final class TicketController extends BaseController
     public function ticketUpdate(ServerRequest $request, Response $response, array $args): ResponseInterface
     {
         $id = $args['id'];
-        $comment = $request->getParam('comment');
+        $comment = $request->getParam('comment') ?? '';
 
         if ($comment === '') {
             return $response->withJson([
                 'ret' => 0,
-                'msg' => '非法输入',
+                'msg' => '工单回复不能为空',
             ]);
         }
 
         $ticket = Ticket::where('id', $id)->where('userid', $this->user->id)->first();
 
         if ($ticket === null) {
-            return $response->withStatus(302)->withHeader('Location', '/user/ticket');
+            return $response->withJson([
+                'ret' => 0,
+                'msg' => '工单不存在',
+            ]);
         }
 
         $antiXss = new AntiXSS();
@@ -201,33 +199,25 @@ final class TicketController extends BaseController
     {
         $id = $args['id'];
         $ticket = Ticket::where('id', '=', $id)->where('userid', $this->user->id)->first();
-        $comments = json_decode($ticket->content, true);
-
-        $ticket->status = Tools::getTicketStatus($ticket);
-        $ticket->type = Tools::getTicketType($ticket);
-        $ticket->datetime = Tools::toDateTime((int) $ticket->datetime);
 
         if ($ticket === null) {
-            if ($request->getParam('json') === 1) {
-                return $response->withJson([
-                    'ret' => 0,
-                    'msg' => '无访问权限',
-                ]);
-            }
             return $response->withStatus(302)->withHeader('Location', '/user/ticket');
         }
-        if ($request->getParam('json') === 1) {
-            return $response->withJson([
-                'ret' => 1,
-                'ticket' => $ticket,
-            ]);
+
+        $comments = json_decode($ticket->content);
+
+        foreach ($comments as $comment) {
+            $comment->datetime = Tools::toDateTime((int) $comment->datetime);
         }
 
+        $ticket->status = $ticket->status();
+        $ticket->type = $ticket->type();
+        $ticket->datetime = Tools::toDateTime((int) $ticket->datetime);
+
         return $response->write(
             $this->view()
                 ->assign('ticket', $ticket)
                 ->assign('comments', $comments)
-                ->registerClass('Tools', Tools::class)
                 ->fetch('user/ticket/view.tpl')
         );
     }

+ 16 - 10
src/Models/Ticket.php

@@ -10,25 +10,31 @@ namespace App\Models;
 final class Ticket extends Model
 {
     protected $connection = 'default';
-
     protected $table = 'ticket';
 
     /**
-     * [静态方法] 删除不存在的用户的记录
+     * 工单类型
      */
-    public static function userIsNull(Ticket $Ticket): void
+    public function type(): string
     {
-        $tickets = Ticket::where('userid', $Ticket->userid)->get();
-        foreach ($tickets as $ticket) {
-            $ticket->delete();
-        }
+        return match ($this->type) {
+            'howto' => '使用',
+            'billing' => '财务',
+            'account' => '账户',
+            default => '其他',
+        };
     }
 
     /**
-     * 时间
+     * 工单状态
      */
-    public function datetime(): string
+    public function status(): string
     {
-        return date('Y-m-d H:i:s', $this->datetime);
+        return match ($this->status) {
+            'closed' => '已结单',
+            'open_wait_user' => '等待用户回复',
+            'open_wait_admin' => '进行中',
+            default => '未知',
+        };
     }
 }

+ 0 - 20
src/Utils/Cookie.php

@@ -6,26 +6,6 @@ namespace App\Utils;
 
 final class Cookie
 {
-    /*
-
-    setcookie(
-        string $name,
-        string $value = "",
-        int $expires = 0,
-        string $path = "",
-        string $domain = "",
-        bool $secure = false,
-        bool $httponly = false
-    ): bool
-
-    PHP 7.3.0 起有效的签名
-
-    setcookie(string $name, string $value = "", array $options = []): bool
-
-    https://www.php.net/manual/zh/function.setcookie.php
-
-    */
-
     public static function set($arg, $time): void
     {
         foreach ($arg as $key => $value) {

+ 0 - 33
src/Utils/Tools.php

@@ -304,39 +304,6 @@ final class Tools
         return is_null($number) ? 0.00 : round(floatval($number), 2);
     }
 
-    /**
-     * 工单状态
-     */
-    public static function getTicketStatus($ticket): string
-    {
-        if ($ticket->status === 'closed') {
-            return '已结单';
-        }
-        if ($ticket->status === 'open_wait_user') {
-            return '等待用户回复';
-        }
-        if ($ticket->status === 'open_wait_admin') {
-            return '进行中';
-        }
-        return '未知';
-    }
-
-    /**
-     * 工单类型
-     */
-    public static function getTicketType($ticket): string
-    {
-        if ($ticket->type === 'howto') {
-            return '使用';
-        }
-        if ($ticket->type === 'billing') {
-            return '财务';
-        }
-        if ($ticket->type === 'account') {
-            return '账户';
-        }
-        return '其他';
-    }
 
     /**
      * 节点状态