This commit is contained in:
unknown 2023-09-19 14:07:28 +08:00
parent 37341885ed
commit 087e72abef
8 changed files with 136 additions and 56 deletions

View File

@ -6,38 +6,52 @@ use app\api\controller\BaseApiController;
use app\common\model\user\User; use app\common\model\user\User;
use app\common\model\user\UserBalance; use app\common\model\user\UserBalance;
use app\common\validate\user\UserBalanceValidate; use app\common\validate\user\UserBalanceValidate;
use think\facade\Db;
use think\facade\Log; use think\facade\Log;
use think\response\Json; use think\response\Json;
class UserBalanceController extends BaseApiController class UserBalanceController extends BaseApiController
{ {
public array $notNeedLogin = ['addBalanceRecord']; //用户余额变更记录
public function addBalanceRecord(): Json public function addBalanceRecord(): Json
{ {
// 获取参数 // 获取参数
$params = (new UserBalanceValidate())->post()->goCheck('add'); $params = (new UserBalanceValidate())->post()->goCheck('add');
//获取用户信息
$user = User::field('id,total_balance')->where('id',$this->userId)->findOrEmpty();
if($user->isEmpty()){
return $this->fail('未找到用户信息');
}
if($params['type'] == 0) {
$total_balance = $user['total_balance'] - $params['amount'];
}else{
$total_balance = $user['total_balance'] + $params['amount'];
}
// 添加数据 // 添加数据
Db::startTrans();
try { try {
$result = UserBalance::create([ UserBalance::create([
'user_id' => $params['user_id'], 'user_id' => $this->userId,
'record_id' => $params['record_id'], 'record_id' => $params['record_id'],
'record_table' => $params['record_table'], 'record_table' => $params['record_table'],
'amount' => $params['amount'], 'amount' => $params['amount'],
'total_amount' => $total_balance,
'type' => $params['type'], 'type' => $params['type'],
'pay_type' => $params['pay_type'], 'pay_type' => $params['pay_type'],
'mark' => $params['mark'], 'mark' => $params['mark'],
'appid' => $this->request->header('appid'), 'appid' => $this->request->header('appid'),
'create_time' => time(), 'create_time' => time(),
]); ]);
if(!empty($result->id)){ User::update([
return $this->success('添加成功'); 'id' => $this->userId,
}else{ 'total_balance' => $total_balance,
return $this->fail('添加失败'); ]);
} Db::commit();
return $this->success('添加成功');
}catch (\Exception $e) { }catch (\Exception $e) {
//记录日志 Db::rollback();
Log::error($e->getMessage()); Log::error($e->getMessage());
return $this->fail('系统错误'); return $this->fail($e->getMessage());
} }
} }
} }

View File

@ -0,0 +1,56 @@
<?php
namespace app\api\controller\user;
use app\api\controller\BaseApiController;
use app\common\model\user\User;
use app\common\model\user\UserIntegral;
use app\common\validate\user\UserIntegralValidate;
use think\facade\Db;
use think\facade\Log;
use think\response\Json;
class UserIntegralController extends BaseApiController
{
//用户余额变更记录
public function addIntegralRecord(): Json
{
// 获取参数
$params = (new UserIntegralValidate())->post()->goCheck('add');
//获取用户信息
$user = User::field('id,total_integral')->where('id',$this->userId)->findOrEmpty();
if($user->isEmpty()){
return $this->fail('未找到用户信息');
}
if($params['type'] == 0) {
$total_integral = $user['total_integral'] - $params['amount'];
}else{
$total_integral = $user['total_integral'] + $params['amount'];
}
// 添加数据
Db::startTrans();
try {
UserIntegral::create([
'user_id' => $this->userId,
'record_id' => $params['record_id'],
'record_table' => $params['record_table'],
'amount' => $params['amount'],
'total_amount' => $total_integral,
'type' => $params['type'],
'mark' => $params['mark'],
'appid' => $this->request->header('appid'),
'create_time' => time(),
]);
User::update([
'id' => $this->userId,
'total_integral' => $total_integral,
]);
Db::commit();
return $this->success('添加成功');
}catch (\Exception $e) {
Db::rollback();
Log::error($e->getMessage());
return $this->fail($e->getMessage());
}
}
}

