type RegisterReq struct { Email string Password string } // @Title Register // @Description Register User // @Param body body controllers.RegisterReq true "body for User register" // @Success 201 {int} models.User // @Failure 403 body is empty // @router /register [post] 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, "@" )[0] 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 // @Param body body controllers.LoginReq true "body for User login" // @Success 201 {int} models.User // @Failure 403 body is empty // @router /login [post] 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[0][ "id" ].(string)) nickname := user[0][ "nickname" ].(string) tokenString := utils.GetToken(id, v.LoginId, nickname) c.Data[ "json" ] = map [string] interface {}{ "success" : 0, "msg" : "登录成功" , "token" :tokenString, "email" :v.LoginId, "nickname" :nickname, "id" :id} } else { c.Data[ "json" ] = map [string] interface {}{ "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 // @Param body body controllers.ChangePasswordReq true "body for Change Password" // @Success 201 {int} models.User // @Failure 403 body is empty // @router /change_password [put] 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" ] = map [string] interface {}{ "success" : -1, "msg" : "账号密码不对" } } } else { c.Data[ "json" ] = err.Error() } c.ServeJSON() } // Put ... // @Title Put // @Description update the User // @Param id path string true "The id you want to update" // @Param body body models.User true "body for User content" // @Success 200 {object} models.User // @Failure 403 :id is not int // @router /:id [put] func (c *UserController) Put() { idStr := c.Ctx.Input.Param( ":id" ) id, _ := strconv.Atoi(idStr) v := models.User{Id: 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 [get] 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 [get] func (c *UserController) Funds() { uid := int(c.GetUserIdByToken()) fund, _ := models.GetAllAccountByUserId(uid) c.Data[ "json" ] = fund c.ServeJSON() } |