213 lines
5.9 KiB
PHP
213 lines
5.9 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
|
// +----------------------------------------------------------------------
|
|
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
|
// | 开源版本可自由商用,可去除界面版权logo
|
|
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
|
// | github下载:https://github.com/likeshop-github/likeadmin
|
|
// | 访问官网:https://www.likeadmin.cn
|
|
// | likeadmin团队 版权所有 拥有最终解释权
|
|
// +----------------------------------------------------------------------
|
|
// | author: likeadminTeam
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\api\controller;
|
|
|
|
|
|
use app\api\lists\article\ArticleCollectLists;
|
|
use app\api\lists\article\ArticleLists;
|
|
use app\api\logic\ArticleLogic;
|
|
use app\api\logic\ArticleInteractLogic;
|
|
use app\common\model\article\Article;
|
|
use app\common\service\article\ArticleImageProxyService;
|
|
|
|
/**
|
|
* 文章管理
|
|
* Class ArticleController
|
|
* @package app\api\controller
|
|
*/
|
|
class ArticleController extends BaseApiController
|
|
{
|
|
|
|
public array $notNeedLogin = ['lists', 'cate', 'detail', 'commentList', 'imageProxy'];
|
|
|
|
|
|
/**
|
|
* @notes 文章列表
|
|
* @return \think\response\Json
|
|
* @author 段誉
|
|
* @date 2022/9/20 15:30
|
|
*/
|
|
public function lists()
|
|
{
|
|
return $this->dataLists(new ArticleLists());
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 文章分类列表
|
|
* @return \think\response\Json
|
|
* @author 段誉
|
|
* @date 2022/9/20 15:30
|
|
*/
|
|
public function cate()
|
|
{
|
|
return $this->data(ArticleLogic::cate());
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 收藏列表
|
|
* @return \think\response\Json
|
|
* @author 段誉
|
|
* @date 2022/9/20 16:31
|
|
*/
|
|
public function collect()
|
|
{
|
|
return $this->dataLists(new ArticleCollectLists());
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 文章详情
|
|
* @return \think\response\Json
|
|
* @author 段誉
|
|
* @date 2022/9/20 17:09
|
|
*/
|
|
public function detail()
|
|
{
|
|
$id = $this->request->get('id/d');
|
|
$result = ArticleLogic::detail($id, $this->userId);
|
|
return $this->data($result);
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 文章翻译
|
|
* @return \think\response\Json
|
|
*/
|
|
public function translate()
|
|
{
|
|
$articleId = $this->request->post('article_id/d', 0);
|
|
if (empty($articleId)) {
|
|
$articleId = $this->request->post('id/d', 0);
|
|
}
|
|
if (empty($articleId)) {
|
|
$articleId = $this->request->get('article_id/d', 0);
|
|
}
|
|
if (empty($articleId)) {
|
|
$articleId = $this->request->get('id/d', 0);
|
|
}
|
|
if (empty($articleId)) {
|
|
return $this->fail('参数缺失');
|
|
}
|
|
|
|
if (!$this->userId) {
|
|
return $this->fail('请先登录', [], 0, 401);
|
|
}
|
|
|
|
$confirm = $this->request->post('confirm/d', 0);
|
|
$result = ArticleLogic::translate((int) $articleId, $this->userId, (int) $confirm);
|
|
if ($result['success'] === false) {
|
|
return $this->fail($result['error'] ?? '翻译失败');
|
|
}
|
|
|
|
return $this->data($result['data']);
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 加入收藏
|
|
* @return \think\response\Json
|
|
* @author 段誉
|
|
* @date 2022/9/20 17:01
|
|
*/
|
|
public function addCollect()
|
|
{
|
|
$articleId = $this->request->post('id/d');
|
|
ArticleLogic::addCollect($articleId, $this->userId);
|
|
return $this->success('操作成功');
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 取消收藏
|
|
* @return \think\response\Json
|
|
* @author 段誉
|
|
* @date 2022/9/20 17:01
|
|
*/
|
|
public function cancelCollect()
|
|
{
|
|
$articleId = $this->request->post('id/d');
|
|
ArticleLogic::cancelCollect($articleId, $this->userId);
|
|
return $this->success('操作成功');
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 评论列表
|
|
*/
|
|
public function commentList()
|
|
{
|
|
$articleId = $this->request->get('article_id/d');
|
|
$pageNo = $this->request->get('page_no/d', 1);
|
|
$pageSize = $this->request->get('page_size/d', 15);
|
|
$result = ArticleInteractLogic::commentList($articleId, $pageNo, $pageSize);
|
|
return $this->data($result);
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 远程文章图片代理
|
|
* @return \think\Response
|
|
*/
|
|
public function imageProxy()
|
|
{
|
|
$url = $this->request->get('url/s', '');
|
|
return ArticleImageProxyService::proxyRemoteImage($url);
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 发布评论
|
|
*/
|
|
public function addComment()
|
|
{
|
|
$params = $this->request->post();
|
|
if (empty($params['article_id']) || empty($params['content'])) {
|
|
return $this->fail('参数缺失');
|
|
}
|
|
$result = ArticleInteractLogic::addComment($this->userId, $params);
|
|
if ($result === false) {
|
|
return $this->fail(ArticleInteractLogic::getError());
|
|
}
|
|
return $this->success('评论成功', ['id' => $result]);
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 点赞/踩
|
|
*/
|
|
public function vote()
|
|
{
|
|
$articleId = $this->request->post('article_id/d');
|
|
$voteType = $this->request->post('vote_type/d', 1);
|
|
if (empty($articleId)) {
|
|
return $this->fail('参数缺失');
|
|
}
|
|
if (!in_array($voteType, [1, 2])) {
|
|
return $this->fail('投票类型错误');
|
|
}
|
|
$result = ArticleInteractLogic::vote($this->userId, $articleId, $voteType);
|
|
if ($result === false) {
|
|
return $this->fail(ArticleInteractLogic::getError());
|
|
}
|
|
// 返回最新计数
|
|
$article = Article::where('id', $articleId)->field('like_count,dislike_count')->findOrEmpty();
|
|
$result['like_count'] = $article->isEmpty() ? 0 : $article->like_count;
|
|
$result['dislike_count'] = $article->isEmpty() ? 0 : $article->dislike_count;
|
|
return $this->success('操作成功', $result);
|
|
}
|
|
}
|