游戏-渠道管理-优化样式增强验证,新增关联删除

This commit is contained in:
2026-04-03 17:49:46 +08:00
parent 6b830f4e25
commit 28bd9f1a09
7 changed files with 578 additions and 26 deletions

View File

@@ -36,7 +36,7 @@ class Channel extends Backend
* adminTree 为辅助接口,默认权限节点名 game/channel/admintree 往往未在后台录入;
* 与列表权限 game/channel/index 对齐,避免子管理员已勾「渠道管理」仍 401。
*/
protected array $noNeedPermission = ['adminTree'];
protected array $noNeedPermission = ['adminTree', 'deleteRelatedCounts'];
protected function initController(WebmanRequest $request): ?Response
{
@@ -44,6 +44,25 @@ class Channel extends Backend
return null;
}
/**
* 列表;附带 delete_related_counts=1 时返回删除前关联数据统计(走与 index 相同的路由入口,避免单独 URL 在部分环境下 404
*
* @throws Throwable
*/
public function index(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
$delPreview = $request->get('delete_related_counts');
if ($delPreview === '1' || $delPreview === 1 || $delPreview === true) {
return $this->deleteRelatedCountsResponse($request);
}
return $this->_index();
}
/**
* 渠道-管理员树(父级=渠道,子级=管理员,仅可选择子级)
*/
@@ -200,6 +219,20 @@ class Channel extends Backend
return $this->error($e->getMessage());
}
if ($result !== false) {
$newChannelId = $this->resolveNewChannelIdAfterInsert($data);
if (!$this->isPositiveChannelId($newChannelId)) {
$code = $data['code'] ?? null;
if (is_string($code) && trim($code) !== '') {
$newChannelId = Db::name('game_channel')->where('code', trim($code))->order('id', 'desc')->value('id');
}
}
if ($this->isPositiveChannelId($newChannelId)) {
try {
$this->copyGameConfigFromChannelZero($newChannelId);
} catch (Throwable $e) {
return $this->error(__('Game channel copy default config failed') . ': ' . $e->getMessage());
}
}
return $this->success(__('Added successfully'));
}
return $this->error(__('No rows were added'));
@@ -316,13 +349,282 @@ class Channel extends Backend
->order($order)
->paginate($limit);
$list = $this->buildChannelListWithRealtimeUserCounts($res->items());
return $this->success('', [
'list' => $res->items(),
'list' => $list,
'total' => $res->total(),
'remark' => get_route_remark(),
]);
}
/**
* 列表 user_count 按 game_user.game_channel_id 实时 COUNT与库字段无关用户增删改时会回写 game_channel.user_count
*
* @param iterable<int|string, mixed> $items
* @return list<array<string, mixed>>
*/
private function buildChannelListWithRealtimeUserCounts(iterable $items): array
{
$rows = [];
foreach ($items as $item) {
$rows[] = is_array($item) ? $item : $item->toArray();
}
if ($rows === []) {
return [];
}
$ids = [];
foreach ($rows as $r) {
if (isset($r['id'])) {
$ids[] = $r['id'];
}
}
if ($ids === []) {
return $rows;
}
$agg = Db::name('game_user')
->where('game_channel_id', 'in', $ids)
->field('game_channel_id, count(*) as cnt')
->group('game_channel_id')
->select()
->toArray();
$countMap = [];
foreach ($agg as $a) {
$countMap[$a['game_channel_id']] = (int) $a['cnt'];
}
foreach ($rows as &$r) {
$cid = $r['id'] ?? null;
$r['user_count'] = ($cid !== null && $cid !== '') ? ($countMap[$cid] ?? 0) : 0;
}
unset($r);
return $rows;
}
/**
* 删除前统计:与当前选中渠道相关的游戏配置条数、游戏用户条数(须具备 game/channel/del
*
* @throws Throwable
*/
public function deleteRelatedCounts(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
return $this->deleteRelatedCountsResponse($request);
}
/**
* @throws Throwable
*/
private function deleteRelatedCountsResponse(WebmanRequest $request): Response
{
if (!$this->auth->check('game/channel/del')) {
return $this->error(__('You have no permission'));
}
$channelIds = $this->getAuthorizedChannelIdsForIncomingIds($request);
if ($channelIds === []) {
return $this->success('', [
'game_config_count' => 0,
'game_user_count' => 0,
]);
}
// 实时统计game_config.channel_id、game_user.game_channel_id与渠道 id 一致)
$configCount = Db::name('game_config')->where('channel_id', 'in', $channelIds)->count();
$userCount = Db::name('game_user')->where('game_channel_id', 'in', $channelIds)->count();
return $this->success('', [
'game_config_count' => $configCount,
'game_user_count' => $userCount,
]);
}
/**
* 删除渠道:若存在关联的游戏配置或用户,须带 confirm_cascade=1同时级联删除关联数据
*
* @throws Throwable
*/
protected function _del(): Response
{
$where = [];
$dataLimitAdminIds = $this->getDataLimitAdminIds();
if ($dataLimitAdminIds) {
$where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
}
$ids = $this->request ? ($this->request->post('ids') ?? $this->request->get('ids') ?? []) : [];
if (!is_array($ids)) {
$ids = $ids !== null && $ids !== '' ? [$ids] : [];
}
if ($ids === []) {
return $this->error(__('Parameter error'));
}
$pk = $this->model->getPk();
$where[] = [$pk, 'in', $ids];
$data = $this->model->where($where)->select();
if (count($data) === 0) {
return $this->error(__('No rows were deleted'));
}
$channelIds = [];
foreach ($data as $v) {
$channelIds[] = $v[$pk];
}
// 删除确认用实时条数game_config.channel_id、game_user.game_channel_id
$configCount = Db::name('game_config')->where('channel_id', 'in', $channelIds)->count();
$userCount = Db::name('game_user')->where('game_channel_id', 'in', $channelIds)->count();
$confirmCascade = $this->request->get('confirm_cascade');
if ($confirmCascade === null || $confirmCascade === '') {
$confirmCascade = $this->request->post('confirm_cascade');
}
$confirmed = $confirmCascade === 1 || $confirmCascade === '1' || $confirmCascade === true;
if (($configCount > 0 || $userCount > 0) && !$confirmed) {
return $this->error(__('Game channel delete need confirm related'), [
'need_confirm' => true,
'game_config_count' => $configCount,
'game_user_count' => $userCount,
]);
}
$count = 0;
$this->model->startTrans();
try {
Db::name('game_config')->where('channel_id', 'in', $channelIds)->delete();
Db::name('game_user')->where('game_channel_id', 'in', $channelIds)->delete();
foreach ($data as $v) {
$count += $v->delete();
}
$this->model->commit();
} catch (Throwable $e) {
$this->model->rollback();
return $this->error($e->getMessage());
}
if ($count) {
return $this->success(__('Deleted successfully'));
}
return $this->error(__('No rows were deleted'));
}
/**
* @param list<string|int> $ids
* @return list<int|string>
*/
private function getAuthorizedChannelIdsForIncomingIds(WebmanRequest $request): array
{
$where = [];
$dataLimitAdminIds = $this->getDataLimitAdminIds();
if ($dataLimitAdminIds) {
$where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
}
$ids = $request->post('ids') ?? $request->get('ids') ?? [];
if (!is_array($ids)) {
$ids = $ids !== null && $ids !== '' ? [$ids] : [];
}
if ($ids === []) {
return [];
}
$pk = $this->model->getPk();
$where[] = [$pk, 'in', $ids];
return $this->model->where($where)->column($pk);
}
/**
* ThinkORM 在连接池/部分驱动下insert 后 getKey() 可能未及时带上自增 id这里多路兜底
*
* @param array<string, mixed> $postedChannelData 已过滤后的入库数据(含 code 等)
*/
private function resolveNewChannelIdAfterInsert(array $postedChannelData): int|string|null
{
$pk = $this->model->getPk();
$id = $this->model->getKey();
if ($this->isPositiveChannelId($id)) {
return $id;
}
$rowData = $this->model->getData();
if (is_array($rowData)) {
if (isset($rowData[$pk]) && $this->isPositiveChannelId($rowData[$pk])) {
return $rowData[$pk];
}
if (isset($rowData['id']) && $this->isPositiveChannelId($rowData['id'])) {
return $rowData['id'];
}
}
$lastInsId = $this->model->db()->getLastInsID();
if ($this->isPositiveChannelId($lastInsId)) {
return $lastInsId;
}
$lastInsId2 = Db::name('game_channel')->getLastInsID();
if ($this->isPositiveChannelId($lastInsId2)) {
return $lastInsId2;
}
$code = $postedChannelData['code'] ?? null;
if (is_string($code) && trim($code) !== '') {
$found = Db::name('game_channel')->where('code', trim($code))->order('id', 'desc')->value('id');
if ($this->isPositiveChannelId($found)) {
return $found;
}
}
return null;
}
private function isPositiveChannelId(mixed $id): bool
{
if ($id === null || $id === '') {
return false;
}
if (is_numeric($id)) {
return $id > 0;
}
return false;
}
/**
* 新建渠道后:将 channel_id=0 的全局默认游戏配置复制一份channel_id 指向新渠道主键
*
* @param int|string $newChannelId 新建 game_channel.id
*/
private function copyGameConfigFromChannelZero(int|string $newChannelId): void
{
$exists = Db::name('game_config')->where('channel_id', $newChannelId)->count();
if ($exists > 0) {
return;
}
// 全局模板channel_id 为 0 或字符串 '0'(与业务约定一致)
$rows = Db::name('game_config')
->whereIn('channel_id', [0, '0'])
->select()
->toArray();
if ($rows === []) {
return;
}
$now = time();
foreach ($rows as $row) {
foreach (['ID', 'id', 'Id'] as $pkField) {
unset($row[$pkField]);
}
$row['channel_id'] = $newChannelId;
$row['create_time'] = $now;
$row['update_time'] = $now;
Db::name('game_config')->strict(false)->insert($row);
}
}
/**
* 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
*/

