Compare commits

31 Commits

Author SHA1 Message Date
e726fc3041 [接口v1]对接平台API-鉴权authtoken接口和getGameUrl接口 2026-03-09 13:50:32 +08:00
a37da0b6f5 [菜单]渠道管理 2026-03-09 11:14:34 +08:00
1de9af703a 修复豹子号5,30不显示中大奖的问题 2026-03-07 15:27:52 +08:00
4cf0da8092 修复打包报错文件 2026-03-07 15:03:00 +08:00
6632923213 优化游玩记录DicePlayRecord 2026-03-07 14:40:33 +08:00
316506b597 优化玩家DicePlayer保存的lottery_config_id 2026-03-07 11:58:47 +08:00
282d73a203 优化玩家DicePlayer权重输入方式 2026-03-07 11:51:34 +08:00
4b6bbab9d1 过滤tier=BIGWIN 2026-03-07 11:27:17 +08:00
e312154b0f DicePlayRecord添加字段super_win_coin和reward_win_coin记录不同的中奖金额类型 2026-03-07 11:09:41 +08:00
fe1ceeb4fb 重构*_weight为*_weight 2026-03-07 10:07:44 +08:00
7e5585aee0 优化后台样式 2026-03-06 18:32:17 +08:00
e087f89df5 优化接口/api/game/playStart新增返回参数 2026-03-06 18:23:45 +08:00
1cb2e26a77 [接口]创建用户接口/api/user/login自动保存时间create_time和update_time 2026-03-06 18:09:02 +08:00
330bd3b525 [接口]新增获取游戏配置接口-玩法 2026-03-06 17:47:12 +08:00
dc86d0ae86 [色子游戏]配置 2026-03-06 17:27:42 +08:00
8cd7de9f1b 优化设计中奖T5获取抽奖券逻辑 2026-03-06 16:15:56 +08:00
27f95a303a 从新设计抽奖逻辑 2026-03-06 16:02:17 +08:00
931af70c36 优化-实例化奖励列表到缓存中 2026-03-06 14:16:01 +08:00
f7d9b18f02 环境文件中配置是否开启生产环境APP_DEBUG 2026-03-06 13:41:13 +08:00
c1b4790f04 优化所有接口使用form-data类型 2026-03-06 12:29:40 +08:00
943d8f7b5f 修复优化后台报错 2026-03-06 11:34:36 +08:00
01f71a4871 优化/api/game/playStart接口 2026-03-06 11:14:08 +08:00
02549f4feb 优化/api/game/playStart接口 2026-03-06 10:54:58 +08:00
7a4d89d216 优化/api/game/playStart接口 2026-03-06 10:42:03 +08:00
cfe026b5eb 优化/api/game/playStart接口 2026-03-06 10:37:58 +08:00
768cf5137c 优化访问接口报错Server internal error 2026-03-06 10:33:44 +08:00
7e8867ed12 [色子游戏]玩家钱包流水记录-优化抽奖逻辑根据结果推断起始位置 2026-03-06 10:33:20 +08:00
005f261e03 优化性能 2026-03-05 17:20:44 +08:00
effdaaa38b 优化 2026-03-05 16:53:08 +08:00
aef404548d 优化 2026-03-05 16:50:52 +08:00
39955a17a8 优化登录接口以及中间件 2026-03-05 16:20:18 +08:00
74 changed files with 4063 additions and 1047 deletions

View File

@@ -0,0 +1,85 @@
import request from '@/utils/http'
/**
* 渠道 API接口
*/
export default {
/**
* 获取数据列表
* @param params 搜索参数
* @returns 数据列表
*/
list(params: Record<string, any>) {
return request.get<Api.Common.ApiPage>({
url: '/channel/manage/ChannelManage/index',
params
})
},
/**
* 读取数据
* @param id 数据ID
* @returns 数据详情
*/
read(id: number | string) {
return request.get<Api.Common.ApiData>({
url: '/channel/manage/ChannelManage/read?id=' + id
})
},
/**
* 创建数据
* @param params 数据参数
* @returns 执行结果
*/
save(params: Record<string, any>) {
return request.post<any>({
url: '/channel/manage/ChannelManage/save',
data: params
})
},
/**
* 更新数据
* @param params 数据参数
* @returns 执行结果
*/
update(params: Record<string, any>) {
return request.put<any>({
url: '/channel/manage/ChannelManage/update',
data: params
})
},
/**
* 删除数据
* @returns 执行结果
* @param params
*/
delete(params: Record<string, any>) {
return request.del<any>({
url: '/channel/manage/ChannelManage/destroy',
data: params
})
},
/**
* 仅更新状态(列表内开关用)
*/
updateStatus(params: { id: number | string; status: number }) {
return request.put<any>({
url: '/channel/manage/ChannelManage/updateStatus',
data: params
})
},
/**
* 获取管理员下拉列表SystemUser 的 id、username、realname
* @returns 管理员列表
*/
getAdminList() {
return request.get<Api.Common.ApiData>({
url: '/channel/manage/ChannelManage/getAdminList'
})
}
}

View File

@@ -0,0 +1,178 @@
<template>
<div class="art-full-height">
<!-- 搜索面板 -->
<TableSearch v-model="searchForm" @search="handleSearch" @reset="resetSearchParams" />
<ElCard class="art-table-card" shadow="never">
<!-- 表格头部 -->
<ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData">
<template #left>
<ElSpace wrap>
<ElButton
v-permission="'channel:manage:index:save'"
@click="showDialog('add')"
v-ripple
>
<template #icon>
<ArtSvgIcon icon="ri:add-fill" />
</template>
新增
</ElButton>
<ElButton
v-permission="'channel:manage:index:destroy'"
:disabled="selectedRows.length === 0"
@click="deleteSelectedRows(api.delete, refreshData)"
v-ripple
>
<template #icon>
<ArtSvgIcon icon="ri:delete-bin-5-line" />
</template>
删除
</ElButton>
</ElSpace>
</template>
</ArtTableHeader>
<!-- 表格 -->
<ArtTable
ref="tableRef"
rowKey="id"
:loading="loading"
:data="data"
:columns="columns"
:pagination="pagination"
@sort-change="handleSortChange"
@selection-change="handleSelectionChange"
@pagination:size-change="handleSizeChange"
@pagination:current-change="handleCurrentChange"
>
<!-- 状态开关直接修改 -->
<template #status="{ row }">
<ElSwitch
v-permission="'channel:manage:index:update'"
:model-value="row.status === 1"
:loading="row._statusLoading"
@change="(v: string | number | boolean) => handleStatusChange(row, v ? 1 : 0)"
/>
</template>
<!-- 操作列 -->
<template #operation="{ row }">
<div class="flex gap-2">
<SaButton
v-permission="'channel:manage:index:update'"
type="secondary"
@click="showDialog('edit', row)"
/>
<SaButton
v-permission="'channel:manage:index:destroy'"
type="error"
@click="deleteRow(row, api.delete, refreshData)"
/>
</div>
</template>
</ArtTable>
</ElCard>
<!-- 编辑弹窗 -->
<EditDialog
v-model="dialogVisible"
:dialog-type="dialogType"
:data="dialogData"
@success="refreshData"
/>
</div>
</template>
<script setup lang="ts">
import { useTable } from '@/hooks/core/useTable'
import { useSaiAdmin } from '@/composables/useSaiAdmin'
import api from '../../api/manage/index'
import TableSearch from './modules/table-search.vue'
import EditDialog from './modules/edit-dialog.vue'
// 搜索表单
const searchForm = ref({
name: undefined,
title: undefined,
status: undefined,
total_recharge: undefined,
total_withdrawal: undefined,
total_profit: undefined
})
// 搜索处理
const handleSearch = (params: Record<string, any>) => {
Object.assign(searchParams, params)
getData()
}
// 表格配置
const {
columns,
columnChecks,
data,
loading,
getData,
searchParams,
pagination,
resetSearchParams,
handleSortChange,
handleSizeChange,
handleCurrentChange,
refreshData
} = useTable({
core: {
apiFn: api.list,
columnsFactory: () => [
{ type: 'selection' },
{ prop: 'title', label: '标题' },
{ prop: 'status', label: '状态', useSlot: true },
{
prop: 'admin_id',
label: '管理员',
formatter: (row: Record<string, any>) => {
const admin = row.admin
if (!admin) return row.admin_id ?? '-'
return (
(admin.realname && String(admin.realname).trim()) || admin.username || `#${admin.id}`
)
}
},
{ prop: 'game_url', label: '游戏地址' },
{ prop: 'image', label: '图标', saiType: 'image' },
{ prop: 'agent', label: '代理agent' },
{ prop: 'ip_white', label: 'IP白名单' },
{ prop: 'total_recharge', label: '总充值' },
{ prop: 'total_withdrawal', label: '总提现' },
{ prop: 'total_profit', label: '总盈利' },
{ prop: 'player_count', label: '玩家数量' },
{ prop: 'operation', label: '操作', width: 100, fixed: 'right', useSlot: true }
]
}
})
// 状态开关切换(列表内直接修改)
const handleStatusChange = async (row: Record<string, any>, status: number) => {
row._statusLoading = true
try {
await api.updateStatus({ id: row.id, status })
row.status = status
} catch {
refreshData()
} finally {
row._statusLoading = false
}
}
// 编辑配置
const {
dialogType,
dialogVisible,
dialogData,
showDialog,
deleteRow,
deleteSelectedRows,
handleSelectionChange,
selectedRows
} = useSaiAdmin()
</script>

View File

@@ -0,0 +1,202 @@
<template>
<el-dialog
v-model="visible"
:title="dialogType === 'add' ? '新增渠道' : '编辑渠道'"
width="600px"
align-center
:close-on-click-modal="false"
@close="handleClose"
>
<el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
<el-form-item label="名称" prop="name">
<el-input v-model="formData.name" placeholder="请输入名称" />
</el-form-item>
<el-form-item label="标题" prop="title">
<el-input v-model="formData.title" placeholder="请输入标题" />
</el-form-item>
<el-form-item label="状态" prop="status">
<sa-switch v-model="formData.status" />
</el-form-item>
<el-form-item label="管理员" prop="admin_id">
<el-select
v-model="formData.admin_id"
placeholder="请选择管理员"
clearable
filterable
style="width: 100%"
:loading="adminOptionsLoading"
>
<el-option
v-for="item in adminOptions"
:key="item.id"
:label="item.name"
:value="item.id"
/>
</el-select>
</el-form-item>
<el-form-item label="游戏地址" prop="game_url">
<el-input v-model="formData.game_url" placeholder="请输入游戏地址" />
</el-form-item>
<el-form-item label="图标" prop="image">
<sa-image-upload v-model="formData.image" :limit="1" :multiple="false" />
</el-form-item>
<el-form-item label="IP白名单" prop="ip_white">
<el-input v-model="formData.ip_white" placeholder="请输入IP白名单" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="handleClose">取消</el-button>
<el-button type="primary" @click="handleSubmit">提交</el-button>
</template>
</el-dialog>
</template>
<script setup lang="ts">
import api from '../../../api/manage/index'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
interface Props {
modelValue: boolean
dialogType: string
data?: Record<string, any>
}
interface Emits {
(e: 'update:modelValue', value: boolean): void
(e: 'success'): void
}
const props = withDefaults(defineProps<Props>(), {
modelValue: false,
dialogType: 'add',
data: undefined
})
const emit = defineEmits<Emits>()
const formRef = ref<FormInstance>()
/**
* 弹窗显示状态双向绑定
*/
const visible = computed({
get: () => props.modelValue,
set: (value) => emit('update:modelValue', value)
})
/**
* 表单验证规则
*/
const rules = reactive<FormRules>({
name: [{ required: true, message: '名称必需填写', trigger: 'blur' }],
title: [{ required: true, message: '标题必需填写', trigger: 'blur' }],
admin_id: [{ required: true, message: '管理员必需填写', trigger: 'blur' }],
game_url: [{ required: true, message: '游戏地址必需填写', trigger: 'blur' }],
image: [{ required: true, message: '图标必需填写', trigger: 'blur' }]
})
/**
* 初始数据
*/
const initialFormData = {
id: null,
name: '',
title: '',
status: 1,
admin_id: null,
game_url: '',
image: '',
ip_white: ''
}
/**
* 表单数据
*/
const formData = reactive({ ...initialFormData })
/**
* 监听弹窗打开,初始化表单数据
*/
watch(
() => props.modelValue,
(newVal) => {
if (newVal) {
initPage()
}
}
)
/** 管理员下拉选项value=SystemUser.id, label=SystemUser.name远程关联 */
const adminOptions = ref<Array<{ id: number; name: string }>>([])
const adminOptionsLoading = ref(false)
const fetchAdminList = async () => {
adminOptionsLoading.value = true
try {
// 接口返回的已是 data 解包后的数组value=SystemUser.id, name=展示名)
const list = await api.getAdminList()
adminOptions.value = Array.isArray(list) ? list : []
} catch {
adminOptions.value = []
} finally {
adminOptionsLoading.value = false
}
}
/**
* 初始化页面数据
*/
const initPage = async () => {
// 先重置为初始值
Object.assign(formData, initialFormData)
await fetchAdminList()
// 如果有数据,则填充数据
if (props.data) {
await nextTick()
initForm()
}
}
/**
* 初始化表单数据
*/
const initForm = () => {
if (props.data) {
for (const key in formData) {
if (props.data[key] != null && props.data[key] != undefined) {
;(formData as any)[key] = props.data[key]
}
}
}
}
/**
* 关闭弹窗并重置表单
*/
const handleClose = () => {
visible.value = false
formRef.value?.resetFields()
}
/**
* 提交表单
*/
const handleSubmit = async () => {
if (!formRef.value) return
try {
await formRef.value.validate()
if (props.dialogType === 'add') {
await api.save(formData)
ElMessage.success('新增成功')
} else {
await api.update(formData)
ElMessage.success('修改成功')
}
emit('success')
handleClose()
} catch (error) {
console.log('表单验证失败:', error)
}
}
</script>

View File

@@ -0,0 +1,92 @@
<template>
<sa-search-bar
ref="searchBarRef"
v-model="formData"
label-width="100px"
:showExpand="false"
@reset="handleReset"
@search="handleSearch"
@expand="handleExpand"
>
<el-col v-bind="setSpan(6)">
<el-form-item label="名称" prop="name">
<el-input v-model="formData.name" placeholder="请输入名称" clearable />
</el-form-item>
</el-col>
<el-col v-bind="setSpan(6)">
<el-form-item label="标题" prop="title">
<el-input v-model="formData.title" placeholder="请输入标题" clearable />
</el-form-item>
</el-col>
<el-col v-bind="setSpan(6)">
<el-form-item label="状态" prop="status">
<el-input v-model="formData.status" placeholder="请输入状态" clearable />
</el-form-item>
</el-col>
<el-col v-bind="setSpan(6)">
<el-form-item label="总充值" prop="total_recharge">
<el-input v-model="formData.total_recharge" placeholder="请输入总充值" clearable />
</el-form-item>
</el-col>
<el-col v-bind="setSpan(6)">
<el-form-item label="总提现" prop="total_withdrawal">
<el-input v-model="formData.total_withdrawal" placeholder="请输入总提现" clearable />
</el-form-item>
</el-col>
<el-col v-bind="setSpan(6)">
<el-form-item label="总盈利" prop="total_profit">
<el-input v-model="formData.total_profit" placeholder="请输入总盈利" clearable />
</el-form-item>
</el-col>
</sa-search-bar>
</template>
<script setup lang="ts">
interface Props {
modelValue: Record<string, any>
}
interface Emits {
(e: 'update:modelValue', value: Record<string, any>): void
(e: 'search', params: Record<string, any>): void
(e: 'reset'): void
}
const props = defineProps<Props>()
const emit = defineEmits<Emits>()
// 展开/收起
const isExpanded = ref<boolean>(false)
// 表单数据双向绑定
const searchBarRef = ref()
const formData = computed({
get: () => props.modelValue,
set: (val) => emit('update:modelValue', val)
})
// 重置
function handleReset() {
searchBarRef.value?.ref.resetFields()
emit('reset')
}
// 搜索
async function handleSearch() {
emit('search', formData.value)
}
// 展开/收起
function handleExpand(expanded: boolean) {
isExpanded.value = expanded
}
// 栅格占据的列数
const setSpan = (span: number) => {
return {
span: span,
xs: 24, // 手机:满宽显示
sm: span >= 12 ? span : 12, // 平板大于等于12保持否则用半宽
md: span >= 8 ? span : 8, // 中等屏幕大于等于8保持否则用三分之一宽
lg: span,
xl: span
}
}
</script>

View File

@@ -0,0 +1,65 @@
import request from '@/utils/http'
/**
* 摇色子配置 API接口
*/
export default {
/**
* 获取数据列表
* @param params 搜索参数
* @returns 数据列表
*/
list(params: Record<string, any>) {
return request.get<Api.Common.ApiPage>({
url: '/dice/config/DiceConfig/index',
params
})
},
/**
* 读取数据
* @param id 数据ID
* @returns 数据详情
*/
read(id: number | string) {
return request.get<Api.Common.ApiData>({
url: '/dice/config/DiceConfig/read?id=' + id
})
},
/**
* 创建数据
* @param params 数据参数
* @returns 执行结果
*/
save(params: Record<string, any>) {
return request.post<any>({
url: '/dice/config/DiceConfig/save',
data: params
})
},
/**
* 更新数据
* @param params 数据参数
* @returns 执行结果
*/
update(params: Record<string, any>) {
return request.put<any>({
url: '/dice/config/DiceConfig/update',
data: params
})
},
/**
* 删除数据
* @param id 数据ID
* @returns 执行结果
*/
delete(params: Record<string, any>) {
return request.del<any>({
url: '/dice/config/DiceConfig/destroy',
data: params
})
}
}

View File

@@ -5,7 +5,7 @@ import request from '@/utils/http'
*/ */
export default { export default {
/** /**
* 获取数据列表 * 获取数据列表DiceLotteryConfig
* @param params 搜索参数 * @param params 搜索参数
* @returns 数据列表 * @returns 数据列表
*/ */
@@ -16,6 +16,20 @@ export default {
}) })
}, },
/**
* 获取 DiceLotteryConfig 列表数据,仅含 id、name用于 lottery_config_id 下拉
* @returns DiceLotteryConfig['id','name'] 列表
*/
async getOptions(): Promise<Array<{ id: number; name: string }>> {
const res = await request.get<any>({
url: '/dice/lottery_config/DiceLotteryConfig/getOptions'
})
const rows = (res?.data ?? []) as Array<{ id: number; name: string }>
return Array.isArray(rows)
? rows.map((r) => ({ id: Number(r.id), name: String(r.name ?? r.id ?? '') }))
: []
},
/** /**
* 读取数据 * 读取数据
* @param id 数据ID * @param id 数据ID

View File

@@ -71,5 +71,17 @@ export default {
url: '/dice/player/DicePlayer/updateStatus', url: '/dice/player/DicePlayer/updateStatus',
data: params data: params
}) })
},
/**
* 获取彩金池配置选项DiceLotteryConfig.id、name供 lottery_config_id 下拉使用
* @returns [ { id, name } ]
*/
async getLotteryConfigOptions(): Promise<Array<{ id: number; name: string }>> {
const res = await request.get<any>({
url: '/dice/player/DicePlayer/getLotteryConfigOptions'
})
const rows = (Array.isArray(res) ? res : (res?.data ?? [])) as Array<{ id: number; name: string }>
return rows.map((r) => ({ id: Number(r.id), name: String(r.name ?? r.id ?? '') }))
} }
} }

View File

@@ -0,0 +1,139 @@
<template>
<div class="art-full-height">
<!-- 搜索面板 -->
<TableSearch v-model="searchForm" @search="handleSearch" @reset="resetSearchParams" />
<ElCard class="art-table-card" shadow="never">
<!-- 表格头部 -->
<ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData">
<template #left>
<ElSpace wrap>
<!-- <ElButton v-permission="'dice:config:index:save'" @click="showDialog('add')" v-ripple>-->
<!-- <template #icon>-->
<!-- <ArtSvgIcon icon="ri:add-fill" />-->
<!-- </template>-->
<!-- 新增-->
<!-- </ElButton>-->
<!-- <ElButton-->
<!-- v-permission="'dice:config:index:destroy'"-->
<!-- :disabled="selectedRows.length === 0"-->
<!-- @click="deleteSelectedRows(api.delete, refreshData)"-->
<!-- v-ripple-->
<!-- >-->
<!-- <template #icon>-->
<!-- <ArtSvgIcon icon="ri:delete-bin-5-line" />-->
<!-- </template>-->
<!-- 删除-->
<!-- </ElButton>-->
</ElSpace>
</template>
</ArtTableHeader>
<!-- 表格 -->
<ArtTable
ref="tableRef"
rowKey="id"
:loading="loading"
:data="data"
:columns="columns"
:pagination="pagination"
@sort-change="handleSortChange"
@selection-change="handleSelectionChange"
@pagination:size-change="handleSizeChange"
@pagination:current-change="handleCurrentChange"
>
<!-- 操作列 -->
<template #operation="{ row }">
<div class="flex gap-2">
<SaButton
v-permission="'dice:config:index:update'"
type="secondary"
@click="showDialog('edit', row)"
/>
<!-- <SaButton-->
<!-- v-permission="'dice:config:index:destroy'"-->
<!-- type="error"-->
<!-- @click="deleteRow(row, api.delete, refreshData)"-->
<!-- />-->
</div>
</template>
</ArtTable>
</ElCard>
<!-- 编辑弹窗 -->
<EditDialog
v-model="dialogVisible"
:dialog-type="dialogType"
:data="dialogData"
@success="refreshData"
/>
</div>
</template>
<script setup lang="ts">
import { useTable } from '@/hooks/core/useTable'
import { useSaiAdmin } from '@/composables/useSaiAdmin'
import api from '../../api/config/index'
import TableSearch from './modules/table-search.vue'
import EditDialog from './modules/edit-dialog.vue'
// 搜索表单
const searchForm = ref({
group: undefined,
title: undefined,
name: undefined
})
// 搜索处理
const handleSearch = (params: Record<string, any>) => {
Object.assign(searchParams, params)
getData()
}
// 表格配置
const {
columns,
columnChecks,
data,
loading,
getData,
searchParams,
pagination,
resetSearchParams,
handleSortChange,
handleSizeChange,
handleCurrentChange,
refreshData
} = useTable({
core: {
apiFn: api.list,
columnsFactory: () => [
// { type: 'selection' },
{ prop: 'group', label: '分组', minWidth: 140, align: 'center' },
{ prop: 'title', label: '标题', minWidth: 160, align: 'center' },
{ prop: 'name', label: '配置名称', align: 'center' },
{ prop: 'value', label: '值', minWidth: 240, align: 'center' },
{
prop: 'operation',
label: '操作',
width: 60,
align: 'center',
fixed: 'right',
useSlot: true
}
]
}
})
// 编辑配置
const {
dialogType,
dialogVisible,
dialogData,
showDialog,
// deleteRow,
// deleteSelectedRows,
handleSelectionChange
// selectedRows
} = useSaiAdmin()
</script>

View File

@@ -0,0 +1,165 @@
<template>
<el-dialog
v-model="visible"
:title="dialogType === 'add' ? '新增摇色子配置' : '编辑摇色子配置'"
width="600px"
align-center
:close-on-click-modal="false"
@close="handleClose"
>
<el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
<el-form-item label="分组" prop="group">
<el-input
v-model="formData.group"
placeholder="请输入分组"
:disabled="dialogType === 'edit'"
/>
</el-form-item>
<el-form-item label="标题" prop="title">
<el-input v-model="formData.title" placeholder="请输入标题" />
</el-form-item>
<el-form-item label="配置名称" prop="name">
<el-input
v-model="formData.name"
placeholder="请输入配置名称"
:disabled="dialogType === 'edit'"
/>
</el-form-item>
<el-form-item label="值" prop="value">
<el-input v-model="formData.value" type="textarea" :rows="5" placeholder="请输入值" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="handleClose">取消</el-button>
<el-button type="primary" @click="handleSubmit">提交</el-button>
</template>
</el-dialog>
</template>
<script setup lang="ts">
import api from '../../../api/config/index'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
interface Props {
modelValue: boolean
dialogType: string
data?: Record<string, any>
}
interface Emits {
(e: 'update:modelValue', value: boolean): void
(e: 'success'): void
}
const props = withDefaults(defineProps<Props>(), {
modelValue: false,
dialogType: 'add',
data: undefined
})
const emit = defineEmits<Emits>()
const formRef = ref<FormInstance>()
/**
* 弹窗显示状态双向绑定
*/
const visible = computed({
get: () => props.modelValue,
set: (value) => emit('update:modelValue', value)
})
/**
* 表单验证规则
*/
const rules = reactive<FormRules>({
group: [{ required: true, message: '分组必需填写', trigger: 'blur' }],
title: [{ required: true, message: '标题必需填写', trigger: 'blur' }],
name: [{ required: true, message: '配置名称必需填写', trigger: 'blur' }],
value: [{ required: true, message: '值必需填写', trigger: 'blur' }]
})
/**
* 初始数据
*/
const initialFormData = {
id: null,
value: '',
name: '',
group: '',
title: ''
}
/**
* 表单数据
*/
const formData = reactive({ ...initialFormData })
/**
* 监听弹窗打开,初始化表单数据
*/
watch(
() => props.modelValue,
(newVal) => {
if (newVal) {
initPage()
}
}
)
/**
* 初始化页面数据
*/
const initPage = async () => {
// 先重置为初始值
Object.assign(formData, initialFormData)
// 如果有数据,则填充数据
if (props.data) {
await nextTick()
initForm()
}
}
/**
* 初始化表单数据
*/
const initForm = () => {
if (props.data) {
for (const key in formData) {
if (props.data[key] != null && props.data[key] != undefined) {
;(formData as any)[key] = props.data[key]
}
}
}
}
/**
* 关闭弹窗并重置表单
*/
const handleClose = () => {
visible.value = false
formRef.value?.resetFields()
}
/**
* 提交表单
*/
const handleSubmit = async () => {
if (!formRef.value) return
try {
await formRef.value.validate()
if (props.dialogType === 'add') {
await api.save(formData)
ElMessage.success('新增成功')
} else {
await api.update(formData)
ElMessage.success('修改成功')
}
emit('success')
handleClose()
} catch (error) {
console.log('表单验证失败:', error)
}
}
</script>

View File

@@ -0,0 +1,77 @@
<template>
<sa-search-bar
ref="searchBarRef"
v-model="formData"
label-width="100px"
:showExpand="false"
@reset="handleReset"
@search="handleSearch"
@expand="handleExpand"
>
<el-col v-bind="setSpan(6)">
<el-form-item label="分组" prop="group">
<el-input v-model="formData.group" placeholder="请输入分组" clearable />
</el-form-item>
</el-col>
<el-col v-bind="setSpan(6)">
<el-form-item label="标题" prop="title">
<el-input v-model="formData.title" placeholder="请输入标题" clearable />
</el-form-item>
</el-col>
<el-col v-bind="setSpan(6)">
<el-form-item label="配置名称" prop="name">
<el-input v-model="formData.name" placeholder="请输入配置名称" clearable />
</el-form-item>
</el-col>
</sa-search-bar>
</template>
<script setup lang="ts">
interface Props {
modelValue: Record<string, any>
}
interface Emits {
(e: 'update:modelValue', value: Record<string, any>): void
(e: 'search', params: Record<string, any>): void
(e: 'reset'): void
}
const props = defineProps<Props>()
const emit = defineEmits<Emits>()
// 展开/收起
const isExpanded = ref<boolean>(false)
// 表单数据双向绑定
const searchBarRef = ref()
const formData = computed({
get: () => props.modelValue,
set: (val) => emit('update:modelValue', val)
})
// 重置
function handleReset() {
searchBarRef.value?.ref.resetFields()
emit('reset')
}
// 搜索
async function handleSearch() {
emit('search', formData.value)
}
// 展开/收起
function handleExpand(expanded: boolean) {
isExpanded.value = expanded
}
// 栅格占据的列数
const setSpan = (span: number) => {
return {
span: span,
xs: 24, // 手机:满宽显示
sm: span >= 12 ? span : 12, // 平板大于等于12保持否则用半宽
md: span >= 8 ? span : 8, // 中等屏幕大于等于8保持否则用三分之一宽
lg: span,
xl: span
}
}
</script>

