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()); } } }