Browse Source

代理登陆接口

master
wanghongjun 3 years ago
parent
commit
7ea2b5e917
  1. 53
      app/controller/Passport.php
  2. 46
      app/model/AgentUser.php
  3. 3
      app/validate/User.php
  4. 1
      route/app.php

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

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

3
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'],
];
/**

1
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 (){

Loading…
Cancel
Save