小程序之旅——第五站(登陆)

一、页面

创建application/admin/view/login/login.html文件

<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta charset="utf-8" />
    <title>后台管理</title>
    <meta name="description" content="User login page" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
    <!-- bootstrap & fontawesome -->
    <link rel="stylesheet" href="{:config('public.static')}/ace1.4/assets/css/bootstrap.min.css" />
    <link rel="stylesheet" href="{:config('public.static')}/ace1.4/assets/font-awesome/4.5.0/css/font-awesome.min.css" />
    <!-- text fonts -->
    <link rel="stylesheet" href="{:config('public.static')}/ace1.4/assets/css/fonts.googleapis.com.css" />
    <!-- ace styles -->
    <link rel="stylesheet" href="{:config('public.static')}/ace1.4/assets/css/ace.min.css" />
    <!--[if lte IE 9]>
                <link rel="stylesheet" href="{:config('public.static')}/ace1.4/assets/css/ace-part2.min.css" />
        <![endif]-->
    <link rel="stylesheet" href="{:config('public.static')}/ace1.4/assets/css/ace-rtl.min.css" />
    <!--[if lte IE 9]>
          <link rel="stylesheet" href="{:config('public.static')}/ace1.4/assets/css/ace-ie.min.css" />
        <![endif]-->
    <!-- HTML5shiv and Respond.js for IE8 to support HTML5 elements and media queries -->
    <!--[if lte IE 8]>
        <script src="{:config('public.static')}/ace1.4/assets/js/html5shiv.min.js"></script>
        <script src="{:config('public.static')}/ace1.4/assets/js/respond.min.js"></script>
        <![endif]-->
</head>

