feat: 支持生成全部项目通用卡 (project_id=0)
This commit is contained in:
1 parent
b4a20080dd
commit
cf65cbd72d
5 files changed
+132
-50
No files matched your search
@@ -319,6 +319,25 @@ class AuthorizationController extends Controller
|
||||
$this->error('卡密不存在');
|
||||
}
|
||||
|
||||
// 通用卡(project_id=0):授权归属到请求指定的项目
|
||||
$reqProjectId = (int) ($input['project_id'] ?? 0);
|
||||
$authProjectId = (int) $card['project_id'];
|
||||
$authProjectName = $card['project_name'] ?? '';
|
||||
if ($authProjectId === 0) {
|
||||
if ($reqProjectId <= 0) {
|
||||
$this->error('通用卡激活请传入 project_id 指定项目');
|
||||
}
|
||||
$proj = $db->fetch(
|
||||
"SELECT name FROM {$db->table('projects')} WHERE id = ? AND deleted_at IS NULL",
|
||||
[$reqProjectId]
|
||||
);
|
||||
if (!$proj) {
|
||||
$this->error('指定的项目不存在');
|
||||
}
|
||||
$authProjectId = $reqProjectId;
|
||||
$authProjectName = $proj['name'];
|
||||
}
|
||||
|
||||
if ($card['status'] === 'disabled') {
|
||||
$this->error('该卡密已被禁用');
|
||||
}
|
||||
@@ -375,8 +394,8 @@ class AuthorizationController extends Controller
|
||||
// 如果当前用户是代理,使用此类卡密需按代理价格扣费
|
||||
if ($currentRole === 'agent' && $currentUserId > 0) {
|
||||
$cardTypeForCost = $db->fetch(
|
||||
"SELECT agent_cost FROM {$db->table('card_types')} WHERE id = ? AND project_id = ?",
|
||||
[$card['card_type_id'], $card['project_id']]
|
||||
"SELECT agent_cost FROM {$db->table('card_types')} WHERE id = ?",
|
||||
[$card['card_type_id']]
|
||||
);
|
||||
|
||||
if (!$cardTypeForCost || (float) $cardTypeForCost['agent_cost'] <= 0) {
|
||||
@@ -403,10 +422,10 @@ class AuthorizationController extends Controller
|
||||
// 检查当前机器人QQ在同一项目下是否已有授权(未撤销),有则续费
|
||||
$existingAuth = $db->fetch(
|
||||
"SELECT * FROM {$db->table('authorizations')}
|
||||
WHERE project_id = ? AND bot_qq = ? AND status != 'revoked'
|
||||
ORDER BY expire_time IS NULL DESC, expire_time DESC, id DESC
|
||||
LIMIT 1",
|
||||
[$card['project_id'], $botQq]
|
||||
WHERE project_id = ? AND bot_qq = ? AND status != 'revoked'
|
||||
ORDER BY expire_time IS NULL DESC, expire_time DESC, id DESC
|
||||
LIMIT 1",
|
||||
[$authProjectId, $botQq]
|
||||
);
|
||||
|
||||
$agentId = (int) ($card['use_user_id'] ?? 0);
|
||||
@@ -459,8 +478,8 @@ class AuthorizationController extends Controller
|
||||
[
|
||||
$card['id'],
|
||||
$card['card_key'],
|
||||
$card['project_id'],
|
||||
$card['project_name'] ?? '',
|
||||
$authProjectId,
|
||||
$authProjectName,
|
||||
$botQq,
|
||||
$contactQq,
|
||||
$contactName,
|
||||
@@ -495,8 +514,8 @@ class AuthorizationController extends Controller
|
||||
'bot_qq' => $botQq,
|
||||
'contact_qq' => $contactQq,
|
||||
'contact_name'=> $contactName,
|
||||
'project_id' => (int) $card['project_id'],
|
||||
'project_name'=> $card['project_name'] ?? '',
|
||||
'project_id' => $authProjectId,
|
||||
'project_name'=> $authProjectName,
|
||||
'expire_time' => $newExpireTime,
|
||||
'is_renew' => true,
|
||||
], '续费成功');
|
||||
@@ -513,8 +532,8 @@ class AuthorizationController extends Controller
|
||||
[
|
||||
$card['id'],
|
||||
$card['card_key'],
|
||||
$card['project_id'],
|
||||
$card['project_name'] ?? '',
|
||||
$authProjectId,
|
||||
$authProjectName,
|
||||
$botQq,
|
||||
$contactQq,
|
||||
$contactName,
|
||||
@@ -553,8 +572,8 @@ class AuthorizationController extends Controller
|
||||
'bot_qq' => $botQq,
|
||||
'contact_qq' => $contactQq,
|
||||
'contact_name'=> $contactName,
|
||||
'project_id' => (int) $card['project_id'],
|
||||
'project_name'=> $card['project_name'] ?? '',
|
||||
'project_id' => $authProjectId,
|
||||
'project_name'=> $authProjectName,
|
||||
'expire_time' => $expireTime,
|
||||
'is_renew' => false,
|
||||
], '授权成功');
|
||||
|
||||
@@ -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'] ?? [];
|
||||
|
||||
@@ -639,11 +639,19 @@ class PublicController extends Controller
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
// 优先匹配本项目卡密;未命中则尝试通用卡(project_id=0)或其他项目兼容
|
||||
$card = $db->fetch(
|
||||
"SELECT * FROM {$db->table('cards')} WHERE card_key = ? AND project_id = ?",
|
||||
[$cardKey, $projectId]
|
||||
);
|
||||
|
||||
if (!$card) {
|
||||
$card = $db->fetch(
|
||||
"SELECT * FROM {$db->table('cards')} WHERE card_key = ? AND project_id = 0",
|
||||
[$cardKey]
|
||||
);
|
||||
}
|
||||
|
||||
if (!$card) {
|
||||
// 尝试查找跨项目卡密(兼容)
|
||||
$card = $db->fetch(
|
||||
|
||||
@@ -68,7 +68,11 @@ class Validator
|
||||
|
||||
private function ruleInteger(string $field, mixed $value, array $params): void
|
||||
{
|
||||
if ($value !== null && $value !== '' && !filter_var($value, FILTER_VALIDATE_INT)) {
|
||||
if ($value === null || $value === '') {
|
||||
return;
|
||||
}
|
||||
// 注意: filter_var(0, FILTER_VALIDATE_INT) === 0,不能用 ! 判断
|
||||
if (filter_var($value, FILTER_VALIDATE_INT) === false) {
|
||||
$this->addError($field, "{$field}必须是整数");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
<el-form :inline="true" :model="filters" class="search-form" label-width="80px">
|
||||
<el-form-item label="项目">
|
||||
<el-select v-model="filters.project_id" placeholder="全部项目" clearable style="width: 180px" @change="handleFilterChange">
|
||||
<el-option v-if="user?.role === 'admin'" label="通用卡(全部项目)" :value="0" />
|
||||
<el-option v-for="p in projects" :key="p.id" :label="p.name" :value="p.id" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
@@ -160,9 +161,14 @@
|
||||
</div>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="project_name" label="项目" width="130" sortable="custom" v-if="visibleColumns.project_name" />
|
||||
<el-table-column prop="project_name" label="项目" width="130" sortable="custom" v-if="visibleColumns.project_name">
|
||||
<template #default="{ row }">
|
||||
<el-tag v-if="Number(row.project_id) === 0" type="warning" size="small">通用</el-tag>
|
||||
<span v-else>{{ row.project_name || row.project_id }}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="card_type_name" label="类型" width="110" sortable="custom" v-if="visibleColumns.card_type_name" />
|
||||
<el-table-column prop="bot_qq" label="机器人QQ" width="140" sortable="custom" v-if="visibleColumns.bot_qq">
|
||||
<el-table-column prop="bot_qq" label="机器码" width="140" sortable="custom" v-if="visibleColumns.bot_qq">
|
||||
<template #default="{ row }">
|
||||
<span v-if="row.bot_qq" class="mono-text">{{ row.bot_qq }}</span>
|
||||
<span v-else class="text-muted">-</span>
|
||||
@@ -215,16 +221,20 @@
|
||||
<el-form ref="genFormRef" :model="genForm" :rules="genRules" label-width="100px">
|
||||
<el-form-item label="项目" prop="project_id">
|
||||
<el-select v-model="genForm.project_id" placeholder="请选择项目" style="width: 100%">
|
||||
<el-option v-if="user?.role === 'admin'" label="全部项目(通用卡)" :value="0" />
|
||||
<el-option v-for="p in projects" :key="p.id" :label="p.name" :value="p.id" />
|
||||
</el-select>
|
||||
<div v-if="genForm.project_id === 0" class="form-tip" style="margin-top:4px; font-size:12px; color:#909399;">
|
||||
通用卡可在所有项目的客户端/激活页使用;套餐仅用于确定时长与类型名
|
||||
</div>
|
||||
</el-form-item>
|
||||
<el-form-item label="套餐类型" prop="card_type_id">
|
||||
<el-select v-model="genForm.card_type_id" placeholder="请选择套餐" style="width: 100%" :disabled="!genForm.project_id">
|
||||
<el-option
|
||||
v-for="t in genCardTypes"
|
||||
:key="t.id"
|
||||
:label="isAgent ? `${t.name} (${t.duration_days == 0 ? '永久' : t.duration_days + '天'} / ¥${t.agent_cost})` : `${t.name} (${t.duration_days == 0 ? '永久' : t.duration_days + '天'} / ¥${t.price})`"
|
||||
:value="t.id"
|
||||
<el-select v-model="genForm.card_type_id" placeholder="请选择套餐" style="width: 100%" :loading="genTypesLoading" :disabled="genForm.project_id === '' || genForm.project_id === null">
|
||||
<el-option
|
||||
v-for="t in genCardTypes"
|
||||
:key="t.id"
|
||||
:label="isAgent ? `${t.name} (${t.duration_days == 0 ? '永久' : t.duration_days + '天'} / ¥${t.agent_cost})` : `${t.name} (${t.duration_days == 0 ? '永久' : t.duration_days + '天'} / ¥${t.price})${t._project ? ' @ ' + t._project : ''}`"
|
||||
:value="t.id"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
@@ -274,7 +284,10 @@
|
||||
</div>
|
||||
<div class="detail-row-group">
|
||||
<span class="detail-row-label">套餐与项目</span>
|
||||
<div class="detail-row"><span class="detail-row-key">项目</span><span class="detail-row-val">{{ detail.project_name || '-' }}</span></div>
|
||||
<div class="detail-row"><span class="detail-row-key">项目</span><span class="detail-row-val">
|
||||
<el-tag v-if="Number(detail.project_id) === 0" type="warning" size="small">全部项目(通用卡)</el-tag>
|
||||
<template v-else>{{ detail.project_name || '-' }}</template>
|
||||
</span></div>
|
||||
<div class="detail-row"><span class="detail-row-key">类型</span><span class="detail-row-val">{{ detail.card_type_name }}</span></div>
|
||||
<div class="detail-row"><span class="detail-row-key">有效天数</span><span class="detail-row-val"><span v-if="detail.duration_days == 0" class="detail-tag green">永久</span><span v-else class="detail-tag blue">{{ detail.duration_days }}天</span></span></div>
|
||||
<div class="detail-row"><span class="detail-row-key">过期时间</span><span class="detail-row-val">{{ detail.duration_days == 0 ? '永久有效' : (detail.expire_time || '-') }}</span></div>
|
||||
@@ -282,7 +295,7 @@
|
||||
</div>
|
||||
<div class="detail-row-group" v-if="detail.status !== 'unused'">
|
||||
<span class="detail-row-label">使用记录</span>
|
||||
<div class="detail-row"><span class="detail-row-key">机器人QQ</span><span class="detail-row-val mono">{{ detail.bot_qq || '-' }}</span></div>
|
||||
<div class="detail-row"><span class="detail-row-key">机器码</span><span class="detail-row-val mono">{{ detail.bot_qq || '-' }}</span></div>
|
||||
<div class="detail-row"><span class="detail-row-key">绑定时间</span><span class="detail-row-val">{{ detail.bound_at || '-' }}</span></div>
|
||||
</div>
|
||||
<div class="detail-row-group" v-if="detail.bind_info && Object.keys(detail.bind_info).length">
|
||||
@@ -396,7 +409,7 @@ const columnOptions = ref([
|
||||
{ prop: 'card_key', label: '卡密', visible: true },
|
||||
{ prop: 'project_name', label: '项目', visible: true },
|
||||
{ prop: 'card_type_name', label: '类型', visible: true },
|
||||
{ prop: 'bot_qq', label: '机器人QQ', visible: true },
|
||||
{ prop: 'bot_qq', label: '机器码', visible: true },
|
||||
{ prop: 'duration_days', label: '有效天数', visible: true },
|
||||
{ prop: 'status', label: '状态', visible: true },
|
||||
{ prop: 'expire_time', label: '过期时间', visible: true },
|
||||
@@ -422,6 +435,7 @@ const genRules = {
|
||||
}
|
||||
const genLoading = ref(false)
|
||||
const genCardTypes = ref([])
|
||||
const genTypesLoading = ref(false)
|
||||
const genCostPreview = computed(() => {
|
||||
if (!isAgent.value) return 0
|
||||
const selected = genCardTypes.value.find(t => t.id === genForm.value.card_type_id)
|
||||
@@ -581,20 +595,40 @@ function showGenerateDialog() {
|
||||
}
|
||||
|
||||
watch(() => genForm.value.project_id, async (pid) => {
|
||||
if (!pid) {
|
||||
if (pid === '' || pid === null || pid === undefined) {
|
||||
genCardTypes.value = []
|
||||
return
|
||||
}
|
||||
genTypesLoading.value = true
|
||||
try {
|
||||
const res = await request.get(`/projects/${pid}/card-types`)
|
||||
let types = res.data || []
|
||||
// 代理只能看到有代理价格的套餐
|
||||
if (isAgent.value) {
|
||||
types = types.filter(t => parseFloat(t.agent_cost) > 0)
|
||||
if (Number(pid) === 0) {
|
||||
// 通用卡:拉取所有项目的套餐
|
||||
const pres = await request.get('/projects/all')
|
||||
let projects = pres.data.list || pres.data || []
|
||||
if (!Array.isArray(projects)) projects = []
|
||||
const all = []
|
||||
for (const p of projects) {
|
||||
try {
|
||||
const tres = await request.get(`/projects/${p.id}/card-types`)
|
||||
const types = tres.data || []
|
||||
for (const t of types) all.push({ ...t, _project: p.name })
|
||||
} catch {}
|
||||
}
|
||||
genCardTypes.value = all
|
||||
} else {
|
||||
const res = await request.get(`/projects/${pid}/card-types`)
|
||||
let types = res.data || []
|
||||
// 代理只能看到有代理价格的套餐
|
||||
if (isAgent.value) {
|
||||
types = types.filter(t => parseFloat(t.agent_cost) > 0)
|
||||
}
|
||||
genCardTypes.value = types
|
||||
}
|
||||
genCardTypes.value = types
|
||||
} catch (e) {
|
||||
console.error('获取套餐列表失败:', e)
|
||||
genCardTypes.value = []
|
||||
} finally {
|
||||
genTypesLoading.value = false
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user