View File

@@ -7,29 +7,29 @@
<!-- 表格头部 --> <!-- 表格头部 -->
<ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData"> <ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData">
<template #left> <template #left>
<ElSpace wrap> <!-- <ElSpace wrap>-->
<ElButton <!-- <ElButton-->
v-permission="'dice:lottery_config:index:save'" <!-- v-permission="'dice:lottery_config:index:save'"-->
@click="showDialog('add')" <!-- @click="showDialog('add')"-->
v-ripple <!-- v-ripple-->
> <!-- >-->
<template #icon> <!-- <template #icon>-->
<ArtSvgIcon icon="ri:add-fill" /> <!-- <ArtSvgIcon icon="ri:add-fill" />-->
</template> <!-- </template>-->
新增 <!-- 新增-->
</ElButton> <!-- </ElButton>-->
<ElButton <!-- <ElButton-->
v-permission="'dice:lottery_config:index:destroy'" <!-- v-permission="'dice:lottery_config:index:destroy'"-->
:disabled="selectedRows.length === 0" <!-- :disabled="selectedRows.length === 0"-->
@click="deleteSelectedRows(api.delete, refreshData)" <!-- @click="deleteSelectedRows(api.delete, refreshData)"-->
v-ripple <!-- v-ripple-->
> <!-- >-->
<template #icon> <!-- <template #icon>-->
<ArtSvgIcon icon="ri:delete-bin-5-line" /> <!-- <ArtSvgIcon icon="ri:delete-bin-5-line" />-->
</template> <!-- </template>-->
删除 <!-- 删除-->
</ElButton> <!-- </ElButton>-->
</ElSpace> <!-- </ElSpace>-->
</template> </template>
</ArtTableHeader> </ArtTableHeader>
@@ -54,11 +54,11 @@
type="secondary" type="secondary"
@click="showDialog('edit', row)" @click="showDialog('edit', row)"
/> />
<SaButton <!-- <SaButton-->
v-permission="'dice:lottery_config:index:destroy'" <!-- v-permission="'dice:lottery_config:index:destroy'"-->
type="error" <!-- type="error"-->
@click="deleteRow(row, api.delete, refreshData)" <!-- @click="deleteRow(row, api.delete, refreshData)"-->
/> <!-- />-->
</div> </div>
</template> </template>
</ArtTable> </ArtTable>
@@ -121,16 +121,52 @@
core: { core: {
apiFn: api.list, apiFn: api.list,
columnsFactory: () => [ columnsFactory: () => [
{ type: 'selection' }, { prop: 'name', label: '名称', align: 'center' },
{ prop: 'name', label: '名称' }, { prop: 'type', label: '奖池类型', width: 100, align: 'center', formatter: typeFormatter },
{ prop: 'type', label: '奖池类型', width: 100, formatter: typeFormatter }, { prop: 'safety_line', label: '安全线', align: 'center' },
{ prop: 'safety_line', label: '安全线' }, {
{ prop: 't1_wight', label: 'T1池权重', width: 100, formatter: weightFormatter('t1_wight') }, prop: 't1_weight',
{ prop: 't2_wight', label: 'T2池权重', width: 100, formatter: weightFormatter('t2_wight') }, label: 'T1池权重',
{ prop: 't3_wight', label: 'T3池权重', width: 100, formatter: weightFormatter('t3_wight') }, width: 100,
{ prop: 't4_wight', label: 'T4池权重', width: 100, formatter: weightFormatter('t4_wight') }, align: 'center',
{ prop: 't5_wight', label: 'T5池权重', width: 100, formatter: weightFormatter('t5_wight') }, formatter: weightFormatter('t1_weight')
{ prop: 'operation', label: '操作', width: 100, fixed: 'right', useSlot: true } },
{
prop: 't2_weight',
label: 'T2池权重',
width: 100,
align: 'center',
formatter: weightFormatter('t2_weight')
},
{
prop: 't3_weight',
label: 'T3池权重',
width: 100,
align: 'center',
formatter: weightFormatter('t3_weight')
},
{
prop: 't4_weight',
label: 'T4池权重',
width: 100,
align: 'center',
formatter: weightFormatter('t4_weight')
},
{
prop: 't5_weight',
label: 'T5池权重',
width: 100,
align: 'center',
formatter: weightFormatter('t5_weight')
},
{
prop: 'operation',
label: '操作',
width: 60,
align: 'center',
fixed: 'right',
useSlot: true
}
] ]
} }
}) })
@@ -141,9 +177,9 @@
dialogVisible, dialogVisible,
dialogData, dialogData,
showDialog, showDialog,
deleteRow, // deleteRow,
deleteSelectedRows, // deleteSelectedRows,
handleSelectionChange, handleSelectionChange
selectedRows // selectedRows
} = useSaiAdmin() } = useSaiAdmin()
</script> </script>

View File

@@ -9,7 +9,11 @@
> >
<el-form ref="formRef" :model="formData" :rules="rules" label-width="120px"> <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
<el-form-item label="名称" prop="name"> <el-form-item label="名称" prop="name">
<el-input v-model="formData.name" placeholder="请输入名称" /> <el-input
v-model="formData.name"
placeholder="请输入名称"
:disabled="dialogType === 'edit'"
/>
</el-form-item> </el-form-item>
<el-form-item label="备注" prop="remark"> <el-form-item label="备注" prop="remark">
<el-input <el-input
@@ -27,6 +31,7 @@
placeholder="请选择奖池类型" placeholder="请选择奖池类型"
clearable clearable
style="width: 100%" style="width: 100%"
:disabled="dialogType === 'edit'"
> >
<el-option label="正常" :value="0" /> <el-option label="正常" :value="0" />
<el-option label="强制杀猪" :value="1" /> <el-option label="强制杀猪" :value="1" />
@@ -41,20 +46,20 @@
style="width: 100%" style="width: 100%"
/> />
</el-form-item> </el-form-item>
<el-form-item label="T1池权重(%)" prop="t1_wight"> <el-form-item label="T1池权重(%)" prop="t1_weight">
<el-slider v-model="formData.t1_wight" :min="0" :max="100" :step="0.01" show-input /> <el-slider v-model="formData.t1_weight" :min="0" :max="100" :step="0.01" show-input />
</el-form-item> </el-form-item>
<el-form-item label="T2池权重(%)" prop="t2_wight"> <el-form-item label="T2池权重(%)" prop="t2_weight">
<el-slider v-model="formData.t2_wight" :min="0" :max="100" :step="0.01" show-input /> <el-slider v-model="formData.t2_weight" :min="0" :max="100" :step="0.01" show-input />
</el-form-item> </el-form-item>
<el-form-item label="T3池权重(%)" prop="t3_wight"> <el-form-item label="T3池权重(%)" prop="t3_weight">
<el-slider v-model="formData.t3_wight" :min="0" :max="100" :step="0.01" show-input /> <el-slider v-model="formData.t3_weight" :min="0" :max="100" :step="0.01" show-input />
</el-form-item> </el-form-item>
<el-form-item label="T4池权重(%)" prop="t4_wight"> <el-form-item label="T4池权重(%)" prop="t4_weight">
<el-slider v-model="formData.t4_wight" :min="0" :max="100" :step="0.01" show-input /> <el-slider v-model="formData.t4_weight" :min="0" :max="100" :step="0.01" show-input />
</el-form-item> </el-form-item>
<el-form-item label="T5池权重(%)" prop="t5_wight"> <el-form-item label="T5池权重(%)" prop="t5_weight">
<el-slider v-model="formData.t5_wight" :min="0" :max="100" :step="0.01" show-input /> <el-slider v-model="formData.t5_weight" :min="0" :max="100" :step="0.01" show-input />
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
<div class="text-gray-500 text-sm"> <div class="text-gray-500 text-sm">
@@ -107,7 +112,7 @@
}) })
/** 五个权重字段名,用于总和校验 */ /** 五个权重字段名,用于总和校验 */
const WEIGHT_KEYS = ['t1_wight', 't2_wight', 't3_wight', 't4_wight', 't5_wight'] as const const WEIGHT_KEYS = ['t1_weight', 't2_weight', 't3_weight', 't4_weight', 't5_weight'] as const
/** 五个池权重总和(用于展示与校验) */ /** 五个池权重总和(用于展示与校验) */
const weightsSum = computed(() => { const weightsSum = computed(() => {
@@ -120,11 +125,11 @@
const rules = reactive<FormRules>({ const rules = reactive<FormRules>({
name: [{ required: true, message: '名称必需填写', trigger: 'blur' }], name: [{ required: true, message: '名称必需填写', trigger: 'blur' }],
type: [{ required: true, message: '请选择奖池类型', trigger: 'change' }], type: [{ required: true, message: '请选择奖池类型', trigger: 'change' }],
t1_wight: [{ required: true, message: 'T1池权重必需填写', trigger: 'blur' }], t1_weight: [{ required: true, message: 'T1池权重必需填写', trigger: 'blur' }],
t2_wight: [{ required: true, message: 'T2池权重必需填写', trigger: 'blur' }], t2_weight: [{ required: true, message: 'T2池权重必需填写', trigger: 'blur' }],
t3_wight: [{ required: true, message: 'T3池权重必需填写', trigger: 'blur' }], t3_weight: [{ required: true, message: 'T3池权重必需填写', trigger: 'blur' }],
t4_wight: [{ required: true, message: 'T4池权重必需填写', trigger: 'blur' }], t4_weight: [{ required: true, message: 'T4池权重必需填写', trigger: 'blur' }],
t5_wight: [{ required: true, message: 'T5池权重必需填写', trigger: 'blur' }] t5_weight: [{ required: true, message: 'T5池权重必需填写', trigger: 'blur' }]
}) })
/** /**
@@ -136,11 +141,11 @@
remark: '', remark: '',
type: null as number | null, type: null as number | null,
safety_line: 0 as number, safety_line: 0 as number,
t1_wight: 0 as number, t1_weight: 0 as number,
t2_wight: 0 as number, t2_weight: 0 as number,
t3_wight: 0 as number, t3_weight: 0 as number,
t4_wight: 0 as number, t4_weight: 0 as number,
t5_wight: 0 as number t5_weight: 0 as number
} }
/** /**
@@ -182,11 +187,11 @@
'id', 'id',
'type', 'type',
'safety_line', 'safety_line',
't1_wight', 't1_weight',
't2_wight', 't2_weight',
't3_wight', 't3_weight',
't4_wight', 't4_weight',
't5_wight' 't5_weight'
] ]
for (const key of Object.keys(formData)) { for (const key of Object.keys(formData)) {
if (!(key in props.data)) continue if (!(key in props.data)) continue

View File

@@ -7,29 +7,29 @@
<!-- 表格头部 --> <!-- 表格头部 -->
<ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData"> <ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData">
<template #left> <template #left>
<ElSpace wrap> <!-- <ElSpace wrap>-->
<ElButton <!-- <ElButton-->
v-permission="'dice:play_record:index:save'" <!-- v-permission="'dice:play_record:index:save'"-->
@click="showDialog('add')" <!-- @click="showDialog('add')"-->
v-ripple <!-- v-ripple-->
> <!-- >-->
<template #icon> <!-- <template #icon>-->
<ArtSvgIcon icon="ri:add-fill" /> <!-- <ArtSvgIcon icon="ri:add-fill" />-->
</template> <!-- </template>-->
新增 <!-- 新增-->
</ElButton> <!-- </ElButton>-->
<ElButton <!-- <ElButton-->
v-permission="'dice:play_record:index:destroy'" <!-- v-permission="'dice:play_record:index:destroy'"-->
:disabled="selectedRows.length === 0" <!-- :disabled="selectedRows.length === 0"-->
@click="deleteSelectedRows(api.delete, refreshData)" <!-- @click="deleteSelectedRows(api.delete, refreshData)"-->
v-ripple <!-- v-ripple-->
> <!-- >-->
<template #icon> <!-- <template #icon>-->
<ArtSvgIcon icon="ri:delete-bin-5-line" /> <!-- <ArtSvgIcon icon="ri:delete-bin-5-line" />-->
</template> <!-- </template>-->
删除 <!-- 删除-->
</ElButton> <!-- </ElButton>-->
</ElSpace> <!-- </ElSpace>-->
</template> </template>
</ArtTableHeader> </ArtTableHeader>
@@ -56,10 +56,10 @@
{{ row.lottery_type === 0 ? '付费' : row.lottery_type === 1 ? '赠送' : '-' }} {{ row.lottery_type === 0 ? '付费' : row.lottery_type === 1 ? '赠送' : '-' }}
</ElTag> </ElTag>
</template> </template>
<!-- tag --> <!-- 是否中大 tag -->
<template #is_win="{ row }"> <template #is_win="{ row }">
<ElTag size="small" :type="row.is_win === 1 ? 'success' : 'info'"> <ElTag size="small" :type="row.is_win === 1 ? 'success' : 'info'">
{{ row.is_win === 0 ? '无' : row.is_win === 1 ? '中奖' : '-' }} {{ row.is_win === 0 ? '无' : row.is_win === 1 ? '中奖' : '-' }}
</ElTag> </ElTag>
</template> </template>
<!-- 方向 tag --> <!-- 方向 tag -->
@@ -82,11 +82,11 @@
type="secondary" type="secondary"
@click="showDialog('edit', row)" @click="showDialog('edit', row)"
/> />
<SaButton <!-- <SaButton-->
v-permission="'dice:play_record:index:destroy'" <!-- v-permission="'dice:play_record:index:destroy'"-->
type="error" <!-- type="error"-->
@click="deleteRow(row, api.delete, refreshData)" <!-- @click="deleteRow(row, api.delete, refreshData)"-->
/> <!-- />-->
</div> </div>
</template> </template>
</ArtTable> </ArtTable>
@@ -117,6 +117,8 @@
is_win: undefined, is_win: undefined,
win_coin_min: undefined, win_coin_min: undefined,
win_coin_max: undefined, win_coin_max: undefined,
roll_number_min: undefined,
roll_number_max: undefined,
reward_ui_text: undefined, reward_ui_text: undefined,
reward_tier: undefined, reward_tier: undefined,
direction: undefined direction: undefined
@@ -168,7 +170,7 @@
core: { core: {
apiFn: api.list, apiFn: api.list,
columnsFactory: () => [ columnsFactory: () => [
{ type: 'selection' }, // { type: 'selection' },
{ prop: 'id', label: 'ID', width: 80 }, { prop: 'id', label: 'ID', width: 80 },
{ {
prop: 'player_id', prop: 'player_id',
@@ -182,12 +184,15 @@
useSlot: true useSlot: true
}, },
{ prop: 'lottery_type', label: '抽奖类型', width: 100, useSlot: true }, { prop: 'lottery_type', label: '抽奖类型', width: 100, useSlot: true },
{ prop: 'is_win', label: '奖', width: 80, useSlot: true }, { prop: 'is_win', label: '是否中大奖', width: 100, useSlot: true },
{ prop: 'win_coin', label: '赢取平台币' }, { prop: 'win_coin', label: '赢取平台币', width: 110 },
{ prop: 'super_win_coin', label: '中大奖平台币', width: 120 },
{ prop: 'reward_win_coin', label: '摇色子中奖平台币', width: 140 },
{ prop: 'direction', label: '方向', width: 90, useSlot: true }, { prop: 'direction', label: '方向', width: 90, useSlot: true },
{ prop: 'start_index', label: '起始索引', width: 90 }, { prop: 'start_index', label: '起始索引', width: 90 },
{ prop: 'target_index', label: '终点索引', width: 90 }, { prop: 'target_index', label: '终点索引', width: 90 },
{ prop: 'roll_array', label: '摇取点数', width: 140, useSlot: true }, { prop: 'roll_array', label: '摇取点数', width: 140, useSlot: true },
{ prop: 'roll_number', label: '摇取点数和', width: 110, sortable: true },
{ {
prop: 'reward_config_id', prop: 'reward_config_id',
label: '奖励配置', label: '奖励配置',
@@ -206,9 +211,9 @@
dialogVisible, dialogVisible,
dialogData, dialogData,
showDialog, showDialog,
deleteRow, // deleteRow,
deleteSelectedRows, // deleteSelectedRows,
handleSelectionChange, handleSelectionChange
selectedRows // selectedRows
} = useSaiAdmin() } = useSaiAdmin()
</script> </script>

View File

@@ -54,7 +54,7 @@
<el-option label="赠送" :value="1" /> <el-option label="赠送" :value="1" />
</el-select> </el-select>
</el-form-item> </el-form-item>
<el-form-item label="奖" prop="is_win"> <el-form-item label="是否中大奖" prop="is_win">
<el-select <el-select
v-model="formData.is_win" v-model="formData.is_win"
placeholder="请选择" placeholder="请选择"
@@ -63,18 +63,38 @@
:disabled="dialogType === 'edit'" :disabled="dialogType === 'edit'"
> >
<el-option label="无" :value="0" /> <el-option label="无" :value="0" />
<el-option label="中奖" :value="1" /> <el-option label="中奖" :value="1" />
</el-select> </el-select>
</el-form-item> </el-form-item>
<el-form-item label="赢取平台币" prop="win_coin"> <el-form-item label="赢取平台币" prop="win_coin">
<el-input-number <el-input-number
v-model="formData.win_coin" v-model="formData.win_coin"
placeholder="请输入赢取平台币" placeholder="= 中大奖 + 摇色子中奖"
:precision="2" :precision="2"
style="width: 100%" style="width: 100%"
:disabled="dialogType === 'edit'" :disabled="dialogType === 'edit'"
/> />
</el-form-item> </el-form-item>
<el-form-item label="中大奖平台币" prop="super_win_coin">
<el-input-number
v-model="formData.super_win_coin"
placeholder="豹子时发放"
:precision="2"
:min="0"
style="width: 100%"
:disabled="dialogType === 'edit'"
/>
</el-form-item>
<el-form-item label="摇色子中奖平台币" prop="reward_win_coin">
<el-input-number
v-model="formData.reward_win_coin"
placeholder="摇色子中奖"
:precision="2"
:min="0"
style="width: 100%"
:disabled="dialogType === 'edit'"
/>
</el-form-item>
<el-form-item label="方向" prop="direction"> <el-form-item label="方向" prop="direction">
<el-select <el-select
v-model="formData.direction" v-model="formData.direction"
@@ -122,6 +142,17 @@
</div> </div>
<div class="roll-array-hint">固定 5 个数每个 16</div> <div class="roll-array-hint">固定 5 个数每个 16</div>
</el-form-item> </el-form-item>
<el-form-item label="摇取点数和" prop="roll_number">
<el-input-number
v-model="formData.roll_number"
placeholder="5 个色子点数之和530"
:min="5"
:max="30"
:precision="0"
style="width: 100%"
:disabled="dialogType === 'edit'"
/>
</el-form-item>
<el-form-item label="奖励配置" prop="reward_config_id"> <el-form-item label="奖励配置" prop="reward_config_id">
<el-select <el-select
v-model="formData.reward_config_id" v-model="formData.reward_config_id"
@@ -186,7 +217,7 @@
player_id: [{ required: true, message: '请选择玩家', trigger: 'change' }], player_id: [{ required: true, message: '请选择玩家', trigger: 'change' }],
lottery_config_id: [{ required: true, message: '请选择彩金池配置', trigger: 'change' }], lottery_config_id: [{ required: true, message: '请选择彩金池配置', trigger: 'change' }],
lottery_type: [{ required: true, message: '请选择抽奖类型', trigger: 'change' }], lottery_type: [{ required: true, message: '请选择抽奖类型', trigger: 'change' }],
is_win: [{ required: true, message: '请选择奖', trigger: 'change' }], is_win: [{ required: true, message: '请选择是否中大奖', trigger: 'change' }],
win_coin: [{ required: true, message: '赢取平台币必填', trigger: 'blur' }], win_coin: [{ required: true, message: '赢取平台币必填', trigger: 'blur' }],
rollArrayItems: [ rollArrayItems: [
{ {
@@ -219,10 +250,13 @@
lottery_type: null as number | null, lottery_type: null as number | null,
is_win: null as number | null, is_win: null as number | null,
win_coin: null as number | null, win_coin: null as number | null,
super_win_coin: null as number | null,
reward_win_coin: null as number | null,
direction: null as number | null, direction: null as number | null,
start_index: null as number | null, start_index: null as number | null,
target_index: null as number | null, target_index: null as number | null,
roll_array: null as string | number[] | null, roll_array: null as string | number[] | null,
roll_number: null as number | null,
reward_config_id: null as number | null reward_config_id: null as number | null
} }
@@ -278,10 +312,13 @@
'lottery_type', 'lottery_type',
'is_win', 'is_win',
'win_coin', 'win_coin',
'super_win_coin',
'reward_win_coin',
'direction', 'direction',
'start_index', 'start_index',
'target_index', 'target_index',
'roll_array', 'roll_array',
'roll_number',
'reward_config_id' 'reward_config_id'
] ]
keys.forEach((key) => { keys.forEach((key) => {
@@ -295,6 +332,10 @@
} }
} }
}) })
// 若后端未返回 roll_number根据摇取点数计算
if (formData.roll_number == null && formData.rollArrayItems.length === 5) {
formData.roll_number = formData.rollArrayItems.reduce((s, n) => (s ?? 0) + (n ?? 0), 0) || null
}
} }
/** 将接口的 roll_array 转为固定 5 项数组,不足补 null */ /** 将接口的 roll_array 转为固定 5 项数组,不足补 null */
@@ -331,10 +372,12 @@
const payload = { ...formData } as Record<string, unknown> const payload = { ...formData } as Record<string, unknown>
// 将 5 个输入值拼成 [1,2,3,4,5] 格式,确保每项为 16 的整数 // 将 5 个输入值拼成 [1,2,3,4,5] 格式,确保每项为 16 的整数
const items = formData.rollArrayItems const items = formData.rollArrayItems
payload.roll_array = items.map((n) => { const rollArray = items.map((n) => {
const v = n != null ? Number(n) : 1 const v = n != null ? Number(n) : 1
return Math.min(6, Math.max(1, Number.isNaN(v) ? 1 : Math.floor(v))) return Math.min(6, Math.max(1, Number.isNaN(v) ? 1 : Math.floor(v)))
}) })
payload.roll_array = rollArray
payload.roll_number = formData.roll_number ?? rollArray.reduce((s, n) => s + n, 0)
delete payload.rollArrayItems delete payload.rollArrayItems
if (props.dialogType === 'add') { if (props.dialogType === 'add') {
delete payload.id delete payload.id

View File

@@ -27,10 +27,10 @@
</el-form-item> </el-form-item>
</el-col> </el-col>
<el-col v-bind="setSpan(6)"> <el-col v-bind="setSpan(6)">
<el-form-item label="奖" prop="is_win"> <el-form-item label="是否中大奖" prop="is_win">
<el-select v-model="formData.is_win" placeholder="全部" clearable style="width: 100%"> <el-select v-model="formData.is_win" placeholder="全部" clearable style="width: 100%">
<el-option label="无" :value="0" /> <el-option label="无" :value="0" />
<el-option label="中奖" :value="1" /> <el-option label="中奖" :value="1" />
</el-select> </el-select>
</el-form-item> </el-form-item>
</el-col> </el-col>
@@ -63,6 +63,31 @@
</div> </div>
</el-form-item> </el-form-item>
</el-col> </el-col>
<el-col v-bind="setSpan(6)">
<el-form-item label="摇取点数和" prop="roll_number_min">
<div class="range-wrap">
<el-input-number
v-model="formData.roll_number_min"
placeholder="最小"
:min="5"
:max="30"
:precision="0"
controls-position="right"
class="range-input"
/>
<span class="range-sep"></span>
<el-input-number
v-model="formData.roll_number_max"
placeholder="最大"
:min="5"
:max="30"
:precision="0"
controls-position="right"
class="range-input"
/>
</div>
</el-form-item>
</el-col>
<el-col v-bind="setSpan(6)"> <el-col v-bind="setSpan(6)">
<el-form-item label="奖励配置" prop="reward_ui_text"> <el-form-item label="奖励配置" prop="reward_ui_text">
<el-input v-model="formData.reward_ui_text" placeholder="前端显示文本模糊" clearable /> <el-input v-model="formData.reward_ui_text" placeholder="前端显示文本模糊" clearable />

View File

@@ -112,7 +112,7 @@
phone: undefined, phone: undefined,
status: undefined, status: undefined,
coin: undefined, coin: undefined,
is_up: undefined lottery_config_id: undefined
}) })
// 搜索处理 // 搜索处理
@@ -127,17 +127,9 @@
return cellValue != null && cellValue !== '' ? `${cellValue}%` : '-' return cellValue != null && cellValue !== '' ? `${cellValue}%` : '-'
} }
// 倍率列展示0=正常 1=强制杀猪 2=T1高倍率 // 彩金池配置列lottery_config_id 关联 DiceLotteryConfig显示 name
const isUpFormatter = (row: any) => { const lotteryConfigNameFormatter = (row: any) =>
const cellValue = row.is_up row?.diceLotteryConfig?.name ?? (row?.lottery_config_id ? `#${row.lottery_config_id}` : '自定义')
return cellValue === 0
? '正常'
: cellValue === 1
? '强制杀猪'
: cellValue === 2
? 'T1高倍率'
: '-'
}
// 表格配置 // 表格配置
const { const {
@@ -158,23 +150,78 @@
apiFn: api.list, apiFn: api.list,
columnsFactory: () => [ columnsFactory: () => [
{ type: 'selection' }, { type: 'selection' },
{ prop: 'username', label: '用户名' }, { prop: 'username', label: '用户名', align: 'center' },
{ prop: 'phone', label: '手机号' }, { prop: 'phone', label: '手机号', align: 'center' },
{ prop: 'name', label: '昵称' }, { prop: 'name', label: '昵称', align: 'center' },
{ prop: 'status', label: '状态', width: 88, useSlot: true }, {
{ prop: 'coin', label: '平台币', width: 100, useSlot: true }, prop: 'status',
{ prop: 'is_up', label: '倍率', width: 80, formatter: isUpFormatter }, label: '状态',
{ prop: 't1_wight', label: 'T1池权重', width: 100, formatter: weightFormatter('t1_wight') }, width: 88,
{ prop: 't2_wight', label: 'T2池权重', width: 100, formatter: weightFormatter('t2_wight') }, align: 'center',
{ prop: 't3_wight', label: 'T3池权重', width: 100, formatter: weightFormatter('t3_wight') }, useSlot: true
{ prop: 't4_wight', label: 'T4池权重', width: 100, formatter: weightFormatter('t4_wight') }, },
{ prop: 't5_wight', label: 'T5池权重', width: 100, formatter: weightFormatter('t5_wight') }, {
{ prop: 'total_ticket_count', label: '总抽奖次数' }, prop: 'coin',
{ prop: 'paid_ticket_count', label: '购买抽奖次数' }, label: '平台币',
{ prop: 'free_ticket_count', label: '赠送抽奖次数' }, width: 100,
{ prop: 'created_at', label: '创建时间' }, align: 'center',
{ prop: 'updated_at', label: '更新时间' }, useSlot: true
{ prop: 'operation', label: '操作', width: 120, fixed: 'right', useSlot: true } },
{
prop: 'lottery_config_id',
label: '彩金池配置',
width: 120,
align: 'center',
formatter: (row: any) => lotteryConfigNameFormatter(row)
},
{
prop: 't1_weight',
label: 'T1池权重',
width: 80,
align: 'center',
formatter: weightFormatter('t1_weight')
},
{
prop: 't2_weight',
label: 'T2池权重',
width: 100,
align: 'center',
formatter: weightFormatter('t2_weight')
},
{
prop: 't3_weight',
label: 'T3池权重',
width: 100,
align: 'center',
formatter: weightFormatter('t3_weight')
},
{
prop: 't4_weight',
label: 'T4池权重',
width: 100,
align: 'center',
formatter: weightFormatter('t4_weight')
},
{
prop: 't5_weight',
label: 'T5池权重',
width: 100,
align: 'center',
formatter: weightFormatter('t5_weight')
},
{ prop: 'total_ticket_count', label: '总抽奖次数', align: 'center' },
{ prop: 'paid_ticket_count', label: '购买抽奖次数', align: 'center' },
{ prop: 'free_ticket_count', label: '赠送抽奖次数', align: 'center' },
{ prop: 'create_time', label: '创建时间', align: 'center' },
{ prop: 'update_time', label: '更新时间', align: 'center' },
{
prop: 'operation',
label: '操作',
width: 100,
align: 'center',
fixed: 'right',
useSlot: true
}
] ]
} }
}) })

