421 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			421 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						||
/**
 | 
						||
 *	文章控制器
 | 
						||
 */
 | 
						||
namespace app\index\controller;
 | 
						||
use app\index\controller\BaseController;
 | 
						||
use think\facade\Db;
 | 
						||
use think\facade\View;
 | 
						||
use think\facade\Request;
 | 
						||
use app\index\model\Articles\Articles;
 | 
						||
use app\index\model\Articles\ArticlesCategory;
 | 
						||
 | 
						||
class ArticlesController extends BaseController
 | 
						||
{
 | 
						||
    //文章中心
 | 
						||
    public function index()
 | 
						||
    {
 | 
						||
        // 获取前端传来的分类ID
 | 
						||
        $cateid = input('cateid/d', 0); // 使用input助手函数获取参数,并转换为整数
 | 
						||
        $page = input('page/d', 1);
 | 
						||
        $limit = input('limit/d', 10);
 | 
						||
 | 
						||
        // 获取所有顶级分类
 | 
						||
        $categories = ArticlesCategory::where('cid', 0)
 | 
						||
            ->where('delete_time', null)
 | 
						||
            ->where('status', 1)
 | 
						||
            ->select()
 | 
						||
            ->toArray();
 | 
						||
 | 
						||
        // 获取顶级分类信息
 | 
						||
        $category = null;
 | 
						||
        if ($cateid > 0) {
 | 
						||
            $category = ArticlesCategory::where('id', $cateid)
 | 
						||
                ->where('delete_time', null)
 | 
						||
                ->where('status', 1)
 | 
						||
                ->find();
 | 
						||
        }
 | 
						||
 | 
						||
        // 获取所有子分类
 | 
						||
        $subCategories = [];
 | 
						||
        if ($cateid > 0) {
 | 
						||
            $subCategories = ArticlesCategory::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', '=', 2]
 | 
						||
        ];
 | 
						||
 | 
						||
        if (!empty($subCategoryIds)) {
 | 
						||
            $where[] = ['cate', 'in', $subCategoryIds];
 | 
						||
        }
 | 
						||
 | 
						||
        // 查询文章
 | 
						||
        $articles = Articles::where($where)
 | 
						||
            ->order('id DESC')
 | 
						||
            ->page($page, $limit)
 | 
						||
            ->select()
 | 
						||
            ->toArray();
 | 
						||
 | 
						||
        // 按子分类分组文章
 | 
						||
        $groupedArticles = [];
 | 
						||
        foreach ($subCategories as $subCategory) {
 | 
						||
            $groupedArticles[$subCategory['id']] = [
 | 
						||
                'id' => $subCategory['id'],
 | 
						||
                'name' => $subCategory['name'],
 | 
						||
                'desc' => $subCategory['desc'],
 | 
						||
                'image' => $subCategory['image'],
 | 
						||
                'list' => []
 | 
						||
            ];
 | 
						||
        }
 | 
						||
 | 
						||
        // 将文章分配到对应的子分类
 | 
						||
        foreach ($articles as $article) {
 | 
						||
            if (isset($groupedArticles[$article['cate']])) {
 | 
						||
                // 如果文章图片为空,使用分类图片
 | 
						||
                if (empty($article['image'])) {
 | 
						||
                    $article['image'] = $groupedArticles[$article['cate']]['image'];
 | 
						||
                }
 | 
						||
                $groupedArticles[$article['cate']]['list'][] = $article;
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        // 获取总数
 | 
						||
        $total = Articles::where($where)->count();
 | 
						||
 | 
						||
        // 准备返回数据
 | 
						||
        $data = [
 | 
						||
            'cate' => [
 | 
						||
                'id' => $cateid,
 | 
						||
                'name' => $category ? $category['name'] : '',
 | 
						||
                'desc' => $category ? $category['desc'] : '',
 | 
						||
                'image' => $category ? $category['image'] : '',
 | 
						||
                'subCategories' => array_values($groupedArticles),
 | 
						||
                '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()
 | 
						||
    {
 | 
						||
        // 获取分类ID
 | 
						||
        $cateId = Request::param('cate/d', 0);
 | 
						||
 | 
						||
        // 构建查询条件
 | 
						||
        $where = [
 | 
						||
            ['a.delete_time', '=', null],
 | 
						||
            ['a.status', '=', 2]
 | 
						||
        ];
 | 
						||
 | 
						||
        if ($cateId > 0) {
 | 
						||
            $where[] = ['a.cate', '=', $cateId];
 | 
						||
        }
 | 
						||
 | 
						||
        // 获取文章列表
 | 
						||
        $articles = Articles::alias('a')
 | 
						||
            ->join('articles_category c', 'a.cate = c.id')
 | 
						||
            ->where($where)
 | 
						||
            ->field([
 | 
						||
                'a.*',
 | 
						||
                'IF(a.image IS NULL OR a.image = "", c.image, a.image) as image'
 | 
						||
            ])
 | 
						||
            ->order('a.id DESC')
 | 
						||
            ->paginate([
 | 
						||
                'list_rows' => 10,
 | 
						||
                'query' => Request::instance()->param()
 | 
						||
            ]);
 | 
						||
 | 
						||
        // 获取分类信息
 | 
						||
        $category = null;
 | 
						||
        if ($cateId > 0) {
 | 
						||
            $category = ArticlesCategory::where('id', $cateId)
 | 
						||
                ->where('delete_time', null)
 | 
						||
                ->where('status', 3)
 | 
						||
                ->find();
 | 
						||
        }
 | 
						||
 | 
						||
        // 获取所有分类
 | 
						||
        $categories = ArticlesCategory::where('delete_time', null)
 | 
						||
            ->where('status', 3)
 | 
						||
            ->select()
 | 
						||
            ->toArray();
 | 
						||
 | 
						||
        // 根据请求方式返回不同的输出
 | 
						||
        if (request()->isPost()) {
 | 
						||
            return json([
 | 
						||
                'code' => 1,
 | 
						||
                'msg' => '获取成功',
 | 
						||
                'data' => [
 | 
						||
                    'articles' => $articles->items(),
 | 
						||
                    'category' => $category,
 | 
						||
                    'categories' => $categories,
 | 
						||
                    'total' => $articles->total(),
 | 
						||
                    'current_page' => $articles->currentPage(),
 | 
						||
                    'per_page' => $articles->listRows()
 | 
						||
                ]
 | 
						||
            ]);
 | 
						||
        } else {
 | 
						||
            // 将变量传递给视图
 | 
						||
            View::assign([
 | 
						||
                'articles' => $articles,
 | 
						||
                'category' => $category,
 | 
						||
                'categories' => $categories
 | 
						||
            ]);
 | 
						||
            return view('list');
 | 
						||
        }
 | 
						||
    }
 | 
						||
 | 
						||
    // 文章详情页
 | 
						||
    public function detail()
 | 
						||
    {
 | 
						||
        $id = Request::param('id/d', 0);
 | 
						||
        $article = Articles::where('id', $id)->find();
 | 
						||
 | 
						||
        if (!$article) {
 | 
						||
            return json(['code' => 0, 'msg' => '文章不存在或已被删除']);
 | 
						||
        }
 | 
						||
 | 
						||
        // 获取分类名称
 | 
						||
        $cateName = ArticlesCategory::where('id', $article['cate'])
 | 
						||
            ->value('name');
 | 
						||
 | 
						||
        // 获取上一篇和下一篇文章
 | 
						||
        $prevArticle = Articles::where('id', '<', $id)
 | 
						||
            ->where('delete_time', null)
 | 
						||
            ->where('status', '<>', 3)
 | 
						||
            ->order('id DESC')
 | 
						||
            ->find();
 | 
						||
 | 
						||
        $nextArticle = Articles::where('id', '>', $id)
 | 
						||
            ->where('delete_time', null)
 | 
						||
            ->where('status', '<>', 3)
 | 
						||
            ->order('id ASC')
 | 
						||
            ->find();
 | 
						||
 | 
						||
        // 获取相关文章(同分类的其他文章)
 | 
						||
        $relatedArticles = Articles::alias('a')
 | 
						||
            ->join('articles_category c', 'a.cate = c.id')
 | 
						||
            ->where('a.cate', $article['cate'])
 | 
						||
            ->where('a.id', '<>', $id)
 | 
						||
            ->where('a.delete_time', null)
 | 
						||
            ->where('a.status', '=', 2)
 | 
						||
            ->field([
 | 
						||
                'a.id',
 | 
						||
                'a.title',
 | 
						||
                'a.desc',
 | 
						||
                'IF(a.image IS NULL OR a.image = "", c.image, a.image) as image'
 | 
						||
            ])
 | 
						||
            ->order('a.id DESC')
 | 
						||
            ->limit(3)
 | 
						||
            ->select()
 | 
						||
            ->toArray();
 | 
						||
 | 
						||
        // 如果是 AJAX 请求,返回 JSON 数据
 | 
						||
        if (Request::isAjax()) {
 | 
						||
            return json([
 | 
						||
                'code' => 1,
 | 
						||
                'msg' => '获取成功',
 | 
						||
                'data' => [
 | 
						||
                    'article' => $article,
 | 
						||
                    'cateName' => $cateName,
 | 
						||
                    'prevArticle' => $prevArticle,
 | 
						||
                    'nextArticle' => $nextArticle,
 | 
						||
                    'relatedArticles' => $relatedArticles
 | 
						||
                ]
 | 
						||
            ]);
 | 
						||
        }
 | 
						||
 | 
						||
        // 非 AJAX 请求返回视图
 | 
						||
        View::assign([
 | 
						||
            'article' => $article,
 | 
						||
            'cateName' => $cateName,
 | 
						||
            'prevArticle' => $prevArticle,
 | 
						||
            'nextArticle' => $nextArticle,
 | 
						||
            'relatedArticles' => $relatedArticles
 | 
						||
        ]);
 | 
						||
 | 
						||
        return view('detail');
 | 
						||
    }
 | 
						||
 | 
						||
    // 文章点赞
 | 
						||
    public function like()
 | 
						||
    {
 | 
						||
        if (!Request::isAjax()) {
 | 
						||
            return json(['code' => 0, 'msg' => '非法请求']);
 | 
						||
        }
 | 
						||
 | 
						||
        $id = Request::param('id/d', 0);
 | 
						||
 | 
						||
        // 检查文章是否存在
 | 
						||
        $article = Articles::where('id', $id)
 | 
						||
            ->where('delete_time', null)
 | 
						||
            ->where('status', 2)
 | 
						||
            ->find();
 | 
						||
 | 
						||
        if (!$article) {
 | 
						||
            return json(['code' => 0, 'msg' => '文章不存在或已被删除']);
 | 
						||
        }
 | 
						||
 | 
						||
        // 更新点赞数
 | 
						||
        $result = Articles::where('id', $id)
 | 
						||
            ->where('delete_time', null)
 | 
						||
            ->inc('likes', 1)
 | 
						||
            ->update();
 | 
						||
 | 
						||
        if ($result) {
 | 
						||
            // 返回更新后的点赞数
 | 
						||
            $newLikes = $article['likes'] + 1;
 | 
						||
            return json([
 | 
						||
                'code' => 1,
 | 
						||
                'msg' => '点赞成功',
 | 
						||
                'data' => [
 | 
						||
                    'likes' => $newLikes
 | 
						||
                ]
 | 
						||
            ]);
 | 
						||
        } else {
 | 
						||
            return json(['code' => 0, 'msg' => '点赞失败']);
 | 
						||
        }
 | 
						||
    }
 | 
						||
 | 
						||
    // 提交评论
 | 
						||
    public function comment()
 | 
						||
    {
 | 
						||
        if (!Request::isAjax() || !Request::isPost()) {
 | 
						||
            return json(['code' => 0, 'msg' => '非法请求']);
 | 
						||
        }
 | 
						||
 | 
						||
        $articleId = Request::param('article_id/d', 0);
 | 
						||
        $content = Request::param('content/s', '');
 | 
						||
        $parentId = Request::param('parent_id/d', 0);
 | 
						||
 | 
						||
        if (empty($content)) {
 | 
						||
            return json(['code' => 0, 'msg' => '评论内容不能为空']);
 | 
						||
        }
 | 
						||
 | 
						||
        // 检查文章是否存在
 | 
						||
        $article = Articles::where('id', $articleId)
 | 
						||
            ->where('delete_time', null)
 | 
						||
            ->where('status', 3)
 | 
						||
            ->find();
 | 
						||
 | 
						||
        if (!$article) {
 | 
						||
            return json(['code' => 0, 'msg' => '文章不存在或已被删除']);
 | 
						||
        }
 | 
						||
 | 
						||
        // 添加评论
 | 
						||
        // $data = [
 | 
						||
        //     'article_id' => $articleId,
 | 
						||
        //     'content' => $content,
 | 
						||
        //     'parent_id' => $parentId,
 | 
						||
        //     'user_id' => $this->getUserId(),
 | 
						||
        //     'user_name' => $this->getUserName(),
 | 
						||
        //     'status' => 1,
 | 
						||
        //     'create_time' => time()
 | 
						||
        // ];
 | 
						||
 | 
						||
        // $result = Db::table('yz_article_comment')->insert($data);
 | 
						||
 | 
						||
        // if ($result) {
 | 
						||
        //     return json(['code' => 1, 'msg' => '评论成功']);
 | 
						||
        // } else {
 | 
						||
        //     return json(['code' => 0, 'msg' => '评论失败']);
 | 
						||
        // }
 | 
						||
    }
 | 
						||
 | 
						||
    // 获取当前用户ID(示例方法,实际应根据您的用户系统实现)
 | 
						||
    private function getUserId()
 | 
						||
    {
 | 
						||
        // 这里应该返回当前登录用户的ID
 | 
						||
        return 1; // 示例返回值
 | 
						||
    }
 | 
						||
 | 
						||
    // 获取当前用户名(示例方法,实际应根据您的用户系统实现)
 | 
						||
    private function getUserName()
 | 
						||
    {
 | 
						||
        // 这里应该返回当前登录用户的用户名
 | 
						||
        return '游客'; // 示例返回值
 | 
						||
    }
 | 
						||
 | 
						||
    // 获取访问统计
 | 
						||
    public function viewStats()
 | 
						||
    {
 | 
						||
        $id = Request::param('id/d', 0);
 | 
						||
 | 
						||
 | 
						||
        // 获取总访问量
 | 
						||
        $totalViews = Articles::where('id', $id)
 | 
						||
            ->value('views');
 | 
						||
 | 
						||
        return json([
 | 
						||
            'code' => 1,
 | 
						||
            'data' => [
 | 
						||
                'total' => $totalViews
 | 
						||
            ]
 | 
						||
        ]);
 | 
						||
    }
 | 
						||
 | 
						||
    /**
 | 
						||
     * 更新文章访问次数
 | 
						||
     */
 | 
						||
    public function updateViews()
 | 
						||
    {
 | 
						||
        if (!Request::isPost()) {
 | 
						||
            return json(['code' => 0, 'msg' => '非法请求']);
 | 
						||
        }
 | 
						||
 | 
						||
        $id = Request::post('id');
 | 
						||
        if (!$id) {
 | 
						||
            return json(['code' => 0, 'msg' => '参数错误']);
 | 
						||
        }
 | 
						||
 | 
						||
        try {
 | 
						||
            // 更新访问次数
 | 
						||
            $article = Articles::where('id', $id)->find();
 | 
						||
            if (!$article) {
 | 
						||
                return json(['code' => 0, 'msg' => '文章不存在']);
 | 
						||
            }
 | 
						||
 | 
						||
            // 更新访问次数
 | 
						||
            Articles::where('id', $id)->inc('views')->update();
 | 
						||
 | 
						||
            // 获取更新后的访问次数
 | 
						||
            $newViews = Articles::where('id', $id)->value('views');
 | 
						||
 | 
						||
            return json(['code' => 1, 'msg' => '更新成功', 'data' => ['views' => $newViews]]);
 | 
						||
        } catch (\Exception $e) {
 | 
						||
            return json(['code' => 0, 'msg' => '更新失败:' . $e->getMessage()]);
 | 
						||
        }
 | 
						||
    }
 | 
						||
}
 |