首页修改,jk8修改,历史记录相关,api权限

This commit is contained in:
2026-06-04 10:53:45 +08:00
parent 95684c784e
commit 1975d24db3
16 changed files with 565 additions and 96 deletions

View File

@@ -2,19 +2,270 @@
namespace app\admin\controller;
use app\admin\model\MoneyLogHistory;
use app\admin\model\User;
use app\admin\model\UserMoneyLog;
use app\common\controller\Backend;
use app\admin\model\Bank;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;
use Throwable;
class Dashboard extends Backend
{
/**
* @var object
* @phpstan-var Bank
*/
protected object $bank;
public function initialize(): void
{
parent::initialize();
$this->bank = new Bank();
}
public function index(): void
{
$rawStart = request()->param('start/s');
$start = $rawStart ? strtotime($rawStart) : strtotime('today');
$rawEnd = request()->param('end/s');
$end = $rawEnd ? strtotime("$rawEnd +1 day") : strtotime('tomorrow');
if ($start > $end) {
list($start, $end) = [$end, $start];
}
$bankList = Bank::field([
'id', 'bank_name', 'bank_account', 'current_balance', 'safe_alert', 'label_color', 'weigh',
// 子查询:入款总额
"(SELECT SUM(money/100) FROM ba_user_money_log WHERE bank_id = ba_bank.id AND type = 1 and create_time BETWEEN {$start} AND {$end}) AS total_fund_in",
// 子查询:出款总额
"(SELECT SUM(money/100) FROM ba_user_money_log WHERE bank_id = ba_bank.id AND type = 2 and create_time BETWEEN {$start} AND {$end}) AS total_fund_out",
// 子查询:入款次数
"(SELECT COUNT(*) FROM ba_user_money_log WHERE bank_id = ba_bank.id AND type = 1 and create_time BETWEEN {$start} AND {$end}) AS count_fund_in",
// 子查询:出款次数
"(SELECT COUNT(*) FROM ba_user_money_log WHERE bank_id = ba_bank.id AND type = 2 and create_time BETWEEN {$start} AND {$end}) AS count_fund_out",
])
->where('status', 1)
->order('weigh', 'desc')
->order('id', 'desc')
->select()
->toArray();
foreach ($bankList as &$bank) {
$bank['total_fund_in'] = (float)$bank['total_fund_in'];
$bank['total_fund_out'] = (float)$bank['total_fund_out'];
$bank['count_fund_in'] = (int)$bank['count_fund_in'];
$bank['count_fund_out'] = (int)$bank['count_fund_out'];
}
unset($bank);
$customer = Db::name('user_money_log')->field([
// 1. 总进款 (type = 1)
'ROUND(SUM(CASE WHEN type = 1 THEN money/100 ELSE 0 END), 2) AS total_deposit',
// 2. 总出款 (type = 2)
'ROUND(SUM(CASE WHEN type = 2 THEN money/100 ELSE 0 END), 2) AS total_withdraw',
// 3. 总进款次数 (type = 1 的记录总数)
'COUNT(CASE WHEN type = 1 THEN 1 END) AS count_deposit',
// 4. 总出款次数 (type = 2 的记录总数)
'COUNT(CASE WHEN type = 2 THEN 1 END) AS count_withdraw',
// 5. 活跃玩家数 (去重计算有多少个不同的 user_id 产生了流水)
'COUNT(DISTINCT user_id) AS active_player',
//6. 首次充值
'COUNT(CASE WHEN label = 1 THEN 1 END) AS first_deposit',
'ROUND(SUM(CASE WHEN label = 2 THEN money/100 ELSE 0 END), 2) AS unclaim_amount',
'COUNT(CASE WHEN label = 2 THEN 1 END) AS unclaim_receipt',
])
->whereBetweenTime('create_time', $start, $end)
->whereNull('created_by')
->find();
$customerData = [
'total_deposit' => (float)($customer['total_deposit'] ?? 0),
'total_withdraw' => (float)($customer['total_withdraw'] ?? 0),
'count_deposit' => (int)($customer['count_deposit'] ?? 0),
'count_withdraw' => (int)($customer['count_withdraw'] ?? 0),
'active_player' => (int)($customer['active_player'] ?? 0),
'first_deposit' => (int)($customer['first_deposit'] ?? 0),
'unclaim_amount' => (float)($customer['unclaim_amount'] ?? 0),
'unclaim_receipt' => (int)($customer['unclaim_receipt'] ?? 0),
];
$limit = request()->param('limit/d') ?? 15;
$bindFields = [
'user' => ['jk_username'],
'bank' => ['bank_name'],
'admin' => ['id', 'username'],
];
$transaction = UserMoneyLog::withJoin($bindFields, 'left')
->with([
'scoreLog' => function($query) {
$query->field(['money_log_id', 'game_type', 'score']);
}
])
->whereBetweenTime('user_money_log.create_time', $start, $end)
->order('id', 'desc')
->paginate($limit);
$transactionList = $transaction->items();
foreach ($transactionList as &$item) {
$item['user_name'] = $item['user']['jk_username'] ?? '';
$item['bank_name'] = $item['bank']['bank_name'] ?? '';
$item['created_by'] = $item['admin']['username'] ?? 'WEBHOOK';
unset($item['user'], $item['bank'], $item['admin']);
}
unset($item);
$transactionTotal = Db::name('user_money_log')->field([
// 1. 总进款 (type = 1)
'ROUND(SUM(CASE WHEN type = 1 THEN money/100 ELSE 0 END), 2) AS total_fund_in',
// 2. 总出款 (type = 2)
'ROUND(SUM(CASE WHEN type = 2 THEN money/100 ELSE 0 END), 2) AS total_fund_out',
])
->whereBetweenTime('create_time', $start, $end)
->find();
$this->success('', [
'remark' => get_route_remark()
'bank' => $bankList,
'customer' => $customerData,
'transaction'=> [
'list' => $transactionList,
'count' => $transaction->total(),
'current_page' => $transaction->currentPage(),
'last_page' => $transaction->lastPage(),
'total_deposit' => (float)($transactionTotal['total_fund_in'] ?? 0),
'total_withdraw' => (float)($transactionTotal['total_fund_out'] ?? 0),
],
]);
}
public function bankTransact(): void
{
$data = $this->request->post();
if (!$data) {
$this->error(__('Parameter %s can not be empty', ['']));
}
$money = isset($data['money']) ? (int)$data['money'] : 0;
$bankFromId = isset($data['bank_from']) ? (int)$data['bank_from'] : 0;
$bankToId = isset($data['bank_to']) ? (int)$data['bank_to'] : 0;
if ($money <= 0) {
$this->error(__('转账金额必须大于 0'));
}
if ($bankFromId === $bankToId) {
$this->error(__('转出银行和转入银行不能相同'));
}
$adminId = $this->auth->getInfo()['id'] ?? null;
\think\facade\Db::startTrans();
try {
$bankFrom = Bank::where('id', $bankFromId)->where('status', 1)->lock(true)->find();
if (!$bankFrom) {
throw new ValidateException('转出银行账户不存在或已被禁用');
}
if ($bankFrom->current_balance < $money) {
throw new ValidateException("转出银行【{$bankFrom->bank_name}】余额不足");
}
$bankTo = Bank::where('id', $bankToId)->where('status', 1)->lock(true)->find();
if (!$bankTo) {
throw new ValidateException('转入银行账户不存在或已被禁用');
}
// 5. 更新转出银行账户余额
$bankFrom->current_balance -= $money;
$bankFrom->save();
$logFrom = new UserMoneyLog();
$logFrom->save([
'user_id' => 0,
'money' => -$money,
'before' => $bankFrom->current_balance + $money,
'after' => $bankFrom->current_balance,
'type' => 4,
'bank_id' => $bankFromId,
'created_by' => $adminId,
'memo' => $data['remark'],
'category' => 2,
'create_time' => time()
]);
$bankTo->current_balance += $money;
$bankTo->save();
$logTo = new UserMoneyLog();
$logTo->save([
'user_id' => 0,
'money' => $money,
'before' => $bankTo->current_balance - $money,
'after' => $bankTo->current_balance,
'type' => 3,
'bank_id' => $bankToId,
'created_by' => $adminId,
'memo' => $data['remark'],
'category' => 2,
'create_time' => time()
]);
\think\facade\Db::commit();
$isCommitSuccess = true;
} catch (ValidateException $e) {
\think\facade\Db::rollback();
$this->error($e->getMessage());
} catch (Throwable $e) {
\think\facade\Db::rollback();
trace($e->getMessage(), 'error');
$this->error(__('No rows were added'));
}
if ($isCommitSuccess) {
$this->success(__('Added successfully'));
}
}
public function newTransact(): void
{
if ($this->request->isPost()) {
$data = $this->request->post();
if (!$data) {
$this->error(__('Parameter %s can not be empty', ['']));
}
$result = false;
$data = $this->excludeFields($data);
if ($data['type'] == 2) {
$data['money'] = -$data['money'];
}
$user_id = User::where('jk_username', $data['user_name'])->value('id');
if (!$user_id) {
$this->error(__("The user can't find it", ['']));
}
$data['user_id'] = $user_id;
$userMoneyLog = new UserMoneyLog();
$userMoneyLog->startTrans();
try {
$data['created_by'] = $this->auth->getInfo()['id'];
$result = $userMoneyLog->save($data);
$userMoneyLog->commit();
} catch (Throwable $e) {
$userMoneyLog->rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success(__('Added successfully'));
} else {
$this->error(__('No rows were added'));
}
}
}
public function logHistory($id): void
{
$bindFields = [
'admin' => ['id','username'],
];
$history = MoneyLogHistory::withJoin($bindFields, 'left')
->where('money_log_id', $id)
->select();
if (!empty($history)) {
foreach ($history as &$item) {
$item['admin_name'] = $item['admin']['username'] ?? '';
unset($item['admin']);
}
unset($item);
}
$this->success('', $history);
}
}

