做文章中心

This commit is contained in:
云泽网
2025-05-28 00:41:02 +08:00
parent 4838f1f589
commit 205343674a
14 changed files with 2895 additions and 209 deletions
+136 -7
View File
@@ -14,6 +14,114 @@ use app\index\model\Attachments;
class ProgramController extends BaseController
{
//资源中心
public function index()
{
// 获取前端传来的分类ID
$cateid = input('cateid/d', 0);
$page = input('page/d', 1);
$limit = input('limit/d', 10);
// 获取所有顶级分类
$categories = ResourcesCategory::where('cid', 0)
->where('delete_time', null)
->where('status', 1)
->select()
->toArray();
// 获取顶级分类信息
$category = null;
if ($cateid > 0) {
$category = ResourcesCategory::where('id', $cateid)
->where('delete_time', null)
->where('status', 1)
->find();
}
// 获取所有子分类
$subCategories = [];
if ($cateid > 0) {
$subCategories = ResourcesCategory::where('cid', $cateid)
->where('delete_time', null)
->where('status', 1)
->select()
->toArray();
}
// 获取所有子分类ID
$subCategoryIds = array_column($subCategories, 'id');
if ($cateid > 0) {
$subCategoryIds[] = $cateid;
}
// 构建资源查询条件
$where = [
['delete_time', '=', null],
['status', '=', 1]
];
if (!empty($subCategoryIds)) {
$where[] = ['cate', 'in', $subCategoryIds];
}
// 查询资源
$programs = Resources::where($where)
->order('id DESC')
->page($page, $limit)
->select()
->toArray();
// 按子分类分组资源
$groupedPrograms = [];
foreach ($subCategories as $subCategory) {
$groupedPrograms[$subCategory['id']] = [
'id' => $subCategory['id'],
'name' => $subCategory['name'],
'list' => []
];
}
// 将资源分配到对应的子分类
foreach ($programs as $program) {
if (isset($groupedPrograms[$program['cate']])) {
$groupedPrograms[$program['cate']]['list'][] = $program;
}
}
// 获取总数
$total = Resources::where($where)->count();
// 准备返回数据
$data = [
'cate' => [
'id' => $cateid,
'name' => $category ? $category['name'] : '',
'desc' => $category ? $category['desc'] : '',
'image' => $category ? $category['image'] : '',
'subCategories' => array_values($groupedPrograms),
'total' => $total,
'page' => $page,
'limit' => $limit
]
];
// 根据请求方式返回不同的输出
if (request()->isPost()) {
return json([
'code' => 1,
'msg' => '获取成功',
'data' => $data
]);
} else {
// 为视图准备数据
$viewData = [
'categories' => $categories,
'cate' => $data['cate']
];
return view('index', $viewData);
}
}
// 程序列表页
public function list()
{
@@ -22,20 +130,26 @@ class ProgramController extends BaseController
// 构建查询条件
$where = [
['delete_time', '=', null],
['status', '=', 1]
['a.delete_time', '=', null],
['a.status', '=', 1]
];
if ($cateId > 0) {
$where[] = ['cate', '=', $cateId];
$where[] = ['a.cate', '=', $cateId];
}
// 获取程序列表
$programs = Resources::where($where)
->order('id DESC')
$programs = Resources::alias('a')
->join('resources_category c', 'a.cate = c.id')
->where($where)
->field([
'a.*',
'IF(a.icon IS NULL OR a.icon = "", c.icon, a.icon) as icon'
])
->order('a.id DESC')
->paginate([
'list_rows' => 10,
'query' => Request::param()
'query' => Request::instance()->param()
]);
// 获取分类信息
@@ -53,7 +167,22 @@ class ProgramController extends BaseController
->select()
->toArray();
// 将变量传递给视图
// 如果是POST请求,返回JSON数据
if (Request::isPost()) {
return json([
'code' => 1,
'msg' => '获取成功',
'data' => [
'programs' => $programs->items(),
'total' => $programs->total(),
'current_page' => $programs->currentPage(),
'per_page' => $programs->listRows(),
'category' => $category
]
]);
}
// GET请求返回渲染的视图
View::assign([
'programs' => $programs,
'category' => $category,