user.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package service
  2. import (
  3. "context"
  4. "go-common/library/log"
  5. "go-common/app/admin/ep/marthe/model"
  6. "go-common/library/ecode"
  7. )
  8. // QueryUserInfo query user info.
  9. func (s *Service) QueryUserInfo(c context.Context, username string) (userInfo *model.User, err error) {
  10. if userInfo, err = s.dao.FindUserByUserName(username); err != nil {
  11. return
  12. }
  13. if userInfo.ID == 0 {
  14. user := model.User{Name: username, EMail: username + "@bilibili.com"}
  15. s.dao.CreateUser(&user)
  16. userInfo, err = s.dao.FindUserByUserName(username)
  17. }
  18. return
  19. }
  20. // HttpSyncWechatContacts Http Sync Wechat Contacts.
  21. func (s *Service) HttpSyncWechatContacts(ctx context.Context) (err error) {
  22. s.taskCache.Do(context.TODO(), func(ctx context.Context) {
  23. s.SyncWechatContacts()
  24. })
  25. return
  26. }
  27. // SyncWechatContacts Sync Wechat Contacts.
  28. func (s *Service) SyncWechatContacts() (err error) {
  29. s.syncWechatContactsLock.Lock()
  30. defer s.syncWechatContactsLock.Unlock()
  31. log.Info("start")
  32. var (
  33. wechatContacts []*model.WechatContact
  34. contactInfosInDB []*model.ContactInfo
  35. )
  36. if wechatContacts, err = s.dao.WechatContacts(context.Background()); err != nil {
  37. return
  38. }
  39. if contactInfosInDB, err = s.dao.QueryAllContactInfos(); err != nil {
  40. return
  41. }
  42. for _, wechatContact := range wechatContacts {
  43. var (
  44. tmpContactInfoInDB *model.ContactInfo
  45. )
  46. for _, contactInfoInDB := range contactInfosInDB {
  47. if contactInfoInDB.UserID == wechatContact.UserID {
  48. tmpContactInfoInDB = contactInfoInDB
  49. break
  50. }
  51. }
  52. if tmpContactInfoInDB != nil {
  53. //update
  54. tmpContactInfoInDB.UserName = wechatContact.EnglishName
  55. tmpContactInfoInDB.NickName = wechatContact.Name
  56. if err = s.dao.UpdateContactInfo(tmpContactInfoInDB); err != nil {
  57. continue
  58. }
  59. } else {
  60. //add
  61. tmpContactInfoInDB = &model.ContactInfo{
  62. UserName: wechatContact.EnglishName,
  63. NickName: wechatContact.Name,
  64. UserID: wechatContact.UserID,
  65. }
  66. if err = s.dao.InsertContactInfo(tmpContactInfoInDB); err != nil {
  67. continue
  68. }
  69. }
  70. }
  71. log.Info("end")
  72. return
  73. }
  74. // AccessToBugly Access To Bugly.
  75. func (s *Service) AccessToBugly(c context.Context, username string) (isAccess bool) {
  76. var (
  77. userInfo *model.User
  78. err error
  79. )
  80. if userInfo, err = s.QueryUserInfo(c, username); err != nil {
  81. return
  82. }
  83. isAccess = userInfo.VisibleBugly
  84. return
  85. }
  86. // UpdateUserVisibleBugly Update User Visible Bugly.
  87. func (s *Service) UpdateUserVisibleBugly(c context.Context, username, updateUsername string, visibleBugly bool) (err error) {
  88. var (
  89. hasRight bool
  90. userInfo *model.User
  91. )
  92. for _, super := range s.c.Bugly.SuperOwner {
  93. if username == super {
  94. hasRight = true
  95. break
  96. }
  97. }
  98. if !hasRight {
  99. err = ecode.AccessDenied
  100. return
  101. }
  102. if userInfo, err = s.dao.FindUserByUserName(updateUsername); err != nil {
  103. return
  104. }
  105. if userInfo.ID == 0 {
  106. err = ecode.NothingFound
  107. return
  108. }
  109. err = s.dao.UpdateUserVisibleBugly(userInfo.ID, visibleBugly)
  110. return
  111. }