194 lines
5.4 KiB
PHP
194 lines
5.4 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Author: ywxapp<admin@ywxapp.cn>
|
|
// +----------------------------------------------------------------------
|
|
declare (strict_types = 1);
|
|
|
|
namespace addon\blog\controller;
|
|
use ywxapp\controller\FrontendBase;
|
|
|
|
use addon\blog\model\BlogArticle as ArticleModel;
|
|
use addon\blog\model\BlogLike as LikeModel;
|
|
use addon\blog\model\BlogFavorite as FavoriteModel;
|
|
use think\facade\Request;
|
|
|
|
/**
|
|
* Interaction 类
|
|
*
|
|
* @author ywxapp <admin@ywxapp.cn>
|
|
*/
|
|
class Interaction extends FrontendBase
|
|
{
|
|
/**
|
|
* 公开访问(登录校验由方法内部通过 $this->auth 处理,会员统一以 uid 标识)
|
|
* @var array
|
|
*/
|
|
protected $noNeedLogin = ['*'];
|
|
|
|
/**
|
|
* 点赞/取消点赞
|
|
*/
|
|
public function like()
|
|
{
|
|
$article_id = Request::param('article_id');
|
|
if (! $article_id) {
|
|
return json(['code' => 0, 'msg' => '参数错误']);
|
|
}
|
|
if (! $this->auth->isLogin) {
|
|
return json(['code' => 0, 'msg' => '请先登录']);
|
|
}
|
|
$uid = $this->auth->model->uid;
|
|
|
|
$article = ArticleModel::find($article_id);
|
|
if (! $article) {
|
|
return json(['code' => 0, 'msg' => '文章不存在']);
|
|
}
|
|
|
|
$like = LikeModel::where('uid', $uid)
|
|
->where('aid', $article_id)
|
|
->find();
|
|
|
|
if ($like) {
|
|
$like->delete();
|
|
$article->like_count = max(0, $article->like_count - 1);
|
|
$action = 'cancel';
|
|
} else {
|
|
$like = new LikeModel();
|
|
$like->uid = $uid;
|
|
$like->aid = $article_id;
|
|
$like->save();
|
|
$article->like_count += 1;
|
|
$action = 'add';
|
|
}
|
|
|
|
$article->save();
|
|
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => $action == 'add' ? '点赞成功' : '已取消点赞',
|
|
'count' => $article->like_count,
|
|
'action' => $action,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 收藏/取消收藏
|
|
*/
|
|
public function favorite()
|
|
{
|
|
$article_id = Request::param('article_id');
|
|
if (! $article_id) {
|
|
return json(['code' => 0, 'msg' => '参数错误']);
|
|
}
|
|
if (! $this->auth->isLogin) {
|
|
return json(['code' => 0, 'msg' => '请先登录']);
|
|
}
|
|
$uid = $this->auth->model->uid;
|
|
|
|
$article = ArticleModel::find($article_id);
|
|
if (! $article) {
|
|
return json(['code' => 0, 'msg' => '文章不存在']);
|
|
}
|
|
|
|
$favorite = FavoriteModel::where('uid', $uid)
|
|
->where('aid', $article_id)
|
|
->find();
|
|
|
|
if ($favorite) {
|
|
$favorite->delete();
|
|
$article->favorite_count = max(0, $favorite->favorite_count - 1);
|
|
$action = 'cancel';
|
|
} else {
|
|
$favorite = new FavoriteModel();
|
|
$favorite->uid = $uid;
|
|
$favorite->aid = $article_id;
|
|
$favorite->save();
|
|
$article->favorite_count += 1;
|
|
$action = 'add';
|
|
}
|
|
|
|
$article->save();
|
|
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => $action == 'add' ? '收藏成功' : '已取消收藏',
|
|
'count' => $article->favorite_count,
|
|
'action' => $action,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 分享计数 +1(无需登录)
|
|
*/
|
|
public function share()
|
|
{
|
|
$article_id = Request::param('article_id');
|
|
$article = ArticleModel::find($article_id);
|
|
if (! $article) {
|
|
return json(['code' => 0, 'msg' => '文章不存在']);
|
|
}
|
|
|
|
$article->share_count += 1;
|
|
$article->save();
|
|
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => '已记录分享',
|
|
'count' => $article->share_count,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取用户互动状态
|
|
*/
|
|
public function getUserInteractions($article_id)
|
|
{
|
|
$interactions = [
|
|
'liked' => false,
|
|
'favorited' => false,
|
|
];
|
|
|
|
if ($this->auth->isLogin) {
|
|
$uid = $this->auth->model->uid;
|
|
$interactions['liked'] = LikeModel::where('uid', $uid)
|
|
->where('aid', $article_id)
|
|
->count() > 0;
|
|
|
|
$interactions['favorited'] = FavoriteModel::where('uid', $uid)
|
|
->where('aid', $article_id)
|
|
->count() > 0;
|
|
}
|
|
|
|
return json([
|
|
'code' => 1,
|
|
'data' => $interactions,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取热门互动文章
|
|
*/
|
|
public function getHotInteractions()
|
|
{
|
|
$type = Request::param('type', 'like');
|
|
$limit = Request::param('limit', 10);
|
|
|
|
$field = $type == 'like' ? 'like_count' : 'favorite_count';
|
|
|
|
$articles = ArticleModel::with(['user', 'category'])
|
|
->where('status', 1)
|
|
->order($field, 'desc')
|
|
->limit($limit)
|
|
->select();
|
|
|
|
return json([
|
|
'code' => 1,
|
|
'data' => $articles,
|
|
]);
|
|
}
|
|
}
|