예제 #1
0
/*
添加用户页面
*/
func (this *AdminController) Adduser() {
	this.TplNames = "user/add.html"
	if this.GetString("dosubmit") == "yes" {
		account := strings.TrimSpace(this.Input().Get("account"))
		password := strings.TrimSpace(this.Input().Get("password"))
		password1 := strings.TrimSpace(this.Input().Get("password1"))
		username := strings.TrimSpace(this.Input().Get("username"))

		/*
			查找数据库是否存在该账号和博客名称
		*/
		searchaccount := models.SearchAccount(account)
		searchusername := models.SearchUsername(username)

		/*
			判断输入是否合法
		*/
		if len(account) == 0 {
			this.Data["errmsg"] = "请输入账号!"
		} else if len(password) == 0 {
			this.Data["errmsg"] = "请输入密码!"
		} else if len(password1) == 0 {
			this.Data["errmsg"] = "请确认密码!"
		} else if len(username) == 0 {
			this.Data["errmsg"] = "请输入博客名称!"
		} else if searchaccount == nil {
			this.Data["errmsg"] = "该账号已被注册!"
		} else if searchusername == nil {
			this.Data["errmsg"] = "该博客名称已被注册!"
		} else if password1 != password {
			this.Data["errmsg"] = "两次输入的密码不一致!"
		} else {
			/*
				加密注册密码
			*/
			this.Data["zhuce"] = true
			hpassword := md5.New()
			io.WriteString(hpassword, beego.AppConfig.String("salt1"))
			io.WriteString(hpassword, password)
			io.WriteString(hpassword, beego.AppConfig.String("salt2"))
			hpasswordfinal := fmt.Sprintf("%x", hpassword.Sum(nil))
			//记录ip地址
			ip := this.Ctx.Input.IP()
			err := models.Register(account, hpasswordfinal, username, ip)
			if err != nil {
				this.Ctx.WriteString("注册失败!")
			}
		}

	}
}
예제 #2
0
/*
注册页面
*/
func (this *LoginController) Register() {
	this.TplNames = "account/proregister.html"
	if this.GetString("dosubmit") == "yes" {
		account := strings.TrimSpace(this.Input().Get("account"))
		password := strings.TrimSpace(this.Input().Get("password"))
		password1 := strings.TrimSpace(this.Input().Get("password1"))
		username := strings.TrimSpace(this.Input().Get("username"))

		/*
			查找数据库是否存在该账号和博客名称
		*/
		searchaccount := models.SearchAccount(account)
		searchusername := models.SearchUsername(username)
		/*
			判断输入是否合法
		*/
		if len(account) == 0 {
			this.Data["errmsg"] = "请输入账号!"
		} else if len(password) == 0 {
			this.Data["errmsg"] = "请输入密码!"
		} else if len(password1) == 0 {
			this.Data["errmsg"] = "请确认密码!"
		} else if len(username) == 0 {
			this.Data["errmsg"] = "请输入博客名称!"
		} else if searchaccount == nil {
			this.Data["errmsg"] = "该账号已被注册!"
		} else if searchusername == nil {
			this.Data["errmsg"] = "该博客名称已被注册!"
		} else if password1 != password {
			this.Data["errmsg"] = "两次输入的密码不一致!"
		} else {
			/*
				加密注册密码
			*/
			hpassword := md5.New()
			io.WriteString(hpassword, beego.AppConfig.String("salt1"))
			io.WriteString(hpassword, password)
			io.WriteString(hpassword, beego.AppConfig.String("salt2"))
			hpasswordfinal := fmt.Sprintf("%x", hpassword.Sum(nil))
			//记录ip地址
			ip := this.GetClientIp()
			err := models.Register(account, hpasswordfinal, username, ip)
			if err != nil {
				this.Ctx.WriteString("注册失败!")
			} else {
				/*
					注册时默认设置session,不设置cookie,根据account字段查找user表主码id
				*/
				userinfo, _ := models.Userinfo(account)
				uid := userinfo.Id
				//普通用户sessionuid
				sessionuid := this.GetSession("sessionuid")
				if sessionuid == nil {
					//如果session不存在,就创建session
					this.SetSession("sessionuid", uid)
					this.Redirect("/index", 301)
				} else {
					/*
						如果session存在,判断是否是本人博客,URL中获取用户博客名称,然后从
						数据库中读取用户账号,加密后判断是否和session数值一致,一致表示是本人
						博客,显示本人博客全部功能,不一致表示是其他人博客,只显示博客内容,不显示
						博客管理功能!
					*/
					this.Redirect("/index", 301)
					return
				}
			}

		}

	}
}