View File

@@ -15,7 +15,13 @@
<el-input v-model="formData.name" placeholder="请输入昵称" /> <el-input v-model="formData.name" placeholder="请输入昵称" />
</el-form-item> </el-form-item>
<el-form-item label="手机号" prop="phone"> <el-form-item label="手机号" prop="phone">
<el-input v-model="formData.phone" placeholder="请输入手机号" clearable maxlength="20" show-word-limit /> <el-input
v-model="formData.phone"
placeholder="请输入手机号"
clearable
maxlength="20"
show-word-limit
/>
</el-form-item> </el-form-item>
<el-form-item label="密码" prop="password" :rules="passwordRules"> <el-form-item label="密码" prop="password" :rules="passwordRules">
<el-input <el-input
@@ -38,29 +44,98 @@
style="width: 100%" style="width: 100%"
/> />
</el-form-item> </el-form-item>
<el-form-item label="倍率" prop="is_up"> <!-- lottery_config_id = 自定义权重否则 = DiceLotteryConfig.id选择后该配置的五个 weight 会写入下方 player.*_weight -->
<el-select v-model="formData.is_up" placeholder="请选择倍率" clearable style="width: 100%"> <el-form-item label="彩金池配置" prop="lottery_config_id">
<el-option label="正常" :value="0" /> <el-select
<el-option label="强制杀猪" :value="1" /> v-model="formData.lottery_config_id"
<el-option label="T1高倍率" :value="2" /> placeholder="留空则使用下方自定义权重,或选择彩金池"
clearable
filterable
style="width: 100%"
:loading="lotteryConfigLoading"
@change="onLotteryConfigChange"
>
<el-option
v-for="item in lotteryConfigOptions"
:key="item.id"
:label="(item.name && String(item.name).trim()) || `#${item.id}`"
:value="item.id"
/>
</el-select> </el-select>
</el-form-item> </el-form-item>
<el-form-item label="T1池权重(%)" prop="t1_wight"> <!-- 当前选中的 DiceLotteryConfig 数据展示 -->
<el-slider v-model="formData.t1_wight" :min="0" :max="100" :step="0.01" show-input /> <el-form-item v-if="currentLotteryConfig" label="当前配置" class="current-config-block">
<div class="current-lottery-config">
<div class="config-row">
<span class="config-label">名称</span>
<span>{{ currentLotteryConfig.name ?? '-' }}</span>
</div>
<div class="config-row">
<span class="config-label">类型</span>
<span>{{ lotteryConfigTypeText(currentLotteryConfig.type) }}</span>
</div>
<div class="config-row">
<span class="config-label">T1T5 权重</span>
<span>{{ currentLotteryConfigWeightsText }}</span>
</div>
<div v-if="currentLotteryConfig.remark" class="config-row">
<span class="config-label">备注</span>
<span>{{ currentLotteryConfig.remark }}</span>
</div>
</div>
</el-form-item> </el-form-item>
<el-form-item label="T2池权重(%)" prop="t2_wight"> <!-- lottery_config_id 为空时自定义权重可编辑有值时来自所选 DiceLotteryConfig仅展示不可编辑 -->
<el-slider v-model="formData.t2_wight" :min="0" :max="100" :step="0.01" show-input /> <el-form-item label="T1池权重(%)" prop="t1_weight">
<el-slider
v-model="formData.t1_weight"
:min="0"
:max="100"
:step="0.01"
show-input
:disabled="!isLotteryConfigEmpty()"
/>
</el-form-item> </el-form-item>
<el-form-item label="T3池权重(%)" prop="t3_wight"> <el-form-item label="T2池权重(%)" prop="t2_weight">
<el-slider v-model="formData.t3_wight" :min="0" :max="100" :step="0.01" show-input /> <el-slider
v-model="formData.t2_weight"
:min="0"
:max="100"
:step="0.01"
show-input
:disabled="!isLotteryConfigEmpty()"
/>
</el-form-item> </el-form-item>
<el-form-item label="T4池权重(%)" prop="t4_wight"> <el-form-item label="T3池权重(%)" prop="t3_weight">
<el-slider v-model="formData.t4_wight" :min="0" :max="100" :step="0.01" show-input /> <el-slider
v-model="formData.t3_weight"
:min="0"
:max="100"
:step="0.01"
show-input
:disabled="!isLotteryConfigEmpty()"
/>
</el-form-item> </el-form-item>
<el-form-item label="T5池权重(%)" prop="t5_wight"> <el-form-item label="T4池权重(%)" prop="t4_weight">
<el-slider v-model="formData.t5_wight" :min="0" :max="100" :step="0.01" show-input /> <el-slider
v-model="formData.t4_weight"
:min="0"
:max="100"
:step="0.01"
show-input
:disabled="!isLotteryConfigEmpty()"
/>
</el-form-item> </el-form-item>
<el-form-item> <el-form-item label="T5池权重(%)" prop="t5_weight">
<el-slider
v-model="formData.t5_weight"
:min="0"
:max="100"
:step="0.01"
show-input
:disabled="!isLotteryConfigEmpty()"
/>
</el-form-item>
<el-form-item v-if="isLotteryConfigEmpty()">
<div class="text-gray-500 text-sm"> <div class="text-gray-500 text-sm">
五个池权重总和<span :class="Math.abs(weightsSum - 100) > 0.01 ? 'text-red-500' : ''">{{ 五个池权重总和<span :class="Math.abs(weightsSum - 100) > 0.01 ? 'text-red-500' : ''">{{
weightsSum weightsSum
@@ -78,9 +153,12 @@
<script setup lang="ts"> <script setup lang="ts">
import api from '../../../api/player/index' import api from '../../../api/player/index'
import lotteryConfigApi from '../../../api/lottery_config/index'
import { ElMessage } from 'element-plus' import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus' import type { FormInstance, FormRules } from 'element-plus'
const WEIGHT_FIELDS = ['t1_weight', 't2_weight', 't3_weight', 't4_weight', 't5_weight'] as const
interface Props { interface Props {
modelValue: boolean modelValue: boolean
dialogType: string dialogType: string
@@ -107,9 +185,20 @@
set: (value) => emit('update:modelValue', value) set: (value) => emit('update:modelValue', value)
}) })
const WEIGHT_KEYS = ['t1_wight', 't2_wight', 't3_wight', 't4_wight', 't5_wight'] as const
const weightsSum = computed(() => { const weightsSum = computed(() => {
return WEIGHT_KEYS.reduce((sum, key) => sum + Number(formData[key] ?? 0), 0) return WEIGHT_FIELDS.reduce((sum, key) => sum + Number(formData[key] ?? 0), 0)
})
/** 当前彩金池配置的 T1T5 权重展示文案 */
const currentLotteryConfigWeightsText = computed(() => {
const c = currentLotteryConfig.value
if (!c) return '-'
const t1 = c.t1_weight ?? 0
const t2 = c.t2_weight ?? 0
const t3 = c.t3_weight ?? 0
const t4 = c.t4_weight ?? 0
const t5 = c.t5_weight ?? 0
return `${t1}% / ${t2}% / ${t3}% / ${t4}% / ${t5}%`
}) })
/** 新增时密码必填,编辑时选填 */ /** 新增时密码必填,编辑时选填 */
@@ -133,16 +222,60 @@
password: '', password: '',
status: 1 as number, status: 1 as number,
coin: 0 as number, coin: 0 as number,
is_up: null as number | null, /** 彩金池配置 ID空 = 自定义权重,否则 = DiceLotteryConfig.id */
t1_wight: 0 as number, lottery_config_id: null as number | null,
t2_wight: 0 as number, t1_weight: 0 as number,
t3_wight: 0 as number, t2_weight: 0 as number,
t4_wight: 0 as number, t3_weight: 0 as number,
t5_wight: 0 as number t4_weight: 0 as number,
t5_weight: 0 as number
} }
const formData = reactive({ ...initialFormData }) const formData = reactive({ ...initialFormData })
/** 彩金池配置下拉选项DiceLotteryConfig id、name */
const lotteryConfigOptions = ref<Array<{ id: number; name: string }>>([])
/** 彩金池选项加载中 */
const lotteryConfigLoading = ref(false)
/** 当前选中的 DiceLotteryConfig 完整数据(用于展示) */
const currentLotteryConfig = ref<Record<string, any> | null>(null)
function lotteryConfigTypeText(type: unknown): string {
const t = Number(type)
if (t === 0) return '付费'
if (t === 1) return '赠送'
return t ? `类型${t}` : '-'
}
/** 是否为空/自定义权重(未选彩金池或选 0 */
function isLotteryConfigEmpty(): boolean {
const v = formData.lottery_config_id
return v == null || v === 0
}
/** 根据当前 lottery_config_id 加载 DiceLotteryConfig并将五个权重写入当前 player.*_weight */
async function loadCurrentLotteryConfig() {
const id = formData.lottery_config_id
if (id == null || id === 0) {
currentLotteryConfig.value = null
return
}
try {
const res = await lotteryConfigApi.read(id)
const row = (res as any)?.data ?? (res as any)
if (row && typeof row === 'object') {
currentLotteryConfig.value = row
WEIGHT_FIELDS.forEach((key) => {
;(formData as any)[key] = Number(row[key] ?? 0)
})
} else {
currentLotteryConfig.value = null
}
} catch {
currentLotteryConfig.value = null
}
}
watch( watch(
() => props.modelValue, () => props.modelValue,
(newVal) => { (newVal) => {
@@ -150,11 +283,51 @@
} }
) )
/** 选择彩金池后,拉取该配置的五个权重并写入当前 player.*_weight并更新当前配置展示 */
async function onLotteryConfigChange(lotteryConfigId: number | null | undefined) {
if (lotteryConfigId == null || lotteryConfigId === 0) {
currentLotteryConfig.value = null
return
}
try {
const res = await lotteryConfigApi.read(lotteryConfigId)
const row = (res as any)?.data ?? (res as any)
if (row && typeof row === 'object') {
WEIGHT_FIELDS.forEach((key) => {
;(formData as any)[key] = Number(row[key] ?? 0)
})
currentLotteryConfig.value = row
} else {
currentLotteryConfig.value = null
}
} catch (err) {
console.warn('拉取彩金池配置失败', err)
currentLotteryConfig.value = null
}
}
const initPage = async () => { const initPage = async () => {
currentLotteryConfig.value = null
Object.assign(formData, initialFormData) Object.assign(formData, initialFormData)
await loadLotteryConfigOptions()
if (props.data) { if (props.data) {
await nextTick() await nextTick()
initForm() initForm()
if (!isLotteryConfigEmpty()) {
await loadCurrentLotteryConfig()
}
}
}
/** 从玩家控制器获取 DiceLotteryConfig id/name 列表,供 lottery_config_id 下拉使用 */
async function loadLotteryConfigOptions() {
lotteryConfigLoading.value = true
try {
lotteryConfigOptions.value = await api.getLotteryConfigOptions()
} catch {
lotteryConfigOptions.value = []
} finally {
lotteryConfigLoading.value = false
} }
} }
@@ -162,12 +335,12 @@
'id', 'id',
'status', 'status',
'coin', 'coin',
'is_up', 'lottery_config_id',
't1_wight', 't1_weight',
't2_wight', 't2_weight',
't3_wight', 't3_weight',
't4_wight', 't4_weight',
't5_wight' 't5_weight'
] ]
const initForm = () => { const initForm = () => {
@@ -180,8 +353,14 @@
} }
const val = props.data[key] const val = props.data[key]
if (numKeys.includes(key)) { if (numKeys.includes(key)) {
;(formData as any)[key] = if (key === 'id') {
key === 'id' ? (val != null ? Number(val) || null : null) : Number(val) || 0 ;(formData as any)[key] = val != null ? Number(val) || null : null
} else if (key === 'lottery_config_id') {
const num = Number(val)
;(formData as any)[key] = val != null && !Number.isNaN(num) && num !== 0 ? num : null
} else {
;(formData as any)[key] = Number(val) || 0
}
} else { } else {
;(formData as any)[key] = val ?? '' ;(formData as any)[key] = val ?? ''
} }
@@ -197,11 +376,15 @@
if (!formRef.value) return if (!formRef.value) return
try { try {
await formRef.value.validate() await formRef.value.validate()
if (Math.abs(weightsSum.value - 100) > 0.01) { const useCustomWeights = isLotteryConfigEmpty()
if (useCustomWeights && Math.abs(weightsSum.value - 100) > 0.01) {
ElMessage.warning('五个池权重总和必须为100%') ElMessage.warning('五个池权重总和必须为100%')
return return
} }
const payload = { ...formData } const payload = { ...formData }
if (isLotteryConfigEmpty()) {
;(payload as any).lottery_config_id = null
}
if (props.dialogType === 'edit' && !payload.password) { if (props.dialogType === 'edit' && !payload.password) {
delete (payload as any).password delete (payload as any).password
} }
@@ -219,3 +402,31 @@
} }
} }
</script> </script>
<style lang="scss" scoped>
.current-config-block {
margin-bottom: 12px;
}
.current-lottery-config {
padding: 10px 12px;
background: var(--el-fill-color-light);
border-radius: 6px;
font-size: 13px;
color: var(--el-text-color-regular);
.config-row {
margin-bottom: 6px;
line-height: 1.5;
&:last-child {
margin-bottom: 0;
}
}
.config-label {
color: var(--el-text-color-secondary);
margin-right: 4px;
}
}
</style>

View File

@@ -44,11 +44,19 @@
</el-form-item> </el-form-item>
</el-col> </el-col>
<el-col v-bind="setSpan(6)"> <el-col v-bind="setSpan(6)">
<el-form-item label="倍率" prop="is_up"> <el-form-item label="彩金池配置" prop="lottery_config_id">
<el-select v-model="formData.is_up" placeholder="全部" clearable style="width: 100%"> <el-select
<el-option label="正常" :value="0" /> v-model="formData.lottery_config_id"
<el-option label="强制杀猪" :value="1" /> placeholder="全部"
<el-option label="T1高倍率" :value="2" /> clearable
style="width: 100%"
>
<el-option
v-for="item in lotteryConfigOptions"
:key="item.id"
:label="item.name"
:value="item.id"
/>
</el-select> </el-select>
</el-form-item> </el-form-item>
</el-col> </el-col>
@@ -56,6 +64,8 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import api from '../../../api/player/index'
interface Props { interface Props {
modelValue: Record<string, any> modelValue: Record<string, any>
} }
@@ -67,6 +77,16 @@
const props = defineProps<Props>() const props = defineProps<Props>()
const emit = defineEmits<Emits>() const emit = defineEmits<Emits>()
const isExpanded = ref<boolean>(false) const isExpanded = ref<boolean>(false)
const lotteryConfigOptions = ref<Array<{ id: number; name: string }>>([])
/** 从玩家控制器获取 DiceLotteryConfig id/name 列表,用于 lottery_config_id 筛选 */
onMounted(async () => {
try {
lotteryConfigOptions.value = await api.getLotteryConfigOptions()
} catch {
lotteryConfigOptions.value = []
}
})
const searchBarRef = ref() const searchBarRef = ref()
const formData = computed({ const formData = computed({

View File

@@ -8,23 +8,23 @@
<ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData"> <ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData">
<template #left> <template #left>
<ElSpace wrap> <ElSpace wrap>
<ElButton v-permission="'dice:player_ticket_record:index:save'" @click="showDialog('add')" v-ripple> <!-- <ElButton v-permission="'dice:player_ticket_record:index:save'" @click="showDialog('add')" v-ripple>-->
<template #icon> <!-- <template #icon>-->
<ArtSvgIcon icon="ri:add-fill" /> <!-- <ArtSvgIcon icon="ri:add-fill" />-->
</template> <!-- </template>-->
新增 <!-- 新增-->
</ElButton> <!-- </ElButton>-->
<ElButton <!-- <ElButton-->
v-permission="'dice:player_ticket_record:index:destroy'" <!-- v-permission="'dice:player_ticket_record:index:destroy'"-->
:disabled="selectedRows.length === 0" <!-- :disabled="selectedRows.length === 0"-->
@click="deleteSelectedRows(api.delete, refreshData)" <!-- @click="deleteSelectedRows(api.delete, refreshData)"-->
v-ripple <!-- v-ripple-->
> <!-- >-->
<template #icon> <!-- <template #icon>-->
<ArtSvgIcon icon="ri:delete-bin-5-line" /> <!-- <ArtSvgIcon icon="ri:delete-bin-5-line" />-->
</template> <!-- </template>-->
删除 <!-- 删除-->
</ElButton> <!-- </ElButton>-->
</ElSpace> </ElSpace>
</template> </template>
</ArtTableHeader> </ArtTableHeader>
@@ -50,11 +50,11 @@
type="secondary" type="secondary"
@click="showDialog('edit', row)" @click="showDialog('edit', row)"
/> />
<SaButton <!-- <SaButton-->
v-permission="'dice:player_ticket_record:index:destroy'" <!-- v-permission="'dice:player_ticket_record:index:destroy'"-->
type="error" <!-- type="error"-->
@click="deleteRow(row, api.delete, refreshData)" <!-- @click="deleteRow(row, api.delete, refreshData)"-->
/> <!-- />-->
</div> </div>
</template> </template>
</ArtTable> </ArtTable>
@@ -77,7 +77,6 @@
import TableSearch from './modules/table-search.vue' import TableSearch from './modules/table-search.vue'
import EditDialog from './modules/edit-dialog.vue' import EditDialog from './modules/edit-dialog.vue'
// 搜索表单 // 搜索表单
const searchForm = ref<Record<string, unknown>>({ const searchForm = ref<Record<string, unknown>>({
username: undefined, username: undefined,
@@ -127,16 +126,28 @@
const usernameFormatter = (row: Record<string, any>) => const usernameFormatter = (row: Record<string, any>) =>
row?.dicePlayer?.username ?? row?.player_id ?? '-' row?.dicePlayer?.username ?? row?.player_id ?? '-'
return [ return [
{ type: 'selection' }, // { type: 'selection' },
{ prop: 'id', label: 'ID', width: 80 }, { prop: 'id', label: 'ID', width: 80, align: 'center' },
{ prop: 'player_id', label: '玩家用户名', formatter: (row: Record<string, any>) => usernameFormatter(row) }, {
{ prop: 'use_coins', label: '消耗硬币' }, prop: 'player_id',
{ prop: 'total_ticket_count', label: '总抽奖次数' }, label: '玩家用户名',
{ prop: 'paid_ticket_count', label: '购买抽奖次数' }, align: 'center',
{ prop: 'free_ticket_count', label: '赠送抽奖次数' }, formatter: (row: Record<string, any>) => usernameFormatter(row)
{ prop: 'remark', label: '备注', width: 100, showOverflowTooltip: true }, },
{ prop: 'create_time', label: '创建时间', width: 170 }, { prop: 'use_coins', label: '消耗硬币', align: 'center' },
{ prop: 'operation', label: '操作', width: 100, fixed: 'right', useSlot: true } { prop: 'total_ticket_count', label: '总抽奖次数', align: 'center' },
{ prop: 'paid_ticket_count', label: '购买抽奖次数', align: 'center' },
{ prop: 'free_ticket_count', label: '赠送抽奖次数', align: 'center' },
{ prop: 'remark', label: '备注', width: 100, align: 'center', showOverflowTooltip: true },
{ prop: 'create_time', label: '创建时间', width: 170, align: 'center' },
{
prop: 'operation',
label: '操作',
width: 60,
align: 'center',
fixed: 'right',
useSlot: true
}
] ]
} }
} }
@@ -148,10 +159,9 @@
dialogVisible, dialogVisible,
dialogData, dialogData,
showDialog, showDialog,
deleteRow, // deleteRow,
deleteSelectedRows, // deleteSelectedRows,
handleSelectionChange, handleSelectionChange
selectedRows // selectedRows
} = useSaiAdmin() } = useSaiAdmin()
</script> </script>

View File

@@ -26,7 +26,12 @@
</el-select> </el-select>
</el-form-item> </el-form-item>
<el-form-item label="消耗硬币" prop="use_coins"> <el-form-item label="消耗硬币" prop="use_coins">
<el-input-number v-model="formData.use_coins" placeholder="请输入消耗硬币" :min="0" /> <el-input-number
v-model="formData.use_coins"
placeholder="请输入消耗硬币"
:min="0"
:disabled="dialogType === 'edit'"
/>
</el-form-item> </el-form-item>
<el-form-item label="购买抽奖次数" prop="paid_ticket_count"> <el-form-item label="购买抽奖次数" prop="paid_ticket_count">
<el-input-number <el-input-number
@@ -34,6 +39,7 @@
placeholder="请输入购买抽奖次数" placeholder="请输入购买抽奖次数"
:min="0" :min="0"
@change="onTicketCountChange" @change="onTicketCountChange"
:disabled="dialogType === 'edit'"
/> />
</el-form-item> </el-form-item>
<el-form-item label="赠送抽奖次数" prop="free_ticket_count"> <el-form-item label="赠送抽奖次数" prop="free_ticket_count">
@@ -42,6 +48,7 @@
placeholder="请输入赠送抽奖次数" placeholder="请输入赠送抽奖次数"
:min="0" :min="0"
@change="onTicketCountChange" @change="onTicketCountChange"
:disabled="dialogType === 'edit'"
/> />
</el-form-item> </el-form-item>
<el-form-item label="总抽奖次数" prop="total_ticket_count"> <el-form-item label="总抽奖次数" prop="total_ticket_count">
@@ -60,6 +67,8 @@
placeholder="请输入备注(必填)" placeholder="请输入备注(必填)"
maxlength="500" maxlength="500"
show-word-limit show-word-limit
style="width: 100%"
:disabled="dialogType === 'edit'"
/> />
</el-form-item> </el-form-item>
</el-form> </el-form>

View File

@@ -7,29 +7,29 @@
<!-- 表格头部 --> <!-- 表格头部 -->
<ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData"> <ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData">
<template #left> <template #left>
<ElSpace wrap> <!-- <ElSpace wrap>-->
<ElButton <!-- <ElButton-->
v-permission="'dice:player_wallet_record:index:save'" <!-- v-permission="'dice:player_wallet_record:index:save'"-->
@click="showDialog('add')" <!-- @click="showDialog('add')"-->
v-ripple <!-- v-ripple-->
> <!-- >-->
<template #icon> <!-- <template #icon>-->
<ArtSvgIcon icon="ri:add-fill" /> <!-- <ArtSvgIcon icon="ri:add-fill" />-->
</template> <!-- </template>-->
新增 <!-- 新增-->
</ElButton> <!-- </ElButton>-->
<ElButton <!-- <ElButton-->
v-permission="'dice:player_wallet_record:index:destroy'" <!-- v-permission="'dice:player_wallet_record:index:destroy'"-->
:disabled="selectedRows.length === 0" <!-- :disabled="selectedRows.length === 0"-->
@click="deleteSelectedRows(api.delete, refreshData)" <!-- @click="deleteSelectedRows(api.delete, refreshData)"-->
v-ripple <!-- v-ripple-->
> <!-- >-->
<template #icon> <!-- <template #icon>-->
<ArtSvgIcon icon="ri:delete-bin-5-line" /> <!-- <ArtSvgIcon icon="ri:delete-bin-5-line" />-->
</template> <!-- </template>-->
删除 <!-- 删除-->
</ElButton> <!-- </ElButton>-->
</ElSpace> <!-- </ElSpace>-->
</template> </template>
</ArtTableHeader> </ArtTableHeader>
@@ -60,11 +60,11 @@
type="secondary" type="secondary"
@click="showDialog('edit', row)" @click="showDialog('edit', row)"
/> />
<SaButton <!-- <SaButton-->
v-permission="'dice:player_wallet_record:index:destroy'" <!-- v-permission="'dice:player_wallet_record:index:destroy'"-->
type="error" <!-- type="error"-->
@click="deleteRow(row, api.delete, refreshData)" <!-- @click="deleteRow(row, api.delete, refreshData)"-->
/> <!-- />-->
</div> </div>
</template> </template>
</ArtTable> </ArtTable>
@@ -203,7 +203,7 @@
{ {
prop: 'operation', prop: 'operation',
label: '操作', label: '操作',
width: 100, width: 60,
align: 'center', align: 'center',
fixed: 'right', fixed: 'right',
useSlot: true useSlot: true
@@ -218,10 +218,10 @@
dialogVisible, dialogVisible,
dialogData, dialogData,
showDialog, showDialog,
deleteRow, // deleteRow,
deleteSelectedRows, // deleteSelectedRows,
handleSelectionChange, handleSelectionChange
selectedRows // selectedRows
} = useSaiAdmin() } = useSaiAdmin()
</script> </script>

View File

@@ -27,7 +27,13 @@
</el-select> </el-select>
</el-form-item> </el-form-item>
<el-form-item label="类型" prop="type"> <el-form-item label="类型" prop="type">
<el-select v-model="formData.type" placeholder="请选择类型" clearable style="width: 100%"> <el-select
v-model="formData.type"
placeholder="请选择类型"
clearable
style="width: 100%"
:disabled="dialogType === 'edit'"
>
<el-option label="充值" :value="0" /> <el-option label="充值" :value="0" />
<el-option label="提现" :value="1" /> <el-option label="提现" :value="1" />
<el-option label="购买抽奖次数" :value="2" /> <el-option label="购买抽奖次数" :value="2" />
@@ -42,6 +48,7 @@
:precision="2" :precision="2"
style="width: 100%" style="width: 100%"
@change="onCoinChange" @change="onCoinChange"
:disabled="dialogType === 'edit'"
/> />
</el-form-item> </el-form-item>
<el-form-item label="钱包操作前" prop="wallet_before"> <el-form-item label="钱包操作前" prop="wallet_before">
@@ -70,6 +77,7 @@
placeholder="选填" placeholder="选填"
maxlength="500" maxlength="500"
show-word-limit show-word-limit
:disabled="dialogType === 'edit'"
/> />
</el-form-item> </el-form-item>
</el-form> </el-form>

View File

@@ -8,27 +8,27 @@
<ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData"> <ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData">
<template #left> <template #left>
<ElSpace wrap> <ElSpace wrap>
<ElButton <!-- <ElButton-->
v-permission="'dice:reward_config:index:save'" <!-- v-permission="'dice:reward_config:index:save'"-->
@click="showDialog('add')" <!-- @click="showDialog('add')"-->
v-ripple <!-- v-ripple-->
> <!-- >-->
<template #icon> <!-- <template #icon>-->
<ArtSvgIcon icon="ri:add-fill" /> <!-- <ArtSvgIcon icon="ri:add-fill" />-->
</template> <!-- </template>-->
新增 <!-- 新增-->
</ElButton> <!-- </ElButton>-->
<ElButton <!-- <ElButton-->
v-permission="'dice:reward_config:index:destroy'" <!-- v-permission="'dice:reward_config:index:destroy'"-->
:disabled="selectedRows.length === 0" <!-- :disabled="selectedRows.length === 0"-->
@click="deleteSelectedRows(api.delete, refreshData)" <!-- @click="deleteSelectedRows(api.delete, refreshData)"-->
v-ripple <!-- v-ripple-->
> <!-- >-->
<template #icon> <!-- <template #icon>-->
<ArtSvgIcon icon="ri:delete-bin-5-line" /> <!-- <ArtSvgIcon icon="ri:delete-bin-5-line" />-->
</template> <!-- </template>-->
删除 <!-- 删除-->
</ElButton> <!-- </ElButton>-->
</ElSpace> </ElSpace>
</template> </template>
</ArtTableHeader> </ArtTableHeader>
@@ -54,11 +54,11 @@
type="secondary" type="secondary"
@click="showDialog('edit', row)" @click="showDialog('edit', row)"
/> />
<SaButton <!-- <SaButton-->
v-permission="'dice:reward_config:index:destroy'" <!-- v-permission="'dice:reward_config:index:destroy'"-->
type="error" <!-- type="error"-->
@click="deleteRow(row, api.delete, refreshData)" <!-- @click="deleteRow(row, api.delete, refreshData)"-->
/> <!-- />-->
</div> </div>
</template> </template>
</ArtTable> </ArtTable>
@@ -116,14 +116,22 @@
apiFn: api.list, apiFn: api.list,
apiParams: { limit: 100 }, apiParams: { limit: 100 },
columnsFactory: () => [ columnsFactory: () => [
{ type: 'selection' }, // { type: 'selection' },
{ prop: 'id', label: 'ID(索引)', width: 80 }, { prop: 'id', label: 'ID(索引)', width: 80, align: 'center' },
{ prop: 'grid_number', label: '色子点数' }, { prop: 'grid_number', label: '色子点数', align: 'center' },
{ prop: 'ui_text', label: '前端显示文本' }, { prop: 'ui_text', label: '前端显示文本', align: 'center' },
{ prop: 'real_ev', label: '真实资金结算' }, { prop: 'real_ev', label: '真实资金结算', align: 'center' },
{ prop: 'tier', label: '所属档位', sortable: true }, { prop: 'tier', label: '所属档位', sortable: true, align: 'center' },
// { prop: 'create_time', label: '创建时间', sortable: true }, { prop: 'weight', label: '权重(%)', width: 100, align: 'center' },
{ prop: 'operation', label: '操作', width: 100, fixed: 'right', useSlot: true } // { prop: 'create_time', label: '创建时间', sortable: true, align: 'center' },
{
prop: 'operation',
label: '操作',
width: 60,
align: 'center',
fixed: 'right',
useSlot: true
}
] ]
} }
}) })
@@ -134,9 +142,9 @@
dialogVisible, dialogVisible,
dialogData, dialogData,
showDialog, showDialog,
deleteRow, // deleteRow,
deleteSelectedRows, // deleteSelectedRows,
handleSelectionChange, handleSelectionChange
selectedRows // selectedRows
} = useSaiAdmin() } = useSaiAdmin()
</script> </script>

