改接口匹配新前端

This commit is contained in:
2025-12-24 17:41:33 +08:00
parent 5691a3f677
commit 2161113eae
18 changed files with 1134 additions and 96 deletions
+140 -2
View File
@@ -32,6 +32,144 @@ use app\index\model\Resources\Resources;
class ArticlesController extends BaseController
{
// 获取siteinformation分类
public function getSiteInformationCategory()
{
// 获取名为 '站点资讯' 的分类
$siteInfoCategory = ArticlesCategory::where('name', '站点资讯')
->where('delete_time', null)
->where('status', 1)
->field('id,cid,name,image,sort')
->find();
if (!$siteInfoCategory) {
return json([
'code' => 1,
'msg' => '未找到站点资讯分类'
]);
}
// 只返回其子分类,cid等于'站点资讯'的id
$children = ArticlesCategory::where('cid', $siteInfoCategory['id'])
->where('delete_time', null)
->where('status', 1)
->field('id,cid,name,image,sort')
->select()
->toArray();
return json([
'code' => 0,
'msg' => '获取站点资讯子分类成功',
'data' => $children
]);
}
// 获取siteinformation内容,传参
public function getSiteInformationLists()
{
try {
// 获取前端传递的分类ID
$cateid = input('cateid/d', 0);
// 验证分类ID
if ($cateid <= 0) {
return json([
'code' => 1,
'msg' => '分类ID不能为空'
]);
}
// 检查分类是否存在且有效
$category = ArticlesCategory::where('id', $cateid)
->where('delete_time', null)
->where('status', 1)
->find();
if (!$category) {
return json([
'code' => 1,
'msg' => '分类不存在或已禁用'
]);
}
// 获取分页参数
$page = input('page/d', 1);
$limit = input('limit/d', 10);
// 获取该分类下的所有子分类ID(包括自身)
$subCategoryIds = [$cateid];
// 查找所有子分类
$subCategories = ArticlesCategory::where('cid', $cateid)
->where('delete_time', null)
->where('status', 1)
->column('id');
if (!empty($subCategories)) {
$subCategoryIds = array_merge($subCategoryIds, $subCategories);
}
// 构建查询条件
$where = [
['delete_time', '=', null],
['status', '=', 2], // 已发布的文章
['cate', 'in', $subCategoryIds]
];
// 查询文章总数
$total = Articles::where($where)->count();
// 查询文章列表
$articles = Articles::where($where)
->field('id,title,cate,image,desc,author,content,publishdate,views,likes,is_trans,transurl,push,create_time')
->order('sort DESC, id DESC')
->page($page, $limit)
->select()
->toArray();
// 处理文章数据
foreach ($articles as &$article) {
// 如果文章没有封面图,使用分类封面图
if (empty($article['image'])) {
$article['image'] = $category['image'] ?? '';
}
// 格式化时间
$article['publishdate'] = date('Y-m-d H:i:s', strtotime($article['publishdate']));
$article['create_time'] = date('Y-m-d H:i:s', strtotime($article['create_time']));
// 获取分类名称
$articleCategory = ArticlesCategory::where('id', $article['cate'])->find();
$article['category_name'] = $articleCategory ? $articleCategory['name'] : '';
}
// 返回数据
return json([
'code' => 0,
'msg' => '获取成功',
'data' => [
'category' => [
'id' => $category['id'],
'name' => $category['name'],
'desc' => $category['desc'],
'image' => $category['image']
],
'articles' => $articles,
'total' => $total,
'page' => $page,
'limit' => $limit,
'total_pages' => ceil($total / $limit)
]
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '获取失败:' . $e->getMessage()
]);
}
}
//文章中心
public function index()
{
@@ -237,7 +375,7 @@ class ArticlesController extends BaseController
$articleCount = Articles::where('author', $article['author'])->count();
// 统计作者的资源数
$resourceCount = Resources::where('uploader', $article['author'])->count();
$authorData = [
'avatar' => $authorInfo['avatar'] ?: '/static/images/avatar.png',
'name' => $authorInfo['name'],
@@ -469,7 +607,7 @@ class ArticlesController extends BaseController
//获取作者信息
public function getAuthorInfo()
{
if (!Request::isPost()) {
if (!Request::isPost()) {
return json(['code' => 0, 'msg' => '非法请求']);
}
+45
View File
@@ -25,6 +25,7 @@ use think\facade\Db;
use think\facade\View;
use think\facade\Env;
use think\facade\Config;
use app\index\model\MenuFront\MenuFront;
use app\index\model\Banner;
use app\index\model\Resources\ResourcesCategory;
use app\index\model\Articles\ArticlesCategory;
@@ -37,6 +38,50 @@ use app\index\model\Attachments;
class IndexController extends BaseController
{
//获取主体菜单
/**
* 获取主菜单列表
* @return \think\Response|\think\response\Json
*/
public function getmainmenu()
{
try {
// 查询所有未删除且启用的一级菜单,只筛选所需字段
$menuList = MenuFront::where('status', 1)
->where('view', 1)
->where('parent_id', 0)
->whereNull('delete_time')
->order('sort desc,id asc')
->field('id,parent_id,title,path,url,icon,sort')
->select()
->toArray();
// 递归查询每个一级菜单的子菜单,只筛选所需字段
foreach ($menuList as &$menu) {
$children = MenuFront::where('status', 1)
->where('view', 1)
->where('parent_id', $menu['id'])
->whereNull('delete_time')
->order('sort desc,id asc')
->field('id,parent_id,title,path,url,icon,sort')
->select()
->toArray();
$menu['children'] = $children;
}
return json([
'code' => 0,
'msg' => '获取主菜单成功',
'data' => $menuList
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '获取主菜单失败: ' . $e->getMessage()
]);
}
}
/**
* 首页
*/