posir 发表于 2018-9-20 11:37:58

golang学习笔记16 beego orm 数据库操作

type RegisterReq struct {  
Email string
  
Password string
  
}
  

  
// @Title Register
  
// @Description Register User
  
// @Parambodybody controllers.RegisterReqtrue"body for User register"
  
// @Success 201 {int} models.User
  
// @Failure 403 body is empty
  
// @router /register
  
func (c *UserController) Register() {
  
var v RegisterReq
  
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
  
md5pwd := c.GetMd5String(v.Password + beego.AppConfig.String("MD5_SALT"))
  
var u models.User
  
u.Email = v.Email
  
u.Password = md5pwd
  
u.Step = 0
  
u.Status = 0
  
u.Level = 0
  
u.Role = 0
  
u.Nickname = strings.Split(v.Email, "@")
  
u.CreateTime = time.Now()
  
u.UpdateTime = time.Now()
  
if _, err := models.AddUser(&u); err == nil {
  
//c.Ctx.Output.SetStatus(201)
  
c.Data["json"] = u
  
utils.SendmailForVerify(v.Email)
  
} else {
  
//"Error 1062: Duplicate entry 'xxx' for key 'email'"
  
c.Data["json"] = err.Error()
  
}
  
} else {
  
c.Data["json"] = err.Error()
  
}
  
c.ServeJSON()
  
}
  

  
type LoginReq struct {
  
LoginId string `description:"Email or Phone"`
  
Password string
  
}
  

  
// @Title Login
  
// @Description Login
  
// @Parambodybody controllers.LoginReqtrue"body for User login"
  
// @Success 201 {int} models.User
  
// @Failure 403 body is empty
  
// @router /login
  
func (c *UserController) Login() {
  
var v LoginReq
  
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
  
md5pwd := c.GetMd5String(v.Password + beego.AppConfig.String("MD5_SALT"))
  
user, _ := models.GetUserByEmailOrMobileAndPassword(v.LoginId,md5pwd)
  
if user != nil {
  
// get token uid nickname
  
id, _:= strconv.Atoi(user["id"].(string))
  
nickname := user["nickname"].(string)
  
tokenString := utils.GetToken(id, v.LoginId, nickname)
  
c.Data["json"] = mapinterface{}{"success": 0, "msg": "登录成功","token":tokenString,"email":v.LoginId,"nickname":nickname,"id":id}
  
} else {
  
c.Data["json"] = mapinterface{}{"success": -1, "msg": "账号密码不对或邮箱未验证激活"}
  
}
  
} else {
  
c.Data["json"] = err.Error()
  
}
  
c.ServeJSON()
  
}
  

  
type ChangePasswordReq struct {
  
OldPassword string
  
NewPassword string
  
}
  

  
// @Title Change Password
  
// @Description Change Password
  
// @Security mySecurityApiKey
  
// @Parambodybody controllers.ChangePasswordReqtrue"body for Change Password"
  
// @Success 201 {int} models.User
  
// @Failure 403 body is empty
  
// @router /change_password
  
func (c *UserController) ChangePassword() {
  
email := c.GetUserMailByToken()
  
var v ChangePasswordReq
  
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
  
md5pwd := c.GetMd5String(v.OldPassword + beego.AppConfig.String("MD5_SALT"))
  
user, _ := models.GetUserByEmailOrMobileAndPassword(email, md5pwd)
  
if user != nil {
  
u, _ := models.GetUserByFilter("email", email)
  
u.Password = c.GetMd5String(v.NewPassword + beego.AppConfig.String("MD5_SALT"))
  
models.UpdateUserById(u)
  
c.Data["json"] = Response{0, "success.", nil}
  
} else {
  
c.Data["json"] = mapinterface{}{"success": -1, "msg": "账号密码不对"}
  
}
  
} else {
  
c.Data["json"] = err.Error()
  
}
  
c.ServeJSON()
  
}
  

  
// Put ...
  
// @Title Put
  
// @Description update the User

  
// @Paramidpath stringtrue"The>  
// @Parambodybody models.Usertrue"body for User content"
  
// @Success 200 {object} models.User
  
// @Failure 403 :id is not int
  
// @router /:id
  
func (c *UserController) Put() {
  
idStr := c.Ctx.Input.Param(":id")
  
id, _ := strconv.Atoi(idStr)

  
v := models.User{Id:>  
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
  
if err := models.UpdateUserById(&v); err == nil {
  
c.Data["json"] = "OK"
  
} else {
  
c.Data["json"] = err.Error()
  
}
  
} else {
  
c.Data["json"] = err.Error()
  
}
  
c.ServeJSON()
  
}
  

  
// @Title Get user profile
  
// @Description get user profile
  
// @Security mySecurityApiKey
  
// @Success 200 {object} models.User
  
// @router /profile
  
func (c *UserController) Profile() {
  
uid := c.GetUserIdByToken()
  
v, err := models.GetUserById(uid)
  
if err != nil {
  
c.Data["json"] = err.Error()
  
} else {
  
c.Data["json"] = v
  
}
  
c.ServeJSON()
  
}
  

  
// Get Funds ...
  
// @Get My Funds
  
// @Security mySecurityApiKey
  
// @Description get my Funds
  
// @Success 200 {object} []models.Fund
  
// @Failure 403
  
// @router /funds
  
func (c *UserController) Funds() {
  
uid := int(c.GetUserIdByToken())
  
fund, _ := models.GetAllAccountByUserId(uid)
  
c.Data["json"] = fund
  
c.ServeJSON()
  
}
  



页: [1]
查看完整版本: golang学习笔记16 beego orm 数据库操作