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('请输入正确的车站动态码。车站码位于车站站牌证明,轻按按钮即可显示。'); } }
/** * 个人信息 * * @return View */ public function profile() { if (empty($this->user->state)) { return redirect()->action('IndexController@redirect'); } // 查看个人信息 $response = view('index.profile')->with('user', $this->user); // 获取系统公告 /* Cache::forever('tip', [ 'type' => 'info', 'message' => '踏鸽行 2.0 正在研发中……', ]);*/ // 系统公告每个会话提示3次 $tipShown = session('tip_shown', 0); if ($tipShown < 3) { session(['tip_shown' => $tipShown + 1]); $response->with('tip', Cache::get('tip')); } // 随机昵称 $nick = ['童鞋', '小盆友', '小伙伴', '小公举']; if (in_array($this->user->state, ['normal', 'rented', 'disabled'])) { if ($this->user->gender == 'male') { $nick = array_merge($nick, ['小帅哥', '大哥哥', '汉纸']); } else { $nick = array_merge($nick, ['小公主', '大美女', '女神']); } if (preg_match('/(信息|计算机|软件)/', $this->user->department)) { $nick = array_merge($nick, ['程序猿', '工程狮']); if ($this->user->gender == 'male') { $nick = array_merge($nick, ['好男人']); } else { $nick = array_merge($nick, ['程序媛']); } } $response->withRank(Rank::fromScore($this->user->score)->first()); } switch ($this->user->state) { case 'normal': $rent = $this->user->rent()->lastRent()->first(); $return = $this->user->rent()->lastReturn()->first(); if ($return && $return->created_at->diffInMinutes() < 5) { $response->with('unlockPassword', $rent->password); $response->with('lockPassword', $return->password); } break; case 'rented': $rent = $this->user->rent()->lastRent()->first(); if ($rent->created_at->diffInMinutes() < 5) { // 借车5分钟内可以报告借车问题,直接重新借车 $response->withReport(true); } $returnTime = $rent->created_at->addHours($rent->max_time); $dateInterval = $returnTime->diff(Carbon::now()); $response->with('rent', $rent); $response->with('returnTime', $returnTime); $response->with('interval', $dateInterval); break; } return $response->with('nick', $nick[rand(0, count($nick) - 1)])->with('stops', Stop::with('bikes')->get()->sortBy(function ($stop) { return $stop->bikes->count(); }, null, true)); }