public function postReturn(Request $request, $stopId) { if (OTPCheck::check(Stop::find($stopId)->code, $request->input('code'))) { $rent = $this->user->rent()->lastRent()->first(); $bike = $rent->bike; // 生成密码 $password = ""; for ($i = 0; $i < 5; ++$i) { $password .= rand(0, 9); } // 修改车辆状态 if ($bike->state == 'rented') { $bike->state = 'normal'; } $bike->stop_id = $stopId; $bike->password = $password; $bike->save(); // 记录Rent(Return) $return = new Rent(); $return->type = 'return'; $return->user_id = $this->user->id; $return->max_time = $rent->max_time; $return->bike_id = $bike->id; $return->stop_id = $stopId; $return->password = $password; $return->save(); // 更新分值 $score = new Score(); $score->user_id = $this->user->id; // 分值计算 $scoreDiff = 0; $returnTime = $rent->created_at->addHours($rent->max_time); if ($returnTime->lt(Carbon::now())) { // 超时 $overHour = $returnTime->diffInHours(); // 基础扣5分,每2小时多扣1分 $scoreDiff = -5 - floor($overHour / 2); $score->reason = '还车超时' . $overHour . '小时,扣分'; } else { // 成功还车+2分 $scoreDiff = 2; $score->reason = '成功按时还车!加分'; } $score->score = $scoreDiff; $score->save(); // 更新user $this->user->state = 'normal'; $this->user->score = $this->user->score + $scoreDiff; $this->user->total += $rent->created_at->diffInSeconds(); $this->user->save(); // 成功 return view('return.success')->withTip($score->reason . $scoreDiff)->withPassword($password); } else { return view('errors.error')->withTitle('车站码错误')->withError('请输入正确的车站动态码。车站码位于车站站牌证明,轻按按钮即可显示。'); } }
public function postRent(Request $request, $stopId, $bikeId) { if (OTPCheck::check(Stop::find($stopId)->code, $request->input('code'))) { $bike = Bike::find($bikeId); if ($stopId != $bike->stop_id) { return view('errors.error')->withTitle('借车失败')->withError('该车不在您选定的车站,请重试!'); } $stop = Stop::find($stopId); $rank = Rank::fromScore($this->user->score)->first(); // 检查车辆状态 if ($bike->state == 'rented') { return view('errors.error')->withTitle('借车失败')->withError('真不巧,这辆车已被其他童鞋抢先借出。'); } else { if ($bike->state != 'normal') { return view('errors.error')->withTitle('借车失败')->withError('对不起,这辆车已经报修,请重新选择车辆。'); } } // 本次借车与上次还车时间间隔应当在20分钟以上 $lastRent = $this->user->rent()->lastReturn()->first(); if ($lastRent) { $diffMinutes = $lastRent->created_at->diffInMinutes(Carbon::now()); if ($diffMinutes < 20) { return view('errors.error')->withTitle('借车失败')->withError('本次借车与上次还车时间间隔应在20分钟以上(含20分钟),您还需等待 ' . (20 - $diffMinutes) . ' 分钟'); } } // 记录Rent $rent = new Rent(); $rent->type = 'rent'; $rent->user_id = $this->user->id; $rent->max_time = Cache::get('special_time', false) ? $rank->max_time_special : $rank->max_time; $rent->bike_id = $bikeId; $rent->stop_id = $stopId; $rent->password = $bike->password; $rent->save(); // 更新user $this->user->state = 'rented'; $this->user->save(); // 更新bike $bike->state = 'rented'; $bike->stop_id = null; $bike->save(); // 成功 return view('rent.success')->withPassword($rent->password); } else { return view('errors.error')->withTitle('车站码错误')->withError('请输入正确的车站动态码。车站码位于车站站牌证明,轻按按钮即可显示。'); } }