username = $username; $this->password = $password; $this->usertype = $usertype; switch ($usertype) { case 2: //企业 $user = Enterprise::where(['username' => $username])->findOrEmpty(); break; case 3: //个人 $user = Person::where('username', $username)->findOrEmpty(); break; default: //管理员 $user = User::where(['account' => $username])->findOrEmpty(); break; } $this->info = $user; return $this; } public function getRole() { $role = Role::find($this->info["roleid"]); return $role->toArray(); } /** * 返回用户信息 * @return type */ public function getUserInfo() { return $this->info->toArray(); } /** * 检查密码 * @return type */ public function checkPwd() { switch ($this->usertype) { case 1: $salt = hash("md5", $this->info["salt"], true); $password = simple_hash("md5", $this->password, $salt, 1024); break; case 2: case 3: $password = hash("md5", $this->password); break; } return $password == $this->info["password"]; } /** * 设置冻结与否 * @param type $freezetype */ public function setFreeze($freezetype = FREEZE_NO) { $this->info->freeze = $freezetype; if ($freezetype == FREEZE_NO) { $this->info->errorCount = null; $this->info->freezeTime = null; } else { $this->info->freezeTime = strtotime(sprintf("+%d minutes", FREEZETIME)); } $this->info->save(); } public function setSession() { $user = $this->getUserInfo(); switch ($this->usertype) { case 1: session("user", [ "uid" => $user["id"], "roleid" => $user["roleid"], "account" => $user["account"], "name" => $user["name"], "avatar" => $user["avatar"], "sex" => $user["sex"], "rolename" => $this->getRole()["name"], "usertype" => $this->usertype ]); break; case 2: session("user", [ "uid" => $user["id"], "account" => $user["username"], "name" => $user["name"], "avatar" => $user["headPortrait"], "rolename" => "企业用户", "usertype" => $this->usertype, "type" => $user["type"] ]); break; case 3: session("user", [ "uid" => $user["id"], "account" => $user["username"], "name" => $user["name"], "avatar" => $user["headPortrait"], "sex" => $user["sex"], "rolename" => "个人用户", "usertype" => $this->usertype, "type" => $user["type"] ]); break; } } }