超管登录
This commit is contained in:
5
Server/application/superadmin/config/route.php
Normal file
5
Server/application/superadmin/config/route.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
use think\facade\Route;
|
||||
|
||||
// 超级管理员认证相关路由
|
||||
Route::post('auth/login', 'app\\superadmin\\controller\\Auth@login');
|
||||
63
Server/application/superadmin/controller/Auth.php
Normal file
63
Server/application/superadmin/controller/Auth.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
namespace app\superadmin\controller;
|
||||
|
||||
use think\Controller;
|
||||
use app\superadmin\model\Administrator;
|
||||
|
||||
class Auth extends Controller
|
||||
{
|
||||
/**
|
||||
* 管理员登录
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
return json(['code' => 405, 'msg' => '请求方法不允许']);
|
||||
}
|
||||
|
||||
$account = $this->request->post('account');
|
||||
$password = $this->request->post('password');
|
||||
|
||||
if (empty($account) || empty($password)) {
|
||||
return json(['code' => 400, 'msg' => '账号和密码不能为空']);
|
||||
}
|
||||
|
||||
$admin = Administrator::login($account, $password);
|
||||
|
||||
if (!$admin) {
|
||||
return json(['code' => 401, 'msg' => '账号或密码错误']);
|
||||
}
|
||||
|
||||
// 更新登录信息
|
||||
$admin->lastLoginTime = time();
|
||||
$admin->lastLoginIp = $this->request->ip();
|
||||
$admin->save();
|
||||
|
||||
// 设置登录Cookie,有效期24小时
|
||||
cookie('admin_id', $admin->id, 86400);
|
||||
cookie('admin_token', $this->createToken($admin), 86400);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '登录成功',
|
||||
'data' => [
|
||||
'id' => $admin->id,
|
||||
'name' => $admin->name,
|
||||
'account' => $admin->account,
|
||||
'token' => cookie('admin_token')
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建登录令牌
|
||||
* @param Administrator $admin
|
||||
* @return string
|
||||
*/
|
||||
private function createToken($admin)
|
||||
{
|
||||
$data = $admin->id . '|' . $admin->account . '|' . time();
|
||||
return md5($data . 'cunkebao_admin_secret');
|
||||
}
|
||||
}
|
||||
2
Server/application/superadmin/data/administrator.sql
Normal file
2
Server/application/superadmin/data/administrator.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
INSERT INTO `tk_administrators` (`name`, `account`, `password`, `status`, `createTime`, `updateTime`)
|
||||
VALUES ('超级管理员', 'admin', MD5('123456'), 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());
|
||||
48
Server/application/superadmin/model/Administrator.php
Normal file
48
Server/application/superadmin/model/Administrator.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
namespace app\superadmin\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 超级管理员模型类
|
||||
*/
|
||||
class Administrator extends Model
|
||||
{
|
||||
// 设置数据表名
|
||||
protected $name = 'administrators';
|
||||
|
||||
// 设置数据表前缀
|
||||
protected $prefix = 'tk_';
|
||||
|
||||
// 设置主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 自动写入时间戳
|
||||
protected $autoWriteTimestamp = true;
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createTime';
|
||||
protected $updateTime = 'updateTime';
|
||||
protected $deleteTime = 'deleteTime';
|
||||
|
||||
// 隐藏字段
|
||||
protected $hidden = [
|
||||
'password'
|
||||
];
|
||||
|
||||
/**
|
||||
* 验证管理员登录
|
||||
* @param string $account 账号
|
||||
* @param string $password 密码(已MD5加密)
|
||||
* @return array|null
|
||||
*/
|
||||
public static function login($account, $password)
|
||||
{
|
||||
return self::where([
|
||||
['account', '=', $account],
|
||||
['password', '=', $password],
|
||||
['status', '=', 1],
|
||||
['deleteTime', '=', 0]
|
||||
])->find();
|
||||
}
|
||||
}
|
||||
@@ -24,12 +24,14 @@ include __DIR__ . '/../application/api/config/route.php';
|
||||
// 加载Common模块路由配置
|
||||
include __DIR__ . '/../application/common/config/route.php';
|
||||
|
||||
// 加载Devices模块路由配置
|
||||
// 加载Cunkebao模块路由配置
|
||||
include __DIR__ . '/../application/cunkebao/config/route.php';
|
||||
|
||||
// 加载Store模块路由配置
|
||||
include __DIR__ . '/../application/store/config/route.php';
|
||||
|
||||
// 加载Superadmin模块路由配置
|
||||
include __DIR__ . '/../application/superadmin/config/route.php';
|
||||
|
||||
// 加载CozeAI模块路由配置
|
||||
include __DIR__ . '/../application/cozeai/config/route.php';
|
||||
|
||||
Reference in New Issue
Block a user