Esempio n. 1
0
func (self *SignupHandler) Post() {

	self.TplNames = "signup.html"

	flash := beego.NewFlash()
	email := strings.ToLower(self.GetString("email"))
	username := strings.ToLower(self.GetString("username"))
	password := self.GetString("password")
	repassword := self.GetString("repassword")

	if password == "" {
		flash.Error("密码为空~")
		flash.Store(&self.Controller)

		return

	}

	if password != repassword {
		flash.Error("两次密码不匹配~")
		flash.Store(&self.Controller)

		return

	}

	if helper.CheckPassword(password) == false {
		flash.Error("密码含有非法字符或密码过短(至少4~30位密码)!")
		flash.Store(&self.Controller)

		return

	}

	if username == "" {
		flash.Error("用户名是为永久性设定,不能少于4个字或多于30个字,请慎重考虑,不能为空~")
		flash.Store(&self.Controller)

		return

	}

	if helper.CheckUsername(username) == false {
		flash.Error("用户名是为永久性设定,不能少于4个字或多于30个字,请慎重考虑,不能为空~")
		flash.Store(&self.Controller)

		return
	}

	if helper.CheckEmail(email) == false {
		flash.Error("Email格式不合符规格~")
		flash.Store(&self.Controller)

		return

	}

	if usrinfo, err := model.GetUserByEmail(email); usrinfo != nil {

		flash.Error("此账号不能使用~")
		flash.Store(&self.Controller)

		return

	} else if err != nil {

		flash.Error("检索账号期间出错~")
		flash.Store(&self.Controller)

		return
	}

	if usrid, err := model.AddUser(email, username, "", "", helper.Encrypt_hash(password, nil), 1); err != nil {
		flash.Error("用户注册信息写入数据库时发生错误~")
		flash.Store(&self.Controller)

		return

	} else {

		if usrinfo, err := model.GetUser(usrid); err == nil {

			//注册账号成功,以下自动登录并设置session
			self.SetSession("userid", usrid)
			self.SetSession("username", usrinfo.Username)
			self.SetSession("userrole", usrinfo.Role)
			self.SetSession("useremail", usrinfo.Email)
			self.SetSession("usercontent", usrinfo.Content)

			flash.Notice("账号登录成功~")
			flash.Store(&self.Controller)

			//session 写入后直接跳到首页
			self.Redirect("/", 302)

		} else {

			flash.Notice("注册账号成功,请手动登录~")
			flash.Store(&self.Controller)

			//注册成功后直接跳转到登录页
			self.Redirect("/signin/", 302)

		}

	}

}
Esempio n. 2
0
func (self *Settings) Profile() {

	flash := beego.NewFlash()
	self.TplNames = "settings.html"

	sess_userid, _ := self.GetSession("userid").(int64)

	username := self.GetString("username")
	email := self.GetString("email")

	nickname := self.GetString("nickname")
	realname := self.GetString("realname")

	content := self.GetString("content")
	birth := self.GetString("birth")
	fmt.Println(birth)
	province := self.GetString("province")
	city := self.GetString("city")
	company := self.GetString("company")
	address := self.GetString("address")

	postcode := self.GetString("postcode")
	mobile := self.GetString("mobile")
	website := self.GetString("website")
	sex, _ := self.GetInt("sex")
	qq := self.GetString("qq")
	msn := self.GetString("msn")
	weibo := self.GetString("weibo")

	if username == "" {
		flash.Error("用户名不能为空!")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

	if email == "" {
		flash.Error("Email是你的主账号,和主要联系方式,不能留空~")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

	if content == "" {
		flash.Error("为了让别人更了解你,请务必填写你的个人签名~")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

	if helper.CheckUsername(username) == false {
		flash.Error("用户名包含非法字符,或不合符规格(限4~30个字符)~")
		flash.Store(&self.Controller)

		return

	}

	if helper.CheckEmail(email) == false {
		flash.Error("Email格式不合符规格~")
		flash.Store(&self.Controller)

		return

	}

	if usrinfo, err := model.GetUser(sess_userid); usrinfo != nil && err == nil {

		usrinfo.Username = username
		usrinfo.Email = email

		usrinfo.Nickname = nickname
		usrinfo.Realname = realname
		usrinfo.Content = content
		usrinfo.Birth = time.Now()
		usrinfo.Province = province
		usrinfo.City = city
		usrinfo.Company = company
		usrinfo.Address = address
		usrinfo.Postcode = postcode
		usrinfo.Mobile = mobile
		usrinfo.Website = website
		usrinfo.Sex = sex
		usrinfo.Qq = qq
		usrinfo.Msn = msn
		usrinfo.Weibo = weibo

		if _, err := model.PutUser(usrinfo.Id, usrinfo); err == nil {

			//更新session
			self.SetSession("userid", usrinfo.Id)
			self.SetSession("username", usrinfo.Username)
			self.SetSession("userrole", usrinfo.Role)
			self.SetSession("useremail", usrinfo.Email)
			self.SetSession("usercontent", usrinfo.Content)

			flash.Notice("设置个人信息成功~")
		} else {
			flash.Error("设置个人信息失败~")
		}
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	} else {

		flash.Error("该账号不存在~")
		flash.Store(&self.Controller)

		self.Redirect("/settings/", 302)
		return
	}

}