Files
YwxAppThink/addon/articles/service/LikeService.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

144 lines
4.3 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\articles\service;
use addon\articles\model\Article;
use addon\articles\model\ArticleComment;
use addon\articles\model\ArticleLike;
use think\facade\Db;
/**
* 点赞服务(文章 / 评论)
*
* 规则:
* - 必须登录(游客不允许点赞,避免刷量)
* - 同一用户对同一目标只能赞一次(唯一键保证),再次调用即取消
* - 计数冗余在目标表的 like_count,取消时用 dec 并保证不为负
*/
class LikeService
{
/**
* 文章点赞/取消
*
* @return array ['liked' => bool, 'count' => int]
*/
public static function toggleArticle(int $uid, int $articleId): array
{
if ($uid <= 0 || $articleId <= 0) {
return ['liked' => false, 'count' => 0];
}
// 文章必须存在且已发布
$exists = Article::where('id', $articleId)->where('status', 1)->where('delete_at', 0)->count();
if (!$exists) {
return ['liked' => false, 'count' => 0];
}
return self::toggle($uid, $articleId, ArticleLike::TYPE_ARTICLE, 'articles_article', $articleId);
}
/**
* 评论点赞/取消
*
* @return array ['liked' => bool, 'count' => int]
*/
public static function toggleComment(int $uid, int $commentId): array
{
if ($uid <= 0 || $commentId <= 0) {
return ['liked' => false, 'count' => 0];
}
$exists = ArticleComment::where('id', $commentId)->where('delete_at', 0)->count();
if (!$exists) {
return ['liked' => false, 'count' => 0];
}
return self::toggle($uid, $commentId, ArticleLike::TYPE_COMMENT, 'articles_comment', $commentId);
}
/**
* 是否已点赞
*/
public static function isLiked(int $uid, int $targetId, int $type): bool
{
if ($uid <= 0) {
return false;
}
return (bool) ArticleLike::where('uid', $uid)
->where('target_id', $targetId)
->where('type', $type)
->count();
}
/**
* 批量判断文章是否已赞(列表/详情页用,避免 N+1)
*
* @param array $ids 文章ID数组
* @return array 已赞的文章ID => true
*/
public static function likedArticleMap(int $uid, array $ids): array
{
if ($uid <= 0 || empty($ids)) {
return [];
}
$rows = ArticleLike::where('uid', $uid)
->where('type', ArticleLike::TYPE_ARTICLE)
->whereIn('target_id', $ids)
->column('target_id');
$map = [];
foreach ($rows as $id) {
$map[(int) $id] = true;
}
return $map;
}
/**
* 通用切换逻辑
*/
private static function toggle(int $uid, int $targetId, int $type, string $countTable, int $targetKey): array
{
$record = ArticleLike::where('uid', $uid)
->where('target_id', $targetId)
->where('type', $type)
->find();
if ($record) {
// 已赞 → 取消
$record->delete();
Db::name($countTable)->where('id', $targetKey)
->where('like_count', '>', 0)
->dec('like_count')
->update();
return ['liked' => false, 'count' => self::count($countTable, $targetKey)];
}
ArticleLike::create([
'uid' => $uid,
'target_id' => $targetId,
'type' => $type,
'create_at' => time(),
]);
Db::name($countTable)->where('id', $targetKey)->inc('like_count')->update();
return ['liked' => true, 'count' => self::count($countTable, $targetKey)];
}
/**
* 读取最新计数
*/
private static function count(string $table, int $id): int
{
return (int) Db::name($table)->where('id', $id)->value('like_count');
}
}