From ff90f574a005e5ac515c7c896abbd1818bdbb4cd Mon Sep 17 00:00:00 2001
From: zhaoxiang <756958008@qq.com>
Date: Tue, 20 Feb 2018 15:29:26 +0800
Subject: [PATCH] =?UTF-8?q?added=20=E6=96=B0=E5=A2=9E=E5=8A=A0=E6=8E=A5?=
 =?UTF-8?q?=E5=8F=A3=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 application/admin/controller/ApiList.php      |  13 --
 .../admin/controller/InterfaceList.php        | 134 ++++++++++++++++++
 application/adminRoute.php                    |  24 ++++
 application/util/ApiLog.php                   |   4 +-
 application/wiki/view/index/detail.html       |   4 +-
 5 files changed, 162 insertions(+), 17 deletions(-)
 delete mode 100644 application/admin/controller/ApiList.php
 create mode 100644 application/admin/controller/InterfaceList.php

diff --git a/application/admin/controller/ApiList.php b/application/admin/controller/ApiList.php
deleted file mode 100644
index 33ab056..0000000
--- a/application/admin/controller/ApiList.php
+++ /dev/null
@@ -1,13 +0,0 @@
-<?php
-/**
- *
- * @since   2018-02-11
- * @author  zhaoxiang <zhaoxiang051405@gmail.com>
- */
-
-namespace app\admin\controller;
-
-
-class ApiList extends Base {
-
-}
diff --git a/application/admin/controller/InterfaceList.php b/application/admin/controller/InterfaceList.php
new file mode 100644
index 0000000..0b67511
--- /dev/null
+++ b/application/admin/controller/InterfaceList.php
@@ -0,0 +1,134 @@
+<?php
+/**
+ *
+ * @since   2018-02-11
+ * @author  zhaoxiang <zhaoxiang051405@gmail.com>
+ */
+
+namespace app\admin\controller;
+
+
+use app\model\ApiList;
+use app\util\ReturnCode;
+
+class InterfaceList extends Base {
+    /**
+     * 获取接口列表
+     * @author zhaoxiang <zhaoxiang051405@gmail.com>
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function index() {
+
+        $limit = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
+        $start = $limit * ($this->request->get('page', 1) - 1);
+        $keywords = $this->request->get('keywords', '');
+        $type = $this->request->get('type', '');
+        $status = $this->request->get('status', '');
+
+        $where = [];
+        if ($status === '1' || $status === '0') {
+            $where['status'] = $status;
+        }
+        if ($type) {
+            switch ($type) {
+                case 1:
+                    $where['hash'] = $keywords;
+                    break;
+                case 2:
+                    $where['info'] = ['like', "%{$keywords}%"];
+                    break;
+                case 3:
+                    $where['apiClass'] = ['like', "%{$keywords}%"];
+                    break;
+            }
+        }
+
+        $listInfo = (new ApiList())->where($where)->order('id', 'DESC')->limit($start, $limit)->select();
+        $count = (new ApiList())->where($where)->count();
+        $listInfo = $this->buildArrFromObj($listInfo);
+
+        return $this->buildSuccess([
+            'list'  => $listInfo,
+            'count' => $count
+        ]);
+    }
+
+    /**
+     * 获取接口唯一标识
+     * @author zhaoxiang <zhaoxiang051405@gmail.com>
+     * @return array
+     */
+    public function getHash() {
+        $res['hash'] = uniqid();
+
+        return $this->buildSuccess($res);
+    }
+
+    /**
+     * 新增接口
+     * @return array
+     * @author zhaoxiang <zhaoxiang051405@gmail.com>
+     */
+    public function add() {
+        $postData = $this->request->post();
+        $res = ApiList::create($postData);
+        if ($res === false) {
+            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
+        } else {
+            return $this->buildSuccess([]);
+        }
+    }
+
+    /**
+     * 接口状态编辑
+     * @return array
+     * @author zhaoxiang <zhaoxiang051405@gmail.com>
+     */
+    public function changeStatus() {
+        $id = $this->request->get('id');
+        $status = $this->request->get('status');
+        $res = ApiList::update([
+            'status' => $status
+        ], [
+            'id' => $id
+        ]);
+        if ($res === false) {
+            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
+        } else {
+            return $this->buildSuccess([]);
+        }
+    }
+
+    /**
+     * 编辑应用
+     * @return array
+     * @author zhaoxiang <zhaoxiang051405@gmail.com>
+     */
+    public function edit() {
+        $postData = $this->request->post();
+        $res = ApiList::update($postData);
+        if ($res === false) {
+            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
+        } else {
+            return $this->buildSuccess([]);
+        }
+    }
+
+    /**
+     * 删除接口
+     * @return array
+     * @author zhaoxiang <zhaoxiang051405@gmail.com>
+     */
+    public function del() {
+        $id = $this->request->get('id');
+        if (!$id) {
+            return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
+        }
+        ApiList::destroy($id);
+
+        return $this->buildSuccess([]);
+    }
+}
diff --git a/application/adminRoute.php b/application/adminRoute.php
index 76c03d5..2fe3bc3 100644
--- a/application/adminRoute.php
+++ b/application/adminRoute.php
@@ -111,6 +111,30 @@ return [
             'admin/App/del',
             ['method' => 'get', 'after_behavior' => $afterBehavior]
         ],
