예제 #1
0
func (c *MainController) Get() {
	// 这里是登出处理
	logout := c.Input().Get("logout")
	if strings.EqualFold(logout, "1") {
		userName := c.Ctx.Input.Cookie("log")
		password := c.Ctx.Input.Cookie("pwd")
		err := models.UpdateUserStatus(userName, password, 1)
		if err == nil {
			// beego.Debug("update user raw count", num)
			c.Ctx.SetCookie("rem", utils.GetIntToStr(0))
			c.Ctx.SetCookie("loginstate", "no")
		} else {
			beego.Error(err)
		}
		c.TplName = "SportCollage.html"
		return
	}
	// 这里是登录处理,登录后判断登录状态,或者第一次打开网站是后判断当前是否自动登录
	userName := c.Ctx.Input.Cookie("log")
	password := c.Ctx.Input.Cookie("pwd")
	rememberme := c.Ctx.Input.Cookie("rem")
	loginstate := c.Ctx.Input.Cookie("loginstate")

	if !strings.EqualFold(userName, "") && !strings.EqualFold(password, "") && !strings.EqualFold(rememberme, "") && !strings.EqualFold(loginstate, "") {
		if strings.EqualFold(rememberme, "1") {
			nickName, err := models.QueryUserNickName(userName, password)
			beego.Error(err)
			//判断是否查询到用户
			if err == nil {
				c.Data["isLogin"] = true
				if !strings.EqualFold(nickName, "") {
					c.Data["nickName"] = nickName
				} else {
					c.Data["nickName"] = userName
				}
			} else {
				c.Data["isLogin"] = false
			}
		} else {
			if strings.EqualFold(loginstate, "yes") {
				nickName, err := models.QueryUserNickName(userName, password)
				beego.Error(err)
				c.Data["isLogin"] = true
				c.Data["nickName"] = nickName
			} else {
				c.Data["isLogin"] = false
			}
		}
	} else {
		c.Data["isLogin"] = false
	}
	c.TplName = "SportCollage.html"
}
예제 #2
0
func (this *LoginControllers) Post() {
	userName := this.Input().Get("log")
	password := this.Input().Get("pwd")
	rememberme := this.Input().Get("rem")
	//判断是否选择了自动登录
	var status int
	if strings.EqualFold(rememberme, "forever") {
		status = 1
	} else {
		status = 0
	}
	num, err := models.QueryUser(userName, utils.GetMD5Str(password))
	beego.Error(err)
	//判断是否查询到用户
	if err == nil && num > 0 {
		//如果选择自动登录更新数据库登录状态
		err := models.UpdateUserStatus(userName, password, 1)
		if err == nil {
			beego.Debug("update user raw count", num)
		} else {
			beego.Error(err)
		}

		// this.SetSession("log", userName)
		// this.SetSession("pwd", utils.GetMD5Str(password))
		// this.SetSession("rem", utils.GetIntToStr(status))
		// this.SetSession("loginstate", "yes")

		this.Ctx.SetCookie("log", userName)
		this.Ctx.SetCookie("pwd", utils.GetMD5Str(password))
		this.Ctx.SetCookie("rem", utils.GetIntToStr(status))
		this.Ctx.SetCookie("loginstate", "yes")

		this.Redirect("/", 302)
	} else {
		this.Data["isUser"] = true
		this.TplName = "login_page.html"
	}
}