golang-im聊天
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

133 行
3.2 KiB

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