diff --git a/application/admin/controller/Base.php b/application/admin/controller/Base.php index 928ab77..7f86dc5 100644 --- a/application/admin/controller/Base.php +++ b/application/admin/controller/Base.php @@ -10,4 +10,9 @@ use think\Controller; class Base extends Controller { + protected $primaryKey; + + public function _initialize(){ + $this->primaryKey = config('SQL_PRIMARY_KEY'); + } } \ No newline at end of file diff --git a/application/admin/controller/User.php b/application/admin/controller/User.php index d5b28e6..33c9fa3 100644 --- a/application/admin/controller/User.php +++ b/application/admin/controller/User.php @@ -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); - } } \ No newline at end of file diff --git a/application/admin/model/User.php b/application/admin/model/User.php index 4c9c1d0..1a75272 100644 --- a/application/admin/model/User.php +++ b/application/admin/model/User.php @@ -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); + } } \ No newline at end of file diff --git a/application/admin/model/UserData.php b/application/admin/model/UserData.php index 7ce1e0f..ad509f0 100644 --- a/application/admin/model/UserData.php +++ b/application/admin/model/UserData.php @@ -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(); + } } \ No newline at end of file diff --git a/application/admin/view/user/login.html b/application/admin/view/user/login.html index f363b15..9fb4370 100644 --- a/application/admin/view/user/login.html +++ b/application/admin/view/user/login.html @@ -6,8 +6,6 @@
请登录您的账户
+请填写您的账号名和密码
-