View File

@@ -2,7 +2,7 @@
namespace app\admin\controller\user;
use app\common\service\Jk8Services;
use app\admin\model\MoneyLogHistory;
use Throwable;
use app\admin\model\User;
use app\admin\model\UserMoneyLog;
@@ -16,19 +16,17 @@ class MoneyLog extends Backend
*/
protected object $model;
protected array $withJoinTable = ['user', 'admin'];
protected array $withJoinTable = ['user', 'admin', 'bank'];
protected array $withTable = ['scoreLog'];
// 排除字段
protected string|array $preExcludeFields = ['create_time'];
protected string|array $quickSearchField = ['user.username', 'user.nickname'];
protected $jk8Services;
public function initialize(): void
{
parent::initialize();
$this->jk8Services = app(Jk8Services::class);
$this->model = new UserMoneyLog();
}
@@ -88,27 +86,13 @@ class MoneyLog extends Backend
$result = false;
$data = $this->excludeFields($data);
$user = User::where('id', $data['user_id'])->find();
if ($data['type'] == 2) {
$data['money'] = -$data['money'];
}
if ($user->money + $data['money'] < 0) {
$this->error(__('Insufficient Credit'));
}
try {
// 模型验证
$this->validateModelData($data, 'add');
$transactionId = $this->jk8Services->setScore($user['jk_username'], $data['money']);
} catch (Throwable $e) {
$this->error($e->getMessage());
}
$this->model->startTrans();
try {
$data['transaction_id'] = $transactionId;
$data['created_by'] = $this->auth->getInfo()['id'];
// $data['type'] = $data['money'] > 0 ? 1 : 2;
$result = $this->model->save($data);
$this->model->commit();
} catch (Throwable $e) {
@@ -143,15 +127,49 @@ class MoneyLog extends Backend
if (!$row) {
$this->error(__('Record not found'));
}
$user = User::where('id', $row['user_id'])->find();
if ($this->request->isPost()) {
parent::edit();
$data = $this->request->post();
if (!$data) {
$this->error(__('Parameter %s can not be empty', ['']));
}
$result = false;
$this->model->startTrans();
try {
$result = $row->save($data);
$this->model->commit();
} catch (Throwable $e) {
$this->model->rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success(__('Update successful'));
} else {
$this->error(__('No rows updated'));
}
return;
}
$row['username'] = $user['username'];
$row['nickname'] = $user['nickname'];
$this->success('', [
'row' => $row
]);
}
public function logHistory($id): void
{
$bindFields = [
'admin' => ['id','username'],
];
$history = MoneyLogHistory::withJoin($bindFields, 'left')
->where('money_log_id', $id)
->select();
if (!empty($history)) {
foreach ($history as &$item) {
$item['admin_name'] = $item['admin']['username'] ?? '';
unset($item['admin']);
}
unset($item);
}
$this->success('', $history);
}
}

