Files
YwxAppThink/addon/blog/controller/Interaction.php
T
yangwx 18101cf11a feat: 上线前安全加固与发布工程化;appmall 由插件迁移为独立应用
安全(P0)
- 发布包签名:新增 scripts/package_keygen.php、scripts/package_verify.php、
  ywxapp\service\PackageVerifier;FrameworkService::apply() 解压前强制验签
  (ywxapp.framework_package_verify,默认开启)
- JWT 收紧:config/jwt.php 移除公开默认密钥,JwtService::assertStrongSecret()
  校验非空 / 长度≥32 / 已知弱值;.env.production.example 键名由 JWT_SECRET_KEY 修正为 JWT_SECRET
- /api/v1/Article:免登录白名单收窄为只读接口,写操作字段白名单 + assertOwner() 越权校验
- 开发者令牌:查找接口不再回显明文、统一失败文案 + 限流
- TemplateInstaller:文件名白名单 + realpath 归一化 + 目录内断言,修复 Zip Slip;补 use ZipArchive
- 后台登录:接入验证码、失败锁定与登录限流(config/ratelimit.php)
- 缓存 / Session 驱动可由 .env 覆盖(CACHE_DRIVER / SESSION_TYPE),集群部署改 redis
- paycenter 支付日志开关改为 PAY_LOG,不再跟随 APP_DEBUG

工程化 / 交付
- 新增 CHANGELOG.md 与 .env.production.example;composer.lock 入库锁定依赖版本
- 插件离线安装 CLI 增加 --uid,后台上传安装透传 uid,付费插件授权校验可通过
- 授权拒绝文案区分「未携带 uid」与「未找到有效授权」,便于排障
- .gitignore 放行 .env.production.example / .env.addon.example / .gitattributes / public/.htaccess

结构
- appmall 由 addon/appmall 迁移为独立应用 app/appmall(市场 / 开发者中心 / 审核 / 分润)
2026-10-22 00:53:39 +08:00

195 lines
5.5 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();
// 注意:取文章的收藏数($favorite 是收藏记录模型,没有 favorite_count 列)
$article->favorite_count = max(0, (int) $article->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,
]);
}
}