<?php
|
|
namespace app\home\controller;
|
|
use think\db\exception\DataNotFoundException;
|
use think\db\exception\DbException;
|
use think\db\exception\ModelNotFoundException;
|
use think\Exception;
|
use think\facade\Db;
|
use think\facade\View;
|
use think\facade\Request;
|
use app\BaseController;
|
use think\response\Json;
|
|
class Blog extends BaseController {
|
public function index() {
|
$list = Db::name('blog')->where('status', 1)->limit(10)->order('id desc')->select()->toArray();
|
View::assign('list', $list);
|
return View::fetch('index');
|
}
|
|
/**
|
* 获取博客列表
|
* @return Json
|
*/
|
public function get_blog_list(): Json {
|
try {
|
if (!Request::isPost()) {
|
throw new Exception('请求方式错误');
|
}
|
$list = Db::name('blog')->where('status', 1)->limit(10)->order('id desc')->select()->toArray();
|
$result = [
|
'code' => 200,
|
'message' => '获取成功',
|
'data' => $list,
|
];
|
} catch (Exception $exc) {
|
$result = [
|
'code' => $exc->getCode(),
|
'message' => $exc->getMessage(),
|
];
|
}
|
return json($result);
|
}
|
|
/**
|
* 博客详情页
|
* @param int $id
|
* @return string
|
*/
|
public function detail(int $id = 0): string {
|
if (!$id || !is_numeric($id)) {
|
$this->error('参数错误');
|
}
|
// $info = Db::name('blog')->where('id', $id)->find();
|
// if (!$info) {
|
// $this->error('数据不存在');
|
// }
|
// View::assign('info', $info);
|
View::assign('id', $id);
|
return View::fetch('detail');
|
}
|
|
/**
|
* 获取博客内容
|
* @return Json
|
*/
|
public function get_blog_info(): Json {
|
try {
|
$id = input('id', 0);
|
if (!$id || !is_numeric($id)) {
|
throw new Exception('参数错误');
|
}
|
$info = Db::name('blog')->where('id', $id)->find();
|
if (!$info) {
|
throw new Exception('数据不存在');
|
}
|
$result = [
|
'code' => 200,
|
'message' => '获取成功',
|
'data' => $info,
|
];
|
} catch (Exception $exc) {
|
$result = [
|
'code' => $exc->getCode(),
|
'message' => $exc->getMessage(),
|
];
|
}
|
return json($result);
|
}
|
}
|