mirror of
https://gitee.com/apiadmin/ApiAdmin.git
synced 2025-04-06 03:58:00 +08:00
基本完成用户登录的操作
This commit is contained in:
parent
b261a80c24
commit
8fadb2bc27
@ -10,4 +10,9 @@ use think\Controller;
|
||||
|
||||
class Base extends Controller {
|
||||
|
||||
protected $primaryKey;
|
||||
|
||||
public function _initialize(){
|
||||
$this->primaryKey = config('SQL_PRIMARY_KEY');
|
||||
}
|
||||
}
|
@ -8,10 +8,15 @@
|
||||
namespace app\admin\controller;
|
||||
|
||||
|
||||
use think\Db;
|
||||
use app\admin\model\UserData;
|
||||
use think\Request;
|
||||
|
||||
class User extends Base {
|
||||
|
||||
/**
|
||||
* 用户登录函数
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function login(){
|
||||
$request = Request::instance();
|
||||
if( $request->isPost() ){
|
||||
@ -20,16 +25,17 @@ class User extends Base {
|
||||
if( !$username || !$password ){
|
||||
return $this->error('缺少关键数据!');
|
||||
}
|
||||
$password = $this->getPwdHash($password);
|
||||
$userInfo = Db::table('user')->where(['username' => $username, 'password' => $password])->find();
|
||||
$userModel = new \app\admin\model\User();
|
||||
$password = $userModel->getPwdHash($password);
|
||||
$userInfo = $userModel->where(['username' => $username, 'password' => $password])->find();
|
||||
if( empty($userInfo) ){
|
||||
$this->error('用户名或者密码错误!');
|
||||
return $this->error('用户名或者密码错误!');
|
||||
}else{
|
||||
if( $userInfo['status'] ){
|
||||
|
||||
//保存用户信息和登录凭证
|
||||
cache($userInfo['id'], session_id(), config('online_time'));
|
||||
session('uid', $userInfo['id']);
|
||||
cache($userInfo[$this->primaryKey], session_id(), config('online_time'));
|
||||
session('uid', $userInfo[$this->primaryKey]);
|
||||
|
||||
//获取跳转链接,做到从哪来到哪去
|
||||
if( $request->has('from', 'get') ){
|
||||
@ -39,32 +45,24 @@ class User extends Base {
|
||||
}
|
||||
|
||||
//更新用户数据
|
||||
$userData = D('UserData')->where(['uid' => $userInfo['_id']])->find();
|
||||
$data = [];
|
||||
if( $userData ){
|
||||
$data['loginTimes'] = $userData['loginTimes'] + 1;
|
||||
$data['lastLoginIp'] = Request::instance()->ip(1);
|
||||
$data['lastLoginTime'] = time();
|
||||
D('UserData')->where(['uid' => $userInfo['_id']])->save($data);
|
||||
$userData = UserData::get(['uid' => $userInfo[$this->primaryKey]]);
|
||||
if( $userData->uid ){
|
||||
$userData->loginTimes += 1;
|
||||
$userData->save();
|
||||
}else{
|
||||
$data['loginTimes'] = 1;
|
||||
$data['uid'] = $userInfo['_id'];
|
||||
D('UserData')->add($data);
|
||||
$newUserData = new UserData();
|
||||
$newUserData->loginTimes = 1;
|
||||
$newUserData->uid = $userInfo[$this->primaryKey];
|
||||
$newUserData->save();
|
||||
}
|
||||
|
||||
$this->success('登录成功', $url);
|
||||
return $this->success('登录成功', $url);
|
||||
}else{
|
||||
$this->error('用户已被封禁,请联系管理员');
|
||||
return $this->error('用户已被封禁,请联系管理员');
|
||||
}
|
||||
}
|
||||
}else{
|
||||
return $this->fetch();
|
||||
}
|
||||
}
|
||||
|
||||
private function getPwdHash( $pwd ){
|
||||
$hashKey = config('auth_key');
|
||||
$newPwd = $pwd.$hashKey;
|
||||
return md5(sha1($newPwd).$hashKey);
|
||||
}
|
||||
}
|
@ -3,7 +3,24 @@ namespace app\admin\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class User extends Model
|
||||
{
|
||||
class User extends Model {
|
||||
|
||||
protected $autoWriteTimestamp = true;
|
||||
protected $insert = ['regIp'];
|
||||
protected $createTime = 'regTime';
|
||||
protected $updateTime = 'updateTime';
|
||||
|
||||
protected function setRegIpAttr(){
|
||||
return request()->ip(1);
|
||||
}
|
||||
|
||||
protected function setPasswordAttr($value) {
|
||||
return $this->getPwdHash($value);
|
||||
}
|
||||
|
||||
public function getPwdHash( $pwd ){
|
||||
$hashKey = config('auth_key');
|
||||
$newPwd = $pwd.$hashKey;
|
||||
return md5(sha1($newPwd).$hashKey);
|
||||
}
|
||||
}
|
@ -11,4 +11,14 @@ use think\Model;
|
||||
|
||||
class UserData extends Model {
|
||||
|
||||
protected $insert = ['lastLoginTime', 'lastLoginIp'];
|
||||
protected $update = ['lastLoginIp', 'lastLoginTime'];
|
||||
|
||||
protected function setLastLoginIpAttr(){
|
||||
return request()->ip(1);
|
||||
}
|
||||
|
||||
protected function setLastLoginTimeAttr(){
|
||||
return time();
|
||||
}
|
||||
}
|
@ -6,8 +6,6 @@
|
||||
<title>7d-vision | 登 录</title>
|
||||
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
|
||||
<link rel="stylesheet" href="__STATIC__/bootstrap/css/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.5.0/css/font-awesome.min.css">
|
||||
<link rel="stylesheet" href="//cdn.bootcss.com/ionicons/2.0.1/css/ionicons.min.css">
|
||||
<link rel="stylesheet" href="__ADMIN__/dist/css/AdminLTE.min.css">
|
||||
<!--[if lt IE 9]>
|
||||
<script src="//cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
|
||||
@ -17,19 +15,19 @@
|
||||
<body class="hold-transition login-page">
|
||||
<div class="login-box">
|
||||
<div class="login-logo">
|
||||
<a href="../../index2.html"><b>7D</b> VISION</a>
|
||||
<a href="{:config('WEBSITE_DOMAIN')}"><b>7D</b> VISION</a>
|
||||
</div>
|
||||
<!-- /.login-logo -->
|
||||
<div class="login-box-body">
|
||||
<p class="login-box-msg">请登录您的账户</p>
|
||||
<p class="login-box-msg">请填写您的账号名和密码</p>
|
||||
|
||||
<form action="../../index2.html" method="post">
|
||||
<form action="{:url('User/login')}" method="post">
|
||||
<div class="form-group has-feedback">
|
||||
<input type="email" class="form-control" placeholder="Email">
|
||||
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
|
||||
<input type="text" class="form-control" name="username" placeholder="Username">
|
||||
<span class="glyphicon glyphicon-user form-control-feedback"></span>
|
||||
</div>
|
||||
<div class="form-group has-feedback">
|
||||
<input type="password" class="form-control" placeholder="Password">
|
||||
<input type="password" name="password" class="form-control" placeholder="Password">
|
||||
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
|
||||
</div>
|
||||
<div class="row">
|
||||
@ -38,8 +36,6 @@
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<a href="#">I forgot my password</a><br>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -47,8 +43,6 @@
|
||||
<script src="__STATIC__/jQuery/2.2.4/jquery.min.js"></script>
|
||||
<!-- Bootstrap 3.3.6 -->
|
||||
<script src="__STATIC__/bootstrap/js/bootstrap.min.js"></script>
|
||||
<!-- iCheck -->
|
||||
<script src="__ADMIN__/plugins/iCheck/icheck.min.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
@ -14,6 +14,7 @@ return [
|
||||
'PRODUCT_NAME' => '7d-vision', //产品名称
|
||||
'WEBSITE_DOMAIN' => 'http://www.7d-vision.com', //官方网址
|
||||
'COMPANY_NAME' => '七维视觉科技有限公司', //公司名称
|
||||
'SQL_PRIMARY_KEY' => 'id',
|
||||
|
||||
// +----------------------------------------------------------------------
|
||||
// | 应用设置
|
||||
|
@ -19,7 +19,7 @@ return [
|
||||
// 用户名
|
||||
'username' => 'root',
|
||||
// 密码
|
||||
'password' => 'root',
|
||||
'password' => '123456',
|
||||
// 端口
|
||||
'hostport' => '',
|
||||
// 连接dsn
|
||||
|
Loading…
x
Reference in New Issue
Block a user