示例#1
0
文件: user.go 项目: jinzhe/GOSNS
func ac_user(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		email := r.FormValue("email")
		user := r.FormValue("user")
		pwd := r.FormValue("pwd")
		if !forms.Reg_email(email) {
			ExitMsg(w, "邮箱格式错误")
			return
		}

		if !forms.Reg_user(user) {
			ExitMsg(w, "用户名格式错误")
			return
		}
		if pwd == "" {
			ExitMsg(w, "密码不可为空")
			return
		}
		pwd = forms.Tomd5(pwd)

		if !captcha.VerifyString(r.FormValue("captchaId"), r.FormValue("captchaSolution")) {
			ExitMsg(w, "验证码错误")
			return
		}
		if User_ishaveEmail(email) {
			ExitMsg(w, "Email 已存在")
			return
		}
		if User_ishaveName(user) {
			ExitMsg(w, "用户名 已存在")
			return
		}
		C := Db.C("go_user")
		id := bson.NewObjectId()
		err := C.Insert(&User{Id_: id, Username: user, Pwd: pwd, Email: email, Adminid: 0,
			Regip: forms.Ip(r), Lastip: forms.Ip(r), Lasttime: time.Now()})
		CheckErr(err)
		fmt.Fprint(w, "添加成功")
		return
	}

	t, err := template.ParseFiles("template/user/index.html")
	CheckErr(err)
	t.Execute(w, struct{ CaptchaId string }{captcha.New()})
}
示例#2
0
文件: user.go 项目: jinzhe/GOSNS
func ac_login(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		email := r.FormValue("email")
		pwd := r.FormValue("pwd")
		loginuser := User{}
		err := Db.C("go_user").Find(bson.M{"email": email}).One(&loginuser)
		if err != nil {
			ExitMsg(w, "用户名不存在")
			return
		}
		if loginuser.Pwd != forms.Tomd5(pwd) {
			ExitMsg(w, "密码错误")
			return
		}
		Login_info(loginuser.Id_, r)
		sess := Gsession.SessionStart(w, r)
		sess.Set("User", loginuser.Id_.Hex())
		http.Redirect(w, r, "/user/center", http.StatusFound)
		return
	}
	IndexTemplate(w, r, map[string]interface{}{}, "template/user/login.html")
}