Exemplo n.º 1
0
func HandlSignUp(w http.ResponseWriter, r *http.Request) {
	tip := SignTip{UnameTip: "", UemailTip: ""}

	if r.Method == "GET" {
		Render.HTML(w, http.StatusOK, "signUp", tip)
	}

	if r.Method == "POST" {
		r.ParseForm()
		uinfo := &models.UserInfo{
			Uname:  r.FormValue("uname"),
			Pwd:    r.FormValue("pwd"),
			Uemail: r.FormValue("uemail")}
		//检查用户名是否已经存在
		if models.IsunameTaken(uinfo.Uname, w) {
			tip.UnameTip = "此用户名已被占用"
			tip.UemailTip = ""
			Render.HTML(w, http.StatusOK, "signUp", tip)
			return
		}
		//检查邮箱是否已经注册过了
		if models.IsuemailTaken(uinfo.Uemail, w) {

			tip.UnameTip = ""
			tip.UemailTip = "此邮箱名已注册"
			Render.HTML(w, http.StatusOK, "signUp", tip)
			return
		}
		//在数据库中进行注册
		if err := uinfo.SignUpInDB(); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		} else {
			Render.HTML(w, http.StatusOK, "signUpSecusess", nil)
		}

	}

}
Exemplo n.º 2
0
func HandlLogin(w http.ResponseWriter, r *http.Request) {
	lgTip := LoginTip{UnameTip: "",
		PwdTip: ""}
	if r.Method == "GET" {
		if r.FormValue("exit") == "true" {
			session, _ := store.Get(r, "normLogin")
			session.Values["uname"] = r.FormValue("uname")
			session.Values["IsLogin"] = false
			lgTip.TmplData.State["IsLogin"] = session.Values["IsLogin"]
			session.Options.MaxAge = -1
			session.Save(r, w)
		}
		Render.HTML(w, http.StatusOK, "login", lgTip)
		return
	}

	if r.Method == "POST" {
		r.ParseForm()
		if !models.IsunameTaken(r.FormValue("uname"), w) {
			lgTip.UnameTip = "用户名不存在"
			lgTip.PwdTip = ""
			Render.HTML(w, http.StatusOK, "login", lgTip)
		}
		if !models.VerifyPwd(r.FormValue("uname"), r.FormValue("pwd")) {
			lgTip.UnameTip = ""
			lgTip.PwdTip = "密码错误"
			Render.HTML(w, http.StatusOK, "login", lgTip)
		}
		session, _ := store.Get(r, "normLogin")
		session.Values["Uname"] = r.FormValue("uname")
		session.Values["IsLogin"] = true
		lgTip.TmplData.State["IsLogin"] = session.Values["IsLogin"]
		session.Options.MaxAge = 0
		session.Save(r, w)
		return

	}
}