huwhois před 3 roky
rodič
revize
68416db1e2

+ 2 - 0
app/api/common.php

@@ -0,0 +1,2 @@
+<?php
+// 这是系统自动生成的公共文件

+ 100 - 0
app/api/controller/Index.php

@@ -0,0 +1,100 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\api\controller;
+
+use think\App;
+use think\exception\HttpException;
+
+use app\common\model\Category;
+use app\common\model\Article;
+use app\common\model\ArticleTags;
+use app\common\model\ArticleDolikeLog;
+
+class Index
+{
+    /**
+     * 构造方法
+     * @access public
+     * @param App $app 应用对象
+     */
+    public function __construct(App $app)
+    {
+        $this->app     = $app;
+        $this->request = $this->app->request;
+    }
+
+    public function index()
+    {
+        return '您好!这是一个[api]示例应用';
+    }
+
+    public function menu()
+    {
+        return json(Category::where('is_nav', 1)->order(['sort desc'])->select());
+    }
+
+    public function list()
+    {
+        $params = $this->app->request->param();
+
+        $cid = isset($params['cid']) ? (int)$params['cid'] : 0;
+
+        $list = Article::queryPage($params);
+
+        $category = Category::find($cid);
+
+        if ($category) {
+            $baseUrl = $category->url;
+        } else {
+            $baseUrl = '/index/all';
+        }
+
+        return json([
+            'baseUrl'  => $baseUrl,
+            'list' => $list->all(),
+            'total' => $list->total(),
+            'limit' => $list->listRows(),
+            'page' => $list->currentPage(),
+            'lastPage' => $list->lastPage(),
+            'cid' => $cid,
+        ]);
+    }
+
+    /**
+     * 文章详情
+     */
+    public function read($id = null)
+    {
+        $data = Article::getOne($id);
+
+        if (!$data) {
+            return json(['msg'=>'页面不存在'], 404);
+        }
+
+        $data->hits += 1;
+
+        $data->save();
+
+        $prev_next = Article::getNextPrev($id, $data->cid);
+
+        if ($data->content_type == 1) {
+            $parsedownExtension = new \ParsedownExtension();
+            $parsedownExtension->setTocEnabled(true);
+            $res = $parsedownExtension->text($data->content);
+            $data->toc = $res['toc'];
+            $data->content = $res['content'];
+        }
+
+        return json([
+            'cid' => $data->cid,
+            'data' => $data,
+            'prev_next' => $prev_next,
+            'seo' => [
+                'title' => $data->title,
+                'key' => $data->keywords,
+                'des' => $data->summary,
+            ]
+        ]);
+    }
+}

+ 25 - 0
app/api/route/app.php

@@ -0,0 +1,25 @@
+<?php
+// +----------------------------------------------------------------------
+// | ThinkPHP [ WE CAN DO IT JUST THINK ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
+// +----------------------------------------------------------------------
+// | Author: liu21st <liu21st@gmail.com>
+// +----------------------------------------------------------------------
+use think\facade\Route;
+
+Route::pattern([
+    'name' => '\w+',
+    'id' => '\d+',
+    'cid' => '\d+',
+    'page' => '\d+',
+    'year' => '\d+',
+    'month' => '\d+',
+    'day' => '\d+',
+]); 
+
+Route::get('menu', 'index/menu');
+Route::get('list', 'index/list');
+Route::get('/read/:id', 'index/read');

+ 23 - 0
app/common/model/Template.php

@@ -0,0 +1,23 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\common\model;
+
+class Template extends Base
+{
+    protected $schema = [
+        
+    ];
+
+
+    public function templateType()
+    {
+        $this->belongsTo('TemplateType', 'typeid', 'typeid')->bind(['typename'=>'typename']);
+    }
+
+    // 获取列表
+    public static function getList()
+    {
+        return self::with('templateType')->order(['id' => 'desc'])->select();
+    }
+}

+ 17 - 0
app/common/model/TemplateType.php

@@ -0,0 +1,17 @@
+<?php
+declare (strict_types = 1);
+
+namespace app\common\model;
+
+class TemplateType extends Base
+{
+    protected $schema = [
+        
+    ];
+
+    // 获取列表
+    public static function getList()
+    {
+        return self::order(['id' => 'desc'])->select();
+    }
+}