<body class="login-layout">
    <div class="main-container">
        <div class="main-content">
            <div class="row">
                <div class="col-sm-10 col-sm-offset-1">
                    <div class="login-container">
                        <div class="center">
                            <h1>

                                    <span class="red"> </span>
                                    <span class="white" id="id-text2">后 台 管 理</span>
                                </h1>
                        </div>
                        <div class="space-6"></div>
                        <div class="position-relative">
                            <div id="login-box" class="login-box visible widget-box no-border">
                                <div class="widget-body">
                                    <div class="widget-main">
                                        <h4 class="header blue lighter bigger">
                                                <i class="ace-icon fa fa-coffee green"></i>
                                                注:公共场请不要记住密码
                                            </h4>
                                        <div class="space-6"></div>
                                        <form action="" method="post" name="myform">
                                            <fieldset>
                                                <label class="block clearfix">
                                                    <span class="block input-icon input-icon-right">
                                                            <input type="text" class="form-control" name="username" placeholder="Username" value="" />
                                                            <i class="ace-icon fa fa-user"></i>
                                                        </span>
                                                </label>
                                                <label class="block clearfix">
                                                    <span class="block input-icon input-icon-right">
                                                            <input type="password" name="password" class="form-control" placeholder="Password"  value=""/>
                                                            <i class="ace-icon fa fa-lock"></i>
                                                        </span>
                                                </label>
                                                <div class="space"></div>
                                                <div class="clearfix">
                                                    <label class="inline">
                                                        <input type="checkbox" name="islogin" class="ace" />
                                                        <span class="lbl">记住密码</span>
                                                    </label>
                                                    <input type="hidden" name="dosubmit" value="1">
                                                    <button onclick="myform.submit()" type="button" class="width-35 pull-right btn btn-sm btn-primary">
                                                        <i class="ace-icon fa fa-key"></i>
                                                        <span class="bigger-110">登陆</span>
                                                    </button>
                                                </div>
                                                <div class="space-4"></div>
                                            </fieldset>
                                        </form>
                                        <div class="social-or-login center">
                                            <span class="bigger-110">其他登陆</span>
                                        </div>
                                        <div class="space-6"></div>
                                        <div class="social-login center">
                                            <a class="btn btn-primary">
                                                    <i class="ace-icon fa fa-wechat"></i>
                                                </a>
                                            <a class="btn btn-info">
                                                    <i class="ace-icon fa fa-qq"></i>
                                                </a>
                                            <a class="btn btn-danger">
                                                    <i class="ace-icon fa fa-google-plus"></i>
                                                </a>
                                        </div>
                                    </div>
                                    <!-- /.widget-main -->
                                    <div class="toolbar clearfix">
                                        <div>
                                            <a href="#" data-target="#forgot-box" class="forgot-password-link">
                                                    <i class="ace-icon fa fa-arrow-left"></i>
                                                    忘记密码
                                                </a>
                                        </div>
                                        <div>
                                            <a href="#" data-target="#signup-box" class="user-signup-link">
                                                    注册
                                                    <i class="ace-icon fa fa-arrow-right"></i>
                                                </a>
                                        </div>
                                    </div>
                                </div>
                                <!-- /.widget-body -->
                            </div>
                            <!-- /.login-box -->
                            <div id="forgot-box" class="forgot-box widget-box no-border">
                                <div class="widget-body">
                                    <div class="widget-main">
                                        <h4 class="header red lighter bigger">
                                                <i class="ace-icon fa fa-key"></i>
                                                Retrieve Password
                                            </h4>
                                        <div class="space-6"></div>
                                        <p>
                                            Enter your email and to receive instructions
                                        </p>
                                        <form>
                                            <fieldset>
                                                <label class="block clearfix">
                                                    <span class="block input-icon input-icon-right">
                                                            <input type="email" class="form-control" placeholder="Email" />
                                                            <i class="ace-icon fa fa-envelope"></i>
                                                        </span>
                                                </label>
                                                <div class="clearfix">
                                                    <button type="button" class="width-35 pull-right btn btn-sm btn-danger">
                                                        <i class="ace-icon fa fa-lightbulb-o"></i>
                                                        <span class="bigger-110">Send Me!</span>
                                                    </button>
                                                </div>
                                            </fieldset>
                                        </form>
                                    </div>
                                    <!-- /.widget-main -->
                                    <div class="toolbar center">
                                        <a href="#" data-target="#login-box" class="back-to-login-link">
                                                Back to login
                                                <i class="ace-icon fa fa-arrow-right"></i>
                                            </a>
                                    </div>
                                </div>
                                <!-- /.widget-body -->
                            </div>
                            <!-- /.forgot-box -->
                            <div id="signup-box" class="signup-box widget-box no-border">
                                <div class="widget-body">
                                    <div class="widget-main">
                                        <h4 class="header green lighter bigger">
                                                <i class="ace-icon fa fa-users blue"></i>
                                                注册
                                            </h4>
                                        <div class="space-6"></div>
                                        <p> 填写以下信息: </p>
                                        <form>
                                            <fieldset>
                                                <label class="block clearfix">
                                                    <span class="block input-icon input-icon-right">
                                                            <input type="email" class="form-control" placeholder="Email" />
                                                            <i class="ace-icon fa fa-envelope"></i>
                                                        </span>
                                                </label>
                                                <label class="block clearfix">
                                                    <span class="block input-icon input-icon-right">
                                                            <input type="text" class="form-control" placeholder="Username" />
                                                            <i class="ace-icon fa fa-user"></i>
                                                        </span>
                                                </label>
                                                <label class="block clearfix">
                                                    <span class="block input-icon input-icon-right">
                                                            <input type="password" class="form-control" placeholder="Password" />
                                                            <i class="ace-icon fa fa-lock"></i>
                                                        </span>
                                                </label>
                                                <label class="block clearfix">
                                                    <span class="block input-icon input-icon-right">
                                                            <input type="password" class="form-control" placeholder="Repeat password" />
                                                            <i class="ace-icon fa fa-retweet"></i>
                                                        </span>
                                                </label>
                                                <label class="block">
                                                    <input type="checkbox" class="ace" />
                                                    <span class="lbl">
                                                            我同意
                                                            <a href="#">用户协议</a>
                                                        </span>
                                                </label>
                                                <div class="space-24"></div>
                                                <div class="clearfix">
                                                    <button type="reset" class="width-30 pull-left btn btn-sm">
                                                        <i class="ace-icon fa fa-refresh"></i>
                                                        <span class="bigger-110">重置</span>
                                                    </button>
                                                    <button type="button" class="width-65 pull-right btn btn-sm btn-success">
                                                        <span class="bigger-110">注册</span>
                                                        <i class="ace-icon fa fa-arrow-right icon-on-right"></i>
                                                    </button>
                                                </div>
                                            </fieldset>
                                        </form>
                                    </div>
                                    <div class="toolbar center">
                                        <a href="#" data-target="#login-box" class="back-to-login-link">
                                                <i class="ace-icon fa fa-arrow-left"></i>
                                                返回登陆
                                            </a>
                                    </div>
                                </div>
                                <!-- /.widget-body -->
                            </div>
                            <!-- /.signup-box -->
                        </div>
                        <!-- /.position-relative -->
                    </div>
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </div>
        <!-- /.main-content -->
    </div>
    <!-- /.main-container -->
    <!-- basic scripts -->
    <!--[if !IE]> -->
    <script src="{:config('public.static')}/ace1.4/assets/js/jquery-2.1.4.min.js"></script>
    <!-- <![endif]-->
    <!--[if IE]>
