user.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package service
  2. import (
  3. "encoding/json"
  4. "github.com/astaxie/beego/logs"
  5. "github.com/astaxie/beego/orm"
  6. "nginx-ui/server/models"
  7. "nginx-ui/server/utils"
  8. )
  9. type UserService struct {
  10. }
  11. func NewUserService() *UserService {
  12. return &UserService{}
  13. }
  14. func (u *UserService) Login(user *models.User) *models.RespData {
  15. cipherPassword := user.Password
  16. o := orm.NewOrm()
  17. err := o.Read(user, "Account")
  18. if err != nil {
  19. return models.NewErrorResp(err)
  20. }
  21. encryptPassword := utils.GetSHA256HashCode(cipherPassword)
  22. if encryptPassword != user.Password {
  23. return models.ErrorResp("用户名或者密码不正确!")
  24. }
  25. user.Password = ""
  26. return models.SuccessResp(user)
  27. }
  28. func (u *UserService) SignUp(req []byte) *models.RespData {
  29. var user models.User
  30. err := json.Unmarshal(req, &user)
  31. if err != nil {
  32. logs.Error(err, req)
  33. return models.NewErrorResp(err)
  34. }
  35. if len(user.Account) == 0 || len(user.Password) == 0 {
  36. return models.ErrorResp("账号或者密码不能为空!")
  37. }
  38. if len(user.Nickname) == 0 {
  39. user.Nickname = user.Account
  40. }
  41. user.Password = utils.GetSHA256HashCode(user.Password)
  42. o := orm.NewOrm()
  43. _, err = o.Insert(&user)
  44. if err != nil {
  45. return models.NewErrorResp(err)
  46. }
  47. return models.SuccessResp(user).SetMsg("注册成功!")
  48. }