From 30d98018584a8bf7dfe67e7330eb7ded079d82a4 Mon Sep 17 00:00:00 2001 From: gaofeng <1212121@qq.com> Date: Wed, 13 May 2026 10:23:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/home/controller/Login.php | 92 +++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 app/home/controller/Login.php diff --git a/app/home/controller/Login.php b/app/home/controller/Login.php new file mode 100644 index 0000000..317b568 --- /dev/null +++ b/app/home/controller/Login.php @@ -0,0 +1,92 @@ +request->post(); + try { + $this->validate($param, ['mobile' => 'require'], ['mobile.require' => '手机号不能为空']); + } catch (ValidateException $e) { + return $this->error($e->getError()); + } + $code = mt_rand(1001, 9999); + //进行发送验证 走接口 + $res = Httpcurl::request(config('app.YZM_URL'), 'post', [ + 'mobile' => $param['mobile'], + 'code' => $code, + ]); + $result = json_decode($res[0], true); + Log::info(json_encode($result)); + if (!$result['code']) { + return $this->error('发送验证码失败'); + } + //进行记录 + if ($result['code'] == 1) { + //把内容存入session + Session::set($param['mobile'] . '_code', $code); + Session::set($param['mobile'] . '_' . $code, time()); + return $this->success('发送成功'); + } else { + return $this->error('发送失败'); + } + + } + + // 登录 + public function login() + { + $param = $this->request->post(); + try { + $this->validate($param, ['phone' => 'require', 'vercode' => 'require'], ['phone.require' => '手机号不能为空', 'vercode.require' => '验证码不能为空']); + } catch (ValidateException $e) { + return $this->error($e->getError()); + } + $mobile = $param['phone']; + $code = $param['vercode']; + $res = 0; + $sms = Session::get($mobile . '_code'); + if (empty($sms)) { + return $this->error('登录失败'); + } + if ($sms != $code) { + return $this->error('验证码不正确'); + } + $time = time(); + $code_time = Session::get($mobile . '_' . $code); + //判断是否已经超时5分钟 + if (($time - $code_time) > 300) { + Session::delete($mobile . '_code'); + Session::delete($mobile . '_' . $code); + return $this->error('验证码超时,请重新发送'); + } + //查询账号 + $user_url = config('app.user_url'); + $res = Httpcurl::request($user_url, 'post', [ + 'model' => MODEL, + 'mobile' => $mobile, + ]); + $result = json_decode($res[0], true); + Log::info(json_encode($result)); + if (!$result['code']) { + return $this->error('账号不存在'); + } + Session::set('user', $result['data']); + return $this->success('登录成功', url('user/index')); + + } + + public function logout() + { + Session::delete('user'); + return redirect('/home/index'); + } +}