Compare commits

41 Commits

Author SHA1 Message Date
6f56574aac 优化主页数据统计 2026-03-10 12:42:29 +08:00
fdd8f6dffa 相关记录表admin_id关联当前管理员id 2026-03-10 12:30:56 +08:00
b1efeb8b31 管理员新增agent_id字段显示 2026-03-10 11:46:30 +08:00
275f94f96d 配置接口lang请求头 2026-03-10 11:42:39 +08:00
9452fd28e2 SystemUser新增agent_id字段 2026-03-10 10:54:40 +08:00
7716929447 移除渠道ChannelManage 2026-03-10 10:45:23 +08:00
a6d87d5c0d 修改渠道ChannelManage关联部门SystemDepart 2026-03-10 10:09:35 +08:00
e94ebd3fe6 优化页面样式 2026-03-10 09:59:24 +08:00
99a0b63f0e [接口v1]对接平台API-优化 2026-03-09 14:55:27 +08:00
fbf8f9d39d [接口v1]对接平台API-新增接口 2026-03-09 14:35:55 +08:00
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
95 changed files with 4172 additions and 1192 deletions

View File

@@ -1,7 +1,7 @@
<!doctype html>
<html>
<head>
<title>SaiAdmin</title>
<title>Dafuweng-Dice</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta

View File

