From 7ea2b5e91762b4c291bc553dad47441a081fbdd9 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Wed, 30 Aug 2023 15:36:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=90=86=E7=99=BB=E9=99=86=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/Passport.php | 53 +++++++++++++++++++++++++++++++++++++ app/model/AgentUser.php | 46 +++++++++++++++++++++++++++++++- app/validate/User.php | 3 ++- route/app.php | 1 + 4 files changed, 101 insertions(+), 2 deletions(-) diff --git a/app/controller/Passport.php b/app/controller/Passport.php index ec6b35f..a928c71 100644 --- a/app/controller/Passport.php +++ b/app/controller/Passport.php @@ -5,6 +5,7 @@ namespace app\controller; use app\BaseController; use app\common\lib\sms\AliSms\AliSms; use app\model\AdminUser; +use app\model\AgentUser; use app\model\Pincode; use app\model\User as UserModel; use app\validate\User as UserValidate; @@ -253,4 +254,56 @@ class Passport extends BaseController return $this->renderError($exception->getMessage(),$data); } } + + /** + * 代理登陆 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author whj + * @date 2023-08-30 15:23 + */ + public function AgentLogin() + { + $data = Request::param(); + + $count = 0; + $defaultCount = 3;# 默认登陆三次提示验证码 + try { + + $cookie_name = 'login_count'.$_SERVER['HTTP_HOST']; + // 验证用户输入 + validate(UserValidate::class)->scene('agentLogin')->check($data); + + # 验证码验证 + if ($count = Cookie::get($cookie_name) ?: 0) { + Cookie::set($cookie_name,$count+1); + } else { + Cookie::set($cookie_name,1); + } + if ($count > $defaultCount) { + $this->validate($data,['captcha|验证码'=>'require|captcha']); + } + + // 管理员登录 + $agentUser = AgentUser::login($data); + + if ($agentUser['status'] == 1) { + + $userinfo = ['agent_user_id' => $agentUser['data']['id'], 'avatar' => get_image_url($agentUser['data']['avatar'])]; + $token = ['token'=>signToken($userinfo)]; + + Cookie::delete($cookie_name); + return $this->renderSuccess('登陆成功',$token); + } else { + throw new ValidateException($agentUser['msg']); + } + } catch (ValidateException $exception) { + + $data = ['captcha_img' => '']; + if ($count >= $defaultCount) $data['captcha_img'] = captcha_src(); + return $this->renderError($exception->getMessage(),$data); + } + } } \ No newline at end of file diff --git a/app/model/AgentUser.php b/app/model/AgentUser.php index c637392..a4d858e 100644 --- a/app/model/AgentUser.php +++ b/app/model/AgentUser.php @@ -3,6 +3,7 @@ declare (strict_types = 1); namespace app\model; +use think\facade\Session; use think\Model; /** @@ -12,10 +13,53 @@ class AgentUser extends Model { // - public static function login() + /** + * 代理登陆 + * @param $data + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public static function login($data) { + // 根据用户名查询用户信息 + $AgentUser = new AgentUser(); + $user = $AgentUser + ->where('phone', $data['phone']) + ->field('id,phone,avatar,password,salt,status') + ->find(); + try { + if (!$user) throw new \Exception('代理账号不存在'); + if ($user->status != 1) throw new \Exception('账号已被停用'); + + // 使用相同的盐值对输入密码进行哈希验证 + $hashedPassword = $AgentUser->generateHashedPassword($data['password'], $user->salt); + + if ($user->password !== $hashedPassword) throw new \Exception('密码错误'); + + # 缓存用户信息 + $login_user_data = $user->toArray(); + unset($login_user_data['password'],$login_user_data['salt'],$login_user_data['status']); + Session::set('login_user_data',$login_user_data); + + return ['status' => 1, 'msg' => '登陆成功', 'data' => $login_user_data]; + } catch (\Exception $e) { + return ['status' => 0, 'msg' => $e->getMessage()]; + } + } + + /** + * 生成密码 + * @param $password + * @param $salt + * @return string + */ + private function generateHashedPassword($password,$salt) + { + return md5(md5($password) . md5($salt)); } } diff --git a/app/validate/User.php b/app/validate/User.php index 4981a9f..418fb8e 100644 --- a/app/validate/User.php +++ b/app/validate/User.php @@ -35,7 +35,8 @@ class User extends Validate 'retrieve' => ['phone','password','sms_code'], 'modifyPassword' => ['password'], 'sendCode' => ['phone'], - 'adminLogin'=> ['account_number','password'] + 'adminLogin'=> ['account_number','password'], + 'agentLogin'=> ['phone','password'], ]; /** diff --git a/route/app.php b/route/app.php index 0aa56cc..009892a 100644 --- a/route/app.php +++ b/route/app.php @@ -27,6 +27,7 @@ Route::group('passport',function (){ Route::post('changeCaptcha','passport/changeCaptcha')->allowCrossDomain(); Route::post('sendCode','passport/sendCode')->allowCrossDomain(); Route::post('adminLogin','passport/adminLogin')->allowCrossDomain(); + Route::post('agentLogin','passport/agentLogin')->allowCrossDomain(); }); Route::group('user',function (){