review_audit.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  6. "net/http"
  7. "net/url"
  8. "path"
  9. "go-common/app/admin/main/member/model"
  10. comodel "go-common/app/service/main/coin/model"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. )
  14. var upNameCostCoins = 6.0
  15. type auditHandler func(ctx context.Context, origin *model.UserPropertyReview, arg *model.ArgReviewAudit) error
  16. func (s *Service) initAuditHandler() {
  17. s.auditHandlers[auditKey(model.ReviewPropertySign, true)] = s.onSignAudit
  18. s.auditHandlers[auditKey(model.ReviewPropertyName, true)] = s.onNameAudit
  19. s.auditHandlers[auditKey(model.ReviewPropertyFace, true)] = s.onFaceMonitorAudit
  20. s.auditHandlers[auditKey(model.ReviewPropertyFace, false)] = s.onFaceAudit
  21. }
  22. func auditKey(property int8, isMonitor bool) string {
  23. return fmt.Sprintf("%d-%t", property, isMonitor)
  24. }
  25. func (s *Service) onSignAudit(ctx context.Context, origin *model.UserPropertyReview, arg *model.ArgReviewAudit) error {
  26. switch arg.State {
  27. case model.ReviewStatePass:
  28. return s.dao.UpSign(ctx, origin.Mid, origin.New)
  29. case model.ReviewStateNoPass:
  30. if err := s.dao.Message(ctx, "违规签名处理通知", "抱歉,由于你的签名涉嫌违规,系统已将你的签名回退。如有疑问请联系客服。", []int64{origin.Mid}); err != nil {
  31. log.Error("Failed to send message: mid: %d: %+v", origin.Mid, err)
  32. }
  33. return nil
  34. }
  35. return nil
  36. }
  37. func (s *Service) onNameAudit(ctx context.Context, origin *model.UserPropertyReview, arg *model.ArgReviewAudit) error {
  38. switch arg.State {
  39. case model.ReviewStatePass:
  40. if origin.NickFree() {
  41. return s.dao.UpdateUname(ctx, origin.Mid, origin.New)
  42. }
  43. coins, err := s.coinRPC.UserCoins(ctx, &comodel.ArgCoinInfo{Mid: origin.Mid})
  44. if err != nil {
  45. return err
  46. }
  47. if coins < upNameCostCoins {
  48. return ecode.UpdateUnameMoneyIsNot
  49. }
  50. if err := s.dao.UpdateUname(ctx, origin.Mid, origin.New); err != nil {
  51. log.Error("faild to update Name, mid: %d, name: %s, %+v", origin.Mid, origin.New, err)
  52. return err
  53. }
  54. arg := &comodel.ArgModifyCoin{
  55. Mid: origin.Mid,
  56. Count: -upNameCostCoins,
  57. Reason: fmt.Sprintf("UPDATE:NICK:%s=>%s", origin.Old, origin.New),
  58. }
  59. if _, err := s.coinRPC.ModifyCoin(ctx, arg); err != nil {
  60. log.Error("faild to modify coin, arg: %+v, %+v", arg, err)
  61. return err
  62. }
  63. case model.ReviewStateNoPass:
  64. if err := s.dao.Message(ctx, "违规昵称处理通知", "抱歉,由于你的昵称涉嫌违规,系统已将你的昵称回退。如有疑问请联系客服。", []int64{origin.Mid}); err != nil {
  65. log.Error("Failed to send message: mid: %d: %+v", origin.Mid, err)
  66. }
  67. return nil
  68. }
  69. return nil
  70. }
  71. func (s *Service) onFaceAudit(ctx context.Context, origin *model.UserPropertyReview, arg *model.ArgReviewAudit) error {
  72. switch arg.State {
  73. case model.ReviewStateNoPass:
  74. if err := s.dao.UpFace(ctx, origin.Mid, ""); err != nil {
  75. return err
  76. }
  77. return s.faceReject(ctx, origin, arg)
  78. }
  79. return nil
  80. }
  81. func (s *Service) onFaceMonitorAudit(ctx context.Context, origin *model.UserPropertyReview, arg *model.ArgReviewAudit) error {
  82. switch arg.State {
  83. case model.ReviewStatePass:
  84. return s.dao.UpFace(ctx, origin.Mid, origin.New)
  85. case model.ReviewStateNoPass:
  86. return s.faceReject(ctx, origin, arg)
  87. }
  88. return nil
  89. }
  90. func (s *Service) mvToPrivate(ctx context.Context, face string) (string, error) {
  91. file, err := s.dao.Image(buildURL(face))
  92. if err != nil {
  93. return "", err
  94. }
  95. ftype := http.DetectContentType(file)
  96. privURL, err := s.dao.UploadImage(ctx, ftype, file, s.c.FacePriBFS)
  97. if err != nil {
  98. return "", err
  99. }
  100. if err := s.dao.DelImage(ctx, path.Base(privURL), s.c.FaceBFS); err != nil {
  101. log.Error("s.dao.DelImage(%v) error(%+v)", privURL, err)
  102. }
  103. return urlPath(privURL), nil
  104. }
  105. func buildURL(path string) string {
  106. return fmt.Sprintf("http://i%d.hdslb.com%s", rand.Int63n(3), path)
  107. }
  108. func urlPath(in string) string {
  109. URL, err := url.Parse(in)
  110. if err != nil {
  111. return ""
  112. }
  113. return URL.Path
  114. }
  115. func (s *Service) faceReject(ctx context.Context, origin *model.UserPropertyReview, arg *model.ArgReviewAudit) error {
  116. privFace, err := s.mvToPrivate(ctx, origin.New)
  117. if err != nil {
  118. log.Error("s.mvToPrivate(%d) error(%+v)", origin.Mid, err)
  119. return err
  120. }
  121. if err := s.dao.UpdateReviewFace(ctx, origin.ID, privFace); err != nil {
  122. log.Error("Failed to update review face: id: %d face: %s: %+v", origin.ID, privFace, err)
  123. return err
  124. }
  125. if err := s.dao.MvArchivedFaceToPriv(ctx, origin.New, privFace, arg.Operator, arg.Remark); err != nil {
  126. log.Error("mv archived face to private bucket mid(%d) error(%v)", origin.Mid, err)
  127. return err
  128. }
  129. if err := s.dao.Message(ctx, "违规头像处理通知", "抱歉,由于你的头像涉嫌违规,系统已将你的头像回退。如有疑问请联系客服。", []int64{origin.Mid}); err != nil {
  130. log.Error("Failed to send message: mid: %d: %+v", origin.Mid, err)
  131. }
  132. if err := s.dao.IncrFaceReject(ctx, origin.Mid); err != nil {
  133. log.Error("IncrFaceReject faild: mid: %d: %+v", origin.Mid, err)
  134. }
  135. return nil
  136. }