[游戏管理]游戏对局

This commit is contained in:
2026-04-15 13:44:50 +08:00
parent ba80e7c392
commit 14b9920667
6 changed files with 632 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
<?php
namespace app\admin\controller\game;
use app\common\controller\Backend;
use app\common\service\GamePeriodService;
use support\Response;
use Throwable;
use Webman\Http\Request as WebmanRequest;
/**
* 游戏对局(期号)
*/
class Period extends Backend
{
protected ?object $model = null;
protected string|array $preExcludeFields = ['id', 'create_time', 'update_time'];
protected string|array $quickSearchField = ['id', 'period_no'];
protected string|array $defaultSortField = ['id' => 'desc'];
protected string|array $orderGuarantee = ['id' => 'desc'];
protected bool $modelValidate = false;
protected function initController(WebmanRequest $request): ?Response
{
$this->model = new \app\common\model\GamePeriod();
return null;
}
/**
* 读取 / 保存:自动创建下一期、手动创建下一期 开关(存 game_config
*/
public function periodSettings(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
$method = $request->method();
if ($method === 'GET') {
return $this->success('', GamePeriodService::getPeriodSettings());
}
if ($method === 'POST') {
$data = $request->post();
if (!is_array($data)) {
return $this->error(__('Parameter %s can not be empty', ['']));
}
try {
GamePeriodService::savePeriodSettings($data);
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
return $this->success(__('Saved successfully'));
}
return $this->error(__('Parameter error'));
}
/**
* 手动创建下一期(受开关与「存在进行中期号」约束)
*/
public function createNextManual(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if ($request->method() !== 'POST') {
return $this->error(__('Parameter error'));
}
$result = GamePeriodService::createNextPeriodForManual();
if ($result['ok']) {
return $this->success($result['msg'], ['period_no' => $result['period_no'] ?? '']);
}
return $this->error($result['msg']);
}
protected function _add(): Response
{
if ($this->request && $this->request->method() === 'POST') {
$data = $this->request->post();
if (!is_array($data)) {
return $this->error(__('Parameter %s can not be empty', ['']));
}
$data = $this->applyInputFilter($data);
$data = $this->excludeFields($data);
if (!isset($data['period_start_at']) || $data['period_start_at'] === '' || $data['period_start_at'] === null) {
$data['period_start_at'] = time();
}
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$data[$this->dataLimitField] = $this->auth->id;
}
$result = false;
$this->model->startTrans();
try {
$result = $this->model->save($data);
$this->model->commit();
} catch (Throwable $e) {
$this->model->rollback();
return $this->error($e->getMessage());
}
if ($result !== false) {
return $this->success(__('Added successfully'));
}
return $this->error(__('No rows were added'));
}
return $this->error(__('Parameter error'));
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace app\common\model;
use support\think\Model;
class GamePeriod extends Model
{
protected $name = 'game_period';
protected $autoWriteTimestamp = true;
protected $type = [
'create_time' => 'integer',
'update_time' => 'integer',
'period_start_at' => 'integer',
'status' => 'integer',
'draw_mode' => 'integer',
'preset_number' => 'integer',
'result_number' => 'integer',
];
/**
* 后台表单 datetime 可能提交字符串,入库前转为 Unix 时间戳
* ThinkPHP 修改器签名为 (mixed $value, array $data)
*/
public function setPeriodStartAtAttr($value, $data = [])
{
if ($value === null || $value === '') {
return 0;
}
if (is_int($value)) {
return $value;
}
if (is_string($value)) {
$t = strtotime($value);
if ($t !== false) {
return $t;
}
}
return 0;
}
}

View File

@@ -0,0 +1,28 @@
export default {
'quick Search Fields': 'Period No. / ID',
id: 'ID',
period_no: 'Period No.',
period_start_at: 'Start time',
status: 'Status',
'status 0': 'Betting open',
'status 1': 'Closed',
'status 2': 'Settling',
'status 3': 'Paying',
'status 4': 'Ended',
'status 5': 'Void',
draw_mode: 'Draw mode',
'draw_mode 0': 'Auto AI',
'draw_mode 1': 'Manual preset',
preset_number: 'Preset number',
result_number: 'Result number',
void_reason: 'Void reason',
create_time: 'Created',
update_time: 'Updated',
section_auto: 'Auto draw & new period',
auto_create_label: 'Allow auto-create next period',
auto_create_tip: 'When enabled, a background ticker inserts a new period if none is in progress',
manual_create_label: 'Allow manual create next period',
manual_create_tip: 'When enabled, the button below can create the next period',
btn_create_next: 'Create next period (manual)',
saving: 'Saving…',
}

View File

@@ -0,0 +1,28 @@
export default {
'quick Search Fields': '期号/ID',
id: 'ID',
period_no: '期号',
period_start_at: '开始时间',
status: '状态',
'status 0': '下注开放',
'status 1': '已封盘',
'status 2': '算票中',
'status 3': '派彩中',
'status 4': '已结束',
'status 5': '已作废',
draw_mode: '开奖方式',
'draw_mode 0': '自动AI',
'draw_mode 1': '手动预设',
preset_number: '预设号码',
result_number: '开奖号码',
void_reason: '作废原因',
create_time: '创建时间',
update_time: '更新时间',
section_auto: '自动开奖与新建期',
auto_create_label: '允许自动创建下一期',
auto_create_tip: '开启后由后台定时任务在无进行中期号时自动插入新期',
manual_create_label: '允许手动创建下一期',
manual_create_tip: '开启后可在本页使用「手动创建下一期」按钮',
btn_create_next: '手动创建下一期',
saving: '保存中…',
}

View File

@@ -0,0 +1,290 @@
<template>
<div class="default-main ba-table-box">
<el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />
<el-card v-if="canSettings" class="period-settings-card" shadow="never">
<template #header>
<span>{{ t('game.period.section_auto') }}</span>
</template>
<div v-loading="settingsLoading" class="period-settings-body">
<div class="period-setting-row">
<span class="period-setting-label">{{ t('game.period.auto_create_label') }}</span>
<el-switch v-model="autoCreate" :disabled="settingsSaving" @change="onSwitchChange" />
<span class="period-setting-tip">{{ t('game.period.auto_create_tip') }}</span>
</div>
<div class="period-setting-row">
<span class="period-setting-label">{{ t('game.period.manual_create_label') }}</span>
<el-switch v-model="manualCreate" :disabled="settingsSaving" @change="onSwitchChange" />
<span class="period-setting-tip">{{ t('game.period.manual_create_tip') }}</span>
</div>
<div v-if="canManual" class="period-setting-actions">
<el-button type="primary" :loading="createLoading" @click="onCreateNextManual">
{{ t('game.period.btn_create_next') }}
</el-button>
</div>
</div>
</el-card>
<TableHeader
:buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
:quick-search-placeholder="t('Quick search placeholder', { fields: t('game.period.quick Search Fields') })"
></TableHeader>
<Table ref="tableRef"></Table>
<PopupForm />
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, provide, ref, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'
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 createAxios from '/@/utils/axios'
import baTableClass from '/@/utils/baTable'
import { auth } from '/@/utils/common'
defineOptions({
name: 'game/period',
})
const { t } = useI18n()
const tableRef = useTemplateRef('tableRef')
const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])
const settingsLoading = ref(false)
const settingsSaving = ref(false)
const createLoading = ref(false)
const autoCreate = ref(false)
const manualCreate = ref(false)
/** 避免初次拉取开关值时触发保存 */
const settingsReady = ref(false)
const canSettings = computed(() => auth('periodSettings'))
const canManual = computed(() => auth('createNextManual'))
const baTable = new baTableClass(
new baTableApi('/admin/game.Period/'),
{
pk: 'id',
column: [
{ type: 'selection', align: 'center', operator: false },
{ label: t('game.period.id'), prop: 'id', align: 'center', width: 100, operator: 'RANGE', sortable: 'custom' },
{
label: t('game.period.period_no'),
prop: 'period_no',
align: 'center',
minWidth: 180,
operatorPlaceholder: t('Fuzzy query'),
operator: 'LIKE',
},
{
label: t('game.period.period_start_at'),
prop: 'period_start_at',
align: 'center',
width: 170,
render: 'datetime',
operator: 'RANGE',
comSearchRender: 'datetime',
sortable: 'custom',
timeFormat: 'yyyy-mm-dd hh:MM:ss',
},
{
label: t('game.period.status'),
prop: 'status',
align: 'center',
width: 110,
operator: 'eq',
render: 'tag',
replaceValue: {
'0': t('game.period.status 0'),
'1': t('game.period.status 1'),
'2': t('game.period.status 2'),
'3': t('game.period.status 3'),
'4': t('game.period.status 4'),
'5': t('game.period.status 5'),
},
},
{
label: t('game.period.draw_mode'),
prop: 'draw_mode',
align: 'center',
width: 110,
operator: 'eq',
render: 'tag',
replaceValue: {
'0': t('game.period.draw_mode 0'),
'1': t('game.period.draw_mode 1'),
},
},
{
label: t('game.period.preset_number'),
prop: 'preset_number',
align: 'center',
width: 100,
operator: 'RANGE',
},
{
label: t('game.period.result_number'),
prop: 'result_number',
align: 'center',
width: 100,
operator: 'RANGE',
},
{
label: t('game.period.void_reason'),
prop: 'void_reason',
align: 'center',
minWidth: 140,
operatorPlaceholder: t('Fuzzy query'),
operator: 'LIKE',
showOverflowTooltip: true,
},
{
label: t('game.period.create_time'),
prop: 'create_time',
align: 'center',
render: 'datetime',
operator: 'RANGE',
comSearchRender: 'datetime',
sortable: 'custom',
width: 170,
timeFormat: 'yyyy-mm-dd hh:MM:ss',
},
{
label: t('game.period.update_time'),
prop: 'update_time',
align: 'center',
render: 'datetime',
operator: 'RANGE',
comSearchRender: 'datetime',
sortable: 'custom',
width: 170,
timeFormat: 'yyyy-mm-dd hh:MM:ss',
},
{ label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false, fixed: 'right' },
],
dblClickNotEditColumn: [undefined],
},
{
defaultItems: {
status: 0,
draw_mode: 0,
void_reason: '',
},
}
)
provide('baTable', baTable)
async function loadPeriodSettings() {
if (!canSettings.value) {
return
}
settingsLoading.value = true
try {
const res = await createAxios({
url: '/admin/game.Period/periodSettings',
method: 'get',
showCodeMessage: false,
})
if (res.code === 1 && res.data) {
autoCreate.value = res.data.period_auto_create_enabled === 1
manualCreate.value = res.data.period_manual_create_enabled === 1
settingsReady.value = true
}
} catch {
// 无权限或接口异常时不打断列表
} finally {
settingsLoading.value = false
}
}
async function onSaveSettings() {
if (!canSettings.value) {
return
}
settingsSaving.value = true
try {
await createAxios({
url: '/admin/game.Period/periodSettings',
method: 'post',
data: {
period_auto_create_enabled: autoCreate.value ? 1 : 0,
period_manual_create_enabled: manualCreate.value ? 1 : 0,
},
showSuccessMessage: true,
})
} finally {
settingsSaving.value = false
}
}
function onSwitchChange() {
if (!settingsReady.value) {
return
}
void onSaveSettings()
}
async function onCreateNextManual() {
if (!canManual.value) {
return
}
createLoading.value = true
try {
await createAxios({
url: '/admin/game.Period/createNextManual',
method: 'post',
showSuccessMessage: true,
})
await baTable.getData()
} finally {
createLoading.value = false
}
}
onMounted(() => {
baTable.table.ref = tableRef.value
baTable.mount()
void loadPeriodSettings()
baTable.getData()?.then(() => {
baTable.initSort()
baTable.dragSort()
})
})
</script>
<style scoped lang="scss">
.period-settings-card {
margin-bottom: 12px;
}
.period-settings-body {
display: flex;
flex-direction: column;
gap: 12px;
}
.period-setting-row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
}
.period-setting-label {
min-width: 160px;
font-weight: 500;
}
.period-setting-tip {
color: var(--el-text-color-secondary);
font-size: 13px;
flex: 1;
min-width: 200px;
}
.period-setting-actions {
margin-top: 4px;
}
</style>

