123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace app\api\controller;
- use app\common\model\content\Category;
- use app\common\model\content\Article as ArticleModel;
- /**
- * 地址
- */
- class Article extends Base
- {
- protected $noNeedLogin = ['getCategoryList', 'getArticleList', 'getArticleInfo'];
- /**
- * 分类列表
- */
- public function getCategoryList()
- {
- $parentId = $this->request->param('parent_id', 0, 'intval');
- $page = $this->request->param('page', 1, 'intval');
- $pageSize = $this->request->param('pageSize', 10, 'intval');
-
- // 如果存在parent_id,先查询父分类信息
- if ($parentId > 0) {
- $parentCategory = Category::get($parentId);
- if (!$parentCategory) {
- $this->error('父分类不存在');
- }
-
- // 处理父分类图片
- $parentCategory['image'] = $parentCategory['image'] ? cdnurl($parentCategory['image']) : '';
-
- // 获取子分类列表
- $children = Category::getCategoryList($parentId, $page, $pageSize);
-
- // 处理子分类图片
- $children->each(function ($item) {
- $item['image'] = $item['image'] ? cdnurl($item['image']) : '';
- });
-
- // 将子分类添加到父分类的children字段
- $parentCategory['children'] = $children;
- $parentCategory['total'] = $children->total();
- $parentCategory['per_page'] = $children->listRows();
- $parentCategory['current_page'] = $children->currentPage();
- $parentCategory['last_page'] = $children->lastPage();
-
- $this->success('', $parentCategory);
- } else {
- // 如果没有parent_id,直接返回顶级分类列表
- $list = Category::getCategoryList($parentId, $page, $pageSize);
-
- // 处理图片
- $list->each(function ($item) {
- $item['image'] = $item['image'] ? cdnurl($item['image']) : '';
- });
-
- $this->success('', $list);
- }
- }
- /**
- * 文章列表
- */
- public function getArticleList()
- {
- $categoryId = $this->request->param('category_id', 0, 'intval');
- $page = $this->request->param('page', 1, 'intval');
- $pageSize = $this->request->param('pageSize', 10, 'intval');
- $list = ArticleModel::getArticleList($categoryId, $page, $pageSize);
- $list->each(function ($item) {
- // 处理图像的
- $item['image'] = $item['image'] ? cdnurl($item['image']) : '';
- });
- $this->success('获取成功', $list);
- }
- //文章详情
- public function getArticleInfo()
- {
- $id = $this->request->param('id', 0, 'intval');
- // 验证id是否为空
- if (empty($id)) {
- $this->error('id不能为空');
- }
- $info = ArticleModel::getArticleInfo($id);
- //判断是否为空
- if (!empty($info)) {
- $info['image'] = $info['image'] ? cdnurl($info['image']) : '';
- }
- $this->success('获取成功', $info);
- }
- }
|