<script src="{:config('public.static')}/ace1.4/assets/js/jquery-1.11.3.min.js"></script>
<![endif]-->
    <script type="text/javascript">
    if ('ontouchstart' in document.documentElement)
        document.write("<script src='assets/js/jquery.mobile.custom.min.js'>" + "<" + "/script>");
    </script>
    <!-- inline scripts related to this page -->
    <script type="text/javascript">
    jQuery(function($) {
        $(document).on('click', '.toolbar a[data-target]', function(e) {
            e.preventDefault();
            var target = $(this).data('target');
            $('.widget-box.visible').removeClass('visible'); //hide others
            $(target).addClass('visible'); //show target
        });
    });



    //you don't need this, just used for changing background
    jQuery(function($) {
        $('#btn-login-dark').on('click', function(e) {
            $('body').attr('class', 'login-layout');
            $('#id-text2').attr('class', 'white');
            $('#id-company-text').attr('class', 'blue');

            e.preventDefault();
        });
        $('#btn-login-light').on('click', function(e) {
            $('body').attr('class', 'login-layout light-login');
            $('#id-text2').attr('class', 'grey');
            $('#id-company-text').attr('class', 'blue');

            e.preventDefault();
        });
        $('#btn-login-blur').on('click', function(e) {
            $('body').attr('class', 'login-layout blur-login');
            $('#id-text2').attr('class', 'white');
            $('#id-company-text').attr('class', 'light-blue');

            e.preventDefault();
        });

    });
    </script>
</body>

</html>
{:config('public.static')}获取config文件定义的变量,这里是指public/static文件夹

二、Controller文件

创建application/admin/controller/Login.php文件

<?php

/**
 * @Author: ZouQH
 * @Date:   2017-10-10 09:48:28
 * @Last Modified by:   ZouQH
 * @Last Modified time: 2017-10-13 15:52:14
 */
namespace app\admin\controller;

use think\Controller;
use think\Loader;

class Login extends Controller {
    
   /**
     * 登入
     */
    public function index() {
        //dump(request()->ip());exit;


        if ($_POST['dosubmit']) {
            $username = input('post.username');
            $password = input('post.password');

            if (!$username) {
                $this->error('用户名不能为空');
            }
            if (!$password) {
                $this->error('密码不能为空');
            }

            $info = db('user')->field('id,name,password')->where('name', $username)->find();

            if (!$info) {
                $this->error('用户不存在');
            }
            // dump(md5($password));
            if (md5($password) != $info['password']) {
                $this->error('密码不正确');
            } else {
                session('user_name', $info['name']);
                session('user_id', $info['id']);
                if (input('post.islogin')) {
                    cookie('user_name', encry_code($info['name']));
                    cookie('user_id', encry_code($info['id']));
                }

                //记录登录信息
                Loader::model('User')->editInfo(1, $info['id']);
                $this->success('登入成功', 'index/index');
            }
        } else {
            if (session('user_name')) {
                $this->success('您已登入', 'index/index');
            }

            if (cookie('user_name')) {
                $username = encry_code(cookie('user_name'),'DECODE');
                $info = db('user')->field('id,name,password')->where('name', $username)->find();
                if ($info) {
                    //记录
                    session('user_name', $info['name']);
                    session('user_id', $info['id']);
                    Loader::model('User')->editInfo(1, $info['id']);
                    $this->success('登入成功', 'index/index');
                }
            }

            $this->view->engine->layout(false);
            return $this->fetch('login');
        }
    }

    /**
     * 登出
     */
    public function logout() {
        session('user_name', null);
        session('user_id', null);
        cookie('user_name', null);
        cookie('user_id', null);
        $this->success('退出成功', 'login/index');
    }

}

用户名、密码正确则存入session中

并且跳转至index/index首页

三、Model模型

创建/application/admin/model/User.php

<?php

/**
 * @Author: ZouQH
 * @Date:   2017-10-10 15:36:55
 * @Last Modified by:   ZouQH
 * @Last Modified time: 2017-10-10 15:41:55
 */
namespace app\admin\model;

use think\Model;
use think\Db;

class User extends Model {
    
    public $status = array(1 => '无效', 2 => '有效');

    public function getInfo($id) {
        $res = $this->field('id,name,lastlogin_ip,lastlogin_time,email,phone,realname,openid,status')
                ->where(array('id' => $id))
                ->find();
        if ($res) {
            $res = $res->data;
        }

        return $res;
    }