View File

@ -1,11 +0,0 @@
<?php
namespace app\common\model\user;
use app\common\model\BaseModel;
class ShopMerchant extends BaseModel
{
protected $connection = 'mysql3';
protected $name = 'eb_merchant';
}

View File

@ -1,11 +0,0 @@
<?php
namespace app\common\model\user;
use app\common\model\BaseModel;
class ShopUser extends BaseModel
{
protected $connection = 'mysql3';
protected $name = 'eb_user';
}

View File

@ -0,0 +1,16 @@
<?php
namespace app\common\model\user;
use app\common\model\BaseModel;
class UserIntegral extends BaseModel
{
protected $name = 'user_integral';
public function getTypeTextAttr($value,$data): string
{
$type = [0=>'支出',1=>'收入'];
return $type[$data['type']];
}
}

View File

@ -1,10 +0,0 @@
<?php
namespace app\common\model\user;
use app\common\model\BaseModel;
class UserSystemConnect extends BaseModel
{
protected $name = 'user_system_connect';
}

View File

@ -2,14 +2,12 @@
namespace app\common\validate\user; namespace app\common\validate\user;
use app\common\model\user\User;
use app\common\validate\BaseValidate; use app\common\validate\BaseValidate;
class UserBalanceValidate extends BaseValidate class UserBalanceValidate extends BaseValidate
{ {
protected $rule = [ protected $rule = [
'id' => 'require', 'id' => 'require',
'user_id' => 'require|checkUser',
'record_id' => 'require', 'record_id' => 'require',
'record_table' => 'require', 'record_table' => 'require',
'amount' => 'require|float|gt:0', 'amount' => 'require|float|gt:0',
@ -20,8 +18,6 @@ class UserBalanceValidate extends BaseValidate
protected $message = [ protected $message = [
'id.require' => '缺少数据主键', 'id.require' => '缺少数据主键',
'user_id.require' => '请选择用户',
'user_id.checkUser' => '用户不存在',
'record_id.require' => '请填写本地记录id', 'record_id.require' => '请填写本地记录id',
'record_table.require' => '请填写本地记录表名', 'record_table.require' => '请填写本地记录表名',
'amount.require' => '请填写金额', 'amount.require' => '请填写金额',
@ -36,20 +32,11 @@ class UserBalanceValidate extends BaseValidate
public function sceneAdd(): UserBalanceValidate public function sceneAdd(): UserBalanceValidate
{ {
return $this->only(['user_id','record_id','record_table','amount','type','pay_type','mark']); return $this->only(['record_id','record_table','amount','type','pay_type','mark']);
} }
public function sceneDetail(): UserBalanceValidate public function sceneDetail(): UserBalanceValidate
{ {
return $this->only(['id']); return $this->only(['id']);
} }
public function checkUser($uid): bool
{
$userInfo = User::where('id',$uid)->field('id')->findOrEmpty();
if($userInfo->isEmpty()){
return false;
}
return true;
}
} }

View File

@ -0,0 +1,39 @@
<?php
namespace app\common\validate\user;
use app\common\validate\BaseValidate;
class UserIntegralValidate extends BaseValidate
{
protected $rule = [
'id' => 'require',
'record_id' => 'require',
'record_table' => 'require',
'amount' => 'require|float|gt:0',
'type' => 'require|in:0,1',
'mark' => 'require',
];
protected $message = [
'id.require' => '缺少数据主键',
'record_id.require' => '请填写本地记录id',
'record_table.require' => '请填写本地记录表名',
'amount.require' => '请填写金额',
'amount.float' => '金额数据格式错误',
'amount.gt' => '金额必须大于零',
'type.require' => '请选择金额变更类型',
'type.in' => '金额变更类型值错误',
'mark.require' => '请填写金额变更具体详情',
];
public function sceneAdd(): UserIntegralValidate
{
return $this->only(['record_id','record_table','amount','type','mark']);
}
public function sceneDetail(): UserIntegralValidate
{
return $this->only(['id']);
}
}