Ejemplo n.º 1
0
// @Title Post
// @Description create TUser
// @Param	body		body 	models.TUser	true		"body for TUser content"
// @Success 200 {int} models.TUser.Id
// @Failure 403 body is empty
// @router / [post]
func (c *TUserController) Post() {
	var v models.TUser
	json.Unmarshal(c.Ctx.Input.RequestBody, &v)
	if id, err := models.AddTUser(&v); err == nil {
		c.Data["json"] = map[string]int64{"id": id}
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJson()
}
Ejemplo n.º 2
0
func (c *TUserController) SignUp() {
	//isAjax:=c.Input.IsAjax()
	isPost := c.Ctx.Input.IsPost()
	//用户已经登录
	userLogIned := c.GetSession("user")
	if userLogIned != nil {
		c.Ctx.Redirect(301, "/")
		return
	}
	//方法不为POST的时候
	if isPost == false {
		c.Layout = "layout.html"
		c.TplNames = "user/signup.html"
		return
	}
	user := models.TUser{}
	//@解析传入的Form
	if err := c.ParseForm(&user); err != nil {
		c.Data["json"] = "数据解析错误"
		c.ServeJson()
		return
	}
	//@校验Form的数据格式
	valid := validation.Validation{}
	b, err := valid.Valid(&user)
	if err != nil || !b {
		c.Data["json"] = "请根据要求填写用户名,密码和邮箱"
		c.ServeJson()
	}
	hashMD5 := md5.New()
	io.WriteString(hashMD5, user.Password)
	user.Password = fmt.Sprintf("%x", hashMD5.Sum(nil))
	if id, err := models.AddTUser(&user); err == nil {
		user.Id = int(id)
	} else {
		c.Data["json"] = "用户名或邮箱已经存在"
		c.ServeJson()
		return
	}
	c.SetSession("user", user)
	c.Ctx.Redirect(301, "/recommend")
}