You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auth.go 2.4 KiB

3 days ago
2 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
3 days ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package service
  2. import (
  3. "context"
  4. "egg-im/internal/business/comm/db"
  5. "egg-im/internal/business/domain/user/model"
  6. "egg-im/internal/business/domain/user/repo"
  7. "egg-im/pkg/gerrors"
  8. "egg-im/pkg/pb"
  9. "egg-im/pkg/rpc"
  10. "egg-im/pkg/util"
  11. "errors"
  12. "strconv"
  13. "time"
  14. )
  15. type authService struct{}
  16. var AuthService = new(authService)
  17. // SignIn 登录
  18. func (*authService) SignIn(ctx context.Context, phoneNumber, code string, deviceId int64, pushAlia, nickname, avatarUrl string) (bool, int64, string, error) {
  19. if !Verify(phoneNumber, code) {
  20. return false, 0, "", gerrors.ErrBadCode
  21. }
  22. user, err := repo.UserRepo.GetByPhoneNumber(phoneNumber)
  23. if err != nil {
  24. return false, 0, "", err
  25. }
  26. var isNew = false
  27. if user == nil {
  28. user = &model.User{
  29. PhoneNumber: phoneNumber,
  30. Nickname: nickname,
  31. AvatarUrl: avatarUrl,
  32. CreateTime: time.Now(),
  33. UpdateTime: time.Now(),
  34. }
  35. err := repo.UserRepo.Save(user)
  36. if err != nil {
  37. return false, 0, "", err
  38. }
  39. isNew = true
  40. }
  41. resp, err := rpc.GetLogicIntClient().GetDevice(ctx, &pb.GetDeviceReq{DeviceId: deviceId})
  42. if err != nil {
  43. return false, 0, "", err
  44. }
  45. // 方便测试
  46. //token := "0"
  47. token := util.RandString(40)
  48. err = repo.AuthRepo.Set(user.Id, resp.Device.DeviceId, model.Device{
  49. Type: resp.Device.Type,
  50. Token: token,
  51. Expire: time.Now().AddDate(0, 3, 0).Unix(),
  52. })
  53. if err != nil {
  54. return false, 0, "", err
  55. }
  56. if pushAlia != "" {
  57. userPushForJg, err := db.DbUserPushForJg.UserPushForJgGetOne(strconv.FormatInt(user.Id, 10))
  58. if err != nil {
  59. return false, 0, "", err
  60. }
  61. if userPushForJg == nil {
  62. save := db.DbUserPushForJg.UserPushForJgInsert(user.Id, pushAlia)
  63. if !save {
  64. return false, 0, "", errors.New("插入user_push_for_jg记录失败")
  65. }
  66. } else {
  67. update := db.DbUserPushForJg.UserPushForJgUpdate(user.Id, pushAlia)
  68. if !update {
  69. return false, 0, "", errors.New("修改user_push_for_jg记录失败")
  70. }
  71. }
  72. }
  73. return isNew, user.Id, token, nil
  74. }
  75. func Verify(phoneNumber, code string) bool {
  76. // 假装他成功了
  77. return true
  78. }
  79. // Auth 验证用户是否登录
  80. func (*authService) Auth(ctx context.Context, userId, deviceId int64, token string) error {
  81. device, err := repo.AuthRepo.Get(userId, deviceId)
  82. if err != nil {
  83. return err
  84. }
  85. if device == nil {
  86. return gerrors.ErrUnauthorized
  87. }
  88. if device.Expire < time.Now().Unix() {
  89. return gerrors.ErrUnauthorized
  90. }
  91. if device.Token != token {
  92. return gerrors.ErrUnauthorized
  93. }
  94. return nil
  95. }