更新若干代码
This commit is contained in:
@@ -10,53 +10,104 @@ use think\facade\View;
|
||||
use think\facade\Cookie;
|
||||
use think\facade\Request;
|
||||
use app\admin\model\YzAdminConfig;
|
||||
use app\admin\model\AdminUser;
|
||||
use app\admin\model\Log\LogsLogin;
|
||||
|
||||
class Login
|
||||
{
|
||||
protected $app;
|
||||
protected $config;
|
||||
|
||||
public function __construct(App $app)
|
||||
{
|
||||
$this->app = $app;
|
||||
$this->config = new YzAdminConfig();
|
||||
}
|
||||
|
||||
// 登录页面
|
||||
public function index()
|
||||
{
|
||||
# 获取配置
|
||||
$YzAdminConfig = new YzAdminConfig();
|
||||
$this->config = $YzAdminConfig->getAll();
|
||||
$config = $this->config->getAll();
|
||||
View::assign([
|
||||
'config' => $this->config
|
||||
'config' => $config
|
||||
]);
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
// 记录登录日志
|
||||
protected function recordLoginLog($username, $status, $reason = '')
|
||||
{
|
||||
$data = [
|
||||
'username' => $username,
|
||||
'ip_address' => Request::ip(),
|
||||
'location' => $this->getLocation(Request::ip()),
|
||||
'device_type' => $this->getDeviceType(),
|
||||
'user_agent' => Request::header('user-agent'),
|
||||
'login_status' => $status,
|
||||
'failure_reason' => $reason,
|
||||
'login_time' => date('Y-m-d H:i:s')
|
||||
];
|
||||
LogsLogin::create($data);
|
||||
}
|
||||
|
||||
// 获取IP地址位置
|
||||
protected function getLocation($ip)
|
||||
{
|
||||
// 这里可以接入IP地址库或第三方API
|
||||
return '未知';
|
||||
}
|
||||
|
||||
// 获取设备类型
|
||||
protected function getDeviceType()
|
||||
{
|
||||
$agent = Request::header('user-agent');
|
||||
if (preg_match('/(iPhone|iPod|Android|ios|iPad|Mobile)/i', $agent)) {
|
||||
return '移动端';
|
||||
}
|
||||
return 'PC端';
|
||||
}
|
||||
|
||||
// 登录
|
||||
public function login()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
$account = trim(input('post.account'));
|
||||
if (empty($account)) {
|
||||
$this->returnCode(1, '账号不能为空');
|
||||
$this->recordLoginLog($account, 0, '账号不能为空');
|
||||
return json(['code' => 1, 'msg' => '账号不能为空']);
|
||||
}
|
||||
$pattern = "/^([0-9A-Za-z-_.]+)@([0-9a-z]+.[a-z]{2,3}(.[a-z]{2})?)$/i";
|
||||
if (!preg_match($pattern, $account)) {
|
||||
$this->returnCode(1, '邮箱格式不正确');
|
||||
$this->recordLoginLog($account, 0, '邮箱格式不正确');
|
||||
return json(['code' => 1, 'msg' => '邮箱格式不正确']);
|
||||
}
|
||||
$password = trim(input('post.password'));
|
||||
if (empty($password)) {
|
||||
$this->returnCode(1, '密码不能为空');
|
||||
$this->recordLoginLog($account, 0, '密码不能为空');
|
||||
return json(['code' => 1, 'msg' => '密码不能为空']);
|
||||
}
|
||||
$code = trim(input('post.code'));
|
||||
if ($code == '') {
|
||||
$this->returnCode(1, '验证码不能为空');
|
||||
$this->recordLoginLog($account, 0, '验证码不能为空');
|
||||
return json(['code' => 1, 'msg' => '验证码不能为空']);
|
||||
}
|
||||
if (!captcha_check($code)) {
|
||||
$this->returnCode(1, '验证码错误');
|
||||
$this->recordLoginLog($account, 0, '验证码错误');
|
||||
return json(['code' => 1, 'msg' => '验证码错误']);
|
||||
}
|
||||
$aUser = Db::table('yz_admin_user')->where('account', $account)->find();
|
||||
$aUser = AdminUser::where('account', $account)->find();
|
||||
if (empty($aUser)) {
|
||||
$this->returnCode(1, '账号不存在');
|
||||
$this->recordLoginLog($account, 0, '账号不存在');
|
||||
return json(['code' => 1, 'msg' => '账号不存在']);
|
||||
}
|
||||
if ($aUser['status'] != 1) {
|
||||
$this->returnCode(1, '账号已被禁用');
|
||||
$this->recordLoginLog($account, 0, '账号已被禁用');
|
||||
return json(['code' => 1, 'msg' => '账号已被禁用']);
|
||||
}
|
||||
if ($aUser['password'] != md5($password)) {
|
||||
$this->returnCode(1, '密码错误');
|
||||
$this->recordLoginLog($account, 0, '密码错误');
|
||||
return json(['code' => 1, 'msg' => '密码错误']);
|
||||
}
|
||||
$remember = input('post.remember');
|
||||
if (!empty($remember)) {
|
||||
@@ -66,10 +117,12 @@ class Login
|
||||
Cookie::set('admin_id', $aUser['uid']);
|
||||
Cookie::set('admin_name', $aUser['name']);
|
||||
}
|
||||
Db::table('yz_admin_user')->where('uid', $aUser['uid'])->update(
|
||||
AdminUser::where('uid', $aUser['uid'])->update(
|
||||
['login_count' => $aUser['login_count'] + 1, 'update_time' => time()]
|
||||
);
|
||||
$this->returnCode(0, [], '登录成功');
|
||||
// 记录登录成功日志
|
||||
$this->recordLoginLog($account, 1);
|
||||
return json(['code' => 0, 'msg' => '登录成功', 'data' => []]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,35 +131,7 @@ class Login
|
||||
{
|
||||
Cookie::delete('admin_id');
|
||||
Cookie::delete('admin_name');
|
||||
$this->returnCode(0, [], '退出成功');
|
||||
}
|
||||
|
||||
// 返回代码
|
||||
protected function returnCode($code, $data = [], $msg = '')
|
||||
{
|
||||
header('Content-type:application/json');
|
||||
if ($code == 0) {
|
||||
$arr = array(
|
||||
'code' => $code,
|
||||
'msg' => $msg,
|
||||
'data' => $data
|
||||
);
|
||||
} else if ($code == 1) {
|
||||
$arr = array(
|
||||
'code' => 1,
|
||||
'msg' => $data
|
||||
);
|
||||
} else {
|
||||
$appapi = new AppApi();
|
||||
$arr = array(
|
||||
'code' => $code,
|
||||
'msg' => $appapi::errorTip($code)
|
||||
);
|
||||
}
|
||||
echo json_encode($arr);
|
||||
if ($code != 0) {
|
||||
exit;
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '退出成功', 'data' => []]);
|
||||
}
|
||||
|
||||
// 密码重置页面
|
||||
@@ -120,34 +145,34 @@ class Login
|
||||
{
|
||||
$account = trim(input('post.account'));
|
||||
if (empty($account)) {
|
||||
$this->returnCode(1, '账号不能为空');
|
||||
return json(['code' => 1, 'msg' => '账号不能为空']);
|
||||
}
|
||||
|
||||
$user = Db::table('yz_admin_user')->where('account', $account)->find();
|
||||
$user = AdminUser::where('account', $account)->find();
|
||||
|
||||
if (!$user) {
|
||||
$this->returnCode(1, '未找到该用户名');
|
||||
return json(['code' => 1, 'msg' => '未找到该用户名']);
|
||||
}
|
||||
|
||||
// 使用md5进行密码加密处理
|
||||
$password = md5('123456');
|
||||
|
||||
try {
|
||||
$res = Db::table('yz_admin_user')
|
||||
->where('account', $account)
|
||||
$res = AdminUser::where('account', $account)
|
||||
->update(['password' => $password]);
|
||||
|
||||
if ($res === false) {
|
||||
$this->returnCode(1, '数据库更新失败');
|
||||
return json(['code' => 1, 'msg' => '数据库更新失败']);
|
||||
}
|
||||
|
||||
if ($res === 0) {
|
||||
$this->returnCode(1, '密码未发生变化');
|
||||
return json(['code' => 1, 'msg' => '密码未发生变化']);
|
||||
}
|
||||
|
||||
$this->returnCode(0, [], '密码重置成功');
|
||||
return json(['code' => 0, 'msg' => '密码重置成功', 'data' => []]);
|
||||
} catch (\Exception $e) {
|
||||
$this->returnCode(1, '系统错误:' . $e->getMessage());
|
||||
return json(['code' => 1, 'msg' => '系统错误:' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user