+        'InterfaceList/index'        => [
+            'admin/InterfaceList/index',
+            ['method' => 'get', 'after_behavior' => $afterBehavior]
+        ],
+        'InterfaceList/changeStatus' => [
+            'admin/InterfaceList/changeStatus',
+            ['method' => 'get', 'after_behavior' => $afterBehavior]
+        ],
+        'InterfaceList/getHash' => [
+            'admin/InterfaceList/getHash',
+            ['method' => 'get', 'after_behavior' => $afterBehavior]
+        ],
+        'InterfaceList/add'          => [
+            'admin/InterfaceList/add',
+            ['method' => 'post', 'after_behavior' => $afterBehavior]
+        ],
+        'InterfaceList/edit'         => [
+            'admin/InterfaceList/edit',
+            ['method' => 'post', 'after_behavior' => $afterBehavior]
+        ],
+        'InterfaceList/del'          => [
+            'admin/InterfaceList/del',
+            ['method' => 'get', 'after_behavior' => $afterBehavior]
+        ],
         '__miss__'          => ['admin/Miss/index'],
     ],
 ];
diff --git a/application/util/ApiLog.php b/application/util/ApiLog.php
index fd2d33c..c2399d6 100644
--- a/application/util/ApiLog.php
+++ b/application/util/ApiLog.php
@@ -33,7 +33,7 @@ class ApiLog {
     }
 
     public static function setApiInfo($data) {
-        self::$apiInfo = isset($data['apiName']) ? $data['apiName'] : '' . self::$separator . isset($data['hash']) ? $data['hash'] : '';
+        self::$apiInfo = isset($data['apiClass']) ? $data['apiClass'] : '' . self::$separator . isset($data['hash']) ? $data['hash'] : '';
     }
 
     public static function setUserInfo($data) {
@@ -106,4 +106,4 @@ class ApiLog {
     }
 
 
-}
\ No newline at end of file
+}
diff --git a/application/wiki/view/index/detail.html b/application/wiki/view/index/detail.html
index 831bf4c..520696f 100644
--- a/application/wiki/view/index/detail.html
+++ b/application/wiki/view/index/detail.html
@@ -45,7 +45,7 @@
         </div>
         <div class="twelve wide column">
             <div class="ui floating message" id="detail" style="overflow: auto;">
-                <h2 class='ui header'>接口唯一标识:<a target="_blank" href="{$http_type}{$_SERVER['HTTP_HOST']}/api/{$hash}">{$hash}</a>({$detail['apiName']})</h2><br />
+                <h2 class='ui header'>接口唯一标识:<a target="_blank" href="{$http_type}{$_SERVER['HTTP_HOST']}/api/{$hash}">{$hash}</a>({$detail['apiClass']})</h2><br />
                 <div class="ui raised segment">
                     <span class="ui red ribbon large label">接口说明</span>
                     {if condition="$detail['status'] eq 0 "}
@@ -196,4 +196,4 @@
     });
     $('.ui .vertical').css('max-height', $('#detail').outerHeight(true));
 </script>
-</html>
\ No newline at end of file
+</html>