[PHP高可用后端]⑧--登陆功能开发下

https://www.kancloud.cn/manual/thinkphp5/135189

[PHP高可用后端]⑧--登陆功能开发下_第1张图片
Paste_Image.png

code.php

 -1,
    'status_normal' => 1,
    //待审
    'status_padding' => 0,
];

admin.php(extra)

 'adminuser',
    'session_user_scope' => 'imooc_app_scope',
];

Login.php

fetch();
    }

    public function check()
    {
        if (request()->isPost()) {
            $data = input('post.');
            if (!captcha_check($data['code'])) {
                $this->error('验证码不正确');
            }

            $validate = validate('Login');
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            try {
                $user = model('AdminUser')->get(
                    ['username' => $data['username']]
                );
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }

            if (!$user || $user->status != config('code.status_normal')) {
                // $this->error 内部会throw一个Exception 所以不需要放在try catch中
                $this->error('该用户不存在');
            }

            if (IAuth::setPassword($data['password']) != $user['password']) {
                $this->error("密码不正确");
            }

            //更新数据库 登陆时间 登陆ip
            $udata = [
                'last_login_time' => time(),
                'last_login_ip' => request()->ip(),
            ];
            try {
                model('AdminUser')->save($udata, ['id' => $user->id]);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            //2.session
            session(config('admin.session_user'), $user, config('admin.session_user_scope'));
            $this->success('登陆成功', 'index/index');
        } else {
            $this->error("请求不合法");
        }
    }
}

http://singwa.com/index.php/admin/login

[PHP高可用后端]⑧--登陆功能开发下_第2张图片
Paste_Image.png

你可能感兴趣的:([PHP高可用后端]⑧--登陆功能开发下)