    /**
     * 
     * @param int $userid 用户ID
     * @return Array
     */
    public function getUserGroups($uid) {

        $res = db('gp_user')->field('gp_id')->where('user_id', $uid)->select();

        $userGroups = '';
        if ($res) {
            foreach ($res as $k => $v) {
                $userGroups .= $v['gp_id'] . ',';
            }
            return trim($userGroups, ',');
        } else {
            return false;
        }
    }

    /**
     * 登陆更新
     * @param int $type 1:登陆更新,2:信息更新
     * @param int $id id
     * @param array $data 更新的数据
     */
    public function editInfo($type, $id, $data = array()) {
        
        if ($type == 1) {
            $data['lastlogin_time'] = time();
            $data['lastlogin_ip'] = ip2long(request()->ip());
        } elseif ($type == 2) {
            $data['updatetime'] = time();
        }
        $res = $this->allowField(true)->save($data, ['id' => $id]);

        return $res;
    }

}

创建Permssion.php模型文件

<?php

/**
 * @Author: ZouQH
 * @Date:   2017-10-13 15:15:22
 * @Last Modified by:   ZouQH
 * @Last Modified time: 2017-10-13 16:32:04
 */
namespace app\admin\model;
use think\Model;
use think\Db;

class Permission extends Model {
    
    function __construct(){
        
    }

    /**
     * 获取用户的权限
     * @param  string $user_id [description]
     * @return string          [description]
     */
    public function getPermsByUser($user_id){
        
        try{
            $res=Db::query('SELECT
                    perm_id
                FROM
                    ord_perm_role
                WHERE
                    role_id IN (
                        SELECT
                            a.*
                        FROM
                            (
                                (
                                    SELECT
                                        ur.role_id
                                    FROM
                                        ord_user_role ur
                                    WHERE
                                        ur.user_id = '.$user_id.'
                                )
                                UNION ALL
                                    (
                                        SELECT
                                            gr.role_id
                                        FROM
                                            ord_gp_role gr
                                        JOIN ord_gp_user gu
                                        WHERE
                                            gu.user_id = '.$user_id.'
                                        AND gu.gp_id = gr.gp_id
                                    )
                            ) a
                    )');
        }catch(Exception $e){
            $e->getMessage();
        }
        if (!$res) {
            return false;
        }
        foreach ($res as $k => $v) {
                $tmp .=$v['perm_id'] . ',';
            }

            $perm_ids = trim($tmp, ',');
        return $perm_ids;
    }
}

 

四、权限获取

在用户登陆成功后调用首页Index控制器前进行权限获取

创建/application/admin/controller/Index/index.php

<?php

/**
 * @Author: ZouQH
 * @Date:   2017-09-30 10:40:08
 * @Last Modified by:   ZouQH
 * @Last Modified time: 2017-10-13 15:52:17
 */
namespace app\admin\controller;

class Index extends Common
{

    /**
     * 后台首页
     */
    public function index(){
       
        return $this->fetch();
    }
}

Index继承/Controller/Common类

创建Common.php文件

<?php

/**
 * @Author: ZouQH
 * @Date:   2017-10-10 09:41:09
 * @Last Modified by:   ZouQH
 * @Last Modified time: 2017-10-13 16:32:12
 */
namespace app\admin\controller;

use think\Controller;

class Common extends Controller {
    
    protected $user_id;
    protected $user_name;

    public function __construct(\think\Request $request = null) {

        parent::__construct($request);

        if (!session('user_id')) {

            $this->error('请登陆', 'login/index', '', 0);
        }

        $this->user_id = session('user_id');
        $this->user_name = session('user_name');

        //权限检查
        if (!$this->_checkAuthor($this->user_id)) {
            $this->error('你无权限操作');
        }

    }

    /**
     * 权限检查
     */
    private function _checkAuthor($user_id) {
        
        if (!$user_id) {
            return false;
        }
        $perms=model('Permission')->getPermsByUser($user_id);
        // var_dump('common---------------'.$perms);
        session('permission',$perms);//权限

        $c = strtolower(request()->controller());
        $a = strtolower(request()->action());

        if (preg_match('/^public_/', $a)) {
            return true;
        }
        if ($c == 'index' && $a == 'index') {
            return true;
        }
       

        $menu = model('Menu')->getMyMenu($user_id);
        foreach ($menu as $k => $v) {
            if (strtolower($v['c']) == $c && strtolower($v['a']) == $a){
                return true;
            }
        }
        return false;
    }
}

 

转载于:https://www.cnblogs.com/rainbowz/p/7662076.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值