508 lines
17 KiB
PHP
508 lines
17 KiB
PHP
<?php
|
|
/**
|
|
* 商业使用授权协议
|
|
*
|
|
* Copyright (c) 2025 [云泽网]. 保留所有权利.
|
|
*
|
|
* 本软件仅供评估使用。任何商业用途必须获得书面授权许可。
|
|
* 未经授权商业使用本软件属于侵权行为,将承担法律责任。
|
|
*
|
|
* 授权购买请联系: 357099073@qq.com
|
|
* 官方网站: https://www.yunzer.cn
|
|
*
|
|
* 评估用户须知:
|
|
* 1. 禁止移除版权声明
|
|
* 2. 禁止用于生产环境
|
|
* 3. 禁止转售或分发
|
|
*/
|
|
|
|
/**
|
|
* 后台管理系统-文章管理
|
|
*/
|
|
namespace app\admin\controller;
|
|
use app\admin\model\Article\Articles;
|
|
use app\admin\model\Article\ArticlesCategory;
|
|
use think\facade\Db;
|
|
use think\facade\View;
|
|
use think\facade\Request;
|
|
use app\admin\controller\LogController as Log;
|
|
use app\admin\controller\BaseController;
|
|
|
|
class ArticlesController extends BaseController
|
|
{
|
|
/**
|
|
* 获取控制器名称
|
|
* @return string
|
|
*/
|
|
public function getControllerName()
|
|
{
|
|
return 'Article';
|
|
}
|
|
|
|
// 文章列表
|
|
public function articlelist()
|
|
{
|
|
if (Request::isPost()) {
|
|
$category = input('post.category');
|
|
$page = intval(input('post.page', 1));
|
|
$limit = intval(input('post.limit', 10));
|
|
$title = input('post.title');
|
|
$author = input('post.author');
|
|
|
|
$query = Articles::where('delete_time', null)
|
|
->where('status', '<>', 3);
|
|
|
|
// 分类筛选
|
|
if (!empty($category)) {
|
|
// 先获取分类ID
|
|
$cateInfo = ArticlesCategory::where('name', $category)
|
|
->where('delete_time', null)
|
|
->where('status', 1)
|
|
->find();
|
|
|
|
if ($cateInfo) {
|
|
$query = $query->where('cate', $cateInfo['id']);
|
|
}
|
|
}
|
|
|
|
// 标题搜索
|
|
if (!empty($title)) {
|
|
$query = $query->where('title', 'like', '%'.$title.'%');
|
|
}
|
|
|
|
// 作者搜索
|
|
if (!empty($author)) {
|
|
$query = $query->where('author', 'like', '%'.$author.'%');
|
|
}
|
|
|
|
// 获取总记录数
|
|
$count = $query->count();
|
|
|
|
// 获取分页数据
|
|
$lists = $query->order('id DESC')
|
|
->page($page, $limit)
|
|
->select()
|
|
->each(function ($item) {
|
|
// 获取分类信息
|
|
$cateInfo = ArticlesCategory::where('id', $item['cate'])
|
|
->field('name, image')
|
|
->find();
|
|
|
|
// 设置分类名称
|
|
$item['cate'] = $cateInfo ? $cateInfo['name'] : '';
|
|
|
|
// 如果文章没有图片,使用分类的图片
|
|
if (empty($item['image']) && $cateInfo && !empty($cateInfo['image'])) {
|
|
$item['image'] = $cateInfo['image'];
|
|
}
|
|
|
|
// 格式化时间
|
|
$item['create_time'] = is_numeric($item['create_time']) ? date('Y-m-d H:i:s', $item['create_time']) : $item['create_time'];
|
|
$item['publishdate'] = is_numeric($item['publishdate']) ? date('Y-m-d H:i:s', $item['publishdate']) : '';
|
|
|
|
return $item;
|
|
});
|
|
|
|
return json([
|
|
'code' => 0,
|
|
'msg' => '获取成功',
|
|
'count' => $count,
|
|
'data' => $lists
|
|
]);
|
|
} else {
|
|
// 获取所有分类并构建父子结构
|
|
$allCategories = ArticlesCategory::where('delete_time', null)
|
|
->where('status', 1)
|
|
->order('sort asc, id asc')
|
|
->select()
|
|
->toArray();
|
|
|
|
$categories = [];
|
|
foreach ($allCategories as $category) {
|
|
if ($category['cid'] == 0) {
|
|
$category['children'] = [];
|
|
foreach ($allCategories as $subCategory) {
|
|
if ($subCategory['cid'] == $category['id']) {
|
|
$category['children'][] = $subCategory;
|
|
}
|
|
}
|
|
$categories[] = $category;
|
|
}
|
|
}
|
|
|
|
View::assign([
|
|
'categories' => $categories
|
|
]);
|
|
return View::fetch();
|
|
}
|
|
}
|
|
|
|
// 添加文章
|
|
public function add()
|
|
{
|
|
if (Request::isPost()) {
|
|
$data = [
|
|
'title' => input('post.title'),
|
|
'cate' => input('post.cate'),
|
|
'image' => input('post.image'),
|
|
'content' => input('post.content'),
|
|
'author' => input('post.author'),
|
|
'desc' => input('post.desc'),
|
|
'status' => input('post.status', 2),
|
|
'publishdate' => time(),
|
|
'create_time' => time()
|
|
];
|
|
|
|
$insert = Articles::insert($data);
|
|
if (empty($insert)) {
|
|
Log::record('添加文章', 0, '添加文章失败', '文章管理');
|
|
return json(['code' => 1, 'msg' => '添加失败', 'data' => []]);
|
|
}
|
|
Log::record('添加文章', 1, '', '文章管理');
|
|
return json(['code' => 0, 'msg' => '添加成功', 'data' => []]);
|
|
} else {
|
|
$lists = Articles::order('id DESC')
|
|
->select()
|
|
->each(function ($item, $key) {
|
|
$item['create_time'] = time();
|
|
return $item;
|
|
});
|
|
View::assign([
|
|
'lists' => $lists
|
|
]);
|
|
return View::fetch();
|
|
}
|
|
}
|
|
|
|
// 编辑文章
|
|
public function edit()
|
|
{
|
|
if (Request::isPost()) {
|
|
$id = input('get.id');
|
|
$data = [
|
|
'title' => input('post.title'),
|
|
'cate' => input('post.cate'),
|
|
'image' => input('post.image'),
|
|
'content' => input('post.content'),
|
|
'author' => input('post.author'),
|
|
'desc' => input('post.desc'),
|
|
'status' => input('post.status', 2),
|
|
'update_time' => time()
|
|
];
|
|
|
|
$update = Articles::where('id', $id)->update($data);
|
|
if ($update === false) {
|
|
Log::record('编辑文章', 0, '编辑文章失败', '文章管理');
|
|
return json(['code' => 1, 'msg' => '更新失败', 'data' => []]);
|
|
}
|
|
Log::record('编辑文章', 1, '', '文章管理');
|
|
return json(['code' => 0, 'msg' => '更新成功', 'data' => []]);
|
|
} else {
|
|
$id = input('get.id');
|
|
$info = Articles::where('id', $id)->find();
|
|
if ($info === null) {
|
|
return json(['code' => 1, 'msg' => '文章不存在', 'data' => []]);
|
|
}
|
|
|
|
$cates = ArticlesCategory::where('delete_time', null)
|
|
->where('status', 1)
|
|
->order('sort asc, id asc')
|
|
->select()
|
|
->toArray();
|
|
|
|
$info['content'] = !empty($info['content']) ? htmlspecialchars_decode(str_replace(["\r\n", "\r", "\n"], '', addslashes($info['content']))) : '';
|
|
|
|
$currentCate = ArticlesCategory::where('id', $info['cate'])
|
|
->where('delete_time', null)
|
|
->where('status', 1)
|
|
->find();
|
|
$info['cate_name'] = $currentCate ? $currentCate['name'] : '';
|
|
|
|
View::assign([
|
|
'info' => $info,
|
|
'cates' => $cates
|
|
]);
|
|
return View::fetch();
|
|
}
|
|
}
|
|
|
|
// 删除文章
|
|
public function delete()
|
|
{
|
|
$id = input('post.id');
|
|
$data = [
|
|
'delete_time' => time(),
|
|
];
|
|
$delete = Articles::where('id', $id)->update($data);
|
|
if ($delete === false) {
|
|
Log::record('删除文章', 0, '删除文章失败', '文章管理');
|
|
return json(['code' => 1, 'msg' => '删除失败', 'data' => []]);
|
|
}
|
|
Log::record('删除文章', 1, '', '文章管理');
|
|
return json(['code' => 0, 'msg' => '删除成功', 'data' => []]);
|
|
}
|
|
|
|
// 文章分类
|
|
public function articlecate()
|
|
{
|
|
if (Request::isPost()) {
|
|
$lists = ArticlesCategory::where('delete_time', null)
|
|
->where('status', 1)
|
|
->order('sort asc, id asc')
|
|
->select()
|
|
->toArray();
|
|
|
|
// 构建树形结构
|
|
$tree = [];
|
|
foreach ($lists as $item) {
|
|
if ($item['cid'] == 0) {
|
|
$node = [
|
|
'id' => $item['id'],
|
|
'title' => $item['name'],
|
|
'children' => []
|
|
];
|
|
|
|
// 查找子分类
|
|
foreach ($lists as $subItem) {
|
|
if ($subItem['cid'] == $item['id']) {
|
|
$node['children'][] = [
|
|
'id' => $subItem['id'],
|
|
'title' => $subItem['name'],
|
|
'children' => []
|
|
];
|
|
}
|
|
}
|
|
|
|
$tree[] = $node;
|
|
}
|
|
}
|
|
|
|
return json(['code' => 0, 'msg' => '获取成功', 'data' => $tree]);
|
|
}
|
|
|
|
// 非 POST 请求返回视图
|
|
return View::fetch();
|
|
}
|
|
|
|
//获取分类结构
|
|
public function getcate()
|
|
{
|
|
// 获取所有分类
|
|
$lists = ArticlesCategory::where('delete_time', null)
|
|
->where('status', 1)
|
|
->order('sort asc, id asc')
|
|
->select()
|
|
->toArray();
|
|
|
|
// 构建父子结构
|
|
$tree = [];
|
|
foreach ($lists as $item) {
|
|
if ($item['cid'] == 0) {
|
|
// 顶级分类
|
|
$tree[] = $item;
|
|
} else {
|
|
// 子分类
|
|
foreach ($tree as &$parent) {
|
|
if ($parent['id'] == $item['cid']) {
|
|
if (!isset($parent['children'])) {
|
|
$parent['children'] = [];
|
|
}
|
|
$parent['children'][] = $item;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return json(['code' => 0, 'msg' => '获取成功', 'data' => $tree]);
|
|
}
|
|
|
|
// 添加文章分类
|
|
public function cateadd()
|
|
{
|
|
if (Request::isPost()) {
|
|
$data = [
|
|
'name' => input('post.name'),
|
|
'image' => input('post.image'),
|
|
'cid' => input('post.cid'),
|
|
'sort' => input('post.sort', 0),
|
|
'status' => input('post.status', 1),
|
|
'create_time' => time()
|
|
];
|
|
|
|
$insert = ArticlesCategory::insert($data);
|
|
if (empty($insert)) {
|
|
Log::record('添加文章分类', 0, '添加文章分类失败', '文章分类');
|
|
return json(['code' => 1, 'msg' => '添加失败', 'data' => []]);
|
|
}
|
|
Log::record('添加文章分类', 1, '', '文章分类');
|
|
return json(['code' => 0, 'msg' => '添加成功', 'data' => []]);
|
|
} else {
|
|
// 获取所有可选的父级分类
|
|
$parentCategories = ArticlesCategory::where('delete_time', null)
|
|
->where('status', 1)
|
|
->where('cid', 0)
|
|
->field('id, name')
|
|
->select()
|
|
->toArray();
|
|
|
|
return json([
|
|
'code' => 0,
|
|
'msg' => '获取成功',
|
|
'data' => [
|
|
'parentOptions' => $parentCategories
|
|
]
|
|
]);
|
|
}
|
|
}
|
|
|
|
//编辑文章分类
|
|
public function cateedit()
|
|
{
|
|
if (Request::isPost()) {
|
|
$data = [
|
|
'id' => input('post.id'),
|
|
'name' => input('post.name'),
|
|
'image' => input('post.image'),
|
|
'cid' => input('post.cid'),
|
|
'sort' => input('post.sort', 0),
|
|
'status' => input('post.status', 1),
|
|
'update_time' => time()
|
|
];
|
|
|
|
$update = ArticlesCategory::where('id', $data['id'])
|
|
->update($data);
|
|
|
|
if ($update === false) {
|
|
Log::record('编辑文章分类', 0, '更新文章分类失败', '文章分类');
|
|
return json(['code' => 1, 'msg' => '更新失败', 'data' => []]);
|
|
}
|
|
Log::record('编辑文章分类', 1, '', '文章分类');
|
|
return json(['code' => 0, 'msg' => '更新成功', 'data' => []]);
|
|
} else {
|
|
$id = input('get.id');
|
|
$info = ArticlesCategory::where('id', $id)->find();
|
|
|
|
// 获取所有可选的父级分类
|
|
$parentCategories = ArticlesCategory::where('delete_time', null)
|
|
->where('status', 1)
|
|
->where('id', '<>', $id) // 排除自己
|
|
->where(function ($query) use ($id) {
|
|
// 排除自己的所有子分类
|
|
$query->where('cid', '<>', $id);
|
|
})
|
|
->field('id, name, cid')
|
|
->select()
|
|
->toArray();
|
|
|
|
// 构建父级分类选项
|
|
$parentOptions = [];
|
|
foreach ($parentCategories as $category) {
|
|
if ($category['cid'] == 0) {
|
|
$parentOptions[] = [
|
|
'id' => $category['id'],
|
|
'name' => $category['name']
|
|
];
|
|
}
|
|
}
|
|
|
|
return json([
|
|
'code' => 0,
|
|
'msg' => '获取成功',
|
|
'data' => [
|
|
'info' => $info,
|
|
'parentOptions' => $parentOptions
|
|
]
|
|
]);
|
|
}
|
|
}
|
|
|
|
//删除文章分类
|
|
public function catedel()
|
|
{
|
|
$id = input('post.id');
|
|
|
|
// 检查是否有子分类
|
|
$hasChildren = ArticlesCategory::where('cid', $id)
|
|
->where('delete_time', null)
|
|
->find();
|
|
|
|
if ($hasChildren) {
|
|
Log::record('删除文章分类', 0, '该分类下有子分类,无法删除', '文章分类');
|
|
return json(['code' => 1, 'msg' => '该分类下有子分类,无法删除', 'data' => []]);
|
|
}
|
|
|
|
$delete = ArticlesCategory::where('id', $id)
|
|
->update(['delete_time' => time()]);
|
|
|
|
if ($delete === false) {
|
|
Log::record('删除文章分类', 0, '删除文章分类失败', '文章分类');
|
|
return json(['code' => 1, 'msg' => '删除失败', 'data' => []]);
|
|
}
|
|
Log::record('删除文章分类', 1, '', '文章分类');
|
|
return json(['code' => 0, 'msg' => '删除成功', 'data' => []]);
|
|
}
|
|
|
|
//统计文章数量
|
|
public function counts() {
|
|
try {
|
|
// 获取文章总数
|
|
$total = Articles::where('delete_time', null)
|
|
->where('status', '<>', 3)
|
|
->count();
|
|
|
|
// 获取今日新增文章数
|
|
$today = strtotime(date('Y-m-d'));
|
|
$todayNew = Articles::where('delete_time', null)
|
|
->where('status', '<>', 3)
|
|
->where('create_time', '>=', $today)
|
|
->count();
|
|
|
|
// 获取最近7天的文章数据
|
|
$dates = [];
|
|
$counts = [];
|
|
$totalCounts = []; // 存储每天的总文章数
|
|
$totalSoFar = 0; // 用于累计总文章数
|
|
|
|
for ($i = 6; $i >= 0; $i--) {
|
|
$date = date('Y-m-d', strtotime("-$i days"));
|
|
$start = strtotime($date);
|
|
$end = $start + 86400;
|
|
|
|
// 获取当天新增文章数
|
|
$count = Articles::where('delete_time', null)
|
|
->where('status', '<>', 3)
|
|
->where('create_time', '>=', $start)
|
|
->where('create_time', '<', $end)
|
|
->count();
|
|
|
|
// 获取截至当天的总文章数
|
|
$totalCount = Articles::where('delete_time', null)
|
|
->where('status', '<>', 3)
|
|
->where('create_time', '<', $end)
|
|
->count();
|
|
|
|
$dates[] = $date;
|
|
$counts[] = $count;
|
|
$totalCounts[] = $totalCount;
|
|
}
|
|
|
|
return json([
|
|
'code' => 0,
|
|
'msg' => '获取成功',
|
|
'data' => [
|
|
'total' => $total,
|
|
'todayNew' => $todayNew,
|
|
'dates' => $dates,
|
|
'counts' => $counts,
|
|
'totalCounts' => $totalCounts
|
|
]
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => '获取失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
} |