View File

@@ -9,7 +9,11 @@
> >
<el-form ref="formRef" :model="formData" :rules="rules" label-width="120px"> <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
<el-form-item label="色子点数" prop="grid_number"> <el-form-item label="色子点数" prop="grid_number">
<el-input-number v-model="formData.grid_number" placeholder="请输入色子点数" /> <el-input-number
v-model="formData.grid_number"
placeholder="请输入色子点数"
:disabled="dialogType === 'edit'"
/>
</el-form-item> </el-form-item>
<el-form-item label="前端显示文本" prop="ui_text"> <el-form-item label="前端显示文本" prop="ui_text">
<el-input v-model="formData.ui_text" placeholder="请输入前端显示文本" /> <el-input v-model="formData.ui_text" placeholder="请输入前端显示文本" />
@@ -23,14 +27,29 @@
placeholder="请选择所属档位" placeholder="请选择所属档位"
clearable clearable
style="width: 100%" style="width: 100%"
:disabled="dialogType === 'edit'"
> >
<el-option label="T1" value="T1" /> <el-option label="T1" value="T1" />
<el-option label="T2" value="T2" /> <el-option label="T2" value="T2" />
<el-option label="T3" value="T3" /> <el-option label="T3" value="T3" />
<el-option label="T4" value="T4" /> <el-option label="T4" value="T4" />
<el-option label="T5" value="T5" /> <el-option label="T5" value="T5" />
<el-option label="BIGWIN超级大奖" value="BIGWIN" />
</el-select> </el-select>
</el-form-item> </el-form-item>
<el-form-item v-if="formData.tier === 'BIGWIN'" label="权重(%)" prop="weight">
<el-slider
v-model="formData.weight"
:min="0"
:max="100"
:step="0.01"
:disabled="isWeightFixed100"
show-input
/>
<div v-if="isWeightFixed100" class="weight-fixed-hint">
色子点数 530 固定 100% 豹子不可修改权重
</div>
</el-form-item>
<el-form-item label="备注" prop="remark"> <el-form-item label="备注" prop="remark">
<el-input <el-input
v-model="formData.remark" v-model="formData.remark"
@@ -83,6 +102,11 @@
set: (value) => emit('update:modelValue', value) set: (value) => emit('update:modelValue', value)
}) })
/** tier=BIGWIN 且 grid_number 为 5 或 30 时权重固定 100%,不可修改 */
const isWeightFixed100 = computed(
() => formData.tier === 'BIGWIN' && (formData.grid_number === 5 || formData.grid_number === 30)
)
/** /**
* 表单验证规则 * 表单验证规则
*/ */
@@ -90,7 +114,24 @@
grid_number: [{ required: true, message: '色子点数必需填写', trigger: 'blur' }], grid_number: [{ required: true, message: '色子点数必需填写', trigger: 'blur' }],
ui_text: [{ required: true, message: '前端显示文本必需填写', trigger: 'blur' }], ui_text: [{ required: true, message: '前端显示文本必需填写', trigger: 'blur' }],
real_ev: [{ required: true, message: '真实资金结算必需填写', trigger: 'blur' }], real_ev: [{ required: true, message: '真实资金结算必需填写', trigger: 'blur' }],
tier: [{ required: true, message: '所属档位必需填写', trigger: 'blur' }] tier: [{ required: true, message: '所属档位必需填写', trigger: 'blur' }],
weight: [
{
validator: (_rule: unknown, value: number | null, callback: (e?: Error) => void) => {
if (formData.tier !== 'BIGWIN') {
callback()
return
}
const n = value != null ? Number(value) : NaN
if (Number.isNaN(n) || n < 0 || n > 100) {
callback(new Error('权重仅 BIGWIN 可设定,且必须为 0-100%'))
return
}
callback()
},
trigger: 'blur'
}
]
}) })
/** /**
@@ -102,6 +143,7 @@
ui_text: '', ui_text: '',
real_ev: '', real_ev: '',
tier: '', tier: '',
weight: 0 as number,
remark: '' remark: ''
} }
@@ -122,6 +164,19 @@
} }
) )
/** 当 BIGWIN 且 grid_number 为 5 或 30 时,权重固定为 100 便于展示 */
watch(
() => [formData.tier, formData.grid_number],
() => {
if (
formData.tier === 'BIGWIN' &&
(formData.grid_number === 5 || formData.grid_number === 30)
) {
formData.weight = 100
}
}
)
/** /**
* 初始化页面数据 * 初始化页面数据
*/ */
@@ -136,14 +191,21 @@
} }
/** /**
* 初始化表单数据 * 初始化表单数据(数值字段转为 number便于滑块/输入框正确回显)
*/ */
const initForm = () => { const initForm = () => {
if (props.data) { if (!props.data) return
for (const key in formData) { const numKeys = ['id', 'grid_number', 'real_ev', 'weight']
if (props.data[key] != null && props.data[key] != undefined) { for (const key of Object.keys(formData)) {
;(formData as any)[key] = props.data[key] if (!(key in props.data)) continue
} const val = props.data[key]
if (val == null || val === undefined) continue
if (numKeys.includes(key)) {
const numVal = Number(val)
;(formData as Record<string, unknown>)[key] =
key === 'id' ? numVal || null : Number.isNaN(numVal) ? 0 : numVal
} else {
;(formData as Record<string, unknown>)[key] = val ?? ''
} }
} }
} }
@@ -163,11 +225,20 @@
if (!formRef.value) return if (!formRef.value) return
try { try {
await formRef.value.validate() await formRef.value.validate()
const payload = { ...formData }
if (payload.tier !== 'BIGWIN') {
payload.weight = 0
} else if (payload.grid_number === 5 || payload.grid_number === 30) {
payload.weight = 100
} else {
const w = Number(payload.weight)
payload.weight = Number.isNaN(w) ? 0 : Math.max(0, Math.min(100, w))
}
if (props.dialogType === 'add') { if (props.dialogType === 'add') {
await api.save(formData) await api.save(payload)
ElMessage.success('新增成功') ElMessage.success('新增成功')
} else { } else {
await api.update(formData) await api.update(payload)
ElMessage.success('修改成功') ElMessage.success('修改成功')
} }
emit('success') emit('success')
@@ -177,3 +248,11 @@
} }
} }
</script> </script>
<style lang="scss" scoped>
.weight-fixed-hint {
margin-top: 6px;
font-size: 12px;
color: var(--el-text-color-secondary);
}
</style>

View File

@@ -16,15 +16,18 @@ REDIS_PORT = 6379
REDIS_PASSWORD = '' REDIS_PASSWORD = ''
REDIS_DB = 0 REDIS_DB = 0
# 游戏地址,用于 /api/v1/getGameUrl 返回
GAME_URL = dice-game.yuliao666.top
# API 鉴权与用户(可选,不填则用默认值) # API 鉴权与用户(可选,不填则用默认值)
# authToken 签名密钥(必填,与客户端约定,用于 signature 校验) # authToken 签名密钥(必填,与客户端约定,用于 signature 校验)
API_AUTH_TOKEN_SECRET = xF75oK91TQj13s0UmNIr1NBWMWGfflNO API_AUTH_TOKEN_SECRET = xF75oK91TQj13s0UmNIr1NBWMWGfflNO
# authToken 时间戳允许误差秒数,防重放,默认 300 # authToken 时间戳允许误差秒数,防重放,默认 300
API_AUTH_TOKEN_TIME_TOLERANCE = 300 API_AUTH_TOKEN_TIME_TOLERANCE = 300
API_AUTH_TOKEN_EXP = 86400 API_AUTH_TOKEN_EXP = 86400
# API_USER_TOKEN_EXP = 604800 # API_USER_TOKEN_EXP = 604800
API_USER_CACHE_EXPIRE = 86400 API_USER_CACHE_EXPIRE = 86400
API_USER_ENCRYPT_KEY = Wj818SK8dhKBKNOY3PUTmZfhQDMCXEZi API_USER_ENCRYPT_KEY = Wj818SK8dhKBKNOY3PUTmZfhQDMCXEZi
# 验证码配置,支持cache|session # 验证码配置,支持cache|session
CAPTCHA_MODE = cache CAPTCHA_MODE = cache
@@ -32,3 +35,6 @@ LOGIN_CAPTCHA_ENABLE = false
#前端目录 #前端目录
FRONTEND_DIR = saiadmin-vue FRONTEND_DIR = saiadmin-vue
#生成环境
APP_DEBUG = false

View File

@@ -6,49 +6,78 @@ namespace app\api\cache;
use support\think\Cache; use support\think\Cache;
/** /**
* 按设备标识存储当前有效的 auth-token同一设备只保留最新一个旧 token 自动失效 * 平台 auth-token Redis 缓存
* 用于 /api/v1/authToken 鉴权接口颁发的 token 存储与校验
*/ */
class AuthTokenCache class AuthTokenCache
{ {
private static function prefix(): string private static function devicePrefix(): string
{ {
return config('api.auth_token_device_prefix', 'api:auth_token:'); return config('api.auth_token_device_prefix', 'api:auth_token:');
} }
/** private static function tokenPrefix(): string
* 设置该设备当前有效的 auth-token会覆盖同设备之前的 token使旧 token 失效)
* @param string $device 设备标识,如 dice
* @param string $token 完整 auth-token 字符串
* @param int $ttl 过期时间(秒),应与 auth_token_exp 一致
*/
public static function setDeviceToken(string $device, string $token, int $ttl): bool
{ {
if ($device === '' || $ttl <= 0) { return config('api.auth_token_prefix', 'api:auth_token:t:');
}
private static function expire(): int
{
return (int) config('api.auth_token_exp', 86400);
}
/**
* 存储 auth-token同一 agent_id 只保留最新一个)
* @param string $agentId 代理 ID
* @param string $token 生成的 auth-token
*/
public static function setToken(string $agentId, string $token): bool
{
if ($agentId === '' || $token === '') {
return false; return false;
} }
$key = self::prefix() . $device; $exp = self::expire();
return Cache::set($key, $token, $ttl); if ($exp <= 0) {
return false;
}
$oldToken = Cache::get(self::devicePrefix() . $agentId);
if ($oldToken !== null && $oldToken !== '') {
Cache::delete(self::tokenPrefix() . $oldToken);
}
Cache::set(self::tokenPrefix() . $token, $agentId, $exp);
Cache::set(self::devicePrefix() . $agentId, $token, $exp);
return true;
} }
/** /**
* 获取该设备当前有效的 auth-token不存在或已过期返回 null * 根据 agent_id 获取当前有效的 token不存在或已过期返回 null
*/ */
public static function getDeviceToken(string $device): ?string public static function getTokenByAgentId(string $agentId): ?string
{ {
if ($device === '') { if ($agentId === '') {
return null; return null;
} }
$key = self::prefix() . $device; $val = Cache::get(self::devicePrefix() . $agentId);
$value = Cache::get($key); return $val !== null && $val !== '' ? (string) $val : null;
return $value !== null && $value !== '' ? (string) $value : null;
} }
/** /**
* 校验请求中的 token 是否为该设备当前唯一有效 token * 根据 auth-token 获取 agent_id不存在或已过期返回 null
*/ */
public static function isCurrentToken(string $device, string $token): bool public static function getAgentIdByToken(string $token): ?string
{ {
$current = self::getDeviceToken($device); if ($token === '') {
return $current !== null && $current === $token; return null;
}
$val = Cache::get(self::tokenPrefix() . $token);
return $val !== null && $val !== '' ? (string) $val : null;
}
/**
* 校验 auth-token 是否有效
*/
public static function isValidToken(string $token): bool
{
return self::getAgentIdByToken($token) !== null;
} }
} }

View File

@@ -178,4 +178,108 @@ class UserCache
$current = self::getCurrentUserToken($userId); $current = self::getCurrentUserToken($userId);
return $current !== null && $current === $token; return $current !== null && $current === $token;
} }
/** 按 username 的登录会话 key 前缀token 中间件:存在即视为已登录) */
private static function sessionUsernamePrefix(): string
{
return config('api.session_username_prefix', 'api:user:session:');
}
private static function sessionExpire(): int
{
return (int) config('api.session_expire', 604800);
}
/** 设置 username 当前有效 tokenJWT重新登录会覆盖实现单点登录 */
public static function setSessionByUsername(string $username, string $token): bool
{
if ($username === '' || $token === '') {
return false;
}
$key = self::sessionUsernamePrefix() . $username;
return Cache::set($key, $token, self::sessionExpire());
}
/** 获取 username 当前在服务端登记的有效 tokenJWT不存在返回 null */
public static function getSessionTokenByUsername(string $username): ?string
{
if ($username === '') {
return null;
}
$key = self::sessionUsernamePrefix() . $username;
$val = Cache::get($key);
return $val !== null && $val !== '' ? (string) $val : null;
}
/** 检查 username 是否已有登录会话Redis 中是否存在当前 token */
public static function hasSessionByUsername(string $username): bool
{
return self::getSessionTokenByUsername($username) !== null;
}
/** 删除 username 登录会话(退出登录时调用) */
public static function deleteSessionByUsername(string $username): bool
{
if ($username === '') {
return false;
}
$key = self::sessionUsernamePrefix() . $username;
return Cache::delete($key);
}
/** 玩家缓存 key 前缀Token 中间件用,减少重复查库) */
private static function playerCachePrefix(): string
{
return config('api.player_cache_prefix', 'api:player:');
}
private static function playerCacheTtl(): int
{
return (int) config('api.player_cache_ttl', 300);
}
/**
* 按 username 缓存玩家信息(仅 id + username供中间件注入 request->player 后使用)
* 登录/信息变更时需调用 deletePlayerByUsername 失效
*/
public static function setPlayerByUsername(string $username, array $playerRow): bool
{
if ($username === '' || empty($playerRow)) {
return false;
}
$ttl = self::playerCacheTtl();
if ($ttl <= 0) {
return true;
}
$key = self::playerCachePrefix() . $username;
return Cache::set($key, json_encode($playerRow), $ttl);
}
/** 按 username 取缓存玩家,未命中返回 null */
public static function getPlayerByUsername(string $username): ?array
{
if ($username === '') {
return null;
}
if (self::playerCacheTtl() <= 0) {
return null;
}
$key = self::playerCachePrefix() . $username;
$val = Cache::get($key);
if ($val === null || $val === '') {
return null;
}
$data = json_decode((string) $val, true);
return is_array($data) ? $data : null;
}
/** 退出登录或玩家信息变更时删除玩家缓存 */
public static function deletePlayerByUsername(string $username): bool
{
if ($username === '') {
return false;
}
$key = self::playerCachePrefix() . $username;
return Cache::delete($key);
}
} }

View File

@@ -1,90 +0,0 @@
<?php
declare(strict_types=1);
namespace app\api\controller;
use support\Request;
use support\Response;
use Tinywan\Jwt\JwtToken;
use plugin\saiadmin\basic\OpenController;
use app\api\util\ReturnCode;
use app\api\cache\AuthTokenCache;
/**
* API 鉴权 Token 接口
* 仅支持 GET必传参数signature、secret、device、time签名规则signature = md5(device . secret . time)
* 后续所有 /api 接口调用均需在请求头携带此接口返回的 auth-token
*/
class AuthTokenController extends OpenController
{
/**
* 获取 auth-token
* GET /api/authToken
* 参数signature签名、secret密钥、device设备标识、time时间戳四者均为必传且非空
*/
public function index(Request $request): Response
{
if (strtoupper($request->method()) !== 'GET') {
return $this->fail('仅支持 GET 请求', ReturnCode::PARAMS_ERROR);
}
$param = $request->get();
$signature = trim((string) ($param['signature'] ?? ''));
$secret = trim((string) ($param['secret'] ?? ''));
$device = trim((string) ($param['device'] ?? ''));
$time = trim((string) ($param['time'] ?? ''));
if ($signature === '' || $secret === '' || $device === '' || $time === '') {
return $this->fail('signature、secret、device、time 均为必传且不能为空', ReturnCode::PARAMS_ERROR);
}
$serverSecret = trim((string) config('api.auth_token_secret', ''));
if ($serverSecret === '') {
return $this->fail('服务未配置 API_AUTH_TOKEN_SECRET', ReturnCode::PARAMS_ERROR);
}
if ($secret !== $serverSecret) {
return $this->fail('密钥错误', ReturnCode::FORBIDDEN);
}
$tolerance = (int) config('api.auth_token_time_tolerance', 300);
$now = time();
$ts = is_numeric($time) ? (int) $time : 0;
if ($ts <= 0 || abs($now - $ts) > $tolerance) {
return $this->fail('时间戳无效或已过期', ReturnCode::PARAMS_ERROR);
}
$sign = $this->getAuthToken($device, $serverSecret, $time);
if ($sign !== $signature) {
return $this->fail('签名验证失败', ReturnCode::FORBIDDEN);
}
$exp = (int) config('api.auth_token_exp', 86400);
$tokenResult = JwtToken::generateToken([
'id' => 0,
'plat' => 'api',
'device' => $device,
'access_exp' => $exp,
]);
// 同一设备只保留最新 token覆盖后旧 token 失效
AuthTokenCache::setDeviceToken($device, $tokenResult['access_token'], $exp);
return $this->success([
'auth-token' => $tokenResult['access_token'],
'expires_in' => $tokenResult['expires_in'],
]);
}
/**
* 生成签名signature = md5(device . secret . time)
*
* @param string $device 设备标识
* @param string $secret 密钥(来自配置)
* @param string $time 时间戳
* @return string
*/
private function getAuthToken(string $device, string $secret, string $time): string
{
return md5($device . $secret . $time);
}
}

View File