View File

@@ -3,6 +3,7 @@
namespace app\admin\model;
use think\Model;
use think\model\relation\HasMany;
/**
* Bank
@@ -46,4 +47,9 @@ class Bank extends Model
{
return is_null($value) ? null : (float)$value;
}
public function MoneyLog(): hasMany
{
return $this->hasMany(UserMoneyLog::class, 'bank_id');
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace app\admin\model;
use think\model\relation\HasMany;
use Throwable;
use think\model;
use think\Exception;
use think\model\relation\BelongsTo;
/**
* MoneyLogHistory 模型
*/
class MoneyLogHistory extends model
{
protected $autoWriteTimestamp = true;
protected $updateTime = false;
public static function onBeforeDelete(): bool
{
return false;
}
public function moneyLog(): BelongsTo
{
return $this->belongsTo(UserMoneyLog::class, 'money_log_id');
}
public function admin(): BelongsTo
{
return $this->belongsTo(Admin::class, 'created_by');
}
}

View File

@@ -2,10 +2,10 @@
namespace app\admin\model;
use app\admin\library\Auth;
use think\model\relation\HasMany;
use Throwable;
use think\model;
use think\Exception;
use think\model\relation\BelongsTo;
/**
@@ -22,23 +22,29 @@ class UserMoneyLog extends model
* 入库前
* @throws Throwable
*/
public static function onBeforeInsert($model): void
// public static function onAfterInsert($model): void
// {
// $new = new MoneyLogHistory();
// $new->money_log_id = $model->id;
// $new->bank_befter = '';
// $new->bank_after = $model->bank->bank_name;
// $new->create_time = time();
// $new->save();
//
// }
public static function onAfterUpdate($model): void
{
$user = User::where('id', $model->user_id)->lock(true)->find();
if (!$user) {
throw new Exception("The user can't find it");
}
if (!$model->memo) {
throw new Exception("Change note cannot be blank");
}
$model->before = $user->money;
$history = MoneyLogHistory::where('money_log_id', $model->id)->find();
$new = new MoneyLogHistory();
$new->money_log_id = $model->id;
$new->bank_befter = $history->bank_after ?? '';
$new->bank_after = $model->bank->bank_name;
$new->created_by = Auth::instance()->getInfo()['id'];
$new->create_time = time();
$new->save();
$user->money += $model->money;
$user->save();
$model->after = $user->money;
}
public static function onBeforeDelete(): bool
{
return false;
@@ -88,4 +94,9 @@ class UserMoneyLog extends model
{
return $this->hasMany(UserScoreLog::class, 'money_log_id');
}
public function bank(): BelongsTo
{
return $this->belongsTo(Bank::class, 'bank_id');
}
}

View File

@@ -17,6 +17,13 @@ class UserScoreLog extends model
protected $autoWriteTimestamp = true;
protected $updateTime = false;
const GAME_TYPE_LIST = [
1001 => 'Lucky Wheel', // 大转盘
1002 => 'Golden Eggs', // 砸金蛋
1003 => 'Daily Mission', // 每日任务
1004 => 'Plinko Ball', // 普林科弹球
];
public static function onBeforeDelete(): bool
{
return false;
@@ -26,4 +33,17 @@ class UserScoreLog extends model
{
return $this->belongsTo(User::class, 'user_id');
}
public function getGameTypeTextAttr($value, $data)
{
$gameType = $data['game_type'] ?? 0;
// 如果需要严格的多语言,可以在这里根据 $lang 切换,或者直接配合系统的 lang() 助手函数
// 这里我们先直接返回对应的标准国际化名称
return self::GAME_TYPE_LIST[$gameType] ?? 'Unknown Game';
}
// 3. 别忘了把获取器追加到模型的可见输出数组里
protected $append = ['game_type_text'];
}