View File

@@ -0,0 +1,131 @@
<template>
<el-dialog
class="ba-operate-dialog"
:close-on-click-modal="false"
:model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
@close="baTable.toggleForm"
>
<template #header>
<div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
{{ baTable.form.operate ? t(baTable.form.operate) : '' }}
</div>
</template>
<el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
<div
class="ba-operate-form"
:class="'ba-' + baTable.form.operate + '-form'"
:style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
>
<el-form
v-if="!baTable.form.loading"
ref="formRef"
@submit.prevent=""
@keyup.enter="baTable.onSubmit(formRef)"
:model="baTable.form.items"
:label-position="config.layout.shrink ? 'top' : 'right'"
:label-width="baTable.form.labelWidth + 'px'"
:rules="rules"
>
<FormItem
:label="t('game.period.period_no')"
type="string"
v-model="baTable.form.items!.period_no"
prop="period_no"
:placeholder="t('Please input field', { field: t('game.period.period_no') })"
/>
<FormItem
:label="t('game.period.period_start_at')"
type="datetime"
v-model="baTable.form.items!.period_start_at"
prop="period_start_at"
:placeholder="t('Please select field', { field: t('game.period.period_start_at') })"
/>
<FormItem
:label="t('game.period.status')"
type="radio"
v-model="baTable.form.items!.status"
prop="status"
:input-attr="{
content: {
'0': t('game.period.status 0'),
'1': t('game.period.status 1'),
'2': t('game.period.status 2'),
'3': t('game.period.status 3'),
'4': t('game.period.status 4'),
'5': t('game.period.status 5'),
},
}"
/>
<FormItem
:label="t('game.period.draw_mode')"
type="radio"
v-model="baTable.form.items!.draw_mode"
prop="draw_mode"
:input-attr="{
content: {
'0': t('game.period.draw_mode 0'),
'1': t('game.period.draw_mode 1'),
},
}"
/>
<FormItem
:label="t('game.period.preset_number')"
type="number"
v-model="baTable.form.items!.preset_number"
prop="preset_number"
:input-attr="{ step: 1, min: 1, max: 36 }"
/>
<FormItem
:label="t('game.period.result_number')"
type="number"
v-model="baTable.form.items!.result_number"
prop="result_number"
:input-attr="{ step: 1, min: 1, max: 36 }"
/>
<FormItem
:label="t('game.period.void_reason')"
type="textarea"
v-model="baTable.form.items!.void_reason"
prop="void_reason"
:input-attr="{ rows: 3 }"
/>
</el-form>
</div>
</el-scrollbar>
<template #footer>
<div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
<el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
<el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
{{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
</el-button>
</div>
</template>
</el-dialog>
</template>
<script setup lang="ts">
import type { FormItemRule } from 'element-plus'
import { inject, reactive, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
const config = useConfig()
const formRef = useTemplateRef('formRef')
const baTable = inject('baTable') as baTableClass
const { t } = useI18n()
const rules: Partial<Record<string, FormItemRule[]>> = reactive({
period_no: [buildRequired()],
})
function buildRequired(): FormItemRule {
return {
required: true,
message: t('Please input field', { field: t('game.period.period_no') }),
}
}
</script>
<style scoped lang="scss"></style>