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 { 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(), ]); 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($e->getMessage()); } } }