HighSpeaker/.history/app/controller/login_20221223205310.php

54 lines
1.6 KiB
PHP
Raw Normal View History

2022-12-24 19:40:40 +05:30
<?php
namespace app\controller;
use support\Request;
use Webman\Captcha\CaptchaBuilder;
use support\View;
use Respect\Validation\Validator as v;
use support\Redis;
class login
{
public function index(Request $request)
{
$to=$request->get('to', '/');
View::assign('to', $to);
return view('login');
}
public function check(Request $request)
{
$name = $request->post('name','0');
$password = $request->post('password','0');
$usernameValidator = v::StringType()->noWhitespace()->length(1, 15);
try {
$usernameValidator->setName('用户名')->check($name);
} catch (ValidationException $exception) {
return json(['code'=>500, 'msg'=>$e->getMessage()]);
}
try {
$usernameValidator->setName('密码')->check($password);
} catch (ValidationException $exception) {
return json(['code'=>500, 'msg'=>$e->getMessage()]);
}
$redis = Redis::connection('default');
$userinfo=$redis->hget('User',"$name");
if($userinfo===false){
return json(['code'=>501, 'msg'=>'No Such Username']);
}
$realpass=json_decode($userinfo)->passwd;
if(hash("sha256", $password)==$realpass){
$session = $request->session();
$session->set('userinfo', json_decode($userinfo));
$to = $request->post('to','/admin');
return json(['code'=>200, 'msg'=>'Login Successfully']);
}else{
return json(['code'=>502, 'msg'=>'Auth Failed']);
}
return json(['code'=>999, 'msg'=>'Unknown Problem']);
}
}