login.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package user
  4. import (
  5. "context"
  6. "crypto/sha256"
  7. "encoding/json"
  8. "fmt"
  9. "git.getensh.com/common/gopkgs/database"
  10. "git.getensh.com/common/gopkgs/logger"
  11. "git.getensh.com/common/gopkgs/util"
  12. "go.uber.org/zap"
  13. "google.golang.org/grpc/status"
  14. "gorm.io/gorm"
  15. "time"
  16. "xingjia-management-gateway/apis"
  17. "xingjia-management-gateway/consts"
  18. "xingjia-management-gateway/errors"
  19. dbmodel "xingjia-management-gateway/model"
  20. )
  21. //
  22. func Login(ctx context.Context, req *apis.LoginRequest) (reply *apis.LoginReply, err error) {
  23. reply = &apis.LoginReply{}
  24. // 捕获各个task中的异常并返回给调用者
  25. defer func() {
  26. if r := recover(); r != nil {
  27. err = fmt.Errorf("%+v", r)
  28. e := &status.Status{}
  29. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  30. logger.Error("err",
  31. zap.String("system_err", err.Error()),
  32. zap.Stack("stacktrace"))
  33. }
  34. }
  35. }()
  36. if req.User == "" || req.Password == "" {
  37. return reply, errors.ParamsError
  38. }
  39. p := &dbmodel.TUser{}
  40. db := database.DB()
  41. where := map[string]interface{}{
  42. "user": req.User,
  43. }
  44. // 查找用户是否存在
  45. err = p.Find(db, where)
  46. if err != nil {
  47. if err == gorm.ErrRecordNotFound {
  48. return reply, errors.UserNotExist
  49. }
  50. return reply, errors.DataBaseError
  51. }
  52. passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
  53. if passwd != p.Password {
  54. return reply, errors.PasswordError
  55. }
  56. if p.UserType == consts.UserTypeTemp && (p.EffectiveEnd < time.Now().Unix() || p.EffectiveStart > time.Now().Unix()) {
  57. return reply, errors.UserNotEffective
  58. }
  59. reply.Uid = p.ID
  60. reply.UserType = p.UserType
  61. reply.EffectiveEnd = p.EffectiveEnd
  62. reply.EffectiveStart = p.EffectiveStart
  63. return reply, nil
  64. }
  65. func InitUser() {
  66. // 检查是否存在默认管理员
  67. p := &dbmodel.TUser{}
  68. count, err := p.Count(database.DB(), nil, nil)
  69. if err != nil {
  70. panic(err.Error())
  71. }
  72. if count > 0 {
  73. return
  74. }
  75. // 不存在则创建
  76. now := time.Now()
  77. password := fmt.Sprintf("%x", sha256.Sum256([]byte("zaq123edc")))
  78. md5pass, _ := util.GetMd5Pass(password, consts.CRYPTO_KEY)
  79. p = &dbmodel.TUser{
  80. User: "admin",
  81. Password: md5pass,
  82. CreatedAt: now,
  83. UpdatedAt: now,
  84. UserType: consts.UserTypeSuper,
  85. RealName: "超级管理员",
  86. EffectiveStart: now.Unix(),
  87. }
  88. err = p.Insert(database.DB())
  89. if err != nil {
  90. panic(err.Error())
  91. }
  92. return
  93. }