钱包修改

This commit is contained in:
2026-04-17 11:53:19 +08:00
parent 7f3a7c34f0
commit ad74accfcc
10 changed files with 136 additions and 24 deletions

View File

@@ -4,6 +4,7 @@ namespace app\admin\model;
use think\Model;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;
/**
* User 模型
@@ -49,4 +50,9 @@ class User extends Model
{
return $this->where(['id' => $uid])->update(['password' => hash_password($newPassword), 'salt' => '']);
}
public function userScore(): HasMany
{
return $this->hasMany(UserScore::class, 'user_id');
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace app\admin\model;
use think\Exception;
use think\model;
use think\model\relation\BelongsTo;
use Throwable;
use app\admin\model\UserScoreLog;
/**
* UserScoreLog 模型
* 1. 创建积分日志自动完成会员积分的添加
* 2. 创建积分日志时,请开启事务
*/
class UserScore extends model
{
protected $updateTime = false;
/**
* 更新前
* @throws Throwable
*/
public static function onBeforeUpdate($model): void
{
$before = $model->getOrigin('score');
$after = $model->score;
// 1. 只有当分数确实发生变化时才记录日志
if (bccomp($before, $after, 2) !== 0) {
// 计算分值变动after - before
$change = bcsub($after, $before);
$userScoreLog = new UserScoreLog();
$userScoreLog->save([
'user_id' => $model->user_id,
'game_type' => $model->game_type,
'before' => $before,
'after' => $after,
'score' => $change,
'memo' => '管理员手动调整', // 建议增加备注字段区分来源
]);
}
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
}

View File

@@ -17,27 +17,6 @@ class UserScoreLog extends model
protected $autoWriteTimestamp = true;
protected $updateTime = false;
/**
* 入库前
* @throws Throwable
*/
public static function onBeforeInsert($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->score;
$user->score += $model->score;
$user->save();
$model->after = $user->score;
}
public static function onBeforeDelete(): bool
{
return false;