钱包修改
This commit is contained in:
@@ -6,6 +6,7 @@ use app\common\service\Jk8Services;
|
||||
use Throwable;
|
||||
use app\common\controller\Backend;
|
||||
use app\admin\model\User as UserModel;
|
||||
use app\admin\model\UserScore;
|
||||
|
||||
class User extends Backend
|
||||
{
|
||||
@@ -14,6 +15,7 @@ class User extends Backend
|
||||
* @phpstan-var UserModel
|
||||
*/
|
||||
protected object $model;
|
||||
protected object $score;
|
||||
|
||||
protected array $withJoinTable = ['userGroup'];
|
||||
protected $jk8Services;
|
||||
@@ -28,6 +30,7 @@ class User extends Backend
|
||||
parent::initialize();
|
||||
$this->jk8Services = app(Jk8Services::class);
|
||||
$this->model = new UserModel();
|
||||
$this->score = new UserScore();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -170,4 +173,55 @@ class User extends Backend
|
||||
'remark' => get_route_remark(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 钱包
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function wallet(): void
|
||||
{
|
||||
$miniGames = config('mini_game') ?: [];
|
||||
$pk = $this->model->getPk();
|
||||
$id = $this->request->param($pk);
|
||||
|
||||
if ($this->request->isPost()) {
|
||||
$this->model = new UserScore();
|
||||
parent::edit();
|
||||
return;
|
||||
}
|
||||
|
||||
$row = $this->model->find($id);
|
||||
if (!$row) {
|
||||
$this->error(__('Record not found'));
|
||||
}
|
||||
$existGameTypes = $row->userScore()
|
||||
->whereIn('game_type', array_keys($miniGames))
|
||||
->column('game_type');
|
||||
|
||||
$scoreInsert = [];
|
||||
foreach ($miniGames as $key => $name) {
|
||||
if (!in_array($key, $existGameTypes)) {
|
||||
$scoreInsert[] = [
|
||||
'user_id' => $id,
|
||||
'game_type' => $key,
|
||||
'score' => 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($scoreInsert)) {
|
||||
$row->userScore()->saveAll($scoreInsert);
|
||||
}
|
||||
|
||||
$data = [
|
||||
'mini_game' => $miniGames,
|
||||
'score' => $row->userScore()
|
||||
->whereIn('game_type', array_keys($miniGames))
|
||||
->withoutField(['user_id'])
|
||||
->order('game_type', 'asc')
|
||||
->select()
|
||||
];
|
||||
$this->success('', $data);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
47
app/admin/model/UserScore.php
Normal file
47
app/admin/model/UserScore.php
Normal 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');
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user