golang-im聊天
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

131 рядки
3.1 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 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. CreateTime: time.Now(),
  34. UpdateTime: time.Now(),
  35. }
  36. err := repo.UserRepo.Save(user)
  37. if err != nil {
  38. return false, 0, "", 0, err
  39. }
  40. isNew = true
  41. }
  42. resp, err := rpc.GetLogicIntClient().GetDevice(ctx, &pb.GetDeviceReq{DeviceId: deviceId})
  43. if err != nil {
  44. return false, 0, "", 0, err
  45. }
  46. // 方便测试
  47. //token := "0"
  48. token := util.RandString(40)
  49. err = repo.AuthRepo.Set(user.Id, resp.Device.DeviceId, model.Device{
  50. Type: resp.Device.Type,
  51. Token: token,
  52. Expire: time.Now().AddDate(0, 3, 0).Unix(),
  53. })
  54. if err != nil {
  55. return false, 0, "", 0, err
  56. }
  57. if pushAlia != "" {
  58. userPushForJg, err := db.DbUserPushForJg.UserPushForJgGetOne(strconv.FormatInt(user.Id, 10), masterId)
  59. if err != nil {
  60. return false, 0, "", 0, err
  61. }
  62. if userPushForJg == nil {
  63. save := db.DbUserPushForJg.UserPushForJgInsert(user.Id, masterId, pushAlia)
  64. if !save {
  65. return false, 0, "", 0, errors.New("插入user_push_for_jg记录失败")
  66. }
  67. } else {
  68. update := db.DbUserPushForJg.UserPushForJgUpdate(user.Id, masterId, pushAlia)
  69. if !update {
  70. return false, 0, "", 0, errors.New("修改user_push_for_jg记录失败")
  71. }
  72. }
  73. }
  74. return isNew, user.Id, token, masterId, nil
  75. }
  76. func Verify(phoneNumber, code string) bool {
  77. // 假装他成功了
  78. return true
  79. }
  80. // Auth 验证用户是否登录
  81. func (*authService) Auth(ctx context.Context, userId, deviceId int64, token string) error {
  82. device, err := repo.AuthRepo.Get(userId, deviceId)
  83. if err != nil {
  84. return err
  85. }
  86. if device == nil {
  87. return gerrors.ErrUnauthorized
  88. }
  89. if device.Expire < time.Now().Unix() {
  90. return gerrors.ErrUnauthorized
  91. }
  92. if device.Token != token {
  93. return gerrors.ErrUnauthorized
  94. }
  95. return nil
  96. }
  97. // AuthMaster 验证站长权限
  98. func (*authService) AuthMaster(ctx context.Context, masterId string) error {
  99. master, err := repo.MasterRepo.Get(utils.StrToInt64(masterId))
  100. fmt.Println(">>>>>>>>>>>>>>AuthMaster>>>>>>>>>>>>>>>", master, err)
  101. if err != nil {
  102. return err
  103. }
  104. if master == nil {
  105. return gerrors.ErrMasterUnauthorized
  106. }
  107. packageExpireTime, _ := utils.StrToTime(master.PackageExpireTime)
  108. fmt.Println(">>>>>>>>>>>>>>packageExpireTime>>>>>>>>>>>>>>>", packageExpireTime, time.Now().Unix())
  109. if packageExpireTime < time.Now().Unix() {
  110. return gerrors.ErrMasterUnauthorized
  111. }
  112. return nil
  113. }