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

2017-11-01  本文已影响15人  子木同

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

Paste_Image.png

code.php

<?php
/**
 * 和状态码相关的文案配置
 */
return [
    'status_delete' => -1,
    'status_normal' => 1,
    //待审
    'status_padding' => 0,
];

admin.php(extra)

<?php
return [
    'session_user' => 'adminuser',
    'session_user_scope' => 'imooc_app_scope',
];

Login.php

<?php
/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/1
 * Time: 14:25
 */

namespace app\admin\controller;

use app\common\lib\IAuth;
use think\Controller;

class Login extends Controller
{

    public function index()
    {
        return $this->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

Paste_Image.png
上一篇下一篇

猜你喜欢

热点阅读