init
This commit is contained in:
98
app/controller/Login.php
Normal file
98
app/controller/Login.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace app\controller;
|
||||
|
||||
use app\model\Access;
|
||||
use app\model\Admin as AdminModel;
|
||||
use Firebase\JWT\JWT;
|
||||
use think\facade\Db;
|
||||
use think\facade\Validate;
|
||||
use think\Request;
|
||||
|
||||
class Login
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
echo '这里是登陆界面';
|
||||
return;
|
||||
}
|
||||
|
||||
public function check(Request $request)
|
||||
{
|
||||
|
||||
$data = $request->param();
|
||||
// var_dump($data);
|
||||
//错误集合
|
||||
$errors = [];
|
||||
//验证
|
||||
$validate = Validate::rule([
|
||||
'username' => 'unique:admin,username^password'
|
||||
]);
|
||||
|
||||
$result = $validate->check([
|
||||
'username' => $data['username'],
|
||||
'password' => $data['password']
|
||||
]);
|
||||
|
||||
//错误提示,反向操作
|
||||
//如果用户名和密码同时比对存在,那其实就是正确的
|
||||
if ($result) {
|
||||
$errors[] = '用户名或密码错误~';
|
||||
}
|
||||
|
||||
//判断跳转
|
||||
if (!empty($errors)) {
|
||||
return json([
|
||||
'code' => 1,
|
||||
'message' => $errors,
|
||||
]);
|
||||
} else {
|
||||
|
||||
session('admin', $data['username']);
|
||||
$admin_id = AdminModel::where('username', $data['username'])->value('id');
|
||||
$roles = AdminModel::where('id', $admin_id)->value('roles');
|
||||
$permission = AdminModel::where('id', $admin_id)->value('permission');
|
||||
$token = $this->GennerteToken($admin_id);
|
||||
|
||||
|
||||
if (!empty($permission)) {
|
||||
$pp = explode(',', substr($permission, 0, -1));
|
||||
} else {
|
||||
$pp = array();
|
||||
}
|
||||
return json([
|
||||
'code' => 200,
|
||||
'message' => '登陆成功',
|
||||
'permissions' => $roles,
|
||||
'authority' => $pp,
|
||||
'adminId' => $admin_id,
|
||||
'token' => $token
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function GennerteToken($admin_id)
|
||||
{
|
||||
$key = 'zuxingzdz';
|
||||
$token = array(
|
||||
"iss" => $key, //签发者 可以为空
|
||||
"aud" => '', //面象的用户,可以为空
|
||||
"iat" => time(), //签发时间
|
||||
"nbf" => time() + 3, //在什么时候jwt开始生效 (这里表示生成100秒后才生效)
|
||||
"exp" => time() + 1296000, //token 过期时间
|
||||
"data" => [ //记录的userid的信息,这里是自已添加上去的,如果有其它信息,可以再添加数组的键值对
|
||||
'admin_id' => $admin_id,
|
||||
]
|
||||
);
|
||||
|
||||
$jwt = JWT::encode($token, $key, "HS256"); //根据参数生成了 token
|
||||
return $jwt;
|
||||
}
|
||||
|
||||
public function out()
|
||||
{
|
||||
session('admin', null);
|
||||
return json(['code' => 200, 'massage' => '退出成功']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user