@@ -3,12 +3,13 @@ declare(strict_types=1);
namespace app\api\controller; namespace app\api\controller;
use support\Log;
use support\Request; use support\Request;
use support\Response; use support\Response;
use app\api\logic\GameLogic; use app\api\logic\GameLogic;
use app\api\logic\PlayStartLogic; use app\api\logic\PlayStartLogic;
use app\api\logic\UserLogic;
use app\api\util\ReturnCode; use app\api\util\ReturnCode;
use app\dice\model\config\DiceConfig;
use app\dice\model\play_record\DicePlayRecord; use app\dice\model\play_record\DicePlayRecord;
use app\dice\model\player\DicePlayer; use app\dice\model\player\DicePlayer;
use app\dice\model\reward_config\DiceRewardConfig; use app\dice\model\reward_config\DiceRewardConfig;
@@ -20,15 +21,40 @@ use plugin\saiadmin\exception\ApiException;
*/ */
class GameController extends OpenController class GameController extends OpenController
{ {
/**
* 获取游戏配置(按 group 分组)
* GET /api/game/config
* 返回 data[group] = [ { name, title, value, create_time, update_time }, ... ]
*/
public function config(Request $request): Response
{
$rows = DiceConfig::select('name', 'group', 'title', 'value', 'create_time', 'update_time')->get();
$data = [];
foreach ($rows as $row) {
$group = $row->group ?? '';
if (!isset($data[$group])) {
$data[$group] = [];
}
$data[$group][] = [
'name' => $row->name,
'title' => $row->title,
'value' => $row->value,
'create_time' => $row->create_time,
'update_time' => $row->update_time,
];
}
return $this->success($data);
}
/** /**
* 购买抽奖券 * 购买抽奖券
* POST /api/game/buyLotteryTickets * POST /api/game/buyLotteryTickets
* header: auth-token, user-token由 CheckUserTokenMiddleware 注入 request->user_id * header: tokenTokenMiddleware 注入 request->player_id
* body: count = 1 | 5 | 101次/100coin, 5次/500coin, 10次/1000coin * body: count = 1 | 5 | 101次/100coin, 5次/500coin, 10次/1000coin
*/ */
public function buyLotteryTickets(Request $request): Response public function buyLotteryTickets(Request $request): Response
{ {
$userId = UserLogic::getUserIdFromRequest($request) ?? 0; $userId = (int) ($request->player_id ?? 0);
$count = (int) $request->post('count', 0); $count = (int) $request->post('count', 0);
if (!in_array($count, [1, 5, 10], true)) { if (!in_array($count, [1, 5, 10], true)) {
return $this->fail('购买抽奖券错误', ReturnCode::PARAMS_ERROR); return $this->fail('购买抽奖券错误', ReturnCode::PARAMS_ERROR);
@@ -52,69 +78,91 @@ class GameController extends OpenController
/** /**
* 获取彩金池(中奖配置表) * 获取彩金池(中奖配置表)
* GET /api/game/lotteryPool * GET /api/game/lotteryPool
* header: auth-token * header: token
* 返回 DiceRewardConfig 列表(彩金池/中奖配置) * 返回 DiceRewardConfig 列表(彩金池/中奖配置),不包含 tier=BIGWIN
*/ */
public function lotteryPool(Request $request): Response public function lotteryPool(Request $request): Response
{ {
$list = DiceRewardConfig::order('id', 'asc')->select()->toArray(); $list = DiceRewardConfig::getCachedList();
$list = array_values(array_filter($list, function ($row) {
return (string) ($row['tier'] ?? '') !== 'BIGWIN';
}));
return $this->success($list); return $this->success($list);
} }
/** /**
* 开始游戏(抽奖一局) * 开始游戏(抽奖一局)
* POST /api/game/playStart * POST /api/game/playStart
* header: auth-token, user-token由 CheckUserTokenMiddleware 注入 request->user_id * header: tokenTokenMiddleware 注入 request->player_id
* body: rediction 必传0=无 1=中奖 * body: direction 必传0=无 1=中奖
*/ */
public function playStart(Request $request): Response public function playStart(Request $request): Response
{ {
$userId = UserLogic::getUserIdFromRequest($request) ?? 0; $userId = (int) ($request->player_id ?? 0);
$rediction = $request->post('rediction'); $direction = $request->post('direction');
if ($rediction === '' || $rediction === null) { if ($direction !== null) {
return $this->fail('请传递 rediction 参数', ReturnCode::PARAMS_ERROR); $direction = (int) $direction;
} }
$direction = (int) $rediction;
if (!in_array($direction, [0, 1], true)) { if (!in_array($direction, [0, 1], true)) {
return $this->fail('rediction 必须为 0 或 1', ReturnCode::PARAMS_ERROR); return $this->fail('direction 必须为 0 或 1', ReturnCode::PARAMS_ERROR);
} }
$player = DicePlayer::find($userId); $player = DicePlayer::find($userId);
if (!$player) { if (!$player) {
return $this->fail('用户不存在', ReturnCode::NOT_FOUND); return $this->fail('用户不存在', ReturnCode::NOT_FOUND);
} }
$minEv = (float) DiceRewardConfig::min('real_ev'); $minEv = DiceRewardConfig::getCachedMinRealEv();
$minCoin = abs($minEv + 100); $minCoin = abs($minEv + 100);
$coin = (float) $player->coin; $coin = (float) $player->coin;
if ($coin < $minCoin) { if ($coin < $minCoin) {
return $this->success([], '当前玩家余额小于DiceRewardConfigMin.real_ev+100无法继续游戏'); return $this->success([], '当前玩家余额'.$coin.'小于'.$minCoin.'无法继续游戏');
} }
try { try {
$logic = new PlayStartLogic(); $logic = new PlayStartLogic();
$data = $logic->run($userId, $direction); $data = $logic->run($userId, (int)$direction);
return $this->success($data); return $this->success($data);
} catch (ApiException $e) { } catch (ApiException $e) {
return $this->fail($e->getMessage(), ReturnCode::BUSINESS_ERROR); return $this->fail($e->getMessage(), ReturnCode::BUSINESS_ERROR);
} catch (\Throwable $e) { } catch (\Throwable $e) {
// 记录抽奖逻辑抛出的真实异常,便于排查“服务超时,没有原因”
Log::error('playStart 异常: ' . $e->getMessage(), [
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
'player_id' => $userId,
'direction' => $direction,
]);
$timeoutRecord = null; $timeoutRecord = null;
$timeout_message = '';
try { try {
$timeoutRecord = DicePlayRecord::create([ $timeoutRecord = DicePlayRecord::create([
'player_id' => $userId, 'player_id' => $userId,
'lottery_config_id' => 0, 'lottery_config_id' => 0,
'lottery_type' => 0, 'lottery_type' => 0,
'is_win' => 0,
'win_coin' => 0, 'win_coin' => 0,
'super_win_coin' => 0,
'reward_win_coin' => 0,
'use_coins' => 0,
'direction' => $direction, 'direction' => $direction,
'reward_config_id' => 0, 'reward_config_id' => 0,
'start_index' => 0, 'start_index' => 0,
'target_index' => 0, 'target_index' => 0,
'roll_array' => '[]', 'roll_array' => '[]',
'roll_number' => 0,
'status' => PlayStartLogic::RECORD_STATUS_TIMEOUT, 'status' => PlayStartLogic::RECORD_STATUS_TIMEOUT,
]); ]);
} catch (\Throwable $_) { } catch (\Exception $inner) {
$timeout_message = $inner->getMessage();
Log::error('游玩记录写入超时: ' . $inner->getMessage());
} }
$payload = $timeoutRecord ? ['record' => $timeoutRecord->toArray()] : []; $payload = $timeoutRecord ? ['record' => $timeoutRecord->toArray()] : [];
return $this->success($payload, '服务超时'); $msg = $timeout_message !== '' ? $timeout_message : $e->getMessage();
if ($msg === '') {
$msg = '没有原因';
}
return $this->fail('服务超时,' . $msg);
} }
} }
} }

View File

@@ -13,77 +13,80 @@ use app\dice\model\player_wallet_record\DicePlayerWalletRecord;
use plugin\saiadmin\basic\OpenController; use plugin\saiadmin\basic\OpenController;
/** /**
* API 用户登录/注册 * API 用户登录
* 需先携带 auth-token登录/注册成功后返回 user-token 与用户信息,用户信息已写入 Rediskey=base64(user_id)value=加密) * 登录接口 /api/user/Login 无需 token其余接口需在请求头携带 tokenbase64(username.-.time)),由 TokenMiddleware 鉴权并注入 request->player_id / request->player
*/ */
class UserController extends OpenController class UserController extends OpenController
{ {
/** /**
* 登录 * 登录form-data 参数)
* POST /api/user/login * POST /api/user/Login
* body: phone (+60), password * body: username, password, lang(可选), coin(可选), time(可选)
* 根据 username 查找或创建 DicePlayer按 coin 增减平台币,会话写 Redis返回带 token 的连接地址
*/ */
public function login(Request $request): Response public function Login(Request $request): Response
{ {
$phone = $request->post('phone', ''); $username = trim((string) ($request->post('username', '')));
$password = $request->post('password', ''); $password = trim((string) ($request->post('password', '')));
if ($phone === '' || $password === '') { $lang = trim((string) ($request->post('lang', 'chs')));
return $this->fail('请填写手机号和密码', ReturnCode::PARAMS_ERROR); $coin = $request->post('coin');
} $coin = $coin !== null && $coin !== '' ? (float) $coin : 0.0;
$logic = new UserLogic(); $time = $request->post('time');
$data = $logic->login($phone, $password); $time = $time !== null && $time !== '' ? (string) $time : (string) time();
return $this->success([ if ($username === '' || $password === '') {
'user' => $data['user'], return $this->fail('username、password 不能为空', ReturnCode::PARAMS_ERROR);
'user-token' => $data['user-token'],
'user_id' => $data['user_id'],
]);
} }
/** try {
* 注册
* POST /api/user/register
* body: phone (+60), password, nickname(可选)
*/
public function register(Request $request): Response
{
$phone = $request->post('phone', '');
$password = $request->post('password', '');
$nickname = $request->post('nickname');
if ($phone === '' || $password === '') {
return $this->fail('请填写手机号和密码', ReturnCode::PARAMS_ERROR);
}
$logic = new UserLogic(); $logic = new UserLogic();
$data = $logic->register($phone, $password, $nickname ? (string) $nickname : null); $result = $logic->loginByUsername($username, $password, $lang, $coin, $time);
return $this->success([ return $this->success([
'user' => $data['user'], 'url' => $result['url'],
'user-token' => $data['user-token'], 'token' => $result['token'],
'user_id' => $data['user_id'], 'lang' => $result['lang'],
'user_id' => $result['user_id'],
'user' => $result['user'],
]); ]);
} catch (\plugin\saiadmin\exception\ApiException $e) {
return $this->fail($e->getMessage(), ReturnCode::PARAMS_ERROR);
}
} }
/** /**
* 退出登录 * 退出登录
* POST /api/user/logout * POST /api/user/logout
* header: user-token由 CheckUserTokenMiddleware 校验并注入 request->userToken * header: tokenJWT清除该 username 的 Redis 会话
*/ */
public function logout(Request $request): Response public function logout(Request $request): Response
{ {
$token = $request->userToken ?? UserLogic::getTokenFromRequest($request); $token = $request->header('token');
if ($token === '' || !UserLogic::logout($token)) { if ($token === null || $token === '') {
return $this->fail('退出失败或 token 已失效', ReturnCode::TOKEN_INVALID); $auth = $request->header('authorization');
if ($auth && stripos($auth, 'Bearer ') === 0) {
$token = trim(substr($auth, 7));
} }
}
$token = $token !== null ? trim((string) $token) : '';
if ($token === '') {
return $this->fail('请携带 token', ReturnCode::UNAUTHORIZED);
}
$username = UserLogic::getUsernameFromJwtPayload($token);
if ($username === null || $username === '') {
return $this->fail('token 无效', ReturnCode::TOKEN_INVALID);
}
UserCache::deleteSessionByUsername($username);
UserCache::deletePlayerByUsername($username);
return $this->success('已退出登录'); return $this->success('已退出登录');
} }
/** /**
* 获取当前用户信息 * 获取当前用户信息
* GET /api/user/info * GET /api/user/info
* header: user-tokenCheckUserTokenMiddleware 校验并注入 request->user_id * header: token由 TokenMiddleware 校验并注入 request->player_id
* 返回id, username, phone, uid, name, coin, total_ticket_count
*/ */
public function info(Request $request): Response public function info(Request $request): Response
{ {
$userId = UserLogic::getUserIdFromRequest($request) ?? 0; $userId = (int) ($request->player_id ?? 0);
$user = UserLogic::getCachedUser($userId); $user = UserLogic::getCachedUser($userId);
if (empty($user)) { if (empty($user)) {
return $this->fail('用户不存在', ReturnCode::NOT_FOUND); return $this->fail('用户不存在', ReturnCode::NOT_FOUND);
@@ -99,13 +102,13 @@ class UserController extends OpenController
} }
/** /**
* 获取钱包余额(优先读缓存,缓存未命中时从库拉取并回写缓存 * 获取钱包余额(优先读缓存)
* GET /api/user/balance * GET /api/user/balance
* header: user-tokenCheckUserTokenMiddleware 校验并注入 request->user_id * header: token由 TokenMiddleware 注入 request->player_id
*/ */
public function balance(Request $request): Response public function balance(Request $request): Response
{ {
$userId = UserLogic::getUserIdFromRequest($request) ?? 0; $userId = (int) ($request->player_id ?? 0);
$user = UserLogic::getCachedUser($userId); $user = UserLogic::getCachedUser($userId);
if (empty($user)) { if (empty($user)) {
return $this->fail('用户不存在', ReturnCode::NOT_FOUND); return $this->fail('用户不存在', ReturnCode::NOT_FOUND);
@@ -124,12 +127,12 @@ class UserController extends OpenController
/** /**
* 玩家钱包流水 * 玩家钱包流水
* GET /api/user/walletRecord * GET /api/user/walletRecord
* header: user-tokenCheckUserTokenMiddleware 校验并注入 request->user_id * header: token由 TokenMiddleware 注入 request->player_id
* 参数: page 页码默认1, limit 每页条数默认10, create_time_min/create_time_max 创建时间范围(可选) * 参数: page 页码默认1, limit 每页条数默认10, create_time_min/create_time_max 创建时间范围(可选)
*/ */
public function walletRecord(Request $request): Response public function walletRecord(Request $request): Response
{ {
$userId = UserLogic::getUserIdFromRequest($request) ?? 0; $userId = (int) ($request->player_id ?? 0);
$page = (int) $request->post('page', 1); $page = (int) $request->post('page', 1);
$limit = (int) $request->post('limit', 10); $limit = (int) $request->post('limit', 10);
if ($page < 1) { if ($page < 1) {
@@ -166,12 +169,12 @@ class UserController extends OpenController
/** /**
* 游玩记录 * 游玩记录
* GET /api/user/playGameRecord * GET /api/user/playGameRecord
* header: user-tokenCheckUserTokenMiddleware 校验并注入 request->user_id * header: token由 TokenMiddleware 注入 request->player_id
* 参数: page 页码默认1, limit 每页条数默认10, create_time_min/create_time_max 创建时间范围(可选) * 参数: page 页码默认1, limit 每页条数默认10, create_time_min/create_time_max 创建时间范围(可选)
*/ */
public function playGameRecord(Request $request): Response public function playGameRecord(Request $request): Response
{ {
$userId = UserLogic::getUserIdFromRequest($request) ?? 0; $userId = (int) ($request->player_id ?? 0);
$page = (int) $request->post('page', 1); $page = (int) $request->post('page', 1);
$limit = (int) $request->post('limit', 10); $limit = (int) $request->post('limit', 10);
if ($page < 1) { if ($page < 1) {

View File

@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace app\api\controller\v1;
use app\api\cache\AuthTokenCache;
use app\api\util\ReturnCode;
use plugin\saiadmin\basic\OpenController;
use support\Request;
use support\Response;
use Tinywan\Jwt\JwtToken;
/**
* 平台鉴权接口
* 鉴权接口:/api/v1/authtoken
* GET 参数signature, secret, time, agent_id
* 签名signature = md5(agent_id.secret.time)
*/
class AuthTokenController extends OpenController
{
/**
* 获取 auth-token
* GET 参数signature, secret, time, agent_id
* 返回 authtoken后续 /api/v1/* 接口需在请求头携带 auth-token
*/
public function index(Request $request): Response
{
$agentId = trim((string) ($request->get('agent_id', '')));
$secret = trim((string) ($request->get('secret', '')));
$time = trim((string) ($request->get('time', '')));
$signature = trim((string) ($request->get('signature', '')));
if ($agentId === '' || $secret === '' || $time === '' || $signature === '') {
return $this->fail('缺少参数agent_id、secret、time、signature 不能为空', ReturnCode::PARAMS_ERROR);
}
$expectedSecret = config('api.auth_token_secret', '');
if ($expectedSecret === '') {
return $this->fail('服务端未配置 API_AUTH_TOKEN_SECRET', ReturnCode::SERVER_ERROR);
}
if ($secret !== $expectedSecret) {
return $this->fail('密钥错误', ReturnCode::FORBIDDEN);
}
$timeVal = (int) $time;
$tolerance = (int) config('api.auth_token_time_tolerance', 300);
$now = time();
if ($timeVal < $now - $tolerance || $timeVal > $now + $tolerance) {
return $this->fail('时间戳已过期或无效,请同步时间', ReturnCode::FORBIDDEN);
}
$expectedSignature = md5($agentId . $secret . $time);
if ($signature !== $expectedSignature) {
return $this->fail('签名验证失败', ReturnCode::FORBIDDEN);
}
$exp = (int) config('api.auth_token_exp', 86400);
$tokenResult = JwtToken::generateToken([
'id' => 0,
'agent_id' => $agentId,
'plat' => 'api_auth_token',
'access_exp' => $exp,
]);
$token = $tokenResult['access_token'];
if (!AuthTokenCache::setToken($agentId, $token)) {
return $this->fail('生成 token 失败', ReturnCode::SERVER_ERROR);
}
return $this->success([
'authtoken' => $token,
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace app\api\controller\v1;
use app\api\logic\UserLogic;
use app\api\util\ReturnCode;
use plugin\saiadmin\basic\OpenController;
use support\Request;
use support\Response;
/**
* 平台 v1 游戏接口
* 获取进入游戏:/api/v1/getGameUrl
* 请求头auth-token
* POST 参数username, password默认123456, time
*/
class GameController extends OpenController
{
/**
* 获取游戏地址
* 根据 username 创建登录 tokenJWT拼接游戏地址返回
*/
public function getGameUrl(Request $request): Response
{
$username = trim((string) ($request->post('username', '')));
$password = trim((string) ($request->post('password', '123456')));
$time = trim((string) ($request->post('time', '')));
if ($username === '') {
return $this->fail('username 不能为空', ReturnCode::PARAMS_ERROR);
}
if ($password === '') {
$password = '123456';
}
if ($time === '') {
$time = (string) time();
}
try {
$logic = new UserLogic();
$result = $logic->loginByUsername($username, $password, 'chs', 0.0, $time);
} catch (\plugin\saiadmin\exception\ApiException $e) {
return $this->fail($e->getMessage(), ReturnCode::PARAMS_ERROR);
}
$gameUrlBase = rtrim(config('api.game_url', 'dice-game.yuliao666.top'), '/');
$tokenInUrl = str_replace('%3D', '=', urlencode($result['token']));
$url = $gameUrlBase . '/?token=' . $tokenInUrl;
return $this->success([
'url' => $url,
]);
}
}

View File

@@ -27,7 +27,8 @@ class GameLogic
/** /**
* 购买抽奖券 * 购买抽奖券
* @param int $playerId 玩家ID即 user_id * 先更新 Redis 玩家信息(后续游玩从 Redis 读),再用事务更新数据库;事务失败则回滚 Redis
* @param int $playerId 玩家ID
* @param int $count 购买档位1 / 5 / 10 * @param int $count 购买档位1 / 5 / 10
* @return array 更新后的 coin, total_ticket_count, paid_ticket_count, free_ticket_count * @return array 更新后的 coin, total_ticket_count, paid_ticket_count, free_ticket_count
*/ */
@@ -55,7 +56,20 @@ class GameLogic
$totalBefore = (int) ($player->total_ticket_count ?? 0); $totalBefore = (int) ($player->total_ticket_count ?? 0);
$paidBefore = (int) ($player->paid_ticket_count ?? 0); $paidBefore = (int) ($player->paid_ticket_count ?? 0);
$freeBefore = (int) ($player->free_ticket_count ?? 0); $freeBefore = (int) ($player->free_ticket_count ?? 0);
$totalAfter = $totalBefore + $addTotal;
$paidAfter = $paidBefore + $addPaid;
$freeAfter = $freeBefore + $addFree;
$oldUserArr = $player->hidden(['password'])->toArray();
$updatedUserArr = $oldUserArr;
$updatedUserArr['coin'] = $coinAfter;
$updatedUserArr['total_ticket_count'] = $totalAfter;
$updatedUserArr['paid_ticket_count'] = $paidAfter;
$updatedUserArr['free_ticket_count'] = $freeAfter;
UserCache::setUser($playerId, $updatedUserArr);
try {
Db::transaction(function () use ( Db::transaction(function () use (
$player, $player,
$playerId, $playerId,
@@ -65,17 +79,16 @@ class GameLogic
$addTotal, $addTotal,
$addPaid, $addPaid,
$addFree, $addFree,
$totalBefore, $totalAfter,
$paidBefore, $paidAfter,
$freeBefore $freeAfter
) { ) {
$player->coin = $coinAfter; $player->coin = $coinAfter;
$player->total_ticket_count = $totalBefore + $addTotal; $player->total_ticket_count = $totalAfter;
$player->paid_ticket_count = $paidBefore + $addPaid; $player->paid_ticket_count = $paidAfter;
$player->free_ticket_count = $freeBefore + $addFree; $player->free_ticket_count = $freeAfter;
$player->save(); $player->save();
// 钱包流水记录
DicePlayerWalletRecord::create([ DicePlayerWalletRecord::create([
'player_id' => $playerId, 'player_id' => $playerId,
'coin' => -$cost, 'coin' => -$cost,
@@ -88,7 +101,6 @@ class GameLogic
'remark' => "购买抽奖券{$addTotal}次(付费{$addPaid}次+赠送{$addFree}次)", 'remark' => "购买抽奖券{$addTotal}次(付费{$addPaid}次+赠送{$addFree}次)",
]); ]);
// 抽奖券获取记录
DicePlayerTicketRecord::create([ DicePlayerTicketRecord::create([
'player_id' => $playerId, 'player_id' => $playerId,
'use_coins' => $cost, 'use_coins' => $cost,
@@ -98,16 +110,16 @@ class GameLogic
'remark' => "购买抽奖券{$addTotal}次(付费{$addPaid}次+赠送{$addFree}次)", 'remark' => "购买抽奖券{$addTotal}次(付费{$addPaid}次+赠送{$addFree}次)",
]); ]);
}); });
} catch (\Throwable $e) {
$updated = DicePlayer::find($playerId); UserCache::setUser($playerId, $oldUserArr);
$userArr = $updated->hidden(['password'])->toArray(); throw $e;
UserCache::setUser($playerId, $userArr); }
return [ return [
'coin' => (float) $updated->coin, 'coin' => (float) $coinAfter,
'total_ticket_count' => (int) $updated->total_ticket_count, 'total_ticket_count' => (int) $totalAfter,
'paid_ticket_count' => (int) $updated->paid_ticket_count, 'paid_ticket_count' => (int) $paidAfter,
'free_ticket_count' => (int) $updated->free_ticket_count, 'free_ticket_count' => (int) $freeAfter,
]; ];
} }
} }

View File

@@ -12,6 +12,7 @@ use app\dice\model\player_ticket_record\DicePlayerTicketRecord;
use app\dice\model\player_wallet_record\DicePlayerWalletRecord; use app\dice\model\player_wallet_record\DicePlayerWalletRecord;
use app\dice\model\reward_config\DiceRewardConfig; use app\dice\model\reward_config\DiceRewardConfig;
use plugin\saiadmin\exception\ApiException; use plugin\saiadmin\exception\ApiException;
use support\Log;
use support\think\Cache; use support\think\Cache;
use support\think\Db; use support\think\Db;
@@ -33,11 +34,17 @@ class PlayStartLogic
/** 开启对局最低余额 = |DiceRewardConfig 最小 real_ev + 100| */ /** 开启对局最低余额 = |DiceRewardConfig 最小 real_ev + 100| */
private const MIN_COIN_EXTRA = 100; private const MIN_COIN_EXTRA = 100;
/** 豹子号中大奖额外平台币(无 BIGWIN 配置时兜底) */
private const SUPER_WIN_BONUS = 500;
/** 可触发超级大奖的 grid_number5=全1 10=全2 15=全3 20=全4 25=全5 30=全6其中 5 和 30 固定 100% 出豹子 */
private const SUPER_WIN_GRID_NUMBERS = [5, 10, 15, 20, 25, 30];
/** grid_number 为 5 或 30 时豹子概率固定 100%DiceRewardConfig tier=BIGWIN 约定) */
private const SUPER_WIN_ALWAYS_GRID_NUMBERS = [5, 30];
/** /**
* 执行一局游戏 * 执行一局游戏
* @param int $playerId 玩家ID * @param int $playerId 玩家ID
* @param int $direction 方向 0=无/顺时针 1=中奖/逆时针(前端 rediction * @param int $direction 方向 0=无/顺时针 1=中奖/逆时针(前端 direction
* @return array 成功返回 DicePlayRecord 数据;余额不足时抛 ApiExceptionmessage 为约定文案 * @return array 成功返回 DicePlayRecord 数据;余额不足时抛 ApiExceptionmessage 为约定文案
*/ */
public function run(int $playerId, int $direction): array public function run(int $playerId, int $direction): array
@@ -47,11 +54,11 @@ class PlayStartLogic
throw new ApiException('用户不存在'); throw new ApiException('用户不存在');
} }
$minEv = (float) DiceRewardConfig::min('real_ev'); $minEv = DiceRewardConfig::getCachedMinRealEv();
$minCoin = abs($minEv + self::MIN_COIN_EXTRA); $minCoin = abs($minEv + self::MIN_COIN_EXTRA);
$coin = (float) $player->coin; $coin = (float) $player->coin;
if ($coin < $minCoin) { if ($coin < $minCoin) {
throw new ApiException('当前玩家余额小于DiceRewardConfigMin.real_ev+100无法继续游戏'); throw new ApiException('当前玩家余额'.$coin.'小于'.$minCoin.'无法继续游戏');
} }
$paid = (int) ($player->paid_ticket_count ?? 0); $paid = (int) ($player->paid_ticket_count ?? 0);
@@ -69,25 +76,90 @@ class PlayStartLogic
throw new ApiException('奖池配置不存在'); throw new ApiException('奖池配置不存在');
} }
$tier = LotteryService::drawTierByWeights($config); // 按玩家权重抽取档位;若该档位无奖励或该方向下均无可用路径则重新摇取档位
$rewards = DiceRewardConfig::where('tier', $tier)->select(); $maxTierRetry = 10;
if ($rewards->isEmpty()) { $chosen = null;
throw new ApiException('该档位暂无奖励配置'); $startCandidates = [];
$tier = null;
for ($tierAttempt = 0; $tierAttempt < $maxTierRetry; $tierAttempt++) {
$tier = LotteryService::drawTierByPlayerWeights($player);
$tierRewards = DiceRewardConfig::getCachedByTier($tier);
if (empty($tierRewards)) {
Log::warning("档位 {$tier} 无任何奖励配置,重新摇取档位");
continue;
} }
$rewardList = $rewards->all(); $maxRewardRetry = count($tierRewards);
$reward = $rewardList[array_rand($rewardList)]; for ($attempt = 0; $attempt < $maxRewardRetry; $attempt++) {
$realEv = (float) $reward->real_ev; $chosen = $tierRewards[array_rand($tierRewards)];
$winCoin = 100 + $realEv; // 赢取平台币 = 100 + DiceRewardConfig.real_ev $chosenId = (int) ($chosen['id'] ?? 0);
$gridNumber = (int) $reward->grid_number; if ($direction === 0) {
$startIndex = (int) Cache::get(LotteryService::getStartIndexKey($playerId), 0); $startCandidates = DiceRewardConfig::getCachedBySEndIndex($chosenId);
$targetIndex = (int) $reward->id; } else {
$rollArray = $this->generateRollArray($gridNumber); $startCandidates = DiceRewardConfig::getCachedByNEndIndex($chosenId);
}
if (!empty($startCandidates)) {
break 2;
}
Log::warning("方向 {$direction} 下无 s_end_index/n_end_index={$chosenId} 的配置,重新摇取");
}
Log::warning("方向 {$direction} 下档位 {$tier} 所有奖励均无可用路径配置,重新摇取档位");
}
if (empty($startCandidates)) {
Log::error("方向 {$direction} 下多次摇取档位后仍无可用路径配置");
throw new ApiException('该方向下暂无可用路径配置');
}
$chosenId = (int) ($chosen['id'] ?? 0);
$startRecord = $startCandidates[array_rand($startCandidates)];
$startIndex = (int) ($startRecord['id'] ?? 0);
$targetIndex = $direction === 0
? (int) ($startRecord['s_end_index'] ?? 0)
: (int) ($startRecord['n_end_index'] ?? 0);
$rollNumber = (int) ($startRecord['grid_number'] ?? 0);
$realEv = (float) ($chosen['real_ev'] ?? 0);
$rewardWinCoin = 100 + $realEv; // 摇色子中奖平台币 = 100 + DiceRewardConfig.real_ev
// 当抽到的 grid_number 为 5/10/15/20/25/30 时,可出豹子;其中 grid_number=5 与 30 固定 100% 豹子BIGWIN 约定)
$superWinCoin = 0;
$isWin = 0;
if (in_array($rollNumber, self::SUPER_WIN_GRID_NUMBERS, true)) {
$bigWinConfig = DiceRewardConfig::getCachedByTierAndGridNumber('BIGWIN', $rollNumber);
$alwaysSuperWin = in_array($rollNumber, self::SUPER_WIN_ALWAYS_GRID_NUMBERS, true);
$doSuperWin = $alwaysSuperWin;
if (!$doSuperWin) {
$weight = $bigWinConfig !== null
? max(0.0, min(100.0, (float) ($bigWinConfig['weight'] ?? 0)))
: 100.0;
$roll = mt_rand(1, 10000) / 10000;
$doSuperWin = $roll <= $weight / 100;
}
if ($doSuperWin) {
$rollArray = $this->getSuperWinRollArray($rollNumber);
$isWin = 1;
$superWinCoin = $bigWinConfig !== null
? 100 + (float) ($bigWinConfig['real_ev'] ?? 0)
: self::SUPER_WIN_BONUS;
} else {
$rollArray = $this->generateNonSuperWinRollArrayWithSum($rollNumber);
}
} else {
$rollArray = $this->generateRollArrayFromSum($rollNumber);
}
Log::info(sprintf(
'摇取点数 roll_number=%d, 方向=%d, start_index=%d, target_index=%d',
$rollNumber,
$direction,
$startIndex,
$targetIndex
));
$winCoin = $superWinCoin + $rewardWinCoin; // 赢取平台币 = 中大奖 + 摇色子中奖
$record = null; $record = null;
$configId = (int) $config->id; $configId = (int) $config->id;
$rewardId = (int) $reward->id; $rewardId = $chosenId;
$configName = (string) ($config->name ?? ''); $configName = (string) ($config->name ?? '');
$isTierT5 = (string) ($reward->tier ?? '') === 'T5'; $isTierT5 = (string) ($chosen['tier'] ?? '') === 'T5';
try { try {
Db::transaction(function () use ( Db::transaction(function () use (
$playerId, $playerId,
@@ -96,6 +168,9 @@ class PlayStartLogic
$configName, $configName,
$ticketType, $ticketType,
$winCoin, $winCoin,
$superWinCoin,
$rewardWinCoin,
$isWin,
$realEv, $realEv,
$direction, $direction,
$startIndex, $startIndex,
@@ -108,12 +183,17 @@ class PlayStartLogic
'player_id' => $playerId, 'player_id' => $playerId,
'lottery_config_id' => $configId, 'lottery_config_id' => $configId,
'lottery_type' => $ticketType, 'lottery_type' => $ticketType,
'is_win' => $isWin,
'win_coin' => $winCoin, 'win_coin' => $winCoin,
'super_win_coin' => $superWinCoin,
'reward_win_coin' => $rewardWinCoin,
'use_coins' => 0,
'direction' => $direction, 'direction' => $direction,
'reward_config_id' => $rewardId, 'reward_config_id' => $rewardId,
'start_index' => $startIndex, 'start_index' => $startIndex,
'target_index' => $targetIndex, 'target_index' => $targetIndex,
'roll_array' => is_array($rollArray) ? json_encode($rollArray) : $rollArray, 'roll_array' => is_array($rollArray) ? json_encode($rollArray) : $rollArray,
'roll_number' => is_array($rollArray) ? array_sum($rollArray) : 0,
'lottery_name' => $configName, 'lottery_name' => $configName,
'status' => self::RECORD_STATUS_SUCCESS, 'status' => self::RECORD_STATUS_SUCCESS,
]); ]);
@@ -162,8 +242,6 @@ class PlayStartLogic
'wallet_after' => $coinAfter, 'wallet_after' => $coinAfter,
'remark' => '抽奖|play_record_id=' . $record->id, 'remark' => '抽奖|play_record_id=' . $record->id,
]); ]);
Cache::set(LotteryService::getStartIndexKey($playerId), $targetIndex, 86400 * 30);
}); });
} catch (\Throwable $e) { } catch (\Throwable $e) {
if ($record === null) { if ($record === null) {
@@ -172,12 +250,17 @@ class PlayStartLogic
'player_id' => $playerId, 'player_id' => $playerId,
'lottery_config_id' => $configId ?? 0, 'lottery_config_id' => $configId ?? 0,
'lottery_type' => $ticketType, 'lottery_type' => $ticketType,
'is_win' => 0,
'win_coin' => 0, 'win_coin' => 0,
'super_win_coin' => 0,
'reward_win_coin' => 0,
'use_coins' => 0,
'direction' => $direction, 'direction' => $direction,
'reward_config_id' => 0, 'reward_config_id' => 0,
'start_index' => $startIndex, 'start_index' => $startIndex,
'target_index' => 0, 'target_index' => 0,
'roll_array' => '[]', 'roll_array' => '[]',
'roll_number' => 0,
'status' => self::RECORD_STATUS_TIMEOUT, 'status' => self::RECORD_STATUS_TIMEOUT,
]); ]);
} catch (\Throwable $_) { } catch (\Throwable $_) {
@@ -199,26 +282,84 @@ class PlayStartLogic
if (isset($arr['roll_array']) && is_string($arr['roll_array'])) { if (isset($arr['roll_array']) && is_string($arr['roll_array'])) {
$arr['roll_array'] = json_decode($arr['roll_array'], true) ?? []; $arr['roll_array'] = json_decode($arr['roll_array'], true) ?? [];
} }
$arr['roll_number'] = is_array($arr['roll_array'] ?? null) ? array_sum($arr['roll_array']) : 0;
$arr['tier'] = $tier ?? '';
// 记录完数据后返回当前玩家余额与抽奖次数
$arr['coin'] = $updated ? (float) $updated->coin : 0;
$arr['total_ticket_count'] = $updated ? (int) $updated->total_ticket_count : 0;
return $arr; return $arr;
} }
/** 生成 5 个 1-6 的点数,和为 grid_number5~30严格不超范围 */ /**
private function generateRollArray(int $gridNumber): array * 根据摇取点数5-30生成 5 个色子数组,每个 1-6总和为 $sum
* @return int[] 如 [1,2,3,4,5]
*/
private function generateRollArrayFromSum(int $sum): array
{ {
$minSum = 5; $sum = max(5, min(30, $sum));
$maxSum = 30; $arr = [1, 1, 1, 1, 1];
$n = max($minSum, min($maxSum, $gridNumber)); $remain = $sum - 5;
$dice = [1, 1, 1, 1, 1]; for ($i = 0; $i < $remain; $i++) {
$remain = $n - 5; $candidates = array_keys(array_filter($arr, function ($v) {
while ($remain > 0) { return $v < 6;
$i = array_rand($dice); }));
if ($dice[$i] < 6) { if (empty($candidates)) {
$add = min($remain, 6 - $dice[$i]); break;
$dice[$i] += $add; }
$remain -= $add; $idx = $candidates[array_rand($candidates)];
$arr[$idx]++;
}
shuffle($arr);
return array_values($arr);
}
/**
* 豹子组合5->[1,1,1,1,1]10->[2,2,2,2,2]15->[3,3,3,3,3]20->[4,4,4,4,4]25->[5,5,5,5,5]30->[6,6,6,6,6]
* @return int[]
*/
private function getSuperWinRollArray(int $gridNumber): array
{
if ($gridNumber === 30) {
return array_fill(0, 5, 6);
}
$n = (int) ($gridNumber / 5);
$n = max(1, min(5, $n));
return array_fill(0, 5, $n);
}
/**
* 生成总和为 $sum 且非豹子的 5 个色子1-6sum=5 时仅 [1,1,1,1,1] 可能,仍返回该组合
* @return int[]
*/
private function generateNonSuperWinRollArrayWithSum(int $sum): array
{
$sum = max(5, min(30, $sum));
$super = $this->getSuperWinRollArray($sum);
if ($sum === 5) {
return $super;
}
$arr = $super;
$maxAttempts = 20;
for ($a = 0; $a < $maxAttempts; $a++) {
$idx = array_rand($arr);
$j = array_rand($arr);
if ($idx === $j) {
$j = ($j + 1) % 5;
}
$i = $idx;
if ($arr[$i] >= 2 && $arr[$j] <= 5) {
$arr[$i]--;
$arr[$j]++;
shuffle($arr);
return array_values($arr);
}
if ($arr[$i] <= 5 && $arr[$j] >= 2) {
$arr[$i]++;
$arr[$j]--;
shuffle($arr);
return array_values($arr);
} }
} }
shuffle($dice); return $this->generateRollArrayFromSum($sum);
return $dice;
} }
} }

View File

@@ -33,78 +33,6 @@ class UserLogic
} }
} }
/**
* 登录:手机号 + 密码,返回用户信息与 user-token并写入 Redis 缓存
*/
public function login(string $phone, string $password): array
{
self::validatePhone($phone);
$user = DicePlayer::where('phone', $phone)->find();
if (!$user) {
throw new ApiException('手机号未注册');
}
if ((int) $user->status !== self::STATUS_NORMAL) {
throw new ApiException('账号已被禁用');
}
$hashed = $this->hashPassword($password);
if ($user->password !== $hashed) {
throw new ApiException('密码错误');
}
$userArr = $user->hidden(['password'])->toArray();
UserCache::setUser((int) $user->id, $userArr);
$userToken = $this->generateUserToken((int) $user->id);
// 同一用户只保留最新一次登录的 token旧 token 自动失效
UserCache::setCurrentUserToken((int) $user->id, $userToken);
return [
'user' => $userArr,
'user-token' => $userToken,
'user_id' => (int) $user->id,
];
}
/**
* 注册:手机号 + 密码(+60创建玩家并返回用户信息与 user-token写入 Redis
*/
public function register(string $phone, string $password, ?string $nickname = null): array
{
self::validatePhone($phone);
if (strlen($password) < 6) {
throw new ApiException('密码至少 6 位');
}
$exists = DicePlayer::where('phone', $phone)->find();
if ($exists) {
throw new ApiException('该手机号已注册');
}
$user = new DicePlayer();
$user->phone = $phone;
$user->username = $phone;
if ($nickname !== null && $nickname !== '') {
$user->name = $nickname;
}
// name 未传时由 DicePlayer::onBeforeInsert 默认设为 uid
$user->password = $this->hashPassword($password);
$user->status = self::STATUS_NORMAL;
$user->save();
$userArr = $user->hidden(['password'])->toArray();
UserCache::setUser((int) $user->id, $userArr);
$userToken = $this->generateUserToken((int) $user->id);
// 同一用户只保留最新一次登录的 token旧 token 自动失效
UserCache::setCurrentUserToken((int) $user->id, $userToken);
return [
'user' => $userArr,
'user-token' => $userToken,
'user_id' => (int) $user->id,
];
}
/** /**
* 与 DicePlayerLogic 一致的密码加密md5(salt . password) * 与 DicePlayerLogic 一致的密码加密md5(salt . password)
*/ */
@@ -114,97 +42,87 @@ class UserLogic
} }
/** /**
* 生成 user-tokenJWTplat=api_userid=user_id * 登录JSONusername, password, lang, coin, time
* 存在则校验密码并更新 coin累加不存在则创建用户并写入 coin。
* 将会话写入 Redis返回 token 与前端连接地址。
*/ */
private function generateUserToken(int $userId): string public function loginByUsername(string $username, string $password, string $lang, float $coin, string $time): array
{ {
$exp = config('api.user_token_exp', 604800); $username = trim($username);
$result = JwtToken::generateToken([ if ($username === '') {
'id' => $userId, throw new ApiException('username 不能为空');
'plat' => 'api_user', }
$player = DicePlayer::where('username', $username)->find();
if ($player) {
if ((int) ($player->status ?? 1) === 0) {
throw new ApiException('账号已被禁用,无法登录');
}
$hashed = $this->hashPassword($password);
if ($player->password !== $hashed) {
throw new ApiException('密码错误');
}
$currentCoin = (float) $player->coin;
$player->coin = $currentCoin + $coin;
$player->save();
} else {
$player = new DicePlayer();
$player->username = $username;
$player->phone = $username;
$player->password = $this->hashPassword($password);
$player->status = self::STATUS_NORMAL;
$player->coin = $coin;
$player->save();
}
$exp = (int) config('api.session_expire', 604800);
$tokenResult = JwtToken::generateToken([
'id' => (int) $player->id,
'username' => $username,
'plat' => 'api_login',
'access_exp' => $exp, 'access_exp' => $exp,
]); ]);
return $result['access_token']; $token = $tokenResult['access_token'];
UserCache::setSessionByUsername($username, $token);
$userArr = $player->hidden(['password', 'lottery_config_id', 't1_weight', 't2_weight', 't3_weight', 't4_weight', 't5_weight'])->toArray();
UserCache::setUser((int) $player->id, $userArr);
UserCache::setPlayerByUsername($username, $userArr);
$baseUrl = rtrim(config('api.login_url_base', 'https://127.0.0.1:6777'), '/');
$lang = in_array($lang, ['chs', 'en'], true) ? $lang : 'chs';
$tokenInUrl = str_replace('%3D', '=', urlencode($token));
$url = $baseUrl . '?token=' . $tokenInUrl . '&lang=' . $lang;
return [
'url' => $url,
'token' => $token,
'lang' => $lang,
'user_id' => (int) $player->id,
'user' => $userArr,
];
} }
/** /**
* 从请求中解析 user-tokenheader: user-token 或 Authorization: Bearer * 从 JWT 中解析 username仅解码 payload不校验签名与过期用于退出时清除会话
* @param object $request 需有 header(string $name) 方法
*/ */
public static function getTokenFromRequest(object $request): string public static function getUsernameFromJwtPayload(string $token): ?string
{ {
$token = $request->header('user-token') ?? ''; $parts = explode('.', $token);
if ($token !== '') { if (count($parts) !== 3) {
return trim((string) $token);
}
$auth = $request->header('authorization');
if ($auth && stripos($auth, 'Bearer ') === 0) {
return trim(substr($auth, 7));
}
return '';
}
/**
* 从请求获取当前用户 ID优先 request->user_id否则从 header 的 user-token 解析
* 中间件未正确注入时仍可兜底解析
* @param object $request 需有 user_id 属性及 header() 方法
*/
public static function getUserIdFromRequest(object $request): ?int
{
$id = $request->user_id ?? null;
if ($id !== null && (int) $id > 0) {
return (int) $id;
}
$token = self::getTokenFromRequest($request);
if ($token === '') {
return null; return null;
} }
return self::getUserIdFromToken($token); $payload = base64_decode(strtr($parts[1], '-_', '+/'), true);
} if ($payload === false) {
/**
* 根据 user-token 获取 user_id不写缓存仅解析 JWT
* 若 token 已通过退出接口加入黑名单,返回 null
*/
public static function getUserIdFromToken(string $userToken): ?int
{
if (UserCache::isTokenBlacklisted($userToken)) {
return null; return null;
} }
try { $data = json_decode($payload, true);
$decoded = JwtToken::verify(1, $userToken); if (!is_array($data)) {
$extend = $decoded['extend'] ?? [];
if (($extend['plat'] ?? '') !== 'api_user') {
return null; return null;
} }
$id = $extend['id'] ?? null; $extend = $data['extend'] ?? $data;
if ($id === null) { $username = $extend['username'] ?? null;
return null; return $username !== null ? trim((string) $username) : null;
}
$userId = (int) $id;
// 同一用户只允许当前登记的 token 生效,重新登录/注册后旧 token 失效
if (!UserCache::isCurrentUserToken($userId, $userToken)) {
return null;
}
return $userId;
} catch (\Throwable $e) {
return null;
}
}
/**
* 退出登录:将当前 user-token 加入黑名单,使该 token 失效
*/
public static function logout(string $userToken): bool
{
try {
$decoded = JwtToken::verify(1, $userToken);
$exp = (int) ($decoded['exp'] ?? 0);
$ttl = $exp > time() ? $exp - time() : 86400;
return UserCache::addTokenToBlacklist($userToken, $ttl);
} catch (\Throwable $e) {
return false;
}
} }
/** /**

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace app\api\middleware;
use app\api\cache\AuthTokenCache;
use app\api\util\ReturnCode;
use plugin\saiadmin\exception\ApiException;
use Tinywan\Jwt\JwtToken;
use Tinywan\Jwt\Exception\JwtTokenException;
use Tinywan\Jwt\Exception\JwtTokenExpiredException;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;
/**
* 校验 auth-token 请求头JWT
* 用于 /api/v1/* 接口(除 /api/v1/authtoken 外)
* 请求头需携带 auth-token通过后注入 request->agent_id
*/
class AuthTokenMiddleware implements MiddlewareInterface
{
public function process(Request $request, callable $handler): Response
{
$token = $request->header('auth-token');
$token = $token !== null ? trim((string) $token) : '';
if ($token === '') {
throw new ApiException('请携带 auth-token', ReturnCode::UNAUTHORIZED);
}
try {
$decoded = JwtToken::verify(1, $token);
} catch (JwtTokenExpiredException $e) {
throw new ApiException('auth-token 已过期', ReturnCode::TOKEN_INVALID);
} catch (JwtTokenException $e) {
throw new ApiException('auth-token 无效', ReturnCode::TOKEN_INVALID);
} catch (\Throwable $e) {
throw new ApiException('auth-token 格式无效', ReturnCode::TOKEN_INVALID);
}
$extend = $decoded['extend'] ?? [];
if ((string) ($extend['plat'] ?? '') !== 'api_auth_token') {
throw new ApiException('auth-token 无效', ReturnCode::TOKEN_INVALID);
}
$agentId = trim((string) ($extend['agent_id'] ?? ''));
if ($agentId === '') {
throw new ApiException('auth-token 无效', ReturnCode::TOKEN_INVALID);
}
$currentToken = AuthTokenCache::getTokenByAgentId($agentId);
if ($currentToken === null || $currentToken !== $token) {
throw new ApiException('auth-token 无效或已失效', ReturnCode::TOKEN_INVALID);
}
$request->agent_id = $agentId;
return $handler($request);
}
}

View File

@@ -1,109 +0,0 @@
<?php
declare(strict_types=1);
namespace app\api\middleware;
use support\Log;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;
use Tinywan\Jwt\JwtToken;
use Tinywan\Jwt\Exception\JwtTokenException;
use Tinywan\Jwt\Exception\JwtTokenExpiredException;
use app\api\util\ReturnCode;
use app\api\cache\AuthTokenCache;
use plugin\saiadmin\exception\ApiException;
/**
* 仅校验 auth-token 请求头
* 白名单路径(如 /api/authToken不校验其它接口必须携带有效 auth-token 或 Authorization: Bearer <token>,且必须通过 JWT 签名与 plat=api 校验
*/
class CheckAuthTokenMiddleware implements MiddlewareInterface
{
/** 不需要 auth-token 的路径 */
private const WHITELIST = [
'api/authToken',
];
/** JWT 至少为 xxx.yyy.zzz 三段 */
private const JWT_PARTS_MIN = 3;
public function process(Request $request, callable $handler): Response
{
$path = trim((string) $request->path(), '/');
if ($this->isWhitelist($path)) {
return $handler($request);
}
$token = $this->getAuthTokenFromRequest($request);
if ($token === '') {
throw new ApiException('请携带 auth-token', ReturnCode::UNAUTHORIZED);
}
if (!$this->looksLikeJwt($token)) {
throw new ApiException('auth-token 格式无效', ReturnCode::TOKEN_INVALID);
}
$decoded = $this->verifyAuthToken($token);
$extend = $decoded['extend'] ?? [];
if (($extend['plat'] ?? '') !== 'api') {
throw new ApiException('auth-token 无效(非 API 凭证)', ReturnCode::TOKEN_INVALID);
}
// 同一设备只允许一个 auth-token 生效,非当前 token 视为已失效
$device = (string) ($extend['device'] ?? '');
if ($device !== '' && !AuthTokenCache::isCurrentToken($device, $token)) {
throw new ApiException('auth-token 已失效(该设备已签发新凭证,请使用新 auth-token', ReturnCode::TOKEN_INVALID);
}
return $handler($request);
}
private function getAuthTokenFromRequest(Request $request): string
{
$token = $request->header('auth-token');
if ($token !== null && $token !== '') {
return trim((string) $token);
}
$auth = $request->header('authorization');
if ($auth && stripos($auth, 'Bearer ') === 0) {
return trim(substr($auth, 7));
}
return '';
}
private function looksLikeJwt(string $token): bool
{
$parts = explode('.', $token);
return count($parts) >= self::JWT_PARTS_MIN;
}
/**
* 校验 auth-token 有效性签名、过期、iss 等),无效或过期必抛 ApiException
*/
private function verifyAuthToken(string $token): array
{
try {
return JwtToken::verify(1, $token);
} catch (JwtTokenExpiredException $e) {
Log::error('auth-token 已过期, 报错信息' . $e);
throw new ApiException('auth-token 已过期', ReturnCode::TOKEN_INVALID);
} catch (JwtTokenException $e) {
Log::error('auth-token 无效, 报错信息' . $e);
throw new ApiException($e->getMessage() ?: 'auth-token 无效', ReturnCode::TOKEN_INVALID);
} catch (\Throwable $e) {
Log::error('auth-token 校验失败, 报错信息' . $e);
throw new ApiException('auth-token 校验失败', ReturnCode::TOKEN_INVALID);
}
}
private function isWhitelist(string $path): bool
{
foreach (self::WHITELIST as $prefix) {
if ($path === $prefix || str_starts_with($path, $prefix . '/')) {
return true;
}
}
return false;
}
}

View File

@@ -1,42 +0,0 @@
<?php
declare(strict_types=1);
namespace app\api\middleware;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;
use app\api\logic\UserLogic;
use app\api\util\ReturnCode;
use plugin\saiadmin\exception\ApiException;
/**
* 校验 user-token 请求头
* 从 header 读取 user-token 或 Authorization: Bearer <user-token>,校验通过后将 user_id、userToken 写入 request 供控制器使用
*/
class CheckUserTokenMiddleware implements MiddlewareInterface
{
public function process(Request $request, callable $handler): Response
{
$token = $request->header('user-token');
if (empty($token)) {
$auth = $request->header('authorization');
if ($auth && stripos($auth, 'Bearer ') === 0) {
$token = trim(substr($auth, 7));
}
}
if (empty($token)) {
throw new ApiException('请携带 user-token', ReturnCode::UNAUTHORIZED);
}
$userId = UserLogic::getUserIdFromToken($token);
if ($userId === null) {
throw new ApiException('user-token 无效或已过期', ReturnCode::TOKEN_INVALID);
}
$request->user_id = $userId;
$request->userToken = $token;
return $handler($request);
}
}

View File

@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace app\api\middleware;
use app\api\cache\UserCache;
use app\api\util\ReturnCode;
use app\dice\model\player\DicePlayer;
use plugin\saiadmin\exception\ApiException;
use Tinywan\Jwt\JwtToken;
use Tinywan\Jwt\Exception\JwtTokenException;
use Tinywan\Jwt\Exception\JwtTokenExpiredException;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;
/**
* 校验 token 请求头JWT
* 解码 JWT 取 username与 Redis 中当前有效 token 比对;不一致则旧 token 已失效,请重新登录
* 通过后注入 request->player_id、request->player
*/
class TokenMiddleware implements MiddlewareInterface
{
public function process(Request $request, callable $handler): Response
{
$token = $request->header('token');
if ($token === null || $token === '') {
$auth = $request->header('authorization');
if ($auth && stripos($auth, 'Bearer ') === 0) {
$token = trim(substr($auth, 7));
}
}
$token = $token !== null ? trim((string) $token) : '';
if ($token === '') {
throw new ApiException('请携带 token', ReturnCode::UNAUTHORIZED);
}
try {
$decoded = JwtToken::verify(1, $token);
} catch (JwtTokenExpiredException $e) {
throw new ApiException('token 已过期,请重新登录', ReturnCode::TOKEN_INVALID);
} catch (JwtTokenException $e) {
throw new ApiException('token 无效', ReturnCode::TOKEN_INVALID);
} catch (\Throwable $e) {
throw new ApiException('token 格式无效', ReturnCode::TOKEN_INVALID);
}
$extend = $decoded['extend'] ?? [];
if ((string) ($extend['plat'] ?? '') !== 'api_login') {
throw new ApiException('token 无效', ReturnCode::TOKEN_INVALID);
}
$username = trim((string) ($extend['username'] ?? ''));
if ($username === '') {
throw new ApiException('token 无效', ReturnCode::TOKEN_INVALID);
}
$currentToken = UserCache::getSessionTokenByUsername($username);
if ($currentToken === null || $currentToken === '') {
$player = DicePlayer::where('username', $username)->find();
if (!$player) {
throw new ApiException('请注册', ReturnCode::TOKEN_INVALID);
}
throw new ApiException('请重新登录', ReturnCode::TOKEN_INVALID);
}
if ($currentToken !== $token) {
throw new ApiException('请重新登录(当前账号已在其他处登录)', ReturnCode::TOKEN_INVALID);
}
// 优先从 Redis 缓存取玩家,避免每次请求都查库
$player = null;
$cached = UserCache::getPlayerByUsername($username);
if ($cached !== null && isset($cached['id'])) {
$player = (new DicePlayer())->data($cached, true);
}
if ($player === null) {
$player = DicePlayer::where('username', $username)->find();
if (!$player) {
UserCache::deleteSessionByUsername($username);
throw new ApiException('请重新登录', ReturnCode::TOKEN_INVALID);
}
UserCache::setPlayerByUsername($username, $player->hidden(['password'])->toArray());
}
$request->player_id = (int) $player->id;
$request->player = $player;
return $handler($request);
}
}

View File

@@ -19,7 +19,7 @@ class LotteryService
private int $playerId; private int $playerId;
private ?int $configType0Id = null; private ?int $configType0Id = null;
private ?int $configType1Id = null; private ?int $configType1Id = null;
/** @var array{t1_wight?:int,t2_wight?:int,t3_wight?:int,t4_wight?:int,t5_wight?:int} */ /** @var array{t1_weight?:int,t2_weight?:int,t3_weight?:int,t4_weight?:int,t5_weight?:int} */
private array $playerWeights = []; private array $playerWeights = [];
public function __construct(int $playerId) public function __construct(int $playerId)
@@ -62,11 +62,11 @@ class LotteryService
$s->configType0Id = $config0 ? (int) $config0->id : null; $s->configType0Id = $config0 ? (int) $config0->id : null;
$s->configType1Id = $config1 ? (int) $config1->id : null; $s->configType1Id = $config1 ? (int) $config1->id : null;
$s->playerWeights = [ $s->playerWeights = [
't1_wight' => (int) ($player->t1_wight ?? 0), 't1_weight' => (int) ($player->t1_weight ?? 0),
't2_wight' => (int) ($player->t2_wight ?? 0), 't2_weight' => (int) ($player->t2_weight ?? 0),
't3_wight' => (int) ($player->t3_wight ?? 0), 't3_weight' => (int) ($player->t3_weight ?? 0),
't4_wight' => (int) ($player->t4_wight ?? 0), 't4_weight' => (int) ($player->t4_weight ?? 0),
't5_wight' => (int) ($player->t5_wight ?? 0), 't5_weight' => (int) ($player->t5_weight ?? 0),
]; ];
$s->save(); $s->save();
return $s; return $s;
@@ -83,17 +83,40 @@ class LotteryService
Cache::set($key, json_encode($data), self::EXPIRE); Cache::set($key, json_encode($data), self::EXPIRE);
} }
/** 根据奖池配置的 t1_wight..t5_wight 权重随机抽取档位 T1-T5 */ /** 根据奖池配置的 t1_weight..t5_weight 权重随机抽取档位 T1-T5 */
public static function drawTierByWeights(DiceLotteryConfig $config): string public static function drawTierByWeights(DiceLotteryConfig $config): string
{ {
$tiers = ['T1', 'T2', 'T3', 'T4', 'T5']; $tiers = ['T1', 'T2', 'T3', 'T4', 'T5'];
$weights = [ $weights = [
(int) ($config->t1_wight ?? 0), (int) ($config->t1_weight ?? 0),
(int) ($config->t2_wight ?? 0), (int) ($config->t2_weight ?? 0),
(int) ($config->t3_wight ?? 0), (int) ($config->t3_weight ?? 0),
(int) ($config->t4_wight ?? 0), (int) ($config->t4_weight ?? 0),
(int) ($config->t5_wight ?? 0), (int) ($config->t5_weight ?? 0),
]; ];
return self::drawTierByWeightArray($tiers, $weights);
}
/**
* 根据玩家 t1_weightt5_weight 权重随机抽取中奖档位 T1-T5
* t1_weight=T1, t2_weight=T2, t3_weight=T3, t4_weight=T4, t5_weight=T5
*/
public static function drawTierByPlayerWeights(DicePlayer $player): string
{
$tiers = ['T1', 'T2', 'T3', 'T4', 'T5'];
$weights = [
(int) ($player->t1_weight ?? 0),
(int) ($player->t2_weight ?? 0),
(int) ($player->t3_weight ?? 0),
(int) ($player->t4_weight ?? 0),
(int) ($player->t5_weight ?? 0),
];
return self::drawTierByWeightArray($tiers, $weights);
}
/** 按档位权重数组抽取 T1-T5 */
private static function drawTierByWeightArray(array $tiers, array $weights): string
{
$total = array_sum($weights); $total = array_sum($weights);
if ($total <= 0) { if ($total <= 0) {
return $tiers[array_rand($tiers)]; return $tiers[array_rand($tiers)];

View File

@@ -0,0 +1,217 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\channel\controller\manage;
use plugin\saiadmin\basic\BaseController;
use app\channel\logic\manage\ChannelManageLogic;
use app\channel\validate\manage\ChannelManageValidate;
use plugin\saiadmin\app\model\system\SystemUser;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;
/**
* 渠道控制器
*/
class ChannelManageController extends BaseController
{
/**
* 构造函数
*/
public function __construct()
{
$this->logic = new ChannelManageLogic();
$this->validate = new ChannelManageValidate;
parent::__construct();
}
/**
* 数据列表
* @param Request $request
* @return Response
*/
#[Permission('渠道列表', 'channel:manage:index:index')]
public function index(Request $request): Response
{
$where = $request->more([
['name', ''],
['title', ''],
['status', ''],
['total_recharge', ''],
['total_withdrawal', ''],
['total_profit', ''],
]);
$query = $this->logic->search($where);
// 不使用 with('admin')SystemUser 为 Think 模型,与 Eloquent 混用会触发 getConnectionName 报错,改为手动填充
$data = $this->logic->getList($query);
$data['data'] = $this->attachAdminToRows($data['data'] ?? []);
return $this->success($data);
}
/**
* 读取数据
* @param Request $request
* @return Response
*/
#[Permission('渠道读取', 'channel:manage:index:read')]
public function read(Request $request): Response
{
$id = $request->input('id', '');
$model = $this->logic->read($id);
if ($model) {
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
} else {
return $this->fail('未查找到信息');
}
}
/**
* 保存数据
* @param Request $request
* @return Response
*/
#[Permission('渠道添加', 'channel:manage:index:save')]
public function save(Request $request): Response
{
$data = $request->post();
$data['agent'] = md5((string)($data['name'] ?? '') . (string)($data['admin_id'] ?? ''));
$this->validate('save', $data);
$result = $this->logic->add($data);
if ($result) {
return $this->success('添加成功');
} else {
return $this->fail('添加失败');
}
}
/**
* 更新数据
* @param Request $request
* @return Response
*/
#[Permission('渠道修改', 'channel:manage:index:update')]
public function update(Request $request): Response
{
$data = $request->post();
$data['agent'] = md5((string)($data['name'] ?? '') . (string)($data['admin_id'] ?? ''));
$this->validate('update', $data);
$result = $this->logic->edit($data['id'], $data);
if ($result) {
return $this->success('修改成功');
} else {
return $this->fail('修改失败');
}
}
/**
* 仅更新状态(列表内开关用)
* @param Request $request
* @return Response
*/
#[Permission('渠道管理-更新状态', 'channel:manage:index:update')]
public function updateStatus(Request $request): Response
{
$id = $request->input('id');
$status = $request->input('status');
if ($id === null || $id === '') {
return $this->fail('缺少 id');
}
if ($status === null || $status === '') {
return $this->fail('缺少 status');
}
$this->logic->edit($id, ['status' => (int) $status]);
return $this->success('修改成功');
}
/**
* 删除数据
* @param Request $request
* @return Response
*/
#[Permission('渠道删除', 'channel:manage:index:destroy')]
public function destroy(Request $request): Response
{
$ids = $request->post('ids', '');
if (empty($ids)) {
return $this->fail('请选择要删除的数据');
}
$result = $this->logic->destroy($ids);
if ($result) {
return $this->success('删除成功');
} else {
return $this->fail('删除失败');
}
}
/**
* 获取管理员下拉列表(远程关联 SystemUservalue=id, label=name
* @param Request $request
* @return Response
*/
#[Permission('渠道管理员列表', 'channel:manage:index:index')]
public function getAdminList(Request $request): Response
{
$list = SystemUser::where('status', 1)
->field('id, username, realname')
->select();
$rows = $list->toArray();
$data = array_map(function ($row) {
$name = !empty($row['realname']) && trim($row['realname']) !== ''
? trim($row['realname'])
: ($row['username'] ?? '#' . $row['id']);
return [
'id' => $row['id'],
'name' => $name,
];
}, $rows);
return $this->success($data);
}
/**
* 为列表行附加管理员信息(避免 Eloquent with(Think 模型) 触发 getConnectionName 报错)
* @param array $rows 列表行,可为 Eloquent 模型或数组
* @return array 已附加 admin 的列表
*/
protected function attachAdminToRows(array $rows): array
{
if (empty($rows)) {
return $rows;
}
$adminIds = [];
foreach ($rows as $row) {
$id = is_array($row) ? ($row['admin_id'] ?? null) : $row->admin_id;
if ($id !== null && $id !== '') {
$adminIds[(string) $id] = true;
}
}
$adminIds = array_keys($adminIds);
if (empty($adminIds)) {
return $rows;
}
$admins = SystemUser::whereIn('id', $adminIds)
->field('id, username, realname')
->select()
->toArray();
$map = [];
foreach ($admins as $a) {
$map[(string) ($a['id'] ?? '')] = $a;
}
foreach ($rows as &$row) {
$aid = is_array($row) ? ($row['admin_id'] ?? null) : $row->admin_id;
$admin = $map[(string) ($aid ?? '')] ?? null;
if (is_array($row)) {
$row['admin'] = $admin;
} else {
$row->setAttribute('admin', $admin);
}
}
unset($row);
return $rows;
}
}

View File

@@ -0,0 +1,27 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\channel\logic\manage;
use plugin\saiadmin\basic\eloquent\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\channel\model\manage\ChannelManage;
/**
* 渠道逻辑层
*/
class ChannelManageLogic extends BaseLogic
{
/**
* 构造函数
*/
public function __construct()
{
$this->model = new ChannelManage();
}
}

View File

@@ -0,0 +1,103 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\channel\model\manage;
use plugin\saiadmin\app\model\system\SystemUser;
use plugin\saiadmin\basic\eloquent\BaseModel;
/**
* 渠道模型
*
* channel_manage 渠道
* @property $id ID
* @property $name 名称
* @property $title 标题
* @property $status 状态
* @property $admin_id 管理员
* @property $game_url 游戏地址
* @property $image 图标
* @property $agent 代理agent
* @property $ip_white IP白名单
* @property $total_recharge 总充值
* @property $total_withdrawal 总提现
* @property $total_profit 总盈利
* @property $player_count 玩家数量
* @property $create_time 创建时间
* @property $update_time 更新时间
*/
class ChannelManage extends BaseModel
{
/**
* 数据表主键
* @var string
*/
protected $primaryKey = 'id';
/**
* 数据库表名称
* @var string
*/
protected $table = 'channel_manage';
/**
* 属性转换
*/
protected function casts(): array
{
return array_merge(parent::casts(), [
]);
}
/**
* 名称 搜索
*/
public function searchNameAttr($query, $value)
{
$query->where('name', 'like', '%'.$value.'%');
}
/**
* 标题 搜索
*/
public function searchTitleAttr($query, $value)
{
$query->where('title', 'like', '%'.$value.'%');
}
/**
* 总充值 搜索
*/
public function searchTotalRechargeAttr($query, $value)
{
$query->where('total_recharge', '>=', $value);
}
/**
* 总提现 搜索
*/
public function searchTotalWithdrawalAttr($query, $value)
{
$query->where('total_withdrawal', '>=', $value);
}
/**
* 总盈利 搜索
*/
public function searchTotalProfitAttr($query, $value)
{
$query->where('total_profit', '>=', $value);
}
/**
* 管理员
* 关联模型 systemUser
*/
public function admin(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(SystemUser::class, 'admin_id', 'id');
}
}

View File

@@ -0,0 +1,58 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\channel\validate\manage;
use plugin\saiadmin\basic\BaseValidate;
/**
* 渠道验证器
*/
class ChannelManageValidate extends BaseValidate
{
/**
* 定义验证规则
*/
protected $rule = [
'name' => 'require',
'title' => 'require',
'admin_id' => 'require',
'game_url' => 'require',
'image' => 'require',
];
/**
* 定义错误信息
*/
protected $message = [
'name' => '名称必须填写',
'title' => '标题必须填写',
'admin_id' => '管理员必须填写',
'game_url' => '游戏地址必须填写',
'image' => '图标必须填写',
];
/**
* 定义场景agent 由后端按 md5(name.admin_id) 自动生成,无需校验)
*/
protected $scene = [
'save' => [
'name',
'title',
'admin_id',
'game_url',
'image',
],
'update' => [
'name',
'title',
'admin_id',
'game_url',
'image',
],
];
}

View File

@@ -0,0 +1,123 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\controller\config;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\config\DiceConfigLogic;
use app\dice\validate\config\DiceConfigValidate;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;
/**
* 摇色子配置控制器
*/
class DiceConfigController extends BaseController
{
/**
* 构造函数
*/
public function __construct()
{
$this->logic = new DiceConfigLogic();
$this->validate = new DiceConfigValidate;
parent::__construct();
}
/**
* 数据列表
* @param Request $request
* @return Response
*/
#[Permission('摇色子配置列表', 'dice:config:index:index')]
public function index(Request $request): Response
{
$where = $request->more([
['name', ''],
['group', ''],
['title', ''],
]);
$query = $this->logic->search($where);
$data = $this->logic->getList($query);
return $this->success($data);
}
/**
* 读取数据
* @param Request $request
* @return Response
*/
#[Permission('摇色子配置读取', 'dice:config:index:read')]
public function read(Request $request): Response
{
$id = $request->input('id', '');
$model = $this->logic->read($id);
if ($model) {
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
} else {
return $this->fail('未查找到信息');
}
}
/**
* 保存数据
* @param Request $request
* @return Response
*/
#[Permission('摇色子配置添加', 'dice:config:index:save')]
public function save(Request $request): Response
{
$data = $request->post();
$this->validate('save', $data);
$result = $this->logic->add($data);
if ($result) {
return $this->success('添加成功');
} else {
return $this->fail('添加失败');
}
}
/**
* 更新数据
* @param Request $request
* @return Response
*/
#[Permission('摇色子配置修改', 'dice:config:index:update')]
public function update(Request $request): Response
{
$data = $request->post();
$this->validate('update', $data);
$result = $this->logic->edit($data['id'], $data);
if ($result) {
return $this->success('修改成功');
} else {
return $this->fail('修改失败');
}
}
/**
* 删除数据
* @param Request $request
* @return Response
*/
#[Permission('摇色子配置删除', 'dice:config:index:destroy')]
public function destroy(Request $request): Response
{
$ids = $request->post('ids', '');
if (empty($ids)) {
return $this->fail('请选择要删除的数据');
}
$result = $this->logic->destroy($ids);
if ($result) {
return $this->success('删除成功');
} else {
return $this->fail('删除失败');
}
}
}

View File

@@ -6,6 +6,7 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace app\dice\controller\lottery_config; namespace app\dice\controller\lottery_config;
use app\dice\model\lottery_config\DiceLotteryConfig;
use plugin\saiadmin\basic\BaseController; use plugin\saiadmin\basic\BaseController;
use app\dice\logic\lottery_config\DiceLotteryConfigLogic; use app\dice\logic\lottery_config\DiceLotteryConfigLogic;
use app\dice\validate\lottery_config\DiceLotteryConfigValidate; use app\dice\validate\lottery_config\DiceLotteryConfigValidate;
@@ -28,6 +29,21 @@ class DiceLotteryConfigController extends BaseController
parent::__construct(); parent::__construct();
} }
/**
* 获取 DiceLotteryConfig 列表数据,仅含 id、name用于 lottery_config_id 下拉(值为 id显示为 name
* @param Request $request
* @return Response 返回 [ ['id' => int, 'name' => string], ... ]
*/
#[Permission('色子奖池配置列表', 'dice:lottery_config:index:index')]
public function getOptions(Request $request): Response
{
$list = DiceLotteryConfig::field('id,name')->order('id', 'asc')->select();
$data = $list->map(function ($item) {
return ['id' => (int) $item['id'], 'name' => (string) ($item['name'] ?? '')];
})->toArray();
return $this->success($data);
}
/** /**
* 数据列表 * 数据列表
* @param Request $request * @param Request $request

View File

@@ -46,6 +46,8 @@ class DicePlayRecordController extends BaseController
['is_win', ''], ['is_win', ''],
['win_coin_min', ''], ['win_coin_min', ''],
['win_coin_max', ''], ['win_coin_max', ''],
['roll_number_min', ''],
['roll_number_max', ''],
['reward_ui_text', ''], ['reward_ui_text', ''],
['reward_tier', ''], ['reward_tier', ''],
['direction', ''], ['direction', ''],

View File

@@ -6,6 +6,7 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace app\dice\controller\player; namespace app\dice\controller\player;
use app\dice\model\lottery_config\DiceLotteryConfig;
use plugin\saiadmin\basic\BaseController; use plugin\saiadmin\basic\BaseController;
use app\dice\logic\player\DicePlayerLogic; use app\dice\logic\player\DicePlayerLogic;
use app\dice\validate\player\DicePlayerValidate; use app\dice\validate\player\DicePlayerValidate;
@@ -28,6 +29,21 @@ class DicePlayerController extends BaseController
parent::__construct(); parent::__construct();
} }
/**
* 获取彩金池配置选项DiceLotteryConfig.id、name供前端 lottery_config_id 下拉使用
* @param Request $request
* @return Response 返回 [ ['id' => int, 'name' => string], ... ]
*/
#[Permission('大富翁-玩家列表', 'dice:player:index:index')]
public function getLotteryConfigOptions(Request $request): Response
{
$list = DiceLotteryConfig::field('id,name')->order('id', 'asc')->select();
$data = $list->map(function ($item) {
return ['id' => (int) $item['id'], 'name' => (string) ($item['name'] ?? '')];
})->toArray();
return $this->success($data);
}
/** /**
* 数据列表 * 数据列表
* @param Request $request * @param Request $request
@@ -42,9 +58,10 @@ class DicePlayerController extends BaseController
['phone', ''], ['phone', ''],
['status', ''], ['status', ''],
['coin', ''], ['coin', ''],
['is_up', ''], ['lottery_config_id', ''],
]); ]);
$query = $this->logic->search($where); $query = $this->logic->search($where);
$query->with(['diceLotteryConfig']);
$data = $this->logic->getList($query); $data = $this->logic->getList($query);
return $this->success($data); return $this->success($data);
} }

View File

@@ -0,0 +1,27 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\logic\config;
use plugin\saiadmin\basic\eloquent\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\dice\model\config\DiceConfig;
/**
* 摇色子配置逻辑层
*/
class DiceConfigLogic extends BaseLogic
{
/**
* 构造函数
*/
public function __construct()
{
$this->model = new DiceConfig();
}
}

View File

@@ -43,16 +43,18 @@ class DicePlayRecordLogic extends BaseLogic
} }
/** /**
* 将 roll_array 从数组转为 JSON 字符串 * 将 roll_array 转为 JSON 字符串,并确保 roll_number 与摇取点数一致
*/ */
private function normalizeRollArray(array $data): array private function normalizeRollArray(array $data): array
{ {
if (!array_key_exists('roll_array', $data)) { if (array_key_exists('roll_array', $data)) {
return $data;
}
$val = $data['roll_array']; $val = $data['roll_array'];
if (is_array($val)) { if (is_array($val)) {
$data['roll_array'] = json_encode($val, JSON_UNESCAPED_UNICODE); $data['roll_array'] = json_encode($val, JSON_UNESCAPED_UNICODE);
if (!isset($data['roll_number'])) {
$data['roll_number'] = array_sum($val);
}
}
} }
return $data; return $data;
} }

View File

@@ -13,6 +13,7 @@ use app\dice\model\reward_config\DiceRewardConfig;
/** /**
* 奖励配置逻辑层 * 奖励配置逻辑层
* weight 仅 tier=BIGWIN 时可设定,保存时非 BIGWIN 强制 weight=0
*/ */
class DiceRewardConfigLogic extends BaseLogic class DiceRewardConfigLogic extends BaseLogic
{ {
@@ -24,4 +25,36 @@ class DiceRewardConfigLogic extends BaseLogic
$this->model = new DiceRewardConfig(); $this->model = new DiceRewardConfig();
} }
/**
* 新增前:非 BIGWIN 时强制 weight=0
*/
public function add(array $data): mixed
{
$data = $this->normalizeWeightByTier($data);
return parent::add($data);
}
/**
* 修改前:非 BIGWIN 时强制 weight=0
*/
public function edit($id, array $data): mixed
{
$data = $this->normalizeWeightByTier($data);
return parent::edit($id, $data);
}
/**
* 仅 tier=BIGWIN 时保留 weight且限制 0-100否则强制为 0
*/
private function normalizeWeightByTier(array $data): array
{
$tier = isset($data['tier']) ? (string) $data['tier'] : '';
if ($tier !== 'BIGWIN') {
$data['weight'] = 0;
return $data;
}
$w = isset($data['weight']) ? (float) $data['weight'] : 0;
$data['weight'] = max(0, min(100, $w));
return $data;
}
} }

View File

@@ -0,0 +1,81 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\model\config;
use plugin\saiadmin\basic\eloquent\BaseModel;
/**
* 摇色子配置模型
*
* dice_config 摇色子配置
*
* @property $id ID
* @property $name 配置名称
* @property $group 分组
* @property $title 标题
* @property $value 值
* @property $create_time 创建时间
* @property $update_time 修改时间
*/
class DiceConfig extends BaseModel
{
/**
* 数据表主键
* @var string
*/
protected $primaryKey = 'id';
/**
* 数据库表名称
* @var string
*/
protected $table = 'dice_config';
/**
* 是否自动维护 create_time / update_time继承基类 CREATED_AT / UPDATED_AT
* @var bool
*/
public $timestamps = true;
/**
* 属性转换
*/
protected function casts(): array
{
return array_merge(parent::casts(), [
]);
}
/**
* 配置名称 搜索
*/
public function searchNameAttr($query, $value)
{
$query->where('name', 'like', '%'.$value.'%');
}
/**
* 分组 搜索
*/
public function searchGroupAttr($query, $value)
{
if ($value === '' || $value === null) {
return;
}
$query->where('group', '=', $value);
}
/**
* 标题 搜索
*/
public function searchTitleAttr($query, $value)
{
if ($value === '' || $value === null) {
return;
}
$query->where('title', 'like', '%' . $value . '%');
}
}

View File

@@ -20,11 +20,11 @@ use plugin\saiadmin\basic\think\BaseModel;
* @property $safety_line 安全线 * @property $safety_line 安全线
* @property $create_time 创建时间 * @property $create_time 创建时间
* @property $update_time 修改时间 * @property $update_time 修改时间
* @property $t1_wight T1池权重 * @property $t1_weight T1池权重
* @property $t2_wight T2池权重 * @property $t2_weight T2池权重
* @property $t3_wight T3池权重 * @property $t3_weight T3池权重
* @property $t4_wight T4池权重 * @property $t4_weight T4池权重
* @property $t5_wight T5池权重 * @property $t5_weight T5池权重
*/ */
class DiceLotteryConfig extends BaseModel class DiceLotteryConfig extends BaseModel
{ {
@@ -48,4 +48,13 @@ class DiceLotteryConfig extends BaseModel
$query->where('name', 'like', '%'.$value.'%'); $query->where('name', 'like', '%'.$value.'%');
} }
/**
* 奖池类型 搜索type=0/1/2 等)
*/
public function searchTypeAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('type', '=', $value);
}
}
} }

View File

@@ -21,14 +21,17 @@ use think\model\relation\BelongsTo;
* @property $player_id 玩家id * @property $player_id 玩家id
* @property $lottery_config_id 彩金池配置 * @property $lottery_config_id 彩金池配置
* @property $lottery_type 抽奖类型 * @property $lottery_type 抽奖类型
* @property $is_win 中奖 * @property $is_win 是否中大奖:豹子号[1,1,1,1,1]~[6,6,6,6,6]为1否则0
* @property $win_coin 赢取平台币 * @property $win_coin 赢取平台币= super_win_coin + reward_win_coin
* @property $super_win_coin 中大奖平台币(豹子时发放)
* @property $reward_win_coin 摇色子中奖平台币
* @property $direction 方向:0=顺时针,1=逆时针 * @property $direction 方向:0=顺时针,1=逆时针
* @property $reward_config_id 奖励配置id * @property $reward_config_id 奖励配置id
* @property $lottery_id 奖池 * @property $lottery_id 奖池
* @property $start_index 起始索引 * @property $start_index 起始索引
* @property $target_index 结束索引 * @property $target_index 结束索引
* @property $roll_array 摇取点数,格式:[1,2,3,4,5]5个点数 * @property $roll_array 摇取点数,格式:[1,2,3,4,5]5个点数
* @property $roll_number 摇取点数和5个色子点数之和5-30
* @property $lottery_name 奖池名 * @property $lottery_name 奖池名
* @property $status 状态:0=超时/失败 1=成功 * @property $status 状态:0=超时/失败 1=成功
* @property $create_time 创建时间 * @property $create_time 创建时间
@@ -111,7 +114,25 @@ class DicePlayRecord extends BaseModel
} }
} }
/** 中奖 */ /**
* 是否豹子号中大奖5 个点数相同且为 1~6 之一(含 [6,6,6,6,6]
* @param int[] $rollArray 摇取点数数组,如 [1,1,1,1,1] 或 [6,6,6,6,6]
* @return bool
*/
public static function isSuperWin(array $rollArray): bool
{
if (count($rollArray) !== 5) {
return false;
}
$unique = array_unique($rollArray);
if (count($unique) !== 1) {
return false;
}
$value = reset($unique);
return in_array($value, [1, 2, 3, 4, 5, 6], true);
}
/** 是否中大奖 */
public function searchIsWinAttr($query, $value) public function searchIsWinAttr($query, $value)
{ {
if ($value !== '' && $value !== null) { if ($value !== '' && $value !== null) {
@@ -135,6 +156,38 @@ class DicePlayRecord extends BaseModel
} }
} }
/** 中大奖平台币下限 */
public function searchSuperWinCoinMinAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('super_win_coin', '>=', $value);
}
}
/** 中大奖平台币上限 */
public function searchSuperWinCoinMaxAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('super_win_coin', '<=', $value);
}
}
/** 摇色子中奖平台币下限 */
public function searchRewardWinCoinMinAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('reward_win_coin', '>=', $value);
}
}
/** 摇色子中奖平台币上限 */
public function searchRewardWinCoinMaxAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('reward_win_coin', '<=', $value);
}
}
/** 按奖励配置前端显示文本模糊diceRewardConfig.ui_text */ /** 按奖励配置前端显示文本模糊diceRewardConfig.ui_text */
public function searchRewardUiTextAttr($query, $value) public function searchRewardUiTextAttr($query, $value)
{ {
@@ -170,4 +223,20 @@ class DicePlayRecord extends BaseModel
$query->where('direction', '=', $value); $query->where('direction', '=', $value);
} }
} }
/** 摇取点数和下限 */
public function searchRollNumberMinAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('roll_number', '>=', $value);
}
}
/** 摇取点数和上限 */
public function searchRollNumberMaxAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('roll_number', '<=', $value);
}
}
} }

View File

@@ -22,18 +22,18 @@ use app\dice\model\lottery_config\DiceLotteryConfig;
* @property $password 密码 * @property $password 密码
* @property $status 状态 * @property $status 状态
* @property $coin 平台币 * @property $coin 平台币
* @property $is_up 倍率 * @property $lottery_config_id 彩金池配置ID0或null时使用自定义权重*_weight
* @property $t1_wight T1池权重 * @property $t1_weight T1池权重
* @property $t2_wight T2池权重 * @property $t2_weight T2池权重
* @property $t3_wight T3池权重 * @property $t3_weight T3池权重
* @property $t4_wight T4池权重 * @property $t4_weight T4池权重
* @property $t5_wight T5池权重 * @property $t5_weight T5池权重
* @property $total_ticket_count 总抽奖次数 * @property $total_ticket_count 总抽奖次数
* @property $paid_ticket_count 购买抽奖次数 * @property $paid_ticket_count 购买抽奖次数
* @property $free_ticket_count 赠送抽奖次数 * @property $free_ticket_count 赠送抽奖次数
* @property $created_at 创建时间 * @property $create_time 创建时间
* @property $updated_at 更新时间 * @property $update_time 更新时间
* @property $deleted_at 删除时间 * @property $delete_time 删除时间
*/ */
class DicePlayer extends BaseModel class DicePlayer extends BaseModel
{ {
@@ -49,6 +49,10 @@ class DicePlayer extends BaseModel
*/ */
protected $table = 'dice_player'; protected $table = 'dice_player';
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
/** /**
* 新增前:生成唯一 uid昵称 name 默认使用 uid * 新增前:生成唯一 uid昵称 name 默认使用 uid
* 用 try-catch 避免表尚未含 uid 时 getAttr/getData 抛 InvalidArgumentException * 用 try-catch 避免表尚未含 uid 时 getAttr/getData 抛 InvalidArgumentException
@@ -73,12 +77,22 @@ class DicePlayer extends BaseModel
if ($name === null || $name === '') { if ($name === null || $name === '') {
$model->setAttr('name', $uid); $model->setAttr('name', $uid);
} }
// 创建玩家时:未指定则自动保存 lottery_config_id 为 DiceLotteryConfig type=0 的 id没有则为 0
try {
$lotteryConfigId = $model->getAttr('lottery_config_id');
} catch (\Throwable $e) {
$lotteryConfigId = null;
}
if ($lotteryConfigId === null || $lotteryConfigId === '' || (int) $lotteryConfigId === 0) {
$config = DiceLotteryConfig::where('type', 0)->find();
$model->setAttr('lottery_config_id', $config ? (int) $config->id : 0);
}
// 彩金池权重默认取 type=0 的奖池配置 // 彩金池权重默认取 type=0 的奖池配置
self::setDefaultWeightsFromLotteryConfig($model); self::setDefaultWeightsFromLotteryConfig($model);
} }
/** /**
* 从 DiceLotteryConfig type=0 取 t1_wightt5_wight 作为玩家未设置时的默认值 * 从 DiceLotteryConfig type=0 取 t1_weightt5_weight 作为玩家未设置时的默认值
*/ */
protected static function setDefaultWeightsFromLotteryConfig(DicePlayer $model): void protected static function setDefaultWeightsFromLotteryConfig(DicePlayer $model): void
{ {
@@ -86,7 +100,7 @@ class DicePlayer extends BaseModel
if (!$config) { if (!$config) {
return; return;
} }
$fields = ['t1_wight', 't2_wight', 't3_wight', 't4_wight', 't5_wight']; $fields = ['t1_weight', 't2_weight', 't3_weight', 't4_weight', 't5_weight'];
foreach ($fields as $field) { foreach ($fields as $field) {
try { try {
$val = $model->getAttr($field); $val = $model->getAttr($field);
@@ -158,13 +172,20 @@ class DicePlayer extends BaseModel
} }
/** /**
* 倍率 搜索 * 彩金池配置ID 搜索
*/ */
public function searchIs_upAttr($query, $value) public function searchLottery_config_idAttr($query, $value)
{ {
if ($value !== '' && $value !== null) { if ($value !== '' && $value !== null) {
$query->where('is_up', '=', $value); $query->where('lottery_config_id', '=', $value);
} }
} }
/**
* 关联彩金池配置
*/
public function diceLotteryConfig()
{
return $this->belongsTo(DiceLotteryConfig::class, 'lottery_config_id', 'id');
}
} }

View File

@@ -7,23 +7,37 @@
namespace app\dice\model\reward_config; namespace app\dice\model\reward_config;
use plugin\saiadmin\basic\think\BaseModel; use plugin\saiadmin\basic\think\BaseModel;
use support\think\Cache;
/** /**
* 奖励配置模型 * 奖励配置模型
* *
* dice_reward_config 奖励配置 * dice_reward_config 奖励配置
* 奖励列表为全玩家通用,保存时刷新缓存,游戏时优先读缓存。
* *
* @property $id ID * @property $id ID
* @property $grid_number 色子点数 * @property $grid_number 色子点数
* @property $ui_text 前端显示文本 * @property $ui_text 前端显示文本
* @property $real_ev 真实资金结算 * @property $real_ev 真实资金结算
* @property $tier 所属档位 * @property $tier 所属档位
* @property $weight 权重%(仅 tier=BIGWIN 时可设定0-100
* @property $s_end_index 顺时针结束索引
* @property $n_end_index 逆时针结束索引
* @property $remark 备注 * @property $remark 备注
* @property $create_time 创建时间 * @property $create_time 创建时间
* @property $update_time 修改时间 * @property $update_time 修改时间
*/ */
class DiceRewardConfig extends BaseModel class DiceRewardConfig extends BaseModel
{ {
/** 缓存键:彩金池奖励列表实例(含列表与索引) */
private const CACHE_KEY_INSTANCE = 'dice:reward_config:instance';
/** 缓存过期时间(秒),保存时会主动刷新故设较长 */
private const CACHE_TTL = 86400 * 30;
/** 当前请求内已加载的实例,避免同请求多次读缓存 */
private static ?array $instance = null;
/** /**
* 数据表主键 * 数据表主键
* @var string * @var string
@@ -36,6 +50,189 @@ class DiceRewardConfig extends BaseModel
*/ */
protected $table = 'dice_reward_config'; protected $table = 'dice_reward_config';
/**
* 获取彩金池实例(含 list / 索引),无则从库加载并写入缓存;同请求内复用
* @return array{list: array, by_tier: array, by_tier_grid: array, by_s_end_index: array, by_n_end_index: array, min_real_ev: float}
*/
public static function getCachedInstance(): array
{
if (self::$instance !== null) {
return self::$instance;
}
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
if ($instance !== null && is_array($instance)) {
self::$instance = $instance;
return $instance;
}
self::refreshCache();
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
self::$instance = is_array($instance) ? $instance : self::buildEmptyInstance();
return self::$instance;
}
/**
* 获取缓存的奖励列表(无则从库加载并写入缓存)
* @return array<int, array>
*/
public static function getCachedList(): array
{
$inst = self::getCachedInstance();
return $inst['list'] ?? [];
}
/**
* 重新从数据库加载并写入缓存DiceRewardConfig 新增/修改/删除后调用),构建列表与索引
* 实例化结果含完整行(含 weight供 playStart 从缓存中查找 BIGWIN 的 weight 按概率抽奖
*/
public static function refreshCache(): void
{
$list = (new self())->order('id', 'asc')->select()->toArray();
$byTier = [];
$byTierGrid = [];
$bySEndIndex = [];
$byNEndIndex = [];
foreach ($list as $row) {
$tier = isset($row['tier']) ? (string) $row['tier'] : '';
if ($tier !== '') {
// 过滤 tier=BIGWIN不参与档位抽奖仅豹子时通过 getCachedByTierAndGridNumber('BIGWIN', ...) 使用
if ($tier !== 'BIGWIN') {
if (!isset($byTier[$tier])) {
$byTier[$tier] = [];
}
$byTier[$tier][] = $row;
}
$gridNum = isset($row['grid_number']) ? (int) $row['grid_number'] : 0;
if (!isset($byTierGrid[$tier])) {
$byTierGrid[$tier] = [];
}
if (!isset($byTierGrid[$tier][$gridNum])) {
$byTierGrid[$tier][$gridNum] = $row;
}
}
$sEnd = isset($row['s_end_index']) ? (int) $row['s_end_index'] : 0;
if ($sEnd !== 0) {
if (!isset($bySEndIndex[$sEnd])) {
$bySEndIndex[$sEnd] = [];
}
$bySEndIndex[$sEnd][] = $row;
}
$nEnd = isset($row['n_end_index']) ? (int) $row['n_end_index'] : 0;
if ($nEnd !== 0) {
if (!isset($byNEndIndex[$nEnd])) {
$byNEndIndex[$nEnd] = [];
}
$byNEndIndex[$nEnd][] = $row;
}
}
$minRealEv = empty($list) ? 0.0 : (float) min(array_column($list, 'real_ev'));
self::$instance = [
'list' => $list,
'by_tier' => $byTier,
'by_tier_grid' => $byTierGrid,
'by_s_end_index' => $bySEndIndex,
'by_n_end_index' => $byNEndIndex,
'min_real_ev' => $minRealEv,
];
Cache::set(self::CACHE_KEY_INSTANCE, self::$instance, self::CACHE_TTL);
}
/** 空实例结构 */
private static function buildEmptyInstance(): array
{
return [
'list' => [],
'by_tier' => [],
'by_tier_grid' => [],
'by_s_end_index' => [],
'by_n_end_index' => [],
'min_real_ev' => 0.0,
];
}
/**
* 从缓存实例按档位 + 色子点数取一条奖励配置(用于超级大奖 tier=BIGWIN + grid_number=roll_number
* 返回行含 weight0-100playStart 据此概率抽奖weight=100 表示摇到该 roll_number 时 100% 中超级大奖
* @param string $tier 档位,如 BIGWIN
* @param int $gridNumber 色子点数(摇出总和 roll_number
* @return array|null 配置行(含 weight、real_ev 等)或 null
*/
public static function getCachedByTierAndGridNumber(string $tier, int $gridNumber): ?array
{
$inst = self::getCachedInstance();
$byTierGrid = $inst['by_tier_grid'] ?? [];
$tierData = $byTierGrid[$tier] ?? [];
$row = $tierData[$gridNumber] ?? null;
return is_array($row) ? $row : null;
}
/**
* 从缓存取最小 real_ev
*/
public static function getCachedMinRealEv(): float
{
$inst = self::getCachedInstance();
return (float) ($inst['min_real_ev'] ?? 0.0);
}
/**
* 从缓存按档位取奖励列表
* @return array<int, array>
*/
public static function getCachedByTier(string $tier): array
{
$inst = self::getCachedInstance();
$byTier = $inst['by_tier'] ?? [];
return $byTier[$tier] ?? [];
}
/**
* 从缓存按顺时针结束索引取列表s_end_index = id 的配置)
* @return array<int, array>
*/
public static function getCachedBySEndIndex(int $id): array
{
$inst = self::getCachedInstance();
$by = $inst['by_s_end_index'] ?? [];
return $by[$id] ?? [];
}
/**
* 从缓存按逆时针结束索引取列表n_end_index = id 的配置)
* @return array<int, array>
*/
public static function getCachedByNEndIndex(int $id): array
{
$inst = self::getCachedInstance();
$by = $inst['by_n_end_index'] ?? [];
return $by[$id] ?? [];
}
/**
* 清除当前请求内实例(如测试或需强制下次读缓存时调用)
*/
public static function clearRequestInstance(): void
{
self::$instance = null;
}
/** 保存后刷新缓存 */
public static function onAfterInsert($model): void
{
self::refreshCache();
}
/** 更新后刷新缓存 */
public static function onAfterUpdate($model): void
{
self::refreshCache();
}
/** 删除后刷新缓存 */
public static function onAfterDelete($model): void
{
self::refreshCache();
}
/** 色子点数下限 */ /** 色子点数下限 */
public function searchGridNumberMinAttr($query, $value) public function searchGridNumberMinAttr($query, $value)
{ {
@@ -83,4 +280,20 @@ class DiceRewardConfig extends BaseModel
$query->where('tier', '=', $value); $query->where('tier', '=', $value);
} }
} }
/** 权重下限(仅 tier=BIGWIN 时有意义) */
public function searchWeightMinAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('weight', '>=', $value);
}
}
/** 权重上限 */
public function searchWeightMaxAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('weight', '<=', $value);
}
}
} }

View File

@@ -0,0 +1,54 @@
<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\validate\config;
use plugin\saiadmin\basic\BaseValidate;
/**
* 摇色子配置验证器
*/
class DiceConfigValidate extends BaseValidate
{
/**
* 定义验证规则
*/
protected $rule = [
'name' => 'require',
'group' => 'require',
'title' => 'require',
'value' => 'require',
];
/**
* 定义错误信息
*/
protected $message = [
'name' => '配置名称必须填写',
'group' => '分组必须填写',
'title' => '标题必须填写',
'value' => '值必须填写',
];
/**
* 定义场景
*/
protected $scene = [
'save' => [
'name',
'group',
'title',
'value',
],
'update' => [
'name',
'group',
'title',
'value',
],
];
}

View File

@@ -19,11 +19,11 @@ class DiceLotteryConfigValidate extends BaseValidate
protected $rule = [ protected $rule = [
'name' => 'require', 'name' => 'require',
'type' => 'require', 'type' => 'require',
't1_wight' => 'require', 't1_weight' => 'require',
't2_wight' => 'require', 't2_weight' => 'require',
't3_wight' => 'require', 't3_weight' => 'require',
't4_wight' => 'require', 't4_weight' => 'require',
't5_wight' => 'require', 't5_weight' => 'require',
]; ];
/** /**
@@ -32,11 +32,11 @@ class DiceLotteryConfigValidate extends BaseValidate
protected $message = [ protected $message = [
'name' => '名称必须填写', 'name' => '名称必须填写',
'type' => '奖池类型必须填写', 'type' => '奖池类型必须填写',
't1_wight' => 'T1池权重必须填写', 't1_weight' => 'T1池权重必须填写',
't2_wight' => 'T2池权重必须填写', 't2_weight' => 'T2池权重必须填写',
't3_wight' => 'T3池权重必须填写', 't3_weight' => 'T3池权重必须填写',
't4_wight' => 'T4池权重必须填写', 't4_weight' => 'T4池权重必须填写',
't5_wight' => 'T5池权重必须填写', 't5_weight' => 'T5池权重必须填写',
]; ];
/** /**
@@ -46,20 +46,20 @@ class DiceLotteryConfigValidate extends BaseValidate
'save' => [ 'save' => [
'name', 'name',
'type', 'type',
't1_wight', 't1_weight',
't2_wight', 't2_weight',
't3_wight', 't3_weight',
't4_wight', 't4_weight',
't5_wight', 't5_weight',
], ],
'update' => [ 'update' => [
'name', 'name',
'type', 'type',
't1_wight', 't1_weight',
't2_wight', 't2_weight',
't3_wight', 't3_weight',
't4_wight', 't4_weight',
't5_wight', 't5_weight',
], ],
]; ];

View File

@@ -10,6 +10,7 @@ use plugin\saiadmin\basic\BaseValidate;
/** /**
* 奖励配置验证器 * 奖励配置验证器
* weight 仅当 tier=BIGWIN 时可设定,且严格限制 0-100%
*/ */
class DiceRewardConfigValidate extends BaseValidate class DiceRewardConfigValidate extends BaseValidate
{ {
@@ -21,6 +22,7 @@ class DiceRewardConfigValidate extends BaseValidate
'ui_text' => 'require', 'ui_text' => 'require',
'real_ev' => 'require', 'real_ev' => 'require',
'tier' => 'require', 'tier' => 'require',
'weight' => 'checkWeight',
]; ];
/** /**
@@ -31,24 +33,33 @@ class DiceRewardConfigValidate extends BaseValidate
'ui_text' => '前端显示文本必须填写', 'ui_text' => '前端显示文本必须填写',
'real_ev' => '真实资金结算必须填写', 'real_ev' => '真实资金结算必须填写',
'tier' => '所属档位必须填写', 'tier' => '所属档位必须填写',
'weight' => '权重仅 tier=BIGWIN 时可设定,且必须为 0-100',
]; ];
/** /**
* 定义场景 * 定义场景
*/ */
protected $scene = [ protected $scene = [
'save' => [ 'save' => ['grid_number', 'ui_text', 'real_ev', 'tier', 'weight'],
'grid_number', 'update' => ['grid_number', 'ui_text', 'real_ev', 'tier', 'weight'],
'ui_text',
'real_ev',
'tier',
],
'update' => [
'grid_number',
'ui_text',
'real_ev',
'tier',
],
]; ];
/**
* weight仅 tier=BIGWIN 时可设定,严格限制 0-100%
*/
protected function checkWeight($value, $rule = '', $data = []): bool
{
$tier = isset($data['tier']) ? (string) $data['tier'] : '';
if ($tier !== 'BIGWIN') {
return true;
}
$num = is_numeric($value) ? (float) $value : null;
if ($num === null) {
return false;
}
if ($num < 0 || $num > 100) {
return false;
}
return true;
}
} }

View File

@@ -3,6 +3,14 @@
* API 鉴权与用户相关配置 * API 鉴权与用户相关配置
*/ */
return [ return [
// 登录成功返回的连接地址前缀,如 https://127.0.0.1:6777
'login_url_base' => env('API_LOGIN_URL_BASE', 'https://127.0.0.1:6777'),
// 游戏地址,用于 /api/v1/getGameUrl 返回拼接 token
'game_url' => env('GAME_URL', 'dice-game.yuliao666.top'),
// 按 username 存储的登录会话 Redis key 前缀,用于 token 中间件校验
'session_username_prefix' => env('API_SESSION_USERNAME_PREFIX', 'api:user:session:'),
// 登录会话过期时间(秒),默认 7 天
'session_expire' => (int) env('API_SESSION_EXPIRE', 604800),
// auth-token 签名密钥(与客户端约定,用于 /api/authToken 的 signature 校验,必填) // auth-token 签名密钥(与客户端约定,用于 /api/authToken 的 signature 校验,必填)
'auth_token_secret' => env('API_AUTH_TOKEN_SECRET', ''), 'auth_token_secret' => env('API_AUTH_TOKEN_SECRET', ''),
// auth-token 时间戳允许误差(秒),防重放,默认 300 秒 // auth-token 时间戳允许误差(秒),防重放,默认 300 秒
@@ -11,6 +19,8 @@ return [
'auth_token_exp' => (int) env('API_AUTH_TOKEN_EXP', 86400), 'auth_token_exp' => (int) env('API_AUTH_TOKEN_EXP', 86400),
// auth-token 按设备存储的 Redis key 前缀(同一设备只保留最新一个 auth-token // auth-token 按设备存储的 Redis key 前缀(同一设备只保留最新一个 auth-token
'auth_token_device_prefix' => env('API_AUTH_TOKEN_DEVICE_PREFIX', 'api:auth_token:'), 'auth_token_device_prefix' => env('API_AUTH_TOKEN_DEVICE_PREFIX', 'api:auth_token:'),
// auth-token 按 token 存储的 Redis key 前缀(用于校验 auth-token 请求头)
'auth_token_prefix' => env('API_AUTH_TOKEN_PREFIX', 'api:auth_token:t:'),
// user-token 有效期(秒),默认 7 天 // user-token 有效期(秒),默认 7 天
'user_token_exp' => (int) env('API_USER_TOKEN_EXP', 604800), 'user_token_exp' => (int) env('API_USER_TOKEN_EXP', 604800),
// 按用户存储当前有效 user-token 的 Redis key 前缀(同一用户仅保留最新一次登录的 token // 按用户存储当前有效 user-token 的 Redis key 前缀(同一用户仅保留最新一次登录的 token
@@ -21,4 +31,7 @@ return [
'user_cache_prefix' => env('API_USER_CACHE_PREFIX', 'api:user:'), 'user_cache_prefix' => env('API_USER_CACHE_PREFIX', 'api:user:'),
// 用户信息加密密钥(用于 Redis 中 value 的加密),建议 32 位 // 用户信息加密密钥(用于 Redis 中 value 的加密),建议 32 位
'user_encrypt_key' => env('API_USER_ENCRYPT_KEY', 'dafuweng_api_user_cache_key_32'), 'user_encrypt_key' => env('API_USER_ENCRYPT_KEY', 'dafuweng_api_user_cache_key_32'),
// 玩家信息按 username 缓存Token 中间件用0 表示不缓存
'player_cache_ttl' => (int) env('API_PLAYER_CACHE_TTL', 300),
'player_cache_prefix' => env('API_PLAYER_CACHE_PREFIX', 'api:player:'),
]; ];

View File

@@ -15,7 +15,8 @@
use support\Request; use support\Request;
return [ return [
'debug' => true, // 生产环境务必设为 false减少 I/O 与堆栈输出,提升接口响应
'debug' => env('APP_DEBUG', false),
'error_reporting' => E_ALL, 'error_reporting' => E_ALL,
'default_timezone' => 'Asia/Shanghai', 'default_timezone' => 'Asia/Shanghai',
'request_class' => Request::class, 'request_class' => Request::class,

View File

@@ -18,10 +18,10 @@ return [
PDO::ATTR_EMULATE_PREPARES => false, // Must be false for Swoole and Swow drivers. PDO::ATTR_EMULATE_PREPARES => false, // Must be false for Swoole and Swow drivers.
], ],
'pool' => [ 'pool' => [
'max_connections' => 5, 'max_connections' => (int) env('DB_POOL_MAX', 20),
'min_connections' => 1, 'min_connections' => (int) env('DB_POOL_MIN', 2),
'wait_timeout' => 3, 'wait_timeout' => (float) env('DB_POOL_WAIT_TIMEOUT', 1.0),
'idle_timeout' => 60, 'idle_timeout' => (int) env('DB_POOL_IDLE_TIMEOUT', 60),
'heartbeat_interval' => 50, 'heartbeat_interval' => 50,
], ],
], ],

View File

@@ -20,7 +20,7 @@ return [
'constructor' => [ 'constructor' => [
runtime_path() . '/logs/webman.log', runtime_path() . '/logs/webman.log',
7, //$maxFiles 7, //$maxFiles
Monolog\Logger::DEBUG, env('LOG_LEVEL', Monolog\Logger::INFO),
], ],
'formatter' => [ 'formatter' => [
'class' => Monolog\Formatter\LineFormatter::class, 'class' => Monolog\Formatter\LineFormatter::class,

View File

@@ -7,9 +7,9 @@ return [
'port' => env('REDIS_PORT', 6379), 'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0), 'database' => env('REDIS_DB', 0),
'pool' => [ 'pool' => [
'max_connections' => 5, 'max_connections' => (int) env('REDIS_POOL_MAX', 20),
'min_connections' => 1, 'min_connections' => (int) env('REDIS_POOL_MIN', 2),
'wait_timeout' => 3, 'wait_timeout' => (float) env('REDIS_POOL_WAIT_TIMEOUT', 1.0),
'idle_timeout' => 60, 'idle_timeout' => 60,
'heartbeat_interval' => 50, 'heartbeat_interval' => 50,
], ],

View File

@@ -13,29 +13,37 @@
*/ */
use Webman\Route; use Webman\Route;
use app\api\middleware\CheckAuthTokenMiddleware; use app\api\middleware\TokenMiddleware;
use app\api\middleware\CheckUserTokenMiddleware; use app\api\middleware\AuthTokenMiddleware;
// 仅需 auth-token 的路由组(authToken 接口在中间件内白名单跳过) // 平台鉴权接口:/api/v1/authToken,请求头 signature/secret/time/agent_id返回 authtToken
Route::group('/api', function () { Route::group('/api/v1', function () {
Route::any('/authToken', [app\api\controller\AuthTokenController::class, 'index']); Route::any('/authToken', [app\api\controller\v1\AuthTokenController::class, 'index']);
Route::any('/user/login', [app\api\controller\UserController::class, 'login']); })->middleware([]);
Route::any('/user/register', [app\api\controller\UserController::class, 'register']);
// 平台 v1 接口:需在请求头携带 auth-token
Route::group('/api/v1', function () {
Route::any('/getGameUrl', [app\api\controller\v1\GameController::class, 'getGameUrl']);
})->middleware([ })->middleware([
CheckAuthTokenMiddleware::class, AuthTokenMiddleware::class,
]); ]);
// 需 auth-token + user-token 的路由组 // 登录接口:无需 token提交 JSON 获取带 token 的连接地址
Route::group('/api', function () {
Route::any('/user/Login', [app\api\controller\UserController::class, 'Login']);
})->middleware([]);
// 其余接口:仅经 token 中间件鉴权header: tokenbase64(username.-.time)
Route::group('/api', function () { Route::group('/api', function () {
Route::any('/user/logout', [app\api\controller\UserController::class, 'logout']); Route::any('/user/logout', [app\api\controller\UserController::class, 'logout']);
Route::any('/user/info', [app\api\controller\UserController::class, 'info']); Route::any('/user/info', [app\api\controller\UserController::class, 'info']);
Route::any('/user/balance', [app\api\controller\UserController::class, 'balance']); Route::any('/user/balance', [app\api\controller\UserController::class, 'balance']);
Route::any('/user/walletRecord', [app\api\controller\UserController::class, 'walletRecord']); Route::any('/user/walletRecord', [app\api\controller\UserController::class, 'walletRecord']);
Route::any('/user/playGameRecord', [app\api\controller\UserController::class, 'playGameRecord']); Route::any('/user/playGameRecord', [app\api\controller\UserController::class, 'playGameRecord']);
Route::any('/game/config', [app\api\controller\GameController::class, 'config']);
Route::any('/game/buyLotteryTickets', [app\api\controller\GameController::class, 'buyLotteryTickets']); Route::any('/game/buyLotteryTickets', [app\api\controller\GameController::class, 'buyLotteryTickets']);
Route::any('/game/lotteryPool', [app\api\controller\GameController::class, 'lotteryPool']); Route::any('/game/lotteryPool', [app\api\controller\GameController::class, 'lotteryPool']);
Route::any('/game/playStart', [app\api\controller\GameController::class, 'playStart']); Route::any('/game/playStart', [app\api\controller\GameController::class, 'playStart']);
})->middleware([ })->middleware([
CheckAuthTokenMiddleware::class, TokenMiddleware::class,
CheckUserTokenMiddleware::class,
]); ]);

View File

@@ -24,13 +24,13 @@ return [
'tag_expire' => 86400 * 30, 'tag_expire' => 86400 * 30,
// 缓存标签前缀 // 缓存标签前缀
'tag_prefix' => 'tag:', 'tag_prefix' => 'tag:',
// 连接池配置 // 连接池配置(与 redis.php 对齐,生产可调大以减少等待)
'pool' => [ 'pool' => [
'max_connections' => 5, // 最大连接数 'max_connections' => (int) env('REDIS_POOL_MAX', 20),
'min_connections' => 1, // 最小连接数 'min_connections' => (int) env('REDIS_POOL_MIN', 2),
'wait_timeout' => 3, // 从连接池获取连接等待超时时间 'wait_timeout' => (float) env('REDIS_POOL_WAIT_TIMEOUT', 1.0),
'idle_timeout' => 60, // 连接最大空闲时间,超过该时间会被回收 'idle_timeout' => 60,
'heartbeat_interval' => 50, // 心跳检测间隔需要小于60秒 'heartbeat_interval' => 50,
], ],
], ],
// 文件缓存 // 文件缓存

View File

@@ -18,8 +18,7 @@ return [
'hostport' => env('DB_PORT', 3306), 'hostport' => env('DB_PORT', 3306),
// 数据库连接参数 // 数据库连接参数
'params' => [ 'params' => [
// 连接超时3秒 \PDO::ATTR_TIMEOUT => (int) env('DB_CONNECT_TIMEOUT', 2),
\PDO::ATTR_TIMEOUT => 3,
], ],
// 数据库编码默认采用utf8 // 数据库编码默认采用utf8
'charset' => 'utf8', 'charset' => 'utf8',
@@ -29,13 +28,13 @@ return [
'break_reconnect' => true, 'break_reconnect' => true,
// 自定义分页类 // 自定义分页类
'bootstrap' => '', 'bootstrap' => '',
// 连接池配置 // 连接池配置(与 database.php 对齐)
'pool' => [ 'pool' => [
'max_connections' => 5, // 最大连接数 'max_connections' => (int) env('DB_POOL_MAX', 20),
'min_connections' => 1, // 最小连接数 'min_connections' => (int) env('DB_POOL_MIN', 2),
'wait_timeout' => 3, // 从连接池获取连接等待超时时间 'wait_timeout' => (float) env('DB_POOL_WAIT_TIMEOUT', 1.0),
'idle_timeout' => 60, // 连接最大空闲时间,超过该时间会被回收 'idle_timeout' => (int) env('DB_POOL_IDLE_TIMEOUT', 60),
'heartbeat_interval' => 50, // 心跳检测间隔需要小于60秒 'heartbeat_interval' => 50,
], ],
], ],
], ],

View File

@@ -33,24 +33,39 @@ class SystemController extends BaseController
*/ */
public function userInfo(): Response public function userInfo(): Response
{ {
$info['user'] = $this->adminInfo; $adminInfo = $this->adminInfo;
if ($adminInfo === null || !is_array($adminInfo) || !isset($adminInfo['id'])) {
$token = getCurrentInfo();
if (!is_array($token) || empty($token['id'])) {
return $this->fail('登录已过期或用户信息无效,请重新登录', 401);
}
$adminInfo = UserInfoCache::getUserInfo($token['id']);
if (empty($adminInfo) || !isset($adminInfo['id'])) {
$adminInfo = UserInfoCache::setUserInfo($token['id']);
}
if (empty($adminInfo) || !isset($adminInfo['id'])) {
return $this->fail('登录已过期或用户信息无效,请重新登录', 401);
}
$this->adminInfo = $adminInfo;
}
$info = []; $info = [];
$info['id'] = $this->adminInfo['id']; $info['id'] = $adminInfo['id'];
$info['username'] = $this->adminInfo['username']; $info['username'] = $adminInfo['username'] ?? '';
$info['dashboard'] = $this->adminInfo['dashboard']; $info['dashboard'] = $adminInfo['dashboard'] ?? '';
$info['avatar'] = $this->adminInfo['avatar']; $info['avatar'] = $adminInfo['avatar'] ?? '';
$info['email'] = $this->adminInfo['email']; $info['email'] = $adminInfo['email'] ?? '';
$info['phone'] = $this->adminInfo['phone']; $info['phone'] = $adminInfo['phone'] ?? '';
$info['gender'] = $this->adminInfo['gender']; $info['gender'] = $adminInfo['gender'] ?? '';
$info['signed'] = $this->adminInfo['signed']; $info['signed'] = $adminInfo['signed'] ?? '';
$info['realname'] = $this->adminInfo['realname']; $info['realname'] = $adminInfo['realname'] ?? '';
$info['department'] = $this->adminInfo['deptList']; $info['department'] = $adminInfo['deptList'] ?? [];
if ($this->adminInfo['id'] === 1) { if (isset($adminInfo['id']) && $adminInfo['id'] == 1) {
$info['buttons'] = ['*']; $info['buttons'] = ['*'];
$info['roles'] = ['super_admin']; $info['roles'] = ['super_admin'];
} else { } else {
$info['buttons'] = UserAuthCache::getUserAuth($this->adminInfo['id']); $info['buttons'] = UserAuthCache::getUserAuth($adminInfo['id']);
$info['roles'] = Arr::getArrayColumn($this->adminInfo['roleList'], 'code'); $info['roles'] = Arr::getArrayColumn($adminInfo['roleList'] ?? [], 'code');
} }
return $this->success($info); return $this->success($info);
} }
@@ -70,6 +85,9 @@ class SystemController extends BaseController
*/ */
public function menu(): Response public function menu(): Response
{ {
if (!$this->ensureAdminInfo()) {
return $this->fail('登录已过期或用户信息无效,请重新登录', 401);
}
$data = UserMenuCache::getUserMenu($this->adminInfo['id']); $data = UserMenuCache::getUserMenu($this->adminInfo['id']);
return $this->success($data); return $this->success($data);
} }

View File

@@ -39,30 +39,4 @@ class Handler extends ExceptionHandler
} }
$this->logger->error($logs); $this->logger->error($logs);
} }
public function render(Request $request, Throwable $exception): Response
{
$debug = config('app.debug', true);
$code = $exception->getCode();
$json = [
'code' => $code ? $code : 500,
'message' => $code !== 500 ? $exception->getMessage() : 'Server internal error',
'type' => 'failed'
];
if ($debug) {
$json['request_url'] = $request->method() . ' ' . $request->uri();
$json['timestamp'] = date('Y-m-d H:i:s');
$json['client_ip'] = $request->getRealIp();
$json['request_param'] = $request->all();
$json['exception_handle'] = get_class($exception);
$json['exception_info'] = [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => explode("\n", $exception->getTraceAsString())
];
}
return new Response(200, ['Content-Type' => 'application/json;charset=utf-8'], json_encode($json));
}
} }

View File

@@ -32,8 +32,11 @@ class CheckLogin implements MiddlewareInterface
if ($token['plat'] !== 'saiadmin') { if ($token['plat'] !== 'saiadmin') {
throw new ApiException('登录凭证校验失败'); throw new ApiException('登录凭证校验失败');
} }
$request->setHeader('check_login', true); // 一次合并设置,避免 setHeader 覆盖导致只保留最后一个
$request->setHeader('check_admin', $token); $request->setHeader(array_merge($request->header() ?: [], [
'check_login' => true,
'check_admin' => $token,
]));
} }
return $handler($request); return $handler($request);
} }

View File

@@ -45,18 +45,47 @@ class BaseController extends OpenController
*/ */
protected function init(): void protected function init(): void
{ {
// 登录模式赋值 // 登录模式赋值:优先从中间件注入的 header 取,否则从 JWT 当前用户取
$isLogin = request()->header('check_login', false);
if ($isLogin) {
$result = request()->header('check_admin'); $result = request()->header('check_admin');
if (!is_array($result) || empty($result['id'])) {
$result = getCurrentInfo();
}
if (is_array($result) && !empty($result['id'])) {
$this->adminId = $result['id']; $this->adminId = $result['id'];
$this->adminName = $result['username']; $this->adminName = $result['username'] ?? '';
$this->adminInfo = UserInfoCache::getUserInfo($result['id']); $this->adminInfo = UserInfoCache::getUserInfo($result['id']);
if (empty($this->adminInfo) || !isset($this->adminInfo['id'])) {
$this->adminInfo = UserInfoCache::setUserInfo($result['id']);
}
// 用户数据传递给逻辑层 // 用户数据传递给逻辑层
$this->logic && $this->logic->init($this->adminInfo); if ($this->logic && !empty($this->adminInfo)) {
$this->logic->init($this->adminInfo);
} }
} }
}
/**
* 确保当前请求已加载管理员信息(用于 init 未正确注入时的回退)
* @return bool 是否已有有效的 adminInfo
*/
protected function ensureAdminInfo(): bool
{
if ($this->adminInfo !== null && is_array($this->adminInfo) && isset($this->adminInfo['id'])) {
return true;
}
$token = getCurrentInfo();
if (!is_array($token) || empty($token['id'])) {
return false;
}
$this->adminId = $token['id'];
$this->adminName = $token['username'] ?? '';
$this->adminInfo = UserInfoCache::getUserInfo($token['id']);
if (empty($this->adminInfo) || !isset($this->adminInfo['id'])) {
$this->adminInfo = UserInfoCache::setUserInfo($token['id']);
}
return is_array($this->adminInfo) && isset($this->adminInfo['id']);
}
/** /**
* 验证器调用 * 验证器调用

View File

@@ -35,6 +35,12 @@ class BaseModel extends Model implements ModelInterface
*/ */
protected $updateTime = 'update_time'; protected $updateTime = 'update_time';
/**
* 自动写入时间戳(创建时写 create_time更新时写 update_time
* @var bool
*/
protected $autoWriteTimestamp = true;
/** /**
* 隐藏字段 * 隐藏字段
* @var array * @var array
@@ -94,24 +100,54 @@ class BaseModel extends Model implements ModelInterface
} }
/** /**
* 新增前事件 * 新增前事件:自动写入 create_time有后台登录信息时写入 created_by
* @param Model $model * @param Model $model
* @return void * @return void
*/ */
public static function onBeforeInsert($model): void public static function onBeforeInsert($model): void
{ {
try {
$createTime = $model->createTime ?? 'create_time';
if ($createTime && !$model->getData($createTime)) {
$model->set($createTime, date('Y-m-d H:i:s'));
}
} catch (\Throwable $e) {
}
try {
if (function_exists('getCurrentInfo')) {
$info = getCurrentInfo(); $info = getCurrentInfo();
$info && $model->setAttr('created_by', $info['id']); if (!empty($info['id'])) {
$model->setAttr('created_by', $info['id']);
}
}
} catch (\Throwable $e) {
}
} }
/** /**
* 写入前事件 * 写入前事件:更新时自动写入 update_time有后台登录信息时写入 updated_by
* @param Model $model * @param Model $model
* @return void * @return void
*/ */
public static function onBeforeWrite($model): void public static function onBeforeWrite($model): void
{ {
try {
if ($model->isExists()) {
$updateTime = $model->updateTime ?? 'update_time';
if ($updateTime) {
$model->set($updateTime, date('Y-m-d H:i:s'));
}
}
} catch (\Throwable $e) {
}
try {
if (function_exists('getCurrentInfo')) {
$info = getCurrentInfo(); $info = getCurrentInfo();
$info && $model->setAttr('updated_by', $info['id']); if (!empty($info['id'])) {
$model->setAttr('updated_by', $info['id']);
}
}
} catch (\Throwable $e) {
}
} }
} }

View File

@@ -4,8 +4,9 @@ use plugin\saiadmin\app\middleware\SystemLog;
use plugin\saiadmin\app\middleware\CheckLogin; use plugin\saiadmin\app\middleware\CheckLogin;
use plugin\saiadmin\app\middleware\CheckAuth; use plugin\saiadmin\app\middleware\CheckAuth;
// 仅对 /core 后台路由生效,避免 /api 请求经过登录/权限/操作日志中间件,提升接口响应
return [ return [
'' => [ 'core' => [
CheckLogin::class, CheckLogin::class,
CheckAuth::class, CheckAuth::class,
SystemLog::class, SystemLog::class,

View File

@@ -20,11 +20,11 @@ namespace support;
*/ */
class Request extends \Webman\Http\Request class Request extends \Webman\Http\Request
{ {
/** 由 CheckUserTokenMiddleware 注入:当前用户 ID */ /** 由 TokenMiddleware 注入:当前玩家 IDDicePlayer.id */
public ?int $user_id = null; public ?int $player_id = null;
/** 由 CheckUserTokenMiddleware 注入:当前 user-token 原始字符串 */ /** 由 TokenMiddleware 注入:当前玩家模型实例 */
public ?string $userToken = null; public $player = null;
/** /**
* 获取参数增强方法 * 获取参数增强方法