golang-im聊天
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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