public function _initialize()
 {
     $curr_action = strtolower(__ACTION__);
     //忽略
     $ignore_arr = array("/admin/getvcode");
     foreach ($ignore_arr as $v) {
         if (strpos($curr_action, $v) !== false) {
             return;
         }
     }
     //检查是否已登录
     if (!cookie("curr_user_name")) {
         if (!session('?user')) {
             if (strpos($curr_action, "/admin/login") === false) {
                 //没有登录
                 header(strtolower("location: " . __ROOT__ . "/" . MODULE_NAME . "/admin/login"));
             }
         } else {
             //如果是已登录状态,停留在登录页面的话,就跳到后台首页
             if (strpos($curr_action, "/admin/login") !== false) {
                 header(strtolower("location: " . __ROOT__ . "/" . MODULE_NAME . "/admin/main"));
             }
         }
     } else {
         $curr_user_name = \Common\Encrypt::decode(cookie("curr_user_name"));
         $where = array("name" => $curr_user_name);
         $user = D("User")->where($where)->find();
         session("user", $user);
     }
     //公用部分
     $this->assign("admin_path", dirname(__APP__) . "/" . strtolower(MODULE_NAME) . "/" . strtolower(CONTROLLER_NAME));
 }
 public function login()
 {
     if (cookie("curr_user_name")) {
         //一周内自动登录
         $name = str_filter(cookie("curr_user_name"));
         $name = \Common\Encrypt::decode($name);
         $where = array("name" => $name, "is_admin" => 1);
         $user = D("User")->where($where)->find();
         unset($user["pwd"]);
         session("user", $user);
         $user["err_login"] = 0;
         D("User")->where(array("id" => $user["id"]))->save($user);
         header("location:main");
         exit;
     }
     if (IS_POST) {
         $name = I("post.name", NULL, "str_filter");
         $pwd = I("post.pwd", NULL, "str_filter");
         //提交登录
         $remember = I("post.remember", 0, "intval");
         $vcode = I("post.vcode", "", "str_filter");
         $this->ajaxReturn(D("User")->adminLogin($name, $pwd, $remember, $vcode), "JSON");
     }
     $this->display();
 }
예제 #3
0
 public function adminLogin($name, $pwd, $remember = 0, $vcode = "")
 {
     $user = $this->where("name = '" . $name . "' and is_admin = 1")->find();
     if ($user) {
         if ($user["err_login"] >= 3) {
             if ($vcode == "") {
                 return array("code" => 2);
             } else {
                 $verify = new \Think\Verify();
                 if (!$verify->check($vcode, 1)) {
                     return array("code" => 1, "desc" => "验证码错误");
                 }
             }
         }
         if ($user["pwd"] == $pwd) {
             session("user", $user);
             $user["err_login"] = 0;
             $this->data($user)->save();
             if ($remember == 1) {
                 $curr_user_name = \Common\Encrypt::encode($user["name"]);
                 cookie('curr_user_name', $curr_user_name, array('expire' => 86400 * 7));
                 //保存7天
                 //echo $curr_user_name;exit();
             }
             $arr = array("code" => 0, "desc" => "登录成功");
         } else {
             $user["err_login"] += 1;
             $id = $user["id"];
             unset($user["id"]);
             $this->where("id = " . $id)->save($user);
             $arr = array("code" => 1, "desc" => "密码错误");
         }
         return $arr;
     } else {
         $arr = array("code" => 1, "desc" => "没有此用户");
         return $arr;
     }
 }