feat: 支持生成全部项目通用卡 (project_id=0)

This commit is contained in:
root committed 2026-10-12 15:02:58 +08:00
1 parent b4a20080dd
commit cf65cbd72d
5 files changed
+132 -50

No files matched your search

+34 -17
View File
@@ -96,6 +96,7 @@ class CardController extends Controller
/**
* 批量生成卡密
* project_id=0 表示全部项目通用卡(仅 admin)
*/
public function generate(): void
{
@@ -115,26 +116,42 @@ class CardController extends Controller
$remark = $input['remark'] ?? '';
$db = Database::getInstance();
// 验证项目存在
$project = $db->fetch("SELECT id FROM {$db->table('projects')} WHERE id = ? AND deleted_at IS NULL", [$projectId]);
if (!$project) {
$this->error('项目不存在');
}
// 验证卡密类型
$cardType = $db->fetch(
"SELECT * FROM {$db->table('card_types')} WHERE id = ? AND project_id = ?",
[$cardTypeId, $projectId]
);
if (!$cardType) {
$this->error('卡密类型不存在');
}
$userId = $this->getUserId();
$userRole = $this->getUserRole();
// 项目管理员:只能在自己负责的项目中生成卡密
// 全部项目通用卡(project_id=0)仅超级管理员
$isUniversal = ($projectId === 0);
if ($isUniversal && $userRole !== 'admin') {
$this->error('通用卡(全部项目)仅超级管理员可生成', 403);
}
if ($isUniversal) {
// 通用卡:按 id 取任意项目的套餐定义时长与名称
$cardType = $db->fetch(
"SELECT * FROM {$db->table('card_types')} WHERE id = ?",
[$cardTypeId]
);
if (!$cardType) {
$this->error('套餐不存在');
}
} else {
// 验证项目存在
$project = $db->fetch("SELECT id FROM {$db->table('projects')} WHERE id = ? AND deleted_at IS NULL", [$projectId]);
if (!$project) {
$this->error('项目不存在');
}
// 验证卡密类型
$cardType = $db->fetch(
"SELECT * FROM {$db->table('card_types')} WHERE id = ? AND project_id = ?",
[$cardTypeId, $projectId]
);
if (!$cardType) {
$this->error('卡密类型不存在');
}
}
// 项目管理员:只能在自己负责的项目中生成卡密(通用卡已在上方拦截)
if ($userRole === 'project_admin') {
$currentUser = $this->getCurrentUser();
$projectIds = $currentUser['project_ids'] ?? [];