diff --git a/app/api/controller/user/UserBalanceController.php b/app/api/controller/user/UserBalanceController.php index 6011972b..441d467e 100644 --- a/app/api/controller/user/UserBalanceController.php +++ b/app/api/controller/user/UserBalanceController.php @@ -6,38 +6,52 @@ use app\api\controller\BaseApiController; use app\common\model\user\User; use app\common\model\user\UserBalance; use app\common\validate\user\UserBalanceValidate; +use think\facade\Db; use think\facade\Log; use think\response\Json; class UserBalanceController extends BaseApiController { - public array $notNeedLogin = ['addBalanceRecord']; + //用户余额变更记录 public function addBalanceRecord(): Json { // 获取参数 $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 { - $result = UserBalance::create([ - 'user_id' => $params['user_id'], + UserBalance::create([ + 'user_id' => $this->userId, 'record_id' => $params['record_id'], 'record_table' => $params['record_table'], 'amount' => $params['amount'], + 'total_amount' => $total_balance, 'type' => $params['type'], 'pay_type' => $params['pay_type'], 'mark' => $params['mark'], 'appid' => $this->request->header('appid'), 'create_time' => time(), ]); - if(!empty($result->id)){ - return $this->success('添加成功'); - }else{ - return $this->fail('添加失败'); - } + User::update([ + 'id' => $this->userId, + 'total_balance' => $total_balance, + ]); + Db::commit(); + return $this->success('添加成功'); }catch (\Exception $e) { - //记录日志 + Db::rollback(); Log::error($e->getMessage()); - return $this->fail('系统错误'); + return $this->fail($e->getMessage()); } } } \ No newline at end of file diff --git a/app/api/controller/user/UserIntegralController.php b/app/api/controller/user/UserIntegralController.php new file mode 100644 index 00000000..56e3b20c --- /dev/null +++ b/app/api/controller/user/UserIntegralController.php @@ -0,0 +1,56 @@ +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()); + } + } +} \ No newline at end of file diff --git a/app/common/model/user/ShopMerchant.php b/app/common/model/user/ShopMerchant.php deleted file mode 100644 index b80805ea..00000000 --- a/app/common/model/user/ShopMerchant.php +++ /dev/null @@ -1,11 +0,0 @@ -'支出',1=>'收入']; + return $type[$data['type']]; + } +} \ No newline at end of file diff --git a/app/common/model/user/UserSystemConnect.php b/app/common/model/user/UserSystemConnect.php deleted file mode 100644 index aa5ffa1b..00000000 --- a/app/common/model/user/UserSystemConnect.php +++ /dev/null @@ -1,10 +0,0 @@ - 'require', - 'user_id' => 'require|checkUser', 'record_id' => 'require', 'record_table' => 'require', 'amount' => 'require|float|gt:0', @@ -20,8 +18,6 @@ class UserBalanceValidate extends BaseValidate protected $message = [ 'id.require' => '缺少数据主键', - 'user_id.require' => '请选择用户', - 'user_id.checkUser' => '用户不存在', 'record_id.require' => '请填写本地记录id', 'record_table.require' => '请填写本地记录表名', 'amount.require' => '请填写金额', @@ -36,20 +32,11 @@ class UserBalanceValidate extends BaseValidate 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 { return $this->only(['id']); } - - public function checkUser($uid): bool - { - $userInfo = User::where('id',$uid)->field('id')->findOrEmpty(); - if($userInfo->isEmpty()){ - return false; - } - return true; - } } \ No newline at end of file diff --git a/app/common/validate/user/UserIntegralValidate.php b/app/common/validate/user/UserIntegralValidate.php new file mode 100644 index 00000000..97fcee72 --- /dev/null +++ b/app/common/validate/user/UserIntegralValidate.php @@ -0,0 +1,39 @@ + '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']); + } +} \ No newline at end of file