View File

@@ -37,6 +37,7 @@ return [
'Topic format error' => '上传存储子目录格式错误!',
'Driver %s not supported' => '不支持的驱动:%s',
'Unknown' => '未知',
'Global default' => '全局默认channel_id=0',
// 权限类语言包-s
'Super administrator' => '超级管理员',
'No permission' => '无权限',

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace app\common\service;
use support\think\Db;
/**
* 按 game_user 表统计各渠道用户数,回写 game_channel.user_count
*/
class GameChannelUserCount
{
/**
* 统计 game_user.game_channel_id = 该渠道 id 的行数,更新 game_channel.user_count
*/
public static function syncFromGameUser(int|string|null $channelId): void
{
if ($channelId === null || $channelId === '') {
return;
}
if (is_numeric($channelId) && (float) $channelId < 1) {
return;
}
$count = Db::name('game_user')->where('game_channel_id', $channelId)->count();
Db::name('game_channel')->where('id', $channelId)->update(['user_count' => $count]);
}
/**
* @param list<int|string|null> $channelIds
*/
public static function syncChannels(array $channelIds): void
{
$seen = [];
foreach ($channelIds as $cid) {
if ($cid === null || $cid === '') {
continue;
}
$k = (string) $cid;
if (isset($seen[$k])) {
continue;
}
$seen[$k] = true;
self::syncFromGameUser($cid);
}
}
}

View File

@@ -1,4 +1,7 @@
export default {
delete_confirm_title: 'Delete channel',
delete_confirm_related:
'This will also delete {countConfig} game config row(s) and {countUser} game user row(s) under this channel. This cannot be undone. Continue?',
id: 'id',
code: 'code',
name: 'name',

View File

@@ -1,4 +1,7 @@
export default {
delete_confirm_title: '删除渠道',
delete_confirm_related:
'将同时删除该渠道下 {countConfig} 条游戏配置、{countUser} 条游戏用户数据,此操作不可恢复。确定删除所选渠道吗?',
id: 'ID',
code: '渠道标识',
name: '渠道名',

View File

@@ -22,12 +22,15 @@
<script setup lang="ts">
import { onMounted, provide, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'
import { ElMessageBox } from 'element-plus'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import createAxios from '/@/utils/axios'
import { adminBaseRoutePath } from '/@/router/static/adminBase'
defineOptions({
name: 'game/channel',
@@ -40,8 +43,10 @@ const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])
/**
* baTable 内包含了表格的所有数据且数据具备响应性,然后通过 provide 注入给了后代组件
*/
const channelApiBase = `${adminBaseRoutePath}/game.Channel/`
const baTable = new baTableClass(
new baTableApi('/admin/game.Channel/'),
new baTableApi(channelApiBase),
{
pk: 'id',
column: [
@@ -127,6 +132,46 @@ const baTable = new baTableClass(
provide('baTable', baTable)
baTable.postDel = (ids: string[]) => {
if (baTable.runBefore('postDel', { ids }) === false) return
void (async () => {
try {
const stats = await createAxios(
{
url: channelApiBase + 'index',
method: 'get',
params: { delete_related_counts: '1', ids },
},
{ showSuccessMessage: false }
)
const counts = stats.data as { game_config_count?: number; game_user_count?: number }
const countConfig = Number(counts?.game_config_count ?? 0)
const countUser = Number(counts?.game_user_count ?? 0)
await ElMessageBox.confirm(
t('game.channel.delete_confirm_related', { countConfig, countUser }),
t('game.channel.delete_confirm_title'),
{
type: 'warning',
confirmButtonText: t('Confirm'),
cancelButtonText: t('Cancel'),
}
)
const delRes = await createAxios(
{
url: channelApiBase + 'del',
method: 'DELETE',
params: { ids, confirm_cascade: 1 },
},
{ showSuccessMessage: true }
)
baTable.onTableHeaderAction('refresh', { event: 'delete', ids })
baTable.runAfter('postDel', { res: delRes })
} catch {
// 用户取消或接口失败axios 已提示)
}
})()
}
onMounted(() => {
baTable.table.ref = tableRef.value
baTable.mount()

View File

@@ -47,11 +47,11 @@
/>
<FormItem
:label="t('game.config.name')"
type="string"
type="select"
v-model="baTable.form.items!.name"
prop="name"
:input-attr="{ disabled: metaFieldsDisabled }"
:placeholder="t('Please input field', { field: t('game.config.name') })"
:input-attr="{ content: nameSelectContent, disabled: metaFieldsDisabled }"
:placeholder="t('Please select field', { field: t('game.config.name') })"
/>
<FormItem
:label="t('game.config.title')"
@@ -79,6 +79,7 @@
class="weight-val"
:placeholder="t('Please input field', { field: t('game.config.weight value') })"
clearable
:disabled="isDefaultBigwinWeight && isBigwinDiceLockedKey(row.key)"
@input="onWeightRowChange"
/>
<el-button v-if="canEditWeightStructure" type="danger" link @click="removeWeightRow(idx)">
@@ -86,6 +87,7 @@
</el-button>
</div>
<el-button v-if="canEditWeightStructure" type="primary" link @click="addWeightRow">{{ t('Add') }}</el-button>
<div v-if="isDefaultBigwinWeight" class="form-help">{{ t('game.config.default_bigwin_weight_help') }}</div>
</div>
</el-form-item>
<FormItem
@@ -137,6 +139,17 @@ import { useConfig } from '/@/stores/config'
import { useAdminInfo } from '/@/stores/adminInfo'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import {
fixedRowsFromKeys,
getFixedKeysForGameConfigName,
isBigwinDiceLockedKey,
jsonStringFromFixedKeys,
normalizeGameWeightConfigName,
parseWeightJsonToMap,
rowsToMap,
weightRowsMatchBigwinDiceKeys,
type WeightRow,
} from '/@/utils/gameWeightFixed'
const config = useConfig()
const formRef = useTemplateRef('formRef')
@@ -169,21 +182,56 @@ const groupSelectContentFiltered = computed(() => {
return groupSelectBase
})
/** game_weight编辑或非超管时键只读仅超管新增时可增删行、改键 */
/** default_tier_weight / default_bigwin_weight及 default_kill_score_weight键固定仅值可改可增删行 */
const isFixedGameWeightConfig = computed(() => getFixedKeysForGameConfigName(baTable.form.items?.name) !== null)
/** game_weight编辑或非超管时键只读仅超管新增非固定项时可增删行、改键 */
const weightKeyReadonly = computed(() => {
if (!isGameWeight.value) return false
if (isFixedGameWeightConfig.value) return true
if (baTable.form.operate === 'Edit') return true
return !isSuperAdmin.value
})
const canEditWeightStructure = computed(() => isGameWeight.value && baTable.form.operate === 'Add' && isSuperAdmin.value)
const canEditWeightStructure = computed(
() => isGameWeight.value && baTable.form.operate === 'Add' && isSuperAdmin.value && !isFixedGameWeightConfig.value
)
type WeightRow = { key: string; val: string }
/** 默认大奖权重:仅校验每项整数与 0100005/30 固定 10000不参与 tier/kill 的「和≤100」 */
const isDefaultBigwinWeight = computed(() => normalizeGameWeightConfigName(baTable.form.items?.name) === 'default_bigwin_weight')
const weightRows = ref<WeightRow[]>([{ key: '', val: '' }])
/** default_tier_weight / default_kill_score_weight每项≤100且权重之和必须=100 */
const WEIGHT_SUM100_NAMES = ['default_tier_weight', 'default_kill_score_weight']
/** 配置标识:按分组限定可选项;编辑时若库中旧值不在列表中则临时追加一条 */
const nameSelectContent = computed((): Record<string, string> => {
const g = baTable.form.items?.group
let base: Record<string, string> = {}
if (g === 'game_config') {
base = {
game_rule: t('game.config.name opt game_rule'),
game_rule_en: t('game.config.name opt game_rule_en'),
}
} else if (g === 'game_weight') {
base = {
default_tier_weight: t('game.config.name opt default_tier_weight'),
default_kill_score_weight: t('game.config.name opt default_kill_score_weight'),
default_bigwin_weight: t('game.config.name opt default_bigwin_weight'),
}
}
const n = baTable.form.items?.name
if (typeof n === 'string' && n.trim() !== '' && base[n] === undefined) {
const norm = normalizeGameWeightConfigName(n)
if (norm !== '' && base[norm] !== undefined) {
return { ...base, [n]: base[norm] }
}
return { ...base, [n]: n }
}
return base
})
const isGameWeight = computed(() => baTable.form.items?.group === 'game_weight')
function parseValueToWeightRows(raw: unknown): WeightRow[] {
@@ -236,11 +284,29 @@ function weightRowsToJsonString(rows: WeightRow[]): string {
function syncWeightRowsToFormValue() {
const items = baTable.form.items
if (!items) return
const fixedKeys = getFixedKeysForGameConfigName(items.name)
if (fixedKeys) {
const map = rowsToMap(weightRows.value)
items.value = jsonStringFromFixedKeys(fixedKeys, map)
return
}
items.value = weightRowsToJsonString(weightRows.value)
}
function enforceDefaultBigwinLockedValues() {
if (normalizeGameWeightConfigName(baTable.form.items?.name) !== 'default_bigwin_weight') {
return
}
for (const r of weightRows.value) {
if (isBigwinDiceLockedKey(r.key)) {
r.val = '10000'
}
}
}
function onWeightRowChange() {
if (isGameWeight.value) {
enforceDefaultBigwinLockedValues()
syncWeightRowsToFormValue()
}
}
@@ -263,6 +329,14 @@ function removeWeightRow(idx: number) {
function hydrateWeightRowsFromForm() {
if (!isGameWeight.value) return
const fixedKeys = getFixedKeysForGameConfigName(baTable.form.items?.name)
if (fixedKeys) {
const map = parseWeightJsonToMap(baTable.form.items?.value)
weightRows.value = fixedRowsFromKeys(fixedKeys, map)
enforceDefaultBigwinLockedValues()
syncWeightRowsToFormValue()
return
}
weightRows.value = parseValueToWeightRows(baTable.form.items?.value)
}
@@ -283,6 +357,27 @@ watch(
watch(
() => baTable.form.items?.group,
() => {
if (baTable.form.items?.group === 'game_weight') {
hydrateWeightRowsFromForm()
}
const items = baTable.form.items
if (!items || !isSuperAdmin.value) {
return
}
const c = nameSelectContent.value
const keys = Object.keys(c)
if (keys.length === 0) {
return
}
if (typeof items.name !== 'string' || items.name === '' || c[items.name] === undefined) {
items.name = keys[0]
}
}
)
watch(
() => baTable.form.items?.name,
() => {
if (baTable.form.items?.group === 'game_weight') {
hydrateWeightRowsFromForm()
@@ -294,28 +389,78 @@ function validateGameWeightRules(): string | undefined {
if (baTable.form.items?.group !== 'game_weight') {
return undefined
}
const name = baTable.form.items?.name ?? ''
const configName = normalizeGameWeightConfigName(baTable.form.items?.name)
const fixedKeys = getFixedKeysForGameConfigName(configName)
const nums: number[] = []
for (const r of weightRows.value) {
const k = r.key.trim()
if (k === '') continue
const vs = r.val.trim()
if (vs === '') {
return t('Please input field', { field: t('game.config.weight value') })
if (fixedKeys) {
const map = rowsToMap(weightRows.value)
if (configName === 'default_bigwin_weight') {
for (const k of fixedKeys) {
const vs = (map[k] ?? '').trim()
if (vs === '') {
return t('Please input field', { field: t('game.config.weight value') })
}
const n = Number(vs)
if (!Number.isFinite(n)) {
return t('game.config.weight value numeric')
}
if (isBigwinDiceLockedKey(k)) {
if (n !== 10000) {
return t('game.config.bigwin weight locked 5 30')
}
} else if (n < 0 || n > 10000) {
return t('game.config.bigwin weight each 0 10000')
}
nums.push(n)
}
} else {
for (const k of fixedKeys) {
const vs = (map[k] ?? '').trim()
if (vs === '') {
return t('Please input field', { field: t('game.config.weight value') })
}
const n = Number(vs)
if (!Number.isFinite(n)) {
return t('game.config.weight value numeric')
}
if (n > 100) {
return t('game.config.weight each max 100')
}
nums.push(n)
}
}
const n = Number(vs)
if (!Number.isFinite(n)) {
return t('game.config.weight value numeric')
} else {
// 非固定键名但行结构已是 530 骰子时,按大奖 010000 校验(避免 name 格式异常时误走每项≤10000
const treatAsBigwin = configName === 'default_bigwin_weight' || weightRowsMatchBigwinDiceKeys(weightRows.value)
for (const r of weightRows.value) {
const k = r.key.trim()
if (k === '') continue
const vs = r.val.trim()
if (vs === '') {
return t('Please input field', { field: t('game.config.weight value') })
}
const n = Number(vs)
if (!Number.isFinite(n)) {
return t('game.config.weight value numeric')
}
if (treatAsBigwin) {
if (isBigwinDiceLockedKey(k)) {
if (n !== 10000) {
return t('game.config.bigwin weight locked 5 30')
}
} else if (n < 0 || n > 10000) {
return t('game.config.bigwin weight each 0 10000')
}
} else if (n > 10000) {
return t('game.config.weight each max 10000')
}
nums.push(n)
}
if (n > 100) {
return t('game.config.weight each max 100')
if (nums.length === 0) {
return t('Please input field', { field: t('game.config.value') })
}
nums.push(n)
}
if (nums.length === 0) {
return t('Please input field', { field: t('game.config.value') })
}
if (WEIGHT_SUM100_NAMES.includes(name)) {
if (WEIGHT_SUM100_NAMES.includes(configName)) {
let sum = 0
for (const x of nums) {
sum += x
@@ -372,4 +517,10 @@ const rules: Partial<Record<string, FormItemRule[]>> = reactive({
flex-shrink: 0;
color: var(--el-text-color-secondary);
}
.form-help {
margin-top: 8px;
font-size: 12px;
line-height: 18px;
color: var(--el-text-color-secondary);
}
</style>