chore: 重写初始提交(清空历史,整理后全量提交)

This commit is contained in:
ywxapp
2026-08-16 16:54:14 +08:00
commit 6c1a106bc1
1808 changed files with 238144 additions and 0 deletions
+193
View File
@@ -0,0 +1,193 @@
<?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,
]);
}
}