[色子游戏]玩家-新增玩家钱包操作
This commit is contained in:
@@ -13,6 +13,7 @@ use app\dice\model\player\DicePlayer;
|
||||
use plugin\saiadmin\service\Permission;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use Tinywan\Jwt\JwtToken;
|
||||
|
||||
/**
|
||||
* 玩家钱包流水控制器
|
||||
@@ -48,6 +49,7 @@ class DicePlayerWalletRecordController extends BaseController
|
||||
$query = $this->logic->search($where);
|
||||
$query->with([
|
||||
'dicePlayer',
|
||||
'operator',
|
||||
]);
|
||||
$data = $this->logic->getList($query);
|
||||
return $this->success($data);
|
||||
@@ -106,6 +108,61 @@ class DicePlayerWalletRecordController extends BaseController
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员钱包操作(加点/扣点):更新玩家平台币并创建流水记录
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
#[Permission('玩家钱包流水添加', 'dice:player_wallet_record:index:save')]
|
||||
public function adminOperate(Request $request): Response
|
||||
{
|
||||
$data = $request->post();
|
||||
$playerId = $data['player_id'] ?? null;
|
||||
$type = isset($data['type']) ? (int) $data['type'] : null;
|
||||
$coin = isset($data['coin']) ? (float) $data['coin'] : null;
|
||||
|
||||
if ($playerId === null || $playerId === '') {
|
||||
return $this->fail('请选择玩家');
|
||||
}
|
||||
if (!in_array($type, [3, 4], true)) {
|
||||
return $this->fail('操作类型必须为 3=加点 或 4=扣点');
|
||||
}
|
||||
if ($coin === null || $coin <= 0) {
|
||||
return $this->fail('平台币变动必须大于 0');
|
||||
}
|
||||
|
||||
$data['player_id'] = $playerId;
|
||||
$data['type'] = $type;
|
||||
$data['coin'] = $coin;
|
||||
$data['remark'] = $data['remark'] ?? '';
|
||||
|
||||
$adminId = null;
|
||||
$checkAdmin = request()->header('check_admin');
|
||||
if (!empty($checkAdmin['id'])) {
|
||||
$adminId = (int) $checkAdmin['id'];
|
||||
}
|
||||
if (($adminId === null || $adminId <= 0)) {
|
||||
try {
|
||||
$token = JwtToken::getExtend();
|
||||
if (!empty($token['id']) && ($token['plat'] ?? '') === 'saiadmin') {
|
||||
$adminId = (int) $token['id'];
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
// JWT 无效或未携带
|
||||
}
|
||||
}
|
||||
if ($adminId === null || $adminId <= 0) {
|
||||
return $this->fail('请先登录');
|
||||
}
|
||||
|
||||
try {
|
||||
$this->logic->adminOperate($data, $adminId);
|
||||
return $this->success('操作成功');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->fail($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存数据
|
||||
* @param Request $request
|
||||
|
||||
@@ -8,8 +8,8 @@ namespace app\dice\logic\player_wallet_record;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\utils\Helper;
|
||||
use app\dice\model\player_wallet_record\DicePlayerWalletRecord;
|
||||
use app\dice\model\player\DicePlayer;
|
||||
|
||||
/**
|
||||
* 玩家钱包流水逻辑层
|
||||
@@ -35,4 +35,57 @@ class DicePlayerWalletRecordLogic extends BaseLogic
|
||||
return parent::add($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员钱包操作(加点/扣点):更新玩家平台币并创建流水记录
|
||||
* @param array $data player_id, type (3=加点 4=扣点), coin (正数), remark (可选)
|
||||
* @param int $adminId 当前管理员 id
|
||||
* @return mixed
|
||||
* @throws ApiException
|
||||
*/
|
||||
public function adminOperate(array $data, int $adminId): mixed
|
||||
{
|
||||
$playerId = (int) ($data['player_id'] ?? 0);
|
||||
$type = (int) ($data['type'] ?? 0);
|
||||
$coin = (float) ($data['coin'] ?? 0);
|
||||
|
||||
if ($playerId <= 0 || !in_array($type, [3, 4], true)) {
|
||||
throw new ApiException('参数错误:需要有效的 player_id 和 type(3=加点,4=扣点)');
|
||||
}
|
||||
if ($coin <= 0) {
|
||||
throw new ApiException('平台币变动必须大于 0');
|
||||
}
|
||||
|
||||
$player = DicePlayer::where('id', $playerId)->find();
|
||||
if (!$player) {
|
||||
throw new ApiException('玩家不存在');
|
||||
}
|
||||
|
||||
$walletBefore = (float) ($player['coin'] ?? 0);
|
||||
if ($type === 4 && $walletBefore < $coin) {
|
||||
throw new ApiException('扣点数量不能大于当前余额');
|
||||
}
|
||||
|
||||
$walletAfter = $type === 3 ? $walletBefore + $coin : $walletBefore - $coin;
|
||||
$remark = trim((string) ($data['remark'] ?? ''));
|
||||
if ($remark === '') {
|
||||
$remark = $type === 3 ? '管理员加点' : '管理员扣点';
|
||||
}
|
||||
|
||||
DicePlayer::where('id', $playerId)->update(['coin' => $walletAfter]);
|
||||
|
||||
$record = [
|
||||
'player_id' => $playerId,
|
||||
'coin' => $type === 3 ? $coin : -$coin,
|
||||
'type' => $type,
|
||||
'wallet_before' => $walletBefore,
|
||||
'wallet_after' => $walletAfter,
|
||||
'remark' => $remark,
|
||||
'user_id' => $adminId,
|
||||
'total_draw_count' => 0,
|
||||
'paid_draw_count' => 0,
|
||||
'free_draw_count' => 0,
|
||||
];
|
||||
|
||||
return $this->model->create($record);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ namespace app\dice\model\player_wallet_record;
|
||||
|
||||
use app\dice\model\player\DicePlayer;
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use plugin\saiadmin\app\model\system\SystemUser;
|
||||
use think\model\relation\BelongsTo;
|
||||
|
||||
/**
|
||||
* 玩家钱包流水模型
|
||||
@@ -24,6 +26,7 @@ use plugin\saiadmin\basic\think\BaseModel;
|
||||
* @property $paid_draw_count 购买抽奖次数
|
||||
* @property $free_draw_count 赠送抽奖次数
|
||||
* @property $remark 备注
|
||||
* @property $user_id 操作管理员id(type 3/4 时记录)
|
||||
* @property $create_time 创建时间
|
||||
* @property $update_time 修改时间
|
||||
*/
|
||||
@@ -44,11 +47,19 @@ class DicePlayerWalletRecord extends BaseModel
|
||||
/**
|
||||
* 关联模型 dicePlayer
|
||||
*/
|
||||
public function dicePlayer(): \think\model\relation\BelongsTo
|
||||
public function dicePlayer(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(DicePlayer::class, 'player_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联操作管理员(type 3/4 时有值)
|
||||
*/
|
||||
public function operator(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SystemUser::class, 'user_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 类型 搜索
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user