@@ -1,31 +1,32 @@
import request from '@/utils/http'
/**
* 基础数据统计
* 大富翁工作台卡片统计(玩家注册、充值、提现、游玩次数,含较上周对比)
* @returns 响应
*/
export function fetchStatistics() {
return request.get<any>({
url: '/core/system/statistics'
url: '/core/dice/dashboard/statistics'
})
}
/**
* 登录统计图表数据
* 近期玩家充值统计近10天每日充值金额
* @returns 响应
*/
export function fetchLoginChart() {
export function fetchRechargeChart() {
return request.get<any>({
url: '/core/system/loginChart'
url: '/core/dice/dashboard/rechargeChart'
})
}
/**
* 登录统计图表数据
* 月度玩家充值汇总当年1-12月每月充值金额
* @returns 响应
*/
export function fetchLoginBarChart() {
export function fetchRechargeBarChart() {
return request.get<any>({
url: '/core/system/loginBarChart'
url: '/core/dice/dashboard/rechargeBarChart'
})
}

View File

@@ -38,7 +38,7 @@ import { headerBarConfig } from './modules/headerBar'
const appConfig: SystemConfig = {
// 系统信息
systemInfo: {
name: 'SaiAdmin' // 系统名称
name: 'Dafuweng-Dice' // 系统名称
},
// 系统主题
systemThemeStyles: {

View File

@@ -150,8 +150,8 @@
},
"login": {
"leftView": {
"title": "A backend system of beauty and efficiency",
"subTitle": "A sleek and practical interface for a great user experience"
"title": "Monopoly Game Management",
"subTitle": "Monopoly Game Management"
},
"title": "Welcome back",
"subTitle": "Please enter your account and password to login",

View File

@@ -150,8 +150,8 @@
},
"login": {
"leftView": {
"title": "一款兼具设计美学与高效开发的后台系统",
"subTitle": "美观实用的界面,经过视觉优化,确保卓越的用户体验"
"title": "大富翁游戏管理",
"subTitle": "大富翁游戏管理"
},
"title": "欢迎回来",
"subTitle": "输入您的账号和密码登录",

View File

@@ -28,7 +28,6 @@
</ElRow>
</template>
<AboutProject />
</div>
</template>
@@ -36,7 +35,6 @@
import CardList from './modules/card-list.vue'
import ActiveUser from './modules/active-user.vue'
import SalesOverview from './modules/sales-overview.vue'
import AboutProject from './modules/about-project.vue'
import NewUser from './modules/new-user.vue'
import Dynamic from './modules/dynamic-stats.vue'
import TodoList from './modules/todo-list.vue'

View File

@@ -1,43 +0,0 @@
<template>
<div class="art-card p-5 flex-b mb-5 max-sm:mb-4">
<div>
<h2 class="text-2xl font-medium">关于项目</h2>
<p class="text-g-700 mt-1">{{ systemName }} 是一款兼具设计美学与高效开发的后台系统</p>
<p class="text-g-700 mt-1">使用了 webman + Vue3 + Element Plus 高性能高颜值技术栈</p>
<div class="flex flex-wrap gap-3.5 max-w-150 mt-9">
<div
class="w-60 flex-cb h-12.5 px-3.5 border border-g-300 c-p rounded-lg text-sm bg-g-100 duration-300 hover:-translate-y-1 max-sm:w-full"
v-for="link in linkList"
:key="link.label"
@click="goPage(link.url)"
>
<span class="text-g-700">{{ link.label }}</span>
<ArtSvgIcon icon="ri:arrow-right-s-line" class="text-lg text-g-600" />
</div>
</div>
</div>
<img class="w-75 max-md:!hidden" src="@imgs/draw/draw1.png" alt="draw1" />
</div>
</template>
<script setup lang="ts">
import AppConfig from '@/config'
const systemName = AppConfig.systemInfo.name
const linkList = [
{ label: '项目官网', url: 'https://saithink.top/' },
{ label: '文档', url: 'https://saithink.top/documents/' },
{ label: 'Github', url: 'https://github.com/saithink/saiadmin' },
{ label: '插件市场', url: 'https://saas.saithink.top/' }
]
/**
* 在新标签页中打开指定 URL
* @param url 要打开的网页地址
*/
const goPage = (url: string): void => {
window.open(url, '_blank', 'noopener,noreferrer')
}
</script>

View File

@@ -2,7 +2,7 @@
<div class="art-card h-105 p-4 box-border mb-5 max-sm:mb-4">
<div class="art-card-header">
<div class="title">
<h4>月度登录汇总</h4>
<h4>月度玩家充值汇总</h4>
</div>
</div>
<ArtBarChart
@@ -17,22 +17,22 @@
</template>
<script setup lang="ts">
import { fetchLoginBarChart } from '@/api/dashboard'
import { fetchRechargeBarChart } from '@/api/dashboard'
/**
* 登录数据
* 充值金额数据
*/
const yData = ref<number[]>([])
/**
* 时间数据
* 月份数据
*/
const xData = ref<string[]>([])
onMounted(async () => {
fetchLoginBarChart().then((data) => {
yData.value = data.login_count
xData.value = data.login_month
fetchRechargeBarChart().then((data: any) => {
yData.value = data?.recharge_amount ?? []
xData.value = data?.recharge_month ?? []
})
})
</script>

View File

@@ -2,73 +2,95 @@
<ElRow :gutter="20" class="flex">
<ElCol :sm="12" :md="6" :lg="6">
<div class="art-card relative flex flex-col justify-center h-35 px-5 mb-5 max-sm:mb-4">
<span class="text-g-700 text-sm">用户统计</span>
<ArtCountTo class="text-[26px] font-medium mt-2" :target="statData.user" :duration="1300" />
<span class="text-g-700 text-sm">玩家注册</span>
<ArtCountTo class="text-[26px] font-medium mt-2" :target="statData.player_count" :duration="1300" />
<div class="flex-c mt-1">
<span class="text-xs text-g-600">较上周</span>
<span class="ml-1 text-xs font-semibold text-success">+10%</span>
<span
class="ml-1 text-xs font-semibold"
:class="changeClass(statData.player_count_change)"
>
{{ formatChange(statData.player_count_change) }}
</span>
</div>
<div
class="absolute top-0 bottom-0 right-5 m-auto size-12.5 rounded-xl flex-cc bg-theme/10"
>
<ArtSvgIcon icon="ri:group-line" class="text-xl text-theme" />
<ArtSvgIcon icon="ri:user-add-line" class="text-xl text-theme" />
</div>
</div>
</ElCol>
<ElCol :sm="12" :md="6" :lg="6">
<div class="art-card relative flex flex-col justify-center h-35 px-5 mb-5 max-sm:mb-4">
<span class="text-g-700 text-sm">附件统计</span>
<span class="text-g-700 text-sm">玩家充值</span>
<ArtCountTo
class="text-[26px] font-medium mt-2"
:target="statData.attach"
:target="statData.charge_amount"
:duration="1300"
:decimals="2"
/>
<div class="flex-c mt-1">
<span class="text-xs text-g-600">较上周</span>
<span class="ml-1 text-xs font-semibold text-success">+10%</span>
<span
class="ml-1 text-xs font-semibold"
:class="changeClass(statData.charge_amount_change)"
>
{{ formatChange(statData.charge_amount_change) }}
</span>
</div>
<div
class="absolute top-0 bottom-0 right-5 m-auto size-12.5 rounded-xl flex-cc bg-theme/10"
>
<ArtSvgIcon icon="ri:attachment-line" class="text-xl text-theme" />
<ArtSvgIcon icon="ri:money-dollar-circle-line" class="text-xl text-theme" />
</div>
</div>
</ElCol>
<ElCol :sm="12" :md="6" :lg="6">
<div class="art-card relative flex flex-col justify-center h-35 px-5 mb-5 max-sm:mb-4">
<span class="text-g-700 text-sm">登录统计</span>
<span class="text-g-700 text-sm">玩家提现</span>
<ArtCountTo
class="text-[26px] font-medium mt-2"
:target="statData.login"
:target="statData.withdraw_amount"
:duration="1300"
:decimals="2"
/>
<div class="flex-c mt-1">
<span class="text-xs text-g-600">较上周</span>
<span class="ml-1 text-xs font-semibold text-success">+12%</span>
<span
class="ml-1 text-xs font-semibold"
:class="changeClass(statData.withdraw_amount_change)"
>
{{ formatChange(statData.withdraw_amount_change) }}
</span>
</div>
<div
class="absolute top-0 bottom-0 right-5 m-auto size-12.5 rounded-xl flex-cc bg-theme/10"
>
<ArtSvgIcon icon="ri:fire-line" class="text-xl text-theme" />
<ArtSvgIcon icon="ri:bank-card-line" class="text-xl text-theme" />
</div>
</div>
</ElCol>
<ElCol :sm="12" :md="6" :lg="6">
<div class="art-card relative flex flex-col justify-center h-35 px-5 mb-5 max-sm:mb-4">
<span class="text-g-700 text-sm">操作统计</span>
<span class="text-g-700 text-sm">玩家游玩次数</span>
<ArtCountTo
class="text-[26px] font-medium mt-2"
:target="statData.operate"
:target="statData.play_count"
:duration="1300"
/>
<div class="flex-c mt-1">
<span class="text-xs text-g-600">较上周</span>
<span class="ml-1 text-xs font-semibold text-danger">-5%</span>
<span
class="ml-1 text-xs font-semibold"
:class="changeClass(statData.play_count_change)"
>
{{ formatChange(statData.play_count_change) }}
</span>
</div>
<div
class="absolute top-0 bottom-0 right-5 m-auto size-12.5 rounded-xl flex-cc bg-theme/10"
>
<ArtSvgIcon icon="ri:pie-chart-line" class="text-xl text-theme" />
<ArtSvgIcon icon="ri:gamepad-line" class="text-xl text-theme" />
</div>
</div>
</ElCol>
@@ -79,15 +101,40 @@
import { fetchStatistics } from '@/api/dashboard'
const statData = ref({
user: 0,
attach: 0,
login: 0,
operate: 0
player_count: 0,
player_count_change: 0,
charge_amount: 0,
charge_amount_change: 0,
withdraw_amount: 0,
withdraw_amount_change: 0,
play_count: 0,
play_count_change: 0
})
function formatChange(val: number): string {
if (val > 0) return `+${val}%`
if (val < 0) return `${val}%`
return '0%'
}
function changeClass(val: number): string {
if (val > 0) return 'text-success'
if (val < 0) return 'text-danger'
return 'text-g-600'
}
onMounted(() => {
fetchStatistics().then((data) => {
statData.value = data
fetchStatistics().then((data: any) => {
statData.value = {
player_count: data?.player_count ?? 0,
player_count_change: data?.player_count_change ?? 0,
charge_amount: data?.charge_amount ?? 0,
charge_amount_change: data?.charge_amount_change ?? 0,
withdraw_amount: data?.withdraw_amount ?? 0,
withdraw_amount_change: data?.withdraw_amount_change ?? 0,
play_count: data?.play_count ?? 0,
play_count_change: data?.play_count_change ?? 0
}
})
})
</script>

View File

@@ -2,7 +2,7 @@
<div class="art-card h-105 p-5 mb-5 max-sm:mb-4">
<div class="art-card-header">
<div class="title">
<h4>近期登录统计</h4>
<h4>近期玩家充值统计</h4>
</div>
</div>
<ArtLineChart
@@ -16,22 +16,22 @@
</template>
<script setup lang="ts">
import { fetchLoginChart } from '@/api/dashboard'
import { fetchRechargeChart } from '@/api/dashboard'
/**
* 登录数据
* 充值金额数据
*/
const yData = ref<number[]>([])
/**
* 时间数据
* 日期数据
*/
const xData = ref<string[]>([])
onMounted(async () => {
fetchLoginChart().then((data) => {
yData.value = data.login_count
xData.value = data.login_date
fetchRechargeChart().then((data: any) => {
yData.value = data?.recharge_amount ?? []
xData.value = data?.recharge_date ?? []
})
})
</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 {
/**
* 获取数据列表
* 获取数据列表DiceLotteryConfig
* @param params 搜索参数
* @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

View File

@@ -71,5 +71,41 @@ export default {
url: '/dice/player/DicePlayer/updateStatus',
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 ?? '') }))
},
/**
* 获取后台管理员选项SystemUser供 admin_id 下拉使用
* @returns [ { id, username, realname, label } ]
*/
async getSystemUserOptions(): Promise<
Array<{ id: number; username: string; realname: string; label: string }>
> {
const res = await request.get<any>({
url: '/dice/player/DicePlayer/getSystemUserOptions'
})
const rows = (Array.isArray(res) ? res : (res?.data ?? [])) as Array<{
id: number
username: string
realname: string
label: string
}>
return rows.map((r) => ({
id: Number(r.id),
username: String(r.username ?? ''),
realname: String(r.realname ?? ''),
label: String(r.label ?? r.username ?? r.id ?? '')
}))
}
}

View File

@@ -11,7 +11,7 @@ export default {
*/
list(params: Record<string, any>) {
return request.get<Api.Common.ApiPage>({
url: '/dice/player_wallet_record/DicePlayerWalletRecord/index',
url: '/core/dice/player_wallet_record/DicePlayerWalletRecord/index',
params
})
},
@@ -23,7 +23,7 @@ export default {
*/
read(id: number | string) {
return request.get<Api.Common.ApiData>({
url: '/dice/player_wallet_record/DicePlayerWalletRecord/read?id=' + id
url: '/core/dice/player_wallet_record/DicePlayerWalletRecord/read?id=' + id
})
},
@@ -34,7 +34,7 @@ export default {
*/
save(params: Record<string, any>) {
return request.post<any>({
url: '/dice/player_wallet_record/DicePlayerWalletRecord/save',
url: '/core/dice/player_wallet_record/DicePlayerWalletRecord/save',
data: params
})
},
@@ -46,7 +46,7 @@ export default {
*/
update(params: Record<string, any>) {
return request.put<any>({
url: '/dice/player_wallet_record/DicePlayerWalletRecord/update',
url: '/core/dice/player_wallet_record/DicePlayerWalletRecord/update',
data: params
})
},
@@ -58,7 +58,7 @@ export default {
*/
delete(params: Record<string, any>) {
return request.del<any>({
url: '/dice/player_wallet_record/DicePlayerWalletRecord/destroy',
url: '/core/dice/player_wallet_record/DicePlayerWalletRecord/destroy',
data: params
})
},
@@ -68,7 +68,7 @@ export default {
*/
getPlayerOptions() {
return request.get<{ id: number; username: string }[]>({
url: '/dice/player_wallet_record/DicePlayerWalletRecord/getPlayerOptions'
url: '/core/dice/player_wallet_record/DicePlayerWalletRecord/getPlayerOptions'
})
},
@@ -77,7 +77,7 @@ export default {
*/
getPlayerWalletBefore(playerId: number | string) {
return request.get<{ wallet_before: number }>({
url: '/dice/player_wallet_record/DicePlayerWalletRecord/getPlayerWalletBefore',
url: '/core/dice/player_wallet_record/DicePlayerWalletRecord/getPlayerWalletBefore',
params: { player_id: playerId }
})
},
@@ -93,7 +93,7 @@ export default {
remark?: string
}) {
return request.post<any>({
url: '/dice/player_wallet_record/DicePlayerWalletRecord/adminOperate',
url: '/core/dice/player_wallet_record/DicePlayerWalletRecord/adminOperate',
data: params
})
}

View File

@@ -11,7 +11,7 @@ export default {
*/
list(params: Record<string, any>) {
return request.get<Api.Common.ApiPage>({
url: '/dice/reward_config/DiceRewardConfig/index',
url: '/core/dice/reward_config/DiceRewardConfig/index',
params
})
},
@@ -23,7 +23,7 @@ export default {
*/
read(id: number | string) {
return request.get<Api.Common.ApiData>({
url: '/dice/reward_config/DiceRewardConfig/read?id=' + id
url: '/core/dice/reward_config/DiceRewardConfig/read?id=' + id
})
},
@@ -34,7 +34,7 @@ export default {
*/
save(params: Record<string, any>) {
return request.post<any>({
url: '/dice/reward_config/DiceRewardConfig/save',
url: '/core/dice/reward_config/DiceRewardConfig/save',
data: params
})
},
@@ -46,7 +46,7 @@ export default {
*/
update(params: Record<string, any>) {
return request.put<any>({
url: '/dice/reward_config/DiceRewardConfig/update',
url: '/core/dice/reward_config/DiceRewardConfig/update',
data: params
})
},
@@ -58,7 +58,7 @@ export default {
*/
delete(params: Record<string, any>) {
return request.del<any>({
url: '/dice/reward_config/DiceRewardConfig/destroy',
url: '/core/dice/reward_config/DiceRewardConfig/destroy',
data: params
})
}

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

View File

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

View File

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

View File

@@ -54,7 +54,7 @@
<el-option label="赠送" :value="1" />
</el-select>
</el-form-item>
<el-form-item label="奖" prop="is_win">
<el-form-item label="是否中大奖" prop="is_win">
<el-select
v-model="formData.is_win"
placeholder="请选择"
@@ -63,18 +63,38 @@
:disabled="dialogType === 'edit'"
>
<el-option label="无" :value="0" />
<el-option label="中奖" :value="1" />
<el-option label="中奖" :value="1" />
</el-select>
</el-form-item>
<el-form-item label="赢取平台币" prop="win_coin">
<el-input-number
v-model="formData.win_coin"
placeholder="请输入赢取平台币"
placeholder="= 中大奖 + 摇色子中奖"
:precision="2"
style="width: 100%"
:disabled="dialogType === 'edit'"
/>
</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-select
v-model="formData.direction"
@@ -122,6 +142,17 @@
</div>
<div class="roll-array-hint">固定 5 个数每个 16</div>
</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-select
v-model="formData.reward_config_id"
@@ -186,7 +217,7 @@
player_id: [{ required: true, message: '请选择玩家', trigger: 'change' }],
lottery_config_id: [{ 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' }],
rollArrayItems: [
{
@@ -219,10 +250,13 @@
lottery_type: null as number | null,
is_win: 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,
start_index: null as number | null,
target_index: null as number | null,
roll_array: null as string | number[] | null,
roll_number: null as number | null,
reward_config_id: null as number | null
}
@@ -278,10 +312,13 @@
'lottery_type',
'is_win',
'win_coin',
'super_win_coin',
'reward_win_coin',
'direction',
'start_index',
'target_index',
'roll_array',
'roll_number',
'reward_config_id'
]
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 */
@@ -331,10 +372,12 @@
const payload = { ...formData } as Record<string, unknown>
// 将 5 个输入值拼成 [1,2,3,4,5] 格式,确保每项为 16 的整数
const items = formData.rollArrayItems
payload.roll_array = items.map((n) => {
const rollArray = items.map((n) => {
const v = n != null ? Number(n) : 1
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
if (props.dialogType === 'add') {
delete payload.id

View File

@@ -27,10 +27,10 @@
</el-form-item>
</el-col>
<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-option label="无" :value="0" />
<el-option label="中奖" :value="1" />
<el-option label="中奖" :value="1" />
</el-select>
</el-form-item>
</el-col>
@@ -63,6 +63,31 @@
</div>
</el-form-item>
</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-form-item label="奖励配置" prop="reward_ui_text">
<el-input v-model="formData.reward_ui_text" placeholder="前端显示文本模糊" clearable />

View File

@@ -112,7 +112,7 @@
phone: undefined,
status: undefined,
coin: undefined,
is_up: undefined
lottery_config_id: undefined
})
// 搜索处理
@@ -127,17 +127,9 @@
return cellValue != null && cellValue !== '' ? `${cellValue}%` : '-'
}
// 倍率列展示0=正常 1=强制杀猪 2=T1高倍率
const isUpFormatter = (row: any) => {
const cellValue = row.is_up
return cellValue === 0
? '正常'
: cellValue === 1
? '强制杀猪'
: cellValue === 2
? 'T1高倍率'
: '-'
}
// 彩金池配置列lottery_config_id 关联 DiceLotteryConfig显示 name
const lotteryConfigNameFormatter = (row: any) =>
row?.diceLotteryConfig?.name ?? (row?.lottery_config_id ? `#${row.lottery_config_id}` : '自定义')
// 表格配置
const {
@@ -158,23 +150,78 @@
apiFn: api.list,
columnsFactory: () => [
{ type: 'selection' },
{ prop: 'username', label: '用户名' },
{ prop: 'phone', label: '手机号' },
{ prop: 'name', label: '昵称' },
{ prop: 'status', label: '状态', width: 88, useSlot: true },
{ prop: 'coin', label: '平台币', width: 100, useSlot: true },
{ prop: 'is_up', label: '倍率', width: 80, formatter: isUpFormatter },
{ prop: 't1_wight', label: 'T1池权重', width: 100, formatter: weightFormatter('t1_wight') },
{ prop: 't2_wight', label: 'T2池权重', width: 100, formatter: weightFormatter('t2_wight') },
{ prop: 't3_wight', label: 'T3池权重', width: 100, formatter: weightFormatter('t3_wight') },
{ 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: 'paid_ticket_count', label: '购买抽奖次数' },
{ prop: 'free_ticket_count', label: '赠送抽奖次数' },
{ prop: 'created_at', label: '创建时间' },
{ prop: 'updated_at', label: '更新时间' },
{ prop: 'operation', label: '操作', width: 120, fixed: 'right', useSlot: true }
{ prop: 'username', label: '用户名', align: 'center' },
{ prop: 'phone', label: '手机号', align: 'center' },
{ prop: 'name', label: '昵称', align: 'center' },
{
prop: 'status',
label: '状态',
width: 88,
align: 'center',
useSlot: true
},
{
prop: 'coin',
label: '平台币',
width: 100,
align: 'center',
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-form-item>
<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 label="密码" prop="password" :rules="passwordRules">
<el-input
@@ -28,6 +34,23 @@
<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="systemUserOptionsLoading"
>
<el-option
v-for="item in systemUserOptions"
:key="item.id"
:label="item.label || item.username || `#${item.id}`"
:value="item.id"
/>
</el-select>
</el-form-item>
<el-form-item label="平台币" prop="coin">
<el-input-number
v-model="formData.coin"
@@ -38,29 +61,98 @@
style="width: 100%"
/>
</el-form-item>
<el-form-item label="倍率" prop="is_up">
<el-select v-model="formData.is_up" placeholder="请选择倍率" clearable style="width: 100%">
<el-option label="正常" :value="0" />
<el-option label="强制杀猪" :value="1" />
<el-option label="T1高倍率" :value="2" />
<!-- lottery_config_id = 自定义权重否则 = DiceLotteryConfig.id选择后该配置的五个 weight 会写入下方 player.*_weight -->
<el-form-item label="彩金池配置" prop="lottery_config_id">
<el-select
v-model="formData.lottery_config_id"
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-form-item>
<el-form-item label="T1池权重(%)" prop="t1_wight">
<el-slider v-model="formData.t1_wight" :min="0" :max="100" :step="0.01" show-input />
<!-- 当前选中的 DiceLotteryConfig 数据展示 -->
<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 label="T2池权重(%)" prop="t2_wight">
<el-slider v-model="formData.t2_wight" :min="0" :max="100" :step="0.01" show-input />
<!-- lottery_config_id 为空时自定义权重可编辑有值时来自所选 DiceLotteryConfig仅展示不可编辑 -->
<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 label="T3池权重(%)" prop="t3_wight">
<el-slider v-model="formData.t3_wight" :min="0" :max="100" :step="0.01" show-input />
<el-form-item label="T2池权重(%)" prop="t2_weight">
<el-slider
v-model="formData.t2_weight"
:min="0"
:max="100"
:step="0.01"
show-input
:disabled="!isLotteryConfigEmpty()"
/>
</el-form-item>
<el-form-item label="T4池权重(%)" prop="t4_wight">
<el-slider v-model="formData.t4_wight" :min="0" :max="100" :step="0.01" show-input />
<el-form-item label="T3池权重(%)" prop="t3_weight">
<el-slider
v-model="formData.t3_weight"
:min="0"
:max="100"
:step="0.01"
show-input
:disabled="!isLotteryConfigEmpty()"
/>
</el-form-item>
<el-form-item label="T5池权重(%)" prop="t5_wight">
<el-slider v-model="formData.t5_wight" :min="0" :max="100" :step="0.01" show-input />
<el-form-item label="T4池权重(%)" prop="t4_weight">
<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 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">
五个池权重总和<span :class="Math.abs(weightsSum - 100) > 0.01 ? 'text-red-500' : ''">{{
weightsSum
@@ -78,9 +170,12 @@
<script setup lang="ts">
import api from '../../../api/player/index'
import lotteryConfigApi from '../../../api/lottery_config/index'
import { ElMessage } 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 {
modelValue: boolean
dialogType: string
@@ -107,9 +202,20 @@
set: (value) => emit('update:modelValue', value)
})
const WEIGHT_KEYS = ['t1_wight', 't2_wight', 't3_wight', 't4_wight', 't5_wight'] as const
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}%`
})
/** 新增时密码必填,编辑时选填 */
@@ -132,17 +238,69 @@
phone: '',
password: '',
status: 1 as number,
/** 所属后台管理员 IDSystemUser.id */
admin_id: null as number | null,
coin: 0 as number,
is_up: null as number | null,
t1_wight: 0 as number,
t2_wight: 0 as number,
t3_wight: 0 as number,
t4_wight: 0 as number,
t5_wight: 0 as number
/** 彩金池配置 ID空 = 自定义权重,否则 = DiceLotteryConfig.id */
lottery_config_id: null as number | null,
t1_weight: 0 as number,
t2_weight: 0 as number,
t3_weight: 0 as number,
t4_weight: 0 as number,
t5_weight: 0 as number
}
const formData = reactive({ ...initialFormData })
/** 彩金池配置下拉选项DiceLotteryConfig id、name */
const lotteryConfigOptions = ref<Array<{ id: number; name: string }>>([])
/** 彩金池选项加载中 */
const lotteryConfigLoading = ref(false)
/** 后台管理员下拉选项SystemUser */
const systemUserOptions = ref<
Array<{ id: number; username: string; realname: string; label: string }>
>([])
/** 管理员选项加载中 */
const systemUserOptionsLoading = 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(
() => props.modelValue,
(newVal) => {
@@ -150,11 +308,63 @@
}
)
/** 选择彩金池后,拉取该配置的五个权重并写入当前 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
}
}
/** 加载后台管理员选项 */
async function loadSystemUserOptions() {
systemUserOptionsLoading.value = true
try {
systemUserOptions.value = await api.getSystemUserOptions()
} catch {
systemUserOptions.value = []
} finally {
systemUserOptionsLoading.value = false
}
}
const initPage = async () => {
currentLotteryConfig.value = null
Object.assign(formData, initialFormData)
await Promise.all([loadLotteryConfigOptions(), loadSystemUserOptions()])
if (props.data) {
await nextTick()
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 +372,12 @@
'id',
'status',
'coin',
'is_up',
't1_wight',
't2_wight',
't3_wight',
't4_wight',
't5_wight'
'lottery_config_id',
't1_weight',
't2_weight',
't3_weight',
't4_weight',
't5_weight'
]
const initForm = () => {
@@ -180,8 +390,14 @@
}
const val = props.data[key]
if (numKeys.includes(key)) {
;(formData as any)[key] =
key === 'id' ? (val != null ? Number(val) || null : null) : Number(val) || 0
if (key === 'id') {
;(formData as any)[key] = val != null ? Number(val) || null : null
} else if (key === 'lottery_config_id' || key === 'admin_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 {
;(formData as any)[key] = val ?? ''
}
@@ -197,11 +413,15 @@
if (!formRef.value) return
try {
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%')
return
}
const payload = { ...formData }
if (isLotteryConfigEmpty()) {
;(payload as any).lottery_config_id = null
}
if (props.dialogType === 'edit' && !payload.password) {
delete (payload as any).password
}
@@ -219,3 +439,31 @@
}
}
</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-col>
<el-col v-bind="setSpan(6)">
<el-form-item label="倍率" prop="is_up">
<el-select v-model="formData.is_up" placeholder="全部" clearable style="width: 100%">
<el-option label="正常" :value="0" />
<el-option label="强制杀猪" :value="1" />
<el-option label="T1高倍率" :value="2" />
<el-form-item label="彩金池配置" prop="lottery_config_id">
<el-select
v-model="formData.lottery_config_id"
placeholder="全部"
clearable
style="width: 100%"
>
<el-option
v-for="item in lotteryConfigOptions"
:key="item.id"
:label="item.name"
:value="item.id"
/>
</el-select>
</el-form-item>
</el-col>
@@ -56,6 +64,8 @@
</template>
<script setup lang="ts">
import api from '../../../api/player/index'
interface Props {
modelValue: Record<string, any>
}
@@ -67,6 +77,16 @@
const props = defineProps<Props>()
const emit = defineEmits<Emits>()
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 formData = computed({

View File

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

View File

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

View File

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

View File

@@ -27,7 +27,13 @@
</el-select>
</el-form-item>
<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="1" />
<el-option label="购买抽奖次数" :value="2" />
@@ -42,6 +48,7 @@
:precision="2"
style="width: 100%"
@change="onCoinChange"
:disabled="dialogType === 'edit'"
/>
</el-form-item>
<el-form-item label="钱包操作前" prop="wallet_before">
@@ -70,6 +77,7 @@
placeholder="选填"
maxlength="500"
show-word-limit
:disabled="dialogType === 'edit'"
/>
</el-form-item>
</el-form>

View File

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

View File

@@ -9,7 +9,11 @@
>
<el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
<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 label="前端显示文本" prop="ui_text">
<el-input v-model="formData.ui_text" placeholder="请输入前端显示文本" />
@@ -23,14 +27,29 @@
placeholder="请选择所属档位"
clearable
style="width: 100%"
:disabled="dialogType === 'edit'"
>
<el-option label="T1" value="T1" />
<el-option label="T2" value="T2" />
<el-option label="T3" value="T3" />
<el-option label="T4" value="T4" />
<el-option label="T5" value="T5" />
<el-option label="BIGWIN超级大奖" value="BIGWIN" />
</el-select>
</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-input
v-model="formData.remark"
@@ -83,6 +102,11 @@
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' }],
ui_text: [{ 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: '',
real_ev: '',
tier: '',
weight: 0 as number,
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 = () => {
if (props.data) {
for (const key in formData) {
if (props.data[key] != null && props.data[key] != undefined) {
;(formData as any)[key] = props.data[key]
}
if (!props.data) return
const numKeys = ['id', 'grid_number', 'real_ev', 'weight']
for (const key of Object.keys(formData)) {
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
try {
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') {
await api.save(formData)
await api.save(payload)
ElMessage.success('新增成功')
} else {
await api.update(formData)
await api.update(payload)
ElMessage.success('修改成功')
}
emit('success')
@@ -177,3 +248,11 @@
}
}
</script>
<style lang="scss" scoped>
.weight-fixed-hint {
margin-top: 6px;
font-size: 12px;
color: var(--el-text-color-secondary);
}
</style>

View File

@@ -190,8 +190,15 @@
saiSecond: 'email'
},
{ prop: 'phone', label: '手机号', width: 120 },
{ prop: 'depts.name', label: '部门', minWidth: 150 },
{
prop: 'dept_id',
label: '部门',
minWidth: 150,
sortable: true,
formatter: (row: any) => row.depts?.name ?? ''
},
{ prop: 'status', label: '状态', width: 80, saiType: 'dict', saiDict: 'data_status' },
{ prop: 'agent_id', label: '代理ID', width: 120, showOverflowTooltip: true },
{ prop: 'dashboard', label: '首页', width: 100, saiType: 'dict', saiDict: 'dashboard' },
{ prop: 'login_time', label: '上次登录', width: 170, sortable: true },
{

View File

@@ -16,19 +16,25 @@ REDIS_PORT = 6379
REDIS_PASSWORD = ''
REDIS_DB = 0
# 游戏地址,用于 /api/v1/getGameUrl 返回
GAME_URL = dice-game.yuliao666.top
# API 鉴权与用户(可选,不填则用默认值)
# authToken 签名密钥(必填,与客户端约定,用于 signature 校验)
API_AUTH_TOKEN_SECRET = xF75oK91TQj13s0UmNIr1NBWMWGfflNO
API_AUTH_TOKEN_SECRET = xF75oK91TQj13s0UmNIr1NBWMWGfflNO
# authToken 时间戳允许误差秒数,防重放,默认 300
API_AUTH_TOKEN_TIME_TOLERANCE = 300
API_AUTH_TOKEN_EXP = 86400
API_AUTH_TOKEN_TIME_TOLERANCE = 300
API_AUTH_TOKEN_EXP = 86400
# API_USER_TOKEN_EXP = 604800
API_USER_CACHE_EXPIRE = 86400
API_USER_ENCRYPT_KEY = Wj818SK8dhKBKNOY3PUTmZfhQDMCXEZi
API_USER_CACHE_EXPIRE = 86400
API_USER_ENCRYPT_KEY = Wj818SK8dhKBKNOY3PUTmZfhQDMCXEZi
# 验证码配置,支持cache|session
CAPTCHA_MODE = cache
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;
/**
* 按设备标识存储当前有效的 auth-token同一设备只保留最新一个旧 token 自动失效
* 平台 auth-token Redis 缓存
* 用于 /api/v1/authToken 鉴权接口颁发的 token 存储与校验
*/
class AuthTokenCache
{
private static function prefix(): string
private static function devicePrefix(): string
{
return config('api.auth_token_device_prefix', 'api:auth_token:');
}
/**
* 设置该设备当前有效的 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
private static function tokenPrefix(): string
{
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;
}
$key = self::prefix() . $device;
return Cache::set($key, $token, $ttl);
$exp = self::expire();
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;
}
$key = self::prefix() . $device;
$value = Cache::get($key);
return $value !== null && $value !== '' ? (string) $value : null;
$val = Cache::get(self::devicePrefix() . $agentId);
return $val !== null && $val !== '' ? (string) $val : 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);
return $current !== null && $current === $token;
if ($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);
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

@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace app\api\controller;
use app\api\util\ApiLang;
use plugin\saiadmin\basic\OpenController;
use support\Response;
/**
* API 控制器基类:根据请求头 langen=英文zh=中文)对返回 message 做双语适配
*/
class BaseController extends OpenController
{
/**
* 成功返回message 按请求头 langen/zh翻译
*/
public function success(array|string $data = [], string $msg = 'success', int $option = JSON_UNESCAPED_UNICODE): Response
{
if (is_string($data)) {
$msg = $data;
$data = [];
}
$msg = ApiLang::translate((string) $msg);
return parent::success($data, $msg, $option);
}
/**
* 失败返回message 按 lang 翻译
*/
public function fail(string $msg = 'fail', int $code = 400): Response
{
$msg = ApiLang::translate($msg);
return parent::fail($msg, $code);
}
}

View File

@@ -3,32 +3,59 @@ declare(strict_types=1);
namespace app\api\controller;
use support\Log;
use support\Request;
use support\Response;
use app\api\logic\GameLogic;
use app\api\logic\PlayStartLogic;
use app\api\logic\UserLogic;
use app\api\util\ReturnCode;
use app\dice\model\config\DiceConfig;
use app\dice\model\play_record\DicePlayRecord;
use app\dice\model\player\DicePlayer;
use app\dice\model\reward_config\DiceRewardConfig;
use plugin\saiadmin\basic\OpenController;
use app\api\controller\BaseController;
use app\api\util\ApiLang;
use plugin\saiadmin\exception\ApiException;
/**
* 游戏相关接口(购买抽奖券等)
*/
class GameController extends OpenController
class GameController extends BaseController
{
/**
* 获取游戏配置(按 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
* header: auth-token, user-token由 CheckUserTokenMiddleware 注入 request->user_id
* header: tokenTokenMiddleware 注入 request->player_id
* body: count = 1 | 5 | 101次/100coin, 5次/500coin, 10次/1000coin
*/
public function buyLotteryTickets(Request $request): Response
{
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
$userId = (int) ($request->player_id ?? 0);
$count = (int) $request->post('count', 0);
if (!in_array($count, [1, 5, 10], true)) {
return $this->fail('购买抽奖券错误', ReturnCode::PARAMS_ERROR);
@@ -52,69 +79,99 @@ class GameController extends OpenController
/**
* 获取彩金池(中奖配置表)
* GET /api/game/lotteryPool
* header: auth-token
* 返回 DiceRewardConfig 列表(彩金池/中奖配置)
* header: token
* 返回 DiceRewardConfig 列表(彩金池/中奖配置),不包含 tier=BIGWIN
*/
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);
}
/**
* 开始游戏(抽奖一局)
* POST /api/game/playStart
* header: auth-token, user-token由 CheckUserTokenMiddleware 注入 request->user_id
* body: rediction 必传0=无 1=中奖
* header: tokenTokenMiddleware 注入 request->player_id
* body: direction 必传0=无 1=中奖
*/
public function playStart(Request $request): Response
{
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
$rediction = $request->post('rediction');
if ($rediction === '' || $rediction === null) {
return $this->fail('请传递 rediction 参数', ReturnCode::PARAMS_ERROR);
$userId = (int) ($request->player_id ?? 0);
$direction = $request->post('direction');
if ($direction !== null) {
$direction = (int) $direction;
}
$direction = (int) $rediction;
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);
if (!$player) {
return $this->fail('用户不存在', ReturnCode::NOT_FOUND);
}
$minEv = (float) DiceRewardConfig::min('real_ev');
$minEv = DiceRewardConfig::getCachedMinRealEv();
$minCoin = abs($minEv + 100);
$coin = (float) $player->coin;
if ($coin < $minCoin) {
return $this->success([], '当前玩家余额小于DiceRewardConfigMin.real_ev+100无法继续游戏');
$msg = ApiLang::translateParams('当前玩家余额%s小于%s无法继续游戏', [$coin, $minCoin], $request);
return $this->success([], $msg);
}
try {
$logic = new PlayStartLogic();
$data = $logic->run($userId, $direction);
$data = $logic->run($userId, (int)$direction);
return $this->success($data);
} catch (ApiException $e) {
return $this->fail($e->getMessage(), ReturnCode::BUSINESS_ERROR);
} catch (\Throwable $e) {
// 记录抽奖逻辑抛出的真实异常,便于排查“服务超时,没有原因”
Log::error('playStart 异常: ' . $e->getMessage(), [
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
'player_id' => $userId,
'direction' => $direction,
]);
$timeoutRecord = null;
$timeout_message = '';
$adminId = null;
try {
$timeoutPlayer = DicePlayer::find($userId);
$adminId = ($timeoutPlayer && ($timeoutPlayer->admin_id ?? null)) ? (int) $timeoutPlayer->admin_id : null;
} catch (\Throwable $_) {
}
try {
$timeoutRecord = DicePlayRecord::create([
'player_id' => $userId,
'admin_id' => $adminId,
'lottery_config_id' => 0,
'lottery_type' => 0,
'is_win' => 0,
'win_coin' => 0,
'super_win_coin' => 0,
'reward_win_coin' => 0,
'use_coins' => 0,
'direction' => $direction,
'reward_config_id' => 0,
'start_index' => 0,
'target_index' => 0,
'roll_array' => '[]',
'roll_number' => 0,
'status' => PlayStartLogic::RECORD_STATUS_TIMEOUT,
]);
} catch (\Throwable $_) {
} catch (\Exception $inner) {
$timeout_message = $inner->getMessage();
Log::error('游玩记录写入超时: ' . $inner->getMessage());
}
$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

@@ -10,80 +10,83 @@ use app\api\logic\UserLogic;
use app\api\util\ReturnCode;
use app\dice\model\play_record\DicePlayRecord;
use app\dice\model\player_wallet_record\DicePlayerWalletRecord;
use plugin\saiadmin\basic\OpenController;
use app\api\controller\BaseController;
/**
* API 用户登录/注册
* 需先携带 auth-token登录/注册成功后返回 user-token 与用户信息,用户信息已写入 Rediskey=base64(user_id)value=加密)
* API 用户登录
* 登录接口 /api/user/Login 无需 token其余接口需在请求头携带 tokenbase64(username.-.time)),由 TokenMiddleware 鉴权并注入 request->player_id / request->player
*/
class UserController extends OpenController
class UserController extends BaseController
{
/**
* 登录
* POST /api/user/login
* body: phone (+60), password
* 登录form-data 参数)
* POST /api/user/Login
* 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', '');
$password = $request->post('password', '');
if ($phone === '' || $password === '') {
return $this->fail('请填写手机号和密码', ReturnCode::PARAMS_ERROR);
$username = trim((string) ($request->post('username', '')));
$password = trim((string) ($request->post('password', '')));
$lang = trim((string) ($request->post('lang', 'chs')));
$coin = $request->post('coin');
$coin = $coin !== null && $coin !== '' ? (float) $coin : 0.0;
$time = $request->post('time');
$time = $time !== null && $time !== '' ? (string) $time : (string) time();
if ($username === '' || $password === '') {
return $this->fail('username、password 不能为空', ReturnCode::PARAMS_ERROR);
}
$logic = new UserLogic();
$data = $logic->login($phone, $password);
return $this->success([
'user' => $data['user'],
'user-token' => $data['user-token'],
'user_id' => $data['user_id'],
]);
}
/**
* 注册
* 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);
try {
$logic = new UserLogic();
$result = $logic->loginByUsername($username, $password, $lang, $coin, $time);
return $this->success([
'url' => $result['url'],
'token' => $result['token'],
'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);
}
$logic = new UserLogic();
$data = $logic->register($phone, $password, $nickname ? (string) $nickname : null);
return $this->success([
'user' => $data['user'],
'user-token' => $data['user-token'],
'user_id' => $data['user_id'],
]);
}
/**
* 退出登录
* POST /api/user/logout
* header: user-token由 CheckUserTokenMiddleware 校验并注入 request->userToken
* header: tokenJWT清除该 username 的 Redis 会话
*/
public function logout(Request $request): Response
{
$token = $request->userToken ?? UserLogic::getTokenFromRequest($request);
if ($token === '' || !UserLogic::logout($token)) {
return $this->fail('退出失败或 token 已失效', ReturnCode::TOKEN_INVALID);
$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 === '') {
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('已退出登录');
}
/**
* 获取当前用户信息
* GET /api/user/info
* header: user-tokenCheckUserTokenMiddleware 校验并注入 request->user_id
* 返回id, username, phone, uid, name, coin, total_ticket_count
* header: token由 TokenMiddleware 校验并注入 request->player_id
*/
public function info(Request $request): Response
{
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
$userId = (int) ($request->player_id ?? 0);
$user = UserLogic::getCachedUser($userId);
if (empty($user)) {
return $this->fail('用户不存在', ReturnCode::NOT_FOUND);
@@ -99,13 +102,13 @@ class UserController extends OpenController
}
/**
* 获取钱包余额(优先读缓存,缓存未命中时从库拉取并回写缓存
* 获取钱包余额(优先读缓存)
* GET /api/user/balance
* header: user-tokenCheckUserTokenMiddleware 校验并注入 request->user_id
* header: token由 TokenMiddleware 注入 request->player_id
*/
public function balance(Request $request): Response
{
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
$userId = (int) ($request->player_id ?? 0);
$user = UserLogic::getCachedUser($userId);
if (empty($user)) {
return $this->fail('用户不存在', ReturnCode::NOT_FOUND);
@@ -124,12 +127,12 @@ class UserController extends OpenController
/**
* 玩家钱包流水
* 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 创建时间范围(可选)
*/
public function walletRecord(Request $request): Response
{
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
$userId = (int) ($request->player_id ?? 0);
$page = (int) $request->post('page', 1);
$limit = (int) $request->post('limit', 10);
if ($page < 1) {
@@ -166,12 +169,12 @@ class UserController extends OpenController
/**
* 游玩记录
* 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 创建时间范围(可选)
*/
public function playGameRecord(Request $request): Response
{
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
$userId = (int) ($request->player_id ?? 0);
$page = (int) $request->post('page', 1);
$limit = (int) $request->post('limit', 10);
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\controller\BaseController;
use app\api\util\ReturnCode;
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 BaseController
{
/**
* 获取 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,301 @@
<?php
declare(strict_types=1);
namespace app\api\controller\v1;
use app\api\logic\UserLogic;
use app\api\util\ReturnCode;
use app\dice\model\player\DicePlayer;
use plugin\saiadmin\app\model\system\SystemUser;
use app\dice\model\play_record\DicePlayRecord;
use app\dice\model\player_wallet_record\DicePlayerWalletRecord;
use app\dice\model\player_ticket_record\DicePlayerTicketRecord;
use support\think\Db;
use app\api\controller\BaseController;
use support\Request;
use support\Response;
/**
* 平台 v1 游戏接口
* 请求头auth-token
*/
class GameController extends BaseController
{
/**
* 获取游戏地址
* 根据 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();
}
$adminId = null;
$agentId = trim((string) ($request->agent_id ?? ''));
if ($agentId !== '') {
$systemUser = SystemUser::where('agent_id', $agentId)->find();
if ($systemUser) {
$adminId = (int) $systemUser->id;
}
}
try {
$logic = new UserLogic();
$result = $logic->loginByUsername($username, $password, 'chs', 0.0, $time, $adminId);
} 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,
]);
}
/**
* 获取用户信息
* POST 参数username
* 返回 DicePlayer 中非敏感信息
*/
public function getPlayerInfo(Request $request): Response
{
$username = trim((string) ($request->post('username', '')));
if ($username === '') {
return $this->fail('username 不能为空', ReturnCode::PARAMS_ERROR);
}
$player = DicePlayer::where('username', $username)->find();
if (!$player) {
return $this->fail('用户不存在', ReturnCode::NOT_FOUND);
}
$hidden = ['password', 'lottery_config_id', 't1_weight', 't2_weight', 't3_weight', 't4_weight', 't5_weight', 'delete_time'];
$info = $player->hidden($hidden)->toArray();
return $this->success($info);
}
/**
* 获取游戏记录
* POST 参数username非必填没填则不按用户筛选, start_create_time, end_create_time非必填没填则不筛选时间
* 返回 DicePlayRecord 中非敏感信息
*/
public function getPlayerGameRecord(Request $request): Response
{
$username = trim((string) ($request->post('username', '')));
$startCreateTime = trim((string) ($request->post('start_create_time', '')));
$endCreateTime = trim((string) ($request->post('end_create_time', '')));
$page = (int) $request->post('page', 1);
$limit = (int) $request->post('limit', 20);
if ($page < 1) {
$page = 1;
}
if ($limit < 1 || $limit > 100) {
$limit = 20;
}
$query = DicePlayRecord::order('id', 'desc');
if ($username !== '') {
$player = DicePlayer::where('username', $username)->find();
if (!$player) {
return $this->success([]);
}
$query->where('player_id', (int) $player->id);
}
if ($startCreateTime !== '') {
$query->where('create_time', '>=', $startCreateTime);
}
if ($endCreateTime !== '') {
$query->where('create_time', '<=', $endCreateTime);
}
$list = $query->page($page, $limit)->select()->toArray();
$playerIds = array_unique(array_column($list, 'player_id'));
if (!empty($playerIds)) {
$players = DicePlayer::whereIn('id', $playerIds)->field('id,username,phone')->select()->toArray();
$playerMap = [];
foreach ($players as $p) {
$playerMap[(int) ($p['id'] ?? 0)] = $p;
}
foreach ($list as &$item) {
$item['dice_player'] = $playerMap[(int) ($item['player_id'] ?? 0)] ?? null;
}
}
return $this->success($list);
}
/**
* 获取钱包流水
* POST 参数username非必填没填则不按用户筛选, start_create_time, end_create_time非必填没填则不筛选时间
* 返回 DicePlayerWalletRecord 中非敏感信息
*/
public function getPlayerWalletRecord(Request $request): Response
{
$username = trim((string) ($request->post('username', '')));
$startCreateTime = trim((string) ($request->post('start_create_time', '')));
$endCreateTime = trim((string) ($request->post('end_create_time', '')));
$page = (int) $request->post('page', 1);
$limit = (int) $request->post('limit', 20);
if ($page < 1) {
$page = 1;
}
if ($limit < 1 || $limit > 100) {
$limit = 20;
}
$query = DicePlayerWalletRecord::order('id', 'desc');
if ($username !== '') {
$player = DicePlayer::where('username', $username)->find();
if (!$player) {
return $this->success([]);
}
$query->where('player_id', (int) $player->id);
}
if ($startCreateTime !== '') {
$query->where('create_time', '>=', $startCreateTime);
}
if ($endCreateTime !== '') {
$query->where('create_time', '<=', $endCreateTime);
}
$list = $query->with(['dicePlayer' => function ($q) {
$q->field('id,username,phone');
}])->page($page, $limit)->select()->toArray();
return $this->success($list);
}
/**
* 获取用户中奖券获取记录
* POST 参数username非必填没填则不按用户筛选, start_create_time, end_create_time非必填没填则不筛选时间
* 返回 DicePlayerTicketRecord 中非敏感信息
*/
public function getPlayerTicketRecord(Request $request): Response
{
$username = trim((string) ($request->post('username', '')));
$startCreateTime = trim((string) ($request->post('start_create_time', '')));
$endCreateTime = trim((string) ($request->post('end_create_time', '')));
$page = (int) $request->post('page', 1);
$limit = (int) $request->post('limit', 20);
if ($page < 1) {
$page = 1;
}
if ($limit < 1 || $limit > 100) {
$limit = 20;
}
$query = DicePlayerTicketRecord::order('id', 'desc');
if ($username !== '') {
$player = DicePlayer::where('username', $username)->find();
if (!$player) {
return $this->success([]);
}
$query->where('player_id', (int) $player->id);
}
if ($startCreateTime !== '') {
$query->where('create_time', '>=', $startCreateTime);
}
if ($endCreateTime !== '') {
$query->where('create_time', '<=', $endCreateTime);
}
$list = $query->with(['dicePlayer' => function ($q) {
$q->field('id,username,phone');
}])->page($page, $limit)->select()->toArray();
return $this->success($list);
}
/**
* 钱包转入转出
* POST 参数username必填, coin转入>0 或 转出<0
* 创建 DicePlayerWalletRecordtype: 0=充值(coin>0), 1=提现(coin<0)
* 返回创建的记录
*/
public function setPlayerWallet(Request $request): Response
{
$username = trim((string) ($request->post('username', '')));
$coin = $request->post('coin');
if ($username === '') {
return $this->fail('username 不能为空', ReturnCode::PARAMS_ERROR);
}
if ($coin === null || $coin === '') {
return $this->fail('coin 不能为空', ReturnCode::PARAMS_ERROR);
}
$coinVal = (float) $coin;
if ($coinVal === 0.0) {
return $this->fail('coin 不能为 0', ReturnCode::PARAMS_ERROR);
}
$player = DicePlayer::where('username', $username)->find();
if (!$player) {
return $this->fail('用户不存在', ReturnCode::NOT_FOUND);
}
$walletBefore = (float) ($player->coin ?? 0);
$walletAfter = $walletBefore + $coinVal;
if ($coinVal < 0 && $walletBefore < -$coinVal) {
return $this->fail('余额不足,无法转出', ReturnCode::BUSINESS_ERROR);
}
$type = $coinVal > 0 ? 0 : 1;
$remark = $coinVal > 0 ? '充值' : '提现';
try {
Db::startTrans();
$player->coin = $walletAfter;
$player->save();
$adminId = ($player->admin_id ?? null) ? (int) $player->admin_id : null;
$record = DicePlayerWalletRecord::create([
'player_id' => (int) $player->id,
'admin_id' => $adminId,
'coin' => $coinVal,
'type' => $type,
'wallet_before' => $walletBefore,
'wallet_after' => $walletAfter,
'total_ticket_count' => 0,
'paid_ticket_count' => 0,
'free_ticket_count' => 0,
'remark' => $remark,
'user_id' => 0,
]);
Db::commit();
} catch (\Throwable $e) {
Db::rollback();
return $this->fail('操作失败:' . $e->getMessage(), ReturnCode::SERVER_ERROR);
}
$recordArr = $record->toArray();
$recordArr['dice_player'] = ['id' => (int) $player->id, 'username' => $player->username ?? '', 'phone' => $player->phone ?? ''];
return $this->success($recordArr);
}
}

View File

@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
/**
* API 英文文案(请求头 lang=en 时使用)
* key 为中文原文value 为英文
*/
return [
'success' => 'Success',
'fail' => 'Fail',
'username、password 不能为空' => 'username and password are required',
'请携带 token' => 'Please provide token',
'token 无效' => 'Invalid or expired token',
'已退出登录' => 'Logged out successfully',
'用户不存在' => 'User not found',
'username 不能为空' => 'username is required',
'密码错误' => 'Wrong password',
'账号已被禁用,无法登录' => 'Account is disabled and cannot log in',
'购买抽奖券错误' => 'Invalid lottery ticket purchase',
'平台币不足' => 'Insufficient balance',
'direction 必须为 0 或 1' => 'direction must be 0 or 1',
'当前玩家余额%s小于%s无法继续游戏' => 'Balance %s is less than %s, cannot continue',
'服务超时,' => 'Service timeout: ',
'没有原因' => 'Unknown reason',
'缺少参数agent_id、secret、time、signature 不能为空' => 'Missing parameters: agent_id, secret, time, signature are required',
'服务端未配置 API_AUTH_TOKEN_SECRET' => 'API_AUTH_TOKEN_SECRET is not configured',
'密钥错误' => 'Invalid secret',
'时间戳已过期或无效,请同步时间' => 'Timestamp expired or invalid, please sync time',
'签名验证失败' => 'Signature verification failed',
'生成 token 失败' => 'Failed to generate token',
'coin 不能为空' => 'coin is required',
'coin 不能为 0' => 'coin cannot be 0',
'余额不足,无法转出' => 'Insufficient balance to transfer',
'操作失败:' => 'Operation failed: ',
'服务超时,没有原因' => 'Service timeout: Unknown reason',
// PlayStartLogic / GameLogic
'抽奖券不足' => 'Insufficient lottery tickets',
'奖池配置不存在' => 'Lottery config not found',
'该方向下暂无可用路径配置' => 'No path config available for this direction',
// UserLogic
'手机号格式错误,仅支持 +60 开头的马来西亚号码(如 +60123456789' => 'Invalid phone format, only +60 Malaysia numbers supported (e.g. +60123456789)',
// TokenMiddleware / Auth (api/user/*, api/game/*)
'请携带 auth-token' => 'Please provide auth-token',
'auth-token 已过期' => 'auth-token expired',
'auth-token 无效' => 'auth-token invalid',
'auth-token 格式无效' => 'auth-token format invalid',
'auth-token 无效或已失效' => 'auth-token invalid or expired',
'token 已过期,请重新登录' => 'Token expired, please login again',
'token 格式无效' => 'Token format invalid',
'请注册' => 'Please register',
'请重新登录' => 'Please login again',
'请重新登录(当前账号已在其他处登录)' => 'Please login again (account logged in elsewhere)',
];

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
* @return array 更新后的 coin, total_ticket_count, paid_ticket_count, free_ticket_count
*/
@@ -55,59 +56,74 @@ class GameLogic
$totalBefore = (int) ($player->total_ticket_count ?? 0);
$paidBefore = (int) ($player->paid_ticket_count ?? 0);
$freeBefore = (int) ($player->free_ticket_count ?? 0);
$totalAfter = $totalBefore + $addTotal;
$paidAfter = $paidBefore + $addPaid;
$freeAfter = $freeBefore + $addFree;
Db::transaction(function () use (
$player,
$playerId,
$cost,
$coinBefore,
$coinAfter,
$addTotal,
$addPaid,
$addFree,
$totalBefore,
$paidBefore,
$freeBefore
) {
$player->coin = $coinAfter;
$player->total_ticket_count = $totalBefore + $addTotal;
$player->paid_ticket_count = $paidBefore + $addPaid;
$player->free_ticket_count = $freeBefore + $addFree;
$player->save();
$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;
// 钱包流水记录
DicePlayerWalletRecord::create([
'player_id' => $playerId,
'coin' => -$cost,
'type' => self::WALLET_TYPE_BUY_DRAW,
'wallet_before' => $coinBefore,
'wallet_after' => $coinAfter,
'total_ticket_count' => $addTotal,
'paid_ticket_count' => $addPaid,
'free_ticket_count' => $addFree,
'remark' => "购买抽奖券{$addTotal}次(付费{$addPaid}次+赠送{$addFree}次)",
]);
UserCache::setUser($playerId, $updatedUserArr);
// 抽奖券获取记录
DicePlayerTicketRecord::create([
'player_id' => $playerId,
'use_coins' => $cost,
'total_ticket_count' => $addTotal,
'paid_ticket_count' => $addPaid,
'free_ticket_count' => $addFree,
'remark' => "购买抽奖券{$addTotal}次(付费{$addPaid}次+赠送{$addFree}次)",
]);
});
$adminId = ($player->admin_id ?? null) ? (int) $player->admin_id : null;
try {
Db::transaction(function () use (
$player,
$playerId,
$adminId,
$cost,
$coinBefore,
$coinAfter,
$addTotal,
$addPaid,
$addFree,
$totalAfter,
$paidAfter,
$freeAfter
) {
$player->coin = $coinAfter;
$player->total_ticket_count = $totalAfter;
$player->paid_ticket_count = $paidAfter;
$player->free_ticket_count = $freeAfter;
$player->save();
$updated = DicePlayer::find($playerId);
$userArr = $updated->hidden(['password'])->toArray();
UserCache::setUser($playerId, $userArr);
DicePlayerWalletRecord::create([
'player_id' => $playerId,
'admin_id' => $adminId,
'coin' => -$cost,
'type' => self::WALLET_TYPE_BUY_DRAW,
'wallet_before' => $coinBefore,
'wallet_after' => $coinAfter,
'total_ticket_count' => $addTotal,
'paid_ticket_count' => $addPaid,
'free_ticket_count' => $addFree,
'remark' => "购买抽奖券{$addTotal}次(付费{$addPaid}次+赠送{$addFree}次)",
]);
DicePlayerTicketRecord::create([
'player_id' => $playerId,
'admin_id' => $adminId,
'use_coins' => $cost,
'total_ticket_count' => $addTotal,
'paid_ticket_count' => $addPaid,
'free_ticket_count' => $addFree,
'remark' => "购买抽奖券{$addTotal}次(付费{$addPaid}次+赠送{$addFree}次)",
]);
});
} catch (\Throwable $e) {
UserCache::setUser($playerId, $oldUserArr);
throw $e;
}
return [
'coin' => (float) $updated->coin,
'total_ticket_count' => (int) $updated->total_ticket_count,
'paid_ticket_count' => (int) $updated->paid_ticket_count,
'free_ticket_count' => (int) $updated->free_ticket_count,
'coin' => (float) $coinAfter,
'total_ticket_count' => (int) $totalAfter,
'paid_ticket_count' => (int) $paidAfter,
'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\reward_config\DiceRewardConfig;
use plugin\saiadmin\exception\ApiException;
use support\Log;
use support\think\Cache;
use support\think\Db;
@@ -33,11 +34,17 @@ class PlayStartLogic
/** 开启对局最低余额 = |DiceRewardConfig 最小 real_ev + 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 $direction 方向 0=无/顺时针 1=中奖/逆时针(前端 rediction
* @param int $direction 方向 0=无/顺时针 1=中奖/逆时针(前端 direction
* @return array 成功返回 DicePlayRecord 数据;余额不足时抛 ApiExceptionmessage 为约定文案
*/
public function run(int $playerId, int $direction): array
@@ -47,11 +54,11 @@ class PlayStartLogic
throw new ApiException('用户不存在');
}
$minEv = (float) DiceRewardConfig::min('real_ev');
$minEv = DiceRewardConfig::getCachedMinRealEv();
$minCoin = abs($minEv + self::MIN_COIN_EXTRA);
$coin = (float) $player->coin;
if ($coin < $minCoin) {
throw new ApiException('当前玩家余额小于DiceRewardConfigMin.real_ev+100无法继续游戏');
throw new ApiException('当前玩家余额'.$coin.'小于'.$minCoin.'无法继续游戏');
}
$paid = (int) ($player->paid_ticket_count ?? 0);
@@ -69,33 +76,103 @@ class PlayStartLogic
throw new ApiException('奖池配置不存在');
}
$tier = LotteryService::drawTierByWeights($config);
$rewards = DiceRewardConfig::where('tier', $tier)->select();
if ($rewards->isEmpty()) {
throw new ApiException('该档位暂无奖励配置');
// 按玩家权重抽取档位;若该档位无奖励或该方向下均无可用路径则重新摇取档位
$maxTierRetry = 10;
$chosen = null;
$startCandidates = [];
$tier = null;
for ($tierAttempt = 0; $tierAttempt < $maxTierRetry; $tierAttempt++) {
$tier = LotteryService::drawTierByPlayerWeights($player);
$tierRewards = DiceRewardConfig::getCachedByTier($tier);
if (empty($tierRewards)) {
Log::warning("档位 {$tier} 无任何奖励配置,重新摇取档位");
continue;
}
$maxRewardRetry = count($tierRewards);
for ($attempt = 0; $attempt < $maxRewardRetry; $attempt++) {
$chosen = $tierRewards[array_rand($tierRewards)];
$chosenId = (int) ($chosen['id'] ?? 0);
if ($direction === 0) {
$startCandidates = DiceRewardConfig::getCachedBySEndIndex($chosenId);
} else {
$startCandidates = DiceRewardConfig::getCachedByNEndIndex($chosenId);
}
if (!empty($startCandidates)) {
break 2;
}
Log::warning("方向 {$direction} 下无 s_end_index/n_end_index={$chosenId} 的配置,重新摇取");
}
Log::warning("方向 {$direction} 下档位 {$tier} 所有奖励均无可用路径配置,重新摇取档位");
}
$rewardList = $rewards->all();
$reward = $rewardList[array_rand($rewardList)];
$realEv = (float) $reward->real_ev;
$winCoin = 100 + $realEv; // 赢取平台币 = 100 + DiceRewardConfig.real_ev
$gridNumber = (int) $reward->grid_number;
$startIndex = (int) Cache::get(LotteryService::getStartIndexKey($playerId), 0);
$targetIndex = (int) $reward->id;
$rollArray = $this->generateRollArray($gridNumber);
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;
$configId = (int) $config->id;
$rewardId = (int) $reward->id;
$rewardId = $chosenId;
$configName = (string) ($config->name ?? '');
$isTierT5 = (string) ($reward->tier ?? '') === 'T5';
$isTierT5 = (string) ($chosen['tier'] ?? '') === 'T5';
$adminId = ($player->admin_id ?? null) ? (int) $player->admin_id : null;
try {
Db::transaction(function () use (
$playerId,
$adminId,
$configId,
$rewardId,
$configName,
$ticketType,
$winCoin,
$superWinCoin,
$rewardWinCoin,
$isWin,
$realEv,
$direction,
$startIndex,
@@ -106,14 +183,20 @@ class PlayStartLogic
) {
$record = DicePlayRecord::create([
'player_id' => $playerId,
'admin_id' => $adminId,
'lottery_config_id' => $configId,
'lottery_type' => $ticketType,
'is_win' => $isWin,
'win_coin' => $winCoin,
'super_win_coin' => $superWinCoin,
'reward_win_coin' => $rewardWinCoin,
'use_coins' => 0,
'direction' => $direction,
'reward_config_id' => $rewardId,
'start_index' => $startIndex,
'target_index' => $targetIndex,
'roll_array' => is_array($rollArray) ? json_encode($rollArray) : $rollArray,
'roll_number' => is_array($rollArray) ? array_sum($rollArray) : 0,
'lottery_name' => $configName,
'status' => self::RECORD_STATUS_SUCCESS,
]);
@@ -138,9 +221,10 @@ class PlayStartLogic
$p->total_ticket_count = (int) $p->total_ticket_count + 1;
DicePlayerTicketRecord::create([
'player_id' => $playerId,
'player_id' => $playerId,
'admin_id' => $adminId,
'free_ticket_count' => 1,
'remark' => '中奖结果为T5',
'remark' => '中奖结果为T5',
]);
}
@@ -156,28 +240,33 @@ class PlayStartLogic
DicePlayerWalletRecord::create([
'player_id' => $playerId,
'admin_id' => $adminId,
'coin' => $winCoin,
'type' => self::WALLET_TYPE_DRAW,
'wallet_before' => $coinBefore,
'wallet_after' => $coinAfter,
'remark' => '抽奖|play_record_id=' . $record->id,
]);
Cache::set(LotteryService::getStartIndexKey($playerId), $targetIndex, 86400 * 30);
});
} catch (\Throwable $e) {
if ($record === null) {
try {
$record = DicePlayRecord::create([
'player_id' => $playerId,
'admin_id' => $adminId ?? null,
'lottery_config_id' => $configId ?? 0,
'lottery_type' => $ticketType,
'is_win' => 0,
'win_coin' => 0,
'super_win_coin' => 0,
'reward_win_coin' => 0,
'use_coins' => 0,
'direction' => $direction,
'reward_config_id' => 0,
'start_index' => $startIndex,
'target_index' => 0,
'roll_array' => '[]',
'roll_number' => 0,
'status' => self::RECORD_STATUS_TIMEOUT,
]);
} catch (\Throwable $_) {
@@ -199,26 +288,84 @@ class PlayStartLogic
if (isset($arr['roll_array']) && is_string($arr['roll_array'])) {
$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;
}
/** 生成 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;
$maxSum = 30;
$n = max($minSum, min($maxSum, $gridNumber));
$dice = [1, 1, 1, 1, 1];
$remain = $n - 5;
while ($remain > 0) {
$i = array_rand($dice);
if ($dice[$i] < 6) {
$add = min($remain, 6 - $dice[$i]);
$dice[$i] += $add;
$remain -= $add;
$sum = max(5, min(30, $sum));
$arr = [1, 1, 1, 1, 1];
$remain = $sum - 5;
for ($i = 0; $i < $remain; $i++) {
$candidates = array_keys(array_filter($arr, function ($v) {
return $v < 6;
}));
if (empty($candidates)) {
break;
}
$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 $dice;
return $this->generateRollArrayFromSum($sum);
}
}

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)
*/
@@ -114,97 +42,92 @@ class UserLogic
}
/**
* 生成 user-tokenJWTplat=api_userid=user_id
* 登录JSONusername, password, lang, coin, time
* 存在则校验密码并更新 coin累加不存在则创建用户并写入 coin。
* 将会话写入 Redis返回 token 与前端连接地址。
*
* @param int|null $adminId 创建新用户时关联的后台管理员IDsa_system_user.id可选
*/
private function generateUserToken(int $userId): string
public function loginByUsername(string $username, string $password, string $lang, float $coin, string $time, ?int $adminId = null): array
{
$exp = config('api.user_token_exp', 604800);
$result = JwtToken::generateToken([
'id' => $userId,
'plat' => 'api_user',
$username = trim($username);
if ($username === '') {
throw new ApiException('username 不能为空');
}
$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;
if ($adminId !== null && $adminId > 0) {
$player->admin_id = $adminId;
}
$player->save();
}
$exp = (int) config('api.session_expire', 604800);
$tokenResult = JwtToken::generateToken([
'id' => (int) $player->id,
'username' => $username,
'plat' => 'api_login',
'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
* @param object $request 需有 header(string $name) 方法
* 从 JWT 中解析 username仅解码 payload不校验签名与过期用于退出时清除会话
*/
public static function getTokenFromRequest(object $request): string
public static function getUsernameFromJwtPayload(string $token): ?string
{
$token = $request->header('user-token') ?? '';
if ($token !== '') {
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 === '') {
$parts = explode('.', $token);
if (count($parts) !== 3) {
return null;
}
return self::getUserIdFromToken($token);
}
/**
* 根据 user-token 获取 user_id不写缓存仅解析 JWT
* 若 token 已通过退出接口加入黑名单,返回 null
*/
public static function getUserIdFromToken(string $userToken): ?int
{
if (UserCache::isTokenBlacklisted($userToken)) {
$payload = base64_decode(strtr($parts[1], '-_', '+/'), true);
if ($payload === false) {
return null;
}
try {
$decoded = JwtToken::verify(1, $userToken);
$extend = $decoded['extend'] ?? [];
if (($extend['plat'] ?? '') !== 'api_user') {
return null;
}
$id = $extend['id'] ?? null;
if ($id === null) {
return null;
}
$userId = (int) $id;
// 同一用户只允许当前登记的 token 生效,重新登录/注册后旧 token 失效
if (!UserCache::isCurrentUserToken($userId, $userToken)) {
return null;
}
return $userId;
} catch (\Throwable $e) {
$data = json_decode($payload, true);
if (!is_array($data)) {
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;
}
$extend = $data['extend'] ?? $data;
$username = $extend['username'] ?? null;
return $username !== null ? trim((string) $username) : null;
}
/**

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 $configType0Id = 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 = [];
public function __construct(int $playerId)
@@ -62,11 +62,11 @@ class LotteryService
$s->configType0Id = $config0 ? (int) $config0->id : null;
$s->configType1Id = $config1 ? (int) $config1->id : null;
$s->playerWeights = [
't1_wight' => (int) ($player->t1_wight ?? 0),
't2_wight' => (int) ($player->t2_wight ?? 0),
't3_wight' => (int) ($player->t3_wight ?? 0),
't4_wight' => (int) ($player->t4_wight ?? 0),
't5_wight' => (int) ($player->t5_wight ?? 0),
't1_weight' => (int) ($player->t1_weight ?? 0),
't2_weight' => (int) ($player->t2_weight ?? 0),
't3_weight' => (int) ($player->t3_weight ?? 0),
't4_weight' => (int) ($player->t4_weight ?? 0),
't5_weight' => (int) ($player->t5_weight ?? 0),
];
$s->save();
return $s;
@@ -83,17 +83,40 @@ class LotteryService
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
{
$tiers = ['T1', 'T2', 'T3', 'T4', 'T5'];
$weights = [
(int) ($config->t1_wight ?? 0),
(int) ($config->t2_wight ?? 0),
(int) ($config->t3_wight ?? 0),
(int) ($config->t4_wight ?? 0),
(int) ($config->t5_wight ?? 0),
(int) ($config->t1_weight ?? 0),
(int) ($config->t2_weight ?? 0),
(int) ($config->t3_weight ?? 0),
(int) ($config->t4_weight ?? 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);
if ($total <= 0) {
return $tiers[array_rand($tiers)];

View File

@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace app\api\util;
use support\Request;
/**
* API 多语言:根据请求头 langen=英文zh=中文)翻译返回文案
*/
class ApiLang
{
private const LANG_HEADER = 'lang';
private const LANG_EN = 'en';
private const LANG_ZH = 'zh';
/** @var array<string, string>|null */
private static ?array $enMap = null;
/**
* 从请求中获取语言lang 请求头 en=英文zh=中文,默认 zh
*/
public static function getLang(?Request $request = null): string
{
$request = $request ?? (function_exists('request') ? request() : null);
if ($request === null) {
return self::LANG_ZH;
}
$lang = $request->header(self::LANG_HEADER);
if ($lang !== null && $lang !== '') {
$lang = strtolower(trim((string) $lang));
if ($lang === self::LANG_EN) {
return self::LANG_EN;
}
if ($lang === self::LANG_ZH || $lang === 'chs') {
return self::LANG_ZH;
}
}
return self::LANG_ZH;
}
/**
* 翻译文案:当前请求语言为 en 时返回英文,否则返回原文(中文)
* @param string $message 中文或原文
* @param Request|null $request 当前请求,不传则自动取 request()
*/
public static function translate(string $message, ?Request $request = null): string
{
$lang = self::getLang($request);
if ($lang !== self::LANG_EN) {
return $message;
}
if (self::$enMap === null) {
$path = dirname(__DIR__) . '/lang/en.php';
self::$enMap = is_file($path) ? (require $path) : [];
}
return self::$enMap[$message] ?? $message;
}
/**
* 带占位符的翻译,如 translateParams('当前玩家余额%s小于%s无法继续游戏', [$coin, $minCoin])
* 先翻译再替换en 文案使用 %s 占位)
*/
public static function translateParams(string $message, array $params = [], ?Request $request = null): string
{
$translated = self::translate($message, $request);
if ($params !== []) {
$translated = sprintf($translated, ...$params);
}
return $translated;
}
}

View File

@@ -0,0 +1,183 @@
<?php
declare(strict_types=1);
namespace app\dice\controller;
use app\dice\helper\AdminScopeHelper;
use app\dice\model\player\DicePlayer;
use app\dice\model\player_wallet_record\DicePlayerWalletRecord;
use app\dice\model\play_record\DicePlayRecord;
use plugin\saiadmin\basic\BaseController;
use plugin\saiadmin\service\Permission;
use support\Response;
use support\think\Db;
/**
* 大富翁工作台数据统计
*/
class DiceDashboardController extends BaseController
{
/**
* 工作台卡片统计:玩家注册、充值、提现、游玩次数(含较上周对比)
*/
#[Permission('工作台数据统计', 'core:console:list')]
public function statistics(): Response
{
$thisWeekStart = date('Y-m-d 00:00:00', strtotime('monday this week'));
$thisWeekEnd = date('Y-m-d 23:59:59', strtotime('sunday this week'));
$lastWeekStart = date('Y-m-d 00:00:00', strtotime('monday last week'));
$lastWeekEnd = date('Y-m-d 23:59:59', strtotime('sunday last week'));
$adminInfo = $this->adminInfo ?? null;
$playerQueryThis = DicePlayer::whereBetween('create_time', [$thisWeekStart, $thisWeekEnd]);
$playerQueryLast = DicePlayer::whereBetween('create_time', [$lastWeekStart, $lastWeekEnd]);
AdminScopeHelper::applyAdminScope($playerQueryThis, $adminInfo);
AdminScopeHelper::applyAdminScope($playerQueryLast, $adminInfo);
$playerThis = $playerQueryThis->count();
$playerLast = $playerQueryLast->count();
$chargeQueryThis = DicePlayerWalletRecord::where('type', 0)
->where('coin', '>', 0)
->whereBetween('create_time', [$thisWeekStart, $thisWeekEnd]);
$chargeQueryLast = DicePlayerWalletRecord::where('type', 0)
->where('coin', '>', 0)
->whereBetween('create_time', [$lastWeekStart, $lastWeekEnd]);
AdminScopeHelper::applyAdminScope($chargeQueryThis, $adminInfo);
AdminScopeHelper::applyAdminScope($chargeQueryLast, $adminInfo);
$chargeThis = $chargeQueryThis->sum('coin');
$chargeLast = $chargeQueryLast->sum('coin');
$withdrawQueryThis = DicePlayerWalletRecord::where('type', 1)
->whereBetween('create_time', [$thisWeekStart, $thisWeekEnd]);
$withdrawQueryLast = DicePlayerWalletRecord::where('type', 1)
->whereBetween('create_time', [$lastWeekStart, $lastWeekEnd]);
AdminScopeHelper::applyAdminScope($withdrawQueryThis, $adminInfo);
AdminScopeHelper::applyAdminScope($withdrawQueryLast, $adminInfo);
$withdrawThis = $withdrawQueryThis->sum(Db::raw('ABS(coin)'));
$withdrawLast = $withdrawQueryLast->sum(Db::raw('ABS(coin)'));
$playQueryThis = DicePlayRecord::whereBetween('create_time', [$thisWeekStart, $thisWeekEnd]);
$playQueryLast = DicePlayRecord::whereBetween('create_time', [$lastWeekStart, $lastWeekEnd]);
AdminScopeHelper::applyAdminScope($playQueryThis, $adminInfo);
AdminScopeHelper::applyAdminScope($playQueryLast, $adminInfo);
$playThis = $playQueryThis->count();
$playLast = $playQueryLast->count();
$playerChange = $this->calcWeekChange($playerThis, $playerLast);
$chargeChange = $this->calcWeekChange((float) $chargeThis, (float) $chargeLast);
$withdrawChange = $this->calcWeekChange((float) $withdrawThis, (float) $withdrawLast);
$playChange = $this->calcWeekChange($playThis, $playLast);
return $this->success([
'player_count' => $playerThis,
'player_count_change' => $playerChange,
'charge_amount' => (float) $chargeThis,
'charge_amount_change' => $chargeChange,
'withdraw_amount' => (float) $withdrawThis,
'withdraw_amount_change' => $withdrawChange,
'play_count' => $playThis,
'play_count_change' => $playChange,
]);
}
/**
* 近期玩家充值统计近10天每日充值金额
*/
#[Permission('工作台数据统计', 'core:console:list')]
public function rechargeChart(): Response
{
$adminInfo = $this->adminInfo ?? null;
$allowedIds = AdminScopeHelper::getAllowedAdminIds($adminInfo);
$adminCondition = '';
if ($allowedIds !== null) {
if (empty($allowedIds)) {
$data = [];
foreach (range(0, 9) as $n) {
$data[] = ['recharge_date' => date('Y-m-d', strtotime("-{$n} days")), 'recharge_amount' => 0];
}
$data = array_reverse($data);
return $this->success([
'recharge_amount' => array_map('floatval', array_column($data, 'recharge_amount')),
'recharge_date' => array_column($data, 'recharge_date'),
]);
}
$idsStr = implode(',', array_map('intval', $allowedIds));
$adminCondition = " AND w.admin_id IN ({$idsStr})";
}
$sql = "
SELECT
d.date AS recharge_date,
IFNULL(SUM(w.coin), 0) AS recharge_amount
FROM
(SELECT CURDATE() - INTERVAL (a.N) DAY AS date
FROM (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3
UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6
UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
) d
LEFT JOIN dice_player_wallet_record w
ON DATE(w.create_time) = d.date AND w.type = 0 AND w.coin > 0 {$adminCondition}
GROUP BY d.date
ORDER BY d.date ASC
";
$data = Db::query($sql);
return $this->success([
'recharge_amount' => array_map('floatval', array_column($data, 'recharge_amount')),
'recharge_date' => array_column($data, 'recharge_date'),
]);
}
/**
* 月度玩家充值汇总当年1-12月每月充值金额
*/
#[Permission('工作台数据统计', 'core:console:list')]
public function rechargeBarChart(): Response
{
$adminInfo = $this->adminInfo ?? null;
$allowedIds = AdminScopeHelper::getAllowedAdminIds($adminInfo);
$adminCondition = '';
if ($allowedIds !== null) {
if (empty($allowedIds)) {
$data = [];
for ($m = 1; $m <= 12; $m++) {
$data[] = ['recharge_month' => sprintf('%02d月', $m), 'recharge_amount' => 0];
}
return $this->success([
'recharge_amount' => array_map('floatval', array_column($data, 'recharge_amount')),
'recharge_month' => array_column($data, 'recharge_month'),
]);
}
$idsStr = implode(',', array_map('intval', $allowedIds));
$adminCondition = " AND w.admin_id IN ({$idsStr})";
}
$sql = "
SELECT
CONCAT(LPAD(m.month_num, 2, '0'), '月') AS recharge_month,
IFNULL(SUM(w.coin), 0) AS recharge_amount
FROM
(SELECT 1 AS month_num UNION ALL SELECT 2 UNION ALL SELECT 3
UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6
UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12) m
LEFT JOIN dice_player_wallet_record w
ON YEAR(w.create_time) = YEAR(CURDATE())
AND MONTH(w.create_time) = m.month_num
AND w.type = 0 AND w.coin > 0 {$adminCondition}
GROUP BY m.month_num
ORDER BY m.month_num ASC
";
$data = Db::query($sql);
return $this->success([
'recharge_amount' => array_map('floatval', array_column($data, 'recharge_amount')),
'recharge_month' => array_column($data, 'recharge_month'),
]);
}
private function calcWeekChange($current, $last): float
{
if ($last == 0) {
return $current > 0 ? 100.0 : 0.0;
}
return round((($current - $last) / $last) * 100, 1);
}
}

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;
use app\dice\model\lottery_config\DiceLotteryConfig;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\lottery_config\DiceLotteryConfigLogic;
use app\dice\validate\lottery_config\DiceLotteryConfigValidate;
@@ -28,6 +29,21 @@ class DiceLotteryConfigController extends BaseController
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

View File

@@ -6,6 +6,7 @@
// +----------------------------------------------------------------------
namespace app\dice\controller\play_record;
use app\dice\helper\AdminScopeHelper;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\play_record\DicePlayRecordLogic;
use app\dice\validate\play_record\DicePlayRecordValidate;
@@ -46,11 +47,14 @@ class DicePlayRecordController extends BaseController
['is_win', ''],
['win_coin_min', ''],
['win_coin_max', ''],
['roll_number_min', ''],
['roll_number_max', ''],
['reward_ui_text', ''],
['reward_tier', ''],
['direction', ''],
]);
$query = $this->logic->search($where);
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$query->with([
'dicePlayer',
'diceRewardConfig',
@@ -66,7 +70,9 @@ class DicePlayRecordController extends BaseController
#[Permission('玩家抽奖记录列表', 'dice:play_record:index:index')]
public function getPlayerOptions(Request $request): Response
{
$list = DicePlayer::field('id,username')->select();
$query = DicePlayer::field('id,username');
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$list = $query->select();
$data = $list->map(function ($item) {
return ['id' => $item['id'], 'username' => $item['username'] ?? ''];
})->toArray();
@@ -113,12 +119,15 @@ class DicePlayRecordController extends BaseController
{
$id = $request->input('id', '');
$model = $this->logic->read($id);
if ($model) {
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
} else {
if (!$model) {
return $this->fail('未查找到信息');
}
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限查看该记录');
}
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
}
/**

View File

@@ -6,6 +6,9 @@
// +----------------------------------------------------------------------
namespace app\dice\controller\player;
use app\dice\helper\AdminScopeHelper;
use app\dice\model\lottery_config\DiceLotteryConfig;
use plugin\saiadmin\app\model\system\SystemUser;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\player\DicePlayerLogic;
use app\dice\validate\player\DicePlayerValidate;
@@ -28,6 +31,50 @@ class DicePlayerController extends BaseController
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);
}
/**
* 获取后台管理员选项SystemUser.id、username、realname供 admin_id 下拉使用
* 根据当前登录用户权限过滤(超级管理员可见全部,普通管理员按部门)
* @param Request $request
* @return Response 返回 [ ['id' => int, 'username' => string, 'realname' => string], ... ]
*/
#[Permission('大富翁-玩家列表', 'dice:player:index:index')]
public function getSystemUserOptions(Request $request): Response
{
$query = SystemUser::field('id,username,realname')->where('status', 1)->order('id', 'asc');
if (isset($this->adminInfo['id']) && (int) $this->adminInfo['id'] > 1) {
$deptList = $this->adminInfo['deptList'] ?? [];
if (!empty($deptList)) {
$query->auth($deptList);
}
}
$list = $query->select();
$data = $list->map(function ($item) {
$label = trim((string) ($item['realname'] ?? '')) ?: (string) ($item['username'] ?? '');
return [
'id' => (int) $item['id'],
'username' => (string) ($item['username'] ?? ''),
'realname' => (string) ($item['realname'] ?? ''),
'label' => $label ?: (string) $item['id'],
];
})->toArray();
return $this->success($data);
}
/**
* 数据列表
* @param Request $request
@@ -42,9 +89,11 @@ class DicePlayerController extends BaseController
['phone', ''],
['status', ''],
['coin', ''],
['is_up', ''],
['lottery_config_id', ''],
]);
$query = $this->logic->search($where);
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$query->with(['diceLotteryConfig']);
$data = $this->logic->getList($query);
return $this->success($data);
}
@@ -59,12 +108,15 @@ class DicePlayerController extends BaseController
{
$id = $request->input('id', '');
$model = $this->logic->read($id);
if ($model) {
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
} else {
if (!$model) {
return $this->fail('未查找到信息');
}
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限查看该玩家');
}
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
}
/**
@@ -77,6 +129,10 @@ class DicePlayerController extends BaseController
{
$data = $request->post();
$this->validate('save', $data);
// 新增时若未选择管理员,默认使用当前登录用户
if (empty($data['admin_id']) && isset($this->adminInfo['id']) && (int) $this->adminInfo['id'] > 0) {
$data['admin_id'] = (int) $this->adminInfo['id'];
}
$result = $this->logic->add($data);
if ($result) {
return $this->success('添加成功');
@@ -95,6 +151,13 @@ class DicePlayerController extends BaseController
{
$data = $request->post();
$this->validate('update', $data);
$model = $this->logic->read($data['id'] ?? 0);
if ($model) {
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限修改该玩家');
}
}
$result = $this->logic->edit($data['id'], $data);
if ($result) {
return $this->success('修改成功');
@@ -119,6 +182,13 @@ class DicePlayerController extends BaseController
if ($status === null || $status === '') {
return $this->fail('缺少 status');
}
$model = $this->logic->read($id);
if ($model) {
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限修改该玩家');
}
}
$this->logic->edit($id, ['status' => (int) $status]);
return $this->success('修改成功');
}
@@ -135,6 +205,22 @@ class DicePlayerController extends BaseController
if (empty($ids)) {
return $this->fail('请选择要删除的数据');
}
$ids = is_array($ids) ? $ids : explode(',', (string) $ids);
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null) {
$models = $this->logic->model->whereIn('id', $ids)->column('admin_id', 'id');
$validIds = [];
foreach ($ids as $id) {
$adminId = (int) ($models[$id] ?? 0);
if (in_array($adminId, $allowedIds, true)) {
$validIds[] = $id;
}
}
$ids = $validIds;
if (empty($ids)) {
return $this->fail('无权限删除所选玩家');
}
}
$result = $this->logic->destroy($ids);
if ($result) {
return $this->success('删除成功');

View File

@@ -6,6 +6,7 @@
// +----------------------------------------------------------------------
namespace app\dice\controller\player_ticket_record;
use app\dice\helper\AdminScopeHelper;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\player_ticket_record\DicePlayerTicketRecordLogic;
use app\dice\validate\player_ticket_record\DicePlayerTicketRecordValidate;
@@ -51,6 +52,7 @@ class DicePlayerTicketRecordController extends BaseController
['create_time_max', ''],
]);
$query = $this->logic->search($where);
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$query->with([
'dicePlayer',
]);
@@ -66,7 +68,9 @@ class DicePlayerTicketRecordController extends BaseController
#[Permission('抽奖券获取记录列表', 'dice:player_ticket_record:index:index')]
public function getPlayerOptions(Request $request): Response
{
$list = DicePlayer::field('id,username')->select();
$query = DicePlayer::field('id,username');
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$list = $query->select();
$data = $list->map(function ($item) {
return ['id' => $item['id'], 'username' => $item['username'] ?? ''];
})->toArray();
@@ -83,12 +87,15 @@ class DicePlayerTicketRecordController extends BaseController
{
$id = $request->input('id', '');
$model = $this->logic->read($id);
if ($model) {
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
} else {
if (!$model) {
return $this->fail('未查找到信息');
}
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限查看该记录');
}
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
}
/**

View File

@@ -6,6 +6,7 @@
// +----------------------------------------------------------------------
namespace app\dice\controller\player_wallet_record;
use app\dice\helper\AdminScopeHelper;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\player_wallet_record\DicePlayerWalletRecordLogic;
use app\dice\validate\player_wallet_record\DicePlayerWalletRecordValidate;
@@ -47,6 +48,7 @@ class DicePlayerWalletRecordController extends BaseController
['create_time_max', ''],
]);
$query = $this->logic->search($where);
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$query->with([
'dicePlayer',
'operator',
@@ -63,7 +65,9 @@ class DicePlayerWalletRecordController extends BaseController
#[Permission('玩家钱包流水列表', 'dice:player_wallet_record:index:index')]
public function getPlayerOptions(Request $request): Response
{
$list = DicePlayer::field('id,username')->select();
$query = DicePlayer::field('id,username');
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$list = $query->select();
$data = $list->map(function ($item) {
return ['id' => $item['id'], 'username' => $item['username'] ?? ''];
})->toArray();
@@ -83,10 +87,14 @@ class DicePlayerWalletRecordController extends BaseController
if ($playerId === null || $playerId === '') {
return $this->fail('缺少 player_id');
}
$player = DicePlayer::field('coin')->where('id', $playerId)->find();
$player = DicePlayer::field('coin,admin_id')->where('id', $playerId)->find();
if (!$player) {
return $this->fail('玩家不存在');
}
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($player->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限操作该玩家');
}
return $this->success(['wallet_before' => (float) $player['coin']]);
}
@@ -100,12 +108,15 @@ class DicePlayerWalletRecordController extends BaseController
{
$id = $request->input('id', '');
$model = $this->logic->read($id);
if ($model) {
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
} else {
if (!$model) {
return $this->fail('未查找到信息');
}
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限查看该记录');
}
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
}
/**
@@ -155,6 +166,14 @@ class DicePlayerWalletRecordController extends BaseController
return $this->fail('请先登录');
}
$player = DicePlayer::field('admin_id')->where('id', $playerId)->find();
if ($player) {
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($player->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限操作该玩家');
}
}
try {
$this->logic->adminOperate($data, $adminId);
return $this->success('操作成功');

View File

@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace app\dice\helper;
use plugin\saiadmin\app\model\system\SystemUser;
/**
* 管理员数据范围辅助类
* 用于获取当前管理员及其部门下属管理员可访问的数据范围
*/
class AdminScopeHelper
{
/**
* 获取当前管理员可访问的 admin_id 列表
* 超级管理员(id=1) 返回 null 表示不限制
* 普通管理员返回其本人及部门下属管理员的 id 列表
*
* @param array|null $adminInfo 当前登录管理员信息(含 id、deptList
* @return int[]|null null=不限制(超级管理员),否则为可访问的 admin_id 数组
*/
public static function getAllowedAdminIds(?array $adminInfo): ?array
{
if (empty($adminInfo) || !isset($adminInfo['id'])) {
return [];
}
$adminId = (int) $adminInfo['id'];
if ($adminId <= 1) {
return null;
}
$deptList = $adminInfo['deptList'] ?? [];
if (empty($deptList) || !isset($deptList['id'])) {
return [$adminId];
}
$query = SystemUser::field('id');
$query->auth($deptList);
$ids = $query->column('id');
return array_map('intval', $ids ?: []);
}
/**
* 对查询应用 admin_id 范围过滤
*
* @param object $query ThinkORM 查询对象
* @param array|null $adminInfo 当前登录管理员信息
* @return void
*/
public static function applyAdminScope($query, ?array $adminInfo): void
{
$allowedIds = self::getAllowedAdminIds($adminInfo);
if ($allowedIds === null) {
return;
}
if (empty($allowedIds)) {
$query->whereRaw('1=0');
return;
}
$query->whereIn('admin_id', $allowedIds);
}
}

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
{
if (!array_key_exists('roll_array', $data)) {
return $data;
}
$val = $data['roll_array'];
if (is_array($val)) {
$data['roll_array'] = json_encode($val, JSON_UNESCAPED_UNICODE);
if (array_key_exists('roll_array', $data)) {
$val = $data['roll_array'];
if (is_array($val)) {
$data['roll_array'] = json_encode($val, JSON_UNESCAPED_UNICODE);
if (!isset($data['roll_number'])) {
$data['roll_number'] = array_sum($val);
}
}
}
return $data;
}

View File

@@ -73,8 +73,10 @@ class DicePlayerWalletRecordLogic extends BaseLogic
DicePlayer::where('id', $playerId)->update(['coin' => $walletAfter]);
$playerAdminId = ($player->admin_id ?? null) ? (int) $player->admin_id : null;
$record = [
'player_id' => $playerId,
'admin_id' => $playerAdminId,
'coin' => $type === 3 ? $coin : -$coin,
'type' => $type,
'wallet_before' => $walletBefore,

View File

@@ -13,6 +13,7 @@ use app\dice\model\reward_config\DiceRewardConfig;
/**
* 奖励配置逻辑层
* weight 仅 tier=BIGWIN 时可设定,保存时非 BIGWIN 强制 weight=0
*/
class DiceRewardConfigLogic extends BaseLogic
{
@@ -24,4 +25,36 @@ class DiceRewardConfigLogic extends BaseLogic
$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 $create_time 创建时间
* @property $update_time 修改时间
* @property $t1_wight T1池权重
* @property $t2_wight T2池权重
* @property $t3_wight T3池权重
* @property $t4_wight T4池权重
* @property $t5_wight T5池权重
* @property $t1_weight T1池权重
* @property $t2_weight T2池权重
* @property $t3_weight T3池权重
* @property $t4_weight T4池权重
* @property $t5_weight T5池权重
*/
class DiceLotteryConfig extends BaseModel
{
@@ -48,4 +48,13 @@ class DiceLotteryConfig extends BaseModel
$query->where('name', 'like', '%'.$value.'%');
}
/**
* 奖池类型 搜索type=0/1/2 等)
*/
public function searchTypeAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('type', '=', $value);
}
}
}

View File

@@ -19,16 +19,20 @@ use think\model\relation\BelongsTo;
*
* @property $id ID
* @property $player_id 玩家id
* @property $admin_id 关联玩家所属管理员IDDicePlayer.admin_id
* @property $lottery_config_id 彩金池配置
* @property $lottery_type 抽奖类型
* @property $is_win 中奖
* @property $win_coin 赢取平台币
* @property $is_win 是否中大奖:豹子号[1,1,1,1,1]~[6,6,6,6,6]为1否则0
* @property $win_coin 赢取平台币= super_win_coin + reward_win_coin
* @property $super_win_coin 中大奖平台币(豹子时发放)
* @property $reward_win_coin 摇色子中奖平台币
* @property $direction 方向:0=顺时针,1=逆时针
* @property $reward_config_id 奖励配置id
* @property $lottery_id 奖池
* @property $start_index 起始索引
* @property $target_index 结束索引
* @property $roll_array 摇取点数,格式:[1,2,3,4,5]5个点数
* @property $roll_number 摇取点数和5个色子点数之和5-30
* @property $lottery_name 奖池名
* @property $status 状态:0=超时/失败 1=成功
* @property $create_time 创建时间
@@ -111,7 +115,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)
{
if ($value !== '' && $value !== null) {
@@ -135,6 +157,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 */
public function searchRewardUiTextAttr($query, $value)
{
@@ -170,4 +224,20 @@ class DicePlayRecord extends BaseModel
$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,19 @@ use app\dice\model\lottery_config\DiceLotteryConfig;
* @property $password 密码
* @property $status 状态
* @property $coin 平台币
* @property $is_up 倍率
* @property $t1_wight T1池权重
* @property $t2_wight T2池权重
* @property $t3_wight T3池权重
* @property $t4_wight T4池权重
* @property $t5_wight T5池权重
* @property $admin_id 创建该玩家的后台管理员ID关联 sa_system_user.id
* @property $lottery_config_id 彩金池配置ID0或null时使用自定义权重*_weight
* @property $t1_weight T1池权重
* @property $t2_weight T2池权重
* @property $t3_weight T3池权重
* @property $t4_weight T4池权重
* @property $t5_weight T5池权重
* @property $total_ticket_count 总抽奖次数
* @property $paid_ticket_count 购买抽奖次数
* @property $free_ticket_count 赠送抽奖次数
* @property $created_at 创建时间
* @property $updated_at 更新时间
* @property $deleted_at 删除时间
* @property $create_time 创建时间
* @property $update_time 更新时间
* @property $delete_time 删除时间
*/
class DicePlayer extends BaseModel
{
@@ -49,6 +50,10 @@ class DicePlayer extends BaseModel
*/
protected $table = 'dice_player';
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
/**
* 新增前:生成唯一 uid昵称 name 默认使用 uid
* 用 try-catch 避免表尚未含 uid 时 getAttr/getData 抛 InvalidArgumentException
@@ -73,12 +78,22 @@ class DicePlayer extends BaseModel
if ($name === null || $name === '') {
$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 的奖池配置
self::setDefaultWeightsFromLotteryConfig($model);
}
/**
* 从 DiceLotteryConfig type=0 取 t1_wightt5_wight 作为玩家未设置时的默认值
* 从 DiceLotteryConfig type=0 取 t1_weightt5_weight 作为玩家未设置时的默认值
*/
protected static function setDefaultWeightsFromLotteryConfig(DicePlayer $model): void
{
@@ -86,7 +101,7 @@ class DicePlayer extends BaseModel
if (!$config) {
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) {
try {
$val = $model->getAttr($field);
@@ -158,13 +173,20 @@ class DicePlayer extends BaseModel
}
/**
* 倍率 搜索
* 彩金池配置ID 搜索
*/
public function searchIs_upAttr($query, $value)
public function searchLottery_config_idAttr($query, $value)
{
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

@@ -17,6 +17,7 @@ use think\model\relation\BelongsTo;
*
* @property $id ID
* @property $player_id 玩家id
* @property $admin_id 关联玩家所属管理员IDDicePlayer.admin_id
* @property $use_coins 消耗硬币
* @property $total_ticket_count 总抽奖次数
* @property $paid_ticket_count 购买抽奖次数

View File

@@ -18,6 +18,7 @@ use think\model\relation\BelongsTo;
*
* @property $id ID
* @property $player_id 用户id
* @property $admin_id 关联玩家所属管理员IDDicePlayer.admin_id
* @property $coin 平台币变化
* @property $type 类型:0=充值 1=提现 2=购买抽奖次数
* @property $wallet_before 钱包操作前

View File

@@ -7,23 +7,37 @@
namespace app\dice\model\reward_config;
use plugin\saiadmin\basic\think\BaseModel;
use support\think\Cache;
/**
* 奖励配置模型
*
* dice_reward_config 奖励配置
* 奖励列表为全玩家通用,保存时刷新缓存,游戏时优先读缓存。
*
* @property $id ID
* @property $grid_number 色子点数
* @property $ui_text 前端显示文本
* @property $real_ev 真实资金结算
* @property $tier 所属档位
* @property $weight 权重%(仅 tier=BIGWIN 时可设定0-100
* @property $s_end_index 顺时针结束索引
* @property $n_end_index 逆时针结束索引
* @property $remark 备注
* @property $create_time 创建时间
* @property $update_time 修改时间
*/
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
@@ -36,6 +50,189 @@ class DiceRewardConfig extends BaseModel
*/
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)
{
@@ -83,4 +280,20 @@ class DiceRewardConfig extends BaseModel
$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 = [
'name' => 'require',
'type' => 'require',
't1_wight' => 'require',
't2_wight' => 'require',
't3_wight' => 'require',
't4_wight' => 'require',
't5_wight' => 'require',
't1_weight' => 'require',
't2_weight' => 'require',
't3_weight' => 'require',
't4_weight' => 'require',
't5_weight' => 'require',
];
/**
@@ -32,11 +32,11 @@ class DiceLotteryConfigValidate extends BaseValidate
protected $message = [
'name' => '名称必须填写',
'type' => '奖池类型必须填写',
't1_wight' => 'T1池权重必须填写',
't2_wight' => 'T2池权重必须填写',
't3_wight' => 'T3池权重必须填写',
't4_wight' => 'T4池权重必须填写',
't5_wight' => 'T5池权重必须填写',
't1_weight' => 'T1池权重必须填写',
't2_weight' => 'T2池权重必须填写',
't3_weight' => 'T3池权重必须填写',
't4_weight' => 'T4池权重必须填写',
't5_weight' => 'T5池权重必须填写',
];
/**
@@ -46,20 +46,20 @@ class DiceLotteryConfigValidate extends BaseValidate
'save' => [
'name',
'type',
't1_wight',
't2_wight',
't3_wight',
't4_wight',
't5_wight',
't1_weight',
't2_weight',
't3_weight',
't4_weight',
't5_weight',
],
'update' => [
'name',
'type',
't1_wight',
't2_wight',
't3_wight',
't4_wight',
't5_wight',
't1_weight',
't2_weight',
't3_weight',
't4_weight',
't5_weight',
],
];

View File

@@ -10,45 +10,56 @@ use plugin\saiadmin\basic\BaseValidate;
/**
* 奖励配置验证器
* weight 仅当 tier=BIGWIN 时可设定,且严格限制 0-100%
*/
class DiceRewardConfigValidate extends BaseValidate
{
/**
* 定义验证规则
*/
protected $rule = [
protected $rule = [
'grid_number' => 'require',
'ui_text' => 'require',
'real_ev' => 'require',
'tier' => 'require',
'ui_text' => 'require',
'real_ev' => 'require',
'tier' => 'require',
'weight' => 'checkWeight',
];
/**
* 定义错误信息
*/
protected $message = [
protected $message = [
'grid_number' => '色子点数必须填写',
'ui_text' => '前端显示文本必须填写',
'real_ev' => '真实资金结算必须填写',
'tier' => '所属档位必须填写',
'ui_text' => '前端显示文本必须填写',
'real_ev' => '真实资金结算必须填写',
'tier' => '所属档位必须填写',
'weight' => '权重仅 tier=BIGWIN 时可设定,且必须为 0-100',
];
/**
* 定义场景
*/
protected $scene = [
'save' => [
'grid_number',
'ui_text',
'real_ev',
'tier',
],
'update' => [
'grid_number',
'ui_text',
'real_ev',
'tier',
],
'save' => ['grid_number', 'ui_text', 'real_ev', 'tier', 'weight'],
'update' => ['grid_number', 'ui_text', 'real_ev', 'tier', 'weight'],
];
/**
* 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 鉴权与用户相关配置
*/
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_secret' => env('API_AUTH_TOKEN_SECRET', ''),
// auth-token 时间戳允许误差(秒),防重放,默认 300 秒
@@ -11,6 +19,8 @@ return [
'auth_token_exp' => (int) env('API_AUTH_TOKEN_EXP', 86400),
// auth-token 按设备存储的 Redis key 前缀(同一设备只保留最新一个 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_exp' => (int) env('API_USER_TOKEN_EXP', 604800),
// 按用户存储当前有效 user-token 的 Redis key 前缀(同一用户仅保留最新一次登录的 token
@@ -21,4 +31,7 @@ return [
'user_cache_prefix' => env('API_USER_CACHE_PREFIX', 'api:user:'),
// 用户信息加密密钥(用于 Redis 中 value 的加密),建议 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;
return [
'debug' => true,
// 生产环境务必设为 false减少 I/O 与堆栈输出,提升接口响应
'debug' => env('APP_DEBUG', false),
'error_reporting' => E_ALL,
'default_timezone' => 'Asia/Shanghai',
'request_class' => Request::class,

View File

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

View File

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

View File

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

View File

@@ -13,29 +13,42 @@
*/
use Webman\Route;
use app\api\middleware\CheckAuthTokenMiddleware;
use app\api\middleware\CheckUserTokenMiddleware;
use app\api\middleware\TokenMiddleware;
use app\api\middleware\AuthTokenMiddleware;
// 仅需 auth-token 的路由组(authToken 接口在中间件内白名单跳过)
Route::group('/api', function () {
Route::any('/authToken', [app\api\controller\AuthTokenController::class, 'index']);
Route::any('/user/login', [app\api\controller\UserController::class, 'login']);
Route::any('/user/register', [app\api\controller\UserController::class, 'register']);
// 平台鉴权接口:/api/v1/authToken,请求头 signature/secret/time/agent_id返回 authtToken
Route::group('/api/v1', function () {
Route::any('/authToken', [app\api\controller\v1\AuthTokenController::class, 'index']);
})->middleware([]);
// 平台 v1 接口:需在请求头携带 auth-token
Route::group('/api/v1', function () {
Route::any('/getGameUrl', [app\api\controller\v1\GameController::class, 'getGameUrl']);
Route::any('/getPlayerInfo', [app\api\controller\v1\GameController::class, 'getPlayerInfo']);
Route::any('/getPlayerGameRecord', [app\api\controller\v1\GameController::class, 'getPlayerGameRecord']);
Route::any('/getPlayerWalletRecord', [app\api\controller\v1\GameController::class, 'getPlayerWalletRecord']);
Route::any('/getPlayerTicketRecord', [app\api\controller\v1\GameController::class, 'getPlayerTicketRecord']);
Route::any('/setPlayerWallet', [app\api\controller\v1\GameController::class, 'setPlayerWallet']);
})->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::any('/user/logout', [app\api\controller\UserController::class, 'logout']);
Route::any('/user/info', [app\api\controller\UserController::class, 'info']);
Route::any('/user/balance', [app\api\controller\UserController::class, 'balance']);
Route::any('/user/walletRecord', [app\api\controller\UserController::class, 'walletRecord']);
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/lotteryPool', [app\api\controller\GameController::class, 'lotteryPool']);
Route::any('/game/playStart', [app\api\controller\GameController::class, 'playStart']);
})->middleware([
CheckAuthTokenMiddleware::class,
CheckUserTokenMiddleware::class,
TokenMiddleware::class,
]);

View File

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

View File

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

View File

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

View File

@@ -39,30 +39,4 @@ class Handler extends ExceptionHandler
}
$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

@@ -117,7 +117,10 @@ class SystemDeptLogic extends BaseLogic
public function accessDept(array $where = []): array
{
$query = $this->search($where);
$query->auth($this->adminInfo['deptList']);
// 超级管理员(id=1)可查看全部部门,普通管理员按部门权限过滤
if (isset($this->adminInfo['id']) && $this->adminInfo['id'] > 1) {
$query->auth($this->adminInfo['deptList'] ?? []);
}
$query->field('id, id as value, name as label, parent_id');
$query->order('sort', 'desc');
$data = $this->getAll($query);

View File

@@ -40,7 +40,10 @@ class SystemUserLogic extends BaseLogic
{
$query = $this->search($where);
$query->with(['depts']);
$query->auth($this->adminInfo['deptList']);
// 超级管理员(id=1)可查看全部用户,普通管理员按部门权限过滤
if (isset($this->adminInfo['id']) && $this->adminInfo['id'] > 1) {
$query->auth($this->adminInfo['deptList'] ?? []);
}
return $this->getList($query);
}
@@ -132,9 +135,11 @@ class SystemUserLogic extends BaseLogic
return $this->transaction(function () use ($data, $id) {
$role_ids = $data['role_ids'] ?? [];
$post_ids = $data['post_ids'] ?? [];
// 仅可修改当前部门和子部门的用户
// 超级管理员可修改任意用户,普通管理员仅可修改当前部门和子部门的用户
$query = $this->model->where('id', $id);
$query->auth($this->adminInfo['deptList']);
if (isset($this->adminInfo['id']) && $this->adminInfo['id'] > 1) {
$query->auth($this->adminInfo['deptList'] ?? []);
}
$user = $query->findOrEmpty();
if ($user->isEmpty()) {
throw new ApiException('没有权限操作该数据');
@@ -182,7 +187,10 @@ class SystemUserLogic extends BaseLogic
throw new ApiException('超级管理员禁止删除');
}
$query = $this->model->where('id', $ids);
$query->auth($this->adminInfo['deptList']);
// 超级管理员可删除任意用户,普通管理员仅可删除当前部门和子部门的用户
if (isset($this->adminInfo['id']) && $this->adminInfo['id'] > 1) {
$query->auth($this->adminInfo['deptList'] ?? []);
}
$user = $query->findOrEmpty();
if ($user->isEmpty()) {
throw new ApiException('没有权限操作该数据');

View File

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

View File

@@ -42,11 +42,14 @@ class SystemDept extends BaseModel
*/
public function scopeAuth($query, $value)
{
if (!empty($value)) {
if (!empty($value) && isset($value['id'])) {
$deptIds = [$value['id']];
$deptLevel = $value['level'] . $value['id'] . ',';
$ids = static::whereLike('level', $deptLevel . '%')->column('id');
$deptIds = array_merge($deptIds, $ids);
$level = $value['level'] ?? '';
if ($level !== '' && $level !== null) {
$deptLevel = $level . $value['id'] . ',';
$ids = static::whereLike('level', $deptLevel . '%')->column('id');
$deptIds = array_merge($deptIds, $ids);
}
$query->whereIn('id', $deptIds);
}
}

View File

@@ -14,6 +14,7 @@ use plugin\saiadmin\basic\think\BaseModel;
* sa_system_user 用户表
*
* @property $id
* @property $agent_id 代理标识md5(id)唯一
* @property $username 登录账号
* @property $password 加密密码
* @property $realname 真实姓名
@@ -49,6 +50,30 @@ class SystemUser extends BaseModel
*/
protected $table = 'sa_system_user';
/**
* 插入后:自动填充 agent_id = md5(id),保证唯一
*/
public static function onAfterInsert($model): void
{
$id = $model->getAttr('id');
if ($id !== null && $id !== '') {
$agentId = md5((string) $id);
(new static())->where('id', $id)->update(['agent_id' => $agentId]);
}
}
/**
* 获取 agent_id若未存储则返回 md5(id)
*/
public function getAgentIdAttr($value, $data)
{
if ($value !== null && $value !== '') {
return $value;
}
$id = $data['id'] ?? null;
return $id !== null ? md5((string) $id) : '';
}
public function searchKeywordAttr($query, $value)
{
if ($value) {

View File

@@ -45,19 +45,48 @@ class BaseController extends OpenController
*/
protected function init(): void
{
// 登录模式赋值
$isLogin = request()->header('check_login', false);
if ($isLogin) {
$result = request()->header('check_admin');
// 登录模式赋值:优先从中间件注入的 header 取,否则从 JWT 当前用户取
$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->adminName = $result['username'];
$this->adminName = $result['username'] ?? '';
$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';
/**
* 自动写入时间戳(创建时写 create_time更新时写 update_time
* @var bool
*/
protected $autoWriteTimestamp = true;
/**
* 隐藏字段
* @var array
@@ -94,24 +100,54 @@ class BaseModel extends Model implements ModelInterface
}
/**
* 新增前事件
* 新增前事件:自动写入 create_time有后台登录信息时写入 created_by
* @param Model $model
* @return void
*/
public static function onBeforeInsert($model): void
{
$info = getCurrentInfo();
$info && $model->setAttr('created_by', $info['id']);
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();
if (!empty($info['id'])) {
$model->setAttr('created_by', $info['id']);
}
}
} catch (\Throwable $e) {
}
}
/**
* 写入前事件
* 写入前事件:更新时自动写入 update_time有后台登录信息时写入 updated_by
* @param Model $model
* @return void
*/
public static function onBeforeWrite($model): void
{
$info = getCurrentInfo();
$info && $model->setAttr('updated_by', $info['id']);
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();
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\CheckAuth;
// 仅对 /core 后台路由生效,避免 /api 请求经过登录/权限/操作日志中间件,提升接口响应
return [
'' => [
'core' => [
CheckLogin::class,
CheckAuth::class,
SystemLog::class,

View File

@@ -17,6 +17,10 @@ Route::group('/core', function () {
Route::get('/system/statistics', [plugin\saiadmin\app\controller\SystemController::class, 'statistics']);
Route::get('/system/loginChart', [plugin\saiadmin\app\controller\SystemController::class, 'loginChart']);
Route::get('/system/loginBarChart', [plugin\saiadmin\app\controller\SystemController::class, 'loginBarChart']);
// 大富翁工作台统计(覆盖默认统计)
Route::get('/dice/dashboard/statistics', [\app\dice\controller\DiceDashboardController::class, 'statistics']);
Route::get('/dice/dashboard/rechargeChart', [\app\dice\controller\DiceDashboardController::class, 'rechargeChart']);
Route::get('/dice/dashboard/rechargeBarChart', [\app\dice\controller\DiceDashboardController::class, 'rechargeBarChart']);
Route::get('/system/clearAllCache', [plugin\saiadmin\app\controller\SystemController::class, 'clearAllCache']);
Route::get("/system/getResourceCategory", [plugin\saiadmin\app\controller\SystemController::class, 'getResourceCategory']);
@@ -82,6 +86,26 @@ Route::group('/core', function () {
Route::get("/server/cache", [\plugin\saiadmin\app\controller\system\SystemServerController::class, 'cache']);
Route::post("/server/clear", [\plugin\saiadmin\app\controller\system\SystemServerController::class, 'clear']);
// 大富翁 Dice 模块
fastRoute('dice/config/DiceConfig', \app\dice\controller\config\DiceConfigController::class);
fastRoute('dice/player/DicePlayer', \app\dice\controller\player\DicePlayerController::class);
Route::put('/dice/player/DicePlayer/updateStatus', [\app\dice\controller\player\DicePlayerController::class, 'updateStatus']);
Route::get('/dice/player/DicePlayer/getLotteryConfigOptions', [\app\dice\controller\player\DicePlayerController::class, 'getLotteryConfigOptions']);
Route::get('/dice/player/DicePlayer/getSystemUserOptions', [\app\dice\controller\player\DicePlayerController::class, 'getSystemUserOptions']);
fastRoute('dice/play_record/DicePlayRecord', \app\dice\controller\play_record\DicePlayRecordController::class);
Route::get('/dice/play_record/DicePlayRecord/getPlayerOptions', [\app\dice\controller\play_record\DicePlayRecordController::class, 'getPlayerOptions']);
Route::get('/dice/play_record/DicePlayRecord/getLotteryConfigOptions', [\app\dice\controller\play_record\DicePlayRecordController::class, 'getLotteryConfigOptions']);
Route::get('/dice/play_record/DicePlayRecord/getRewardConfigOptions', [\app\dice\controller\play_record\DicePlayRecordController::class, 'getRewardConfigOptions']);
fastRoute('dice/player_wallet_record/DicePlayerWalletRecord', \app\dice\controller\player_wallet_record\DicePlayerWalletRecordController::class);
Route::get('/dice/player_wallet_record/DicePlayerWalletRecord/getPlayerOptions', [\app\dice\controller\player_wallet_record\DicePlayerWalletRecordController::class, 'getPlayerOptions']);
Route::get('/dice/player_wallet_record/DicePlayerWalletRecord/getPlayerWalletBefore', [\app\dice\controller\player_wallet_record\DicePlayerWalletRecordController::class, 'getPlayerWalletBefore']);
Route::post('/dice/player_wallet_record/DicePlayerWalletRecord/adminOperate', [\app\dice\controller\player_wallet_record\DicePlayerWalletRecordController::class, 'adminOperate']);
fastRoute('dice/player_ticket_record/DicePlayerTicketRecord', \app\dice\controller\player_ticket_record\DicePlayerTicketRecordController::class);
Route::get('/dice/player_ticket_record/DicePlayerTicketRecord/getPlayerOptions', [\app\dice\controller\player_ticket_record\DicePlayerTicketRecordController::class, 'getPlayerOptions']);
fastRoute('dice/reward_config/DiceRewardConfig', \app\dice\controller\reward_config\DiceRewardConfigController::class);
fastRoute('dice/lottery_config/DiceLotteryConfig', \app\dice\controller\lottery_config\DiceLotteryConfigController::class);
Route::get('/dice/lottery_config/DiceLotteryConfig/getOptions', [\app\dice\controller\lottery_config\DiceLotteryConfigController::class, 'getOptions']);
// 数据表维护
Route::get("/database/index", [\plugin\saiadmin\app\controller\system\DataBaseController::class, 'index']);
Route::get("/database/recycle", [\plugin\saiadmin\app\controller\system\DataBaseController::class, 'recycle']);

View File

@@ -17,6 +17,11 @@ class ApiException extends BusinessException
{
public function render(Request $request): ?Response
{
return json(['code' => $this->getCode() ?: 500, 'message' => $this->getMessage()]);
$message = $this->getMessage();
$path = $request->path();
if (str_contains($path, 'api/')) {
$message = \app\api\util\ApiLang::translate($message, $request);
}
return json(['code' => $this->getCode() ?: 500, 'message' => $message]);
}
}

View File

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