apply.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/openplatform/article/model"
  6. "go-common/library/ecode"
  7. )
  8. const (
  9. // identify state
  10. // 0: normal phone number 1: virtual phone number 2: no phone number
  11. _identifyPhoneVirtual = 1 // virtual phone number
  12. _identifyPhoneEmpty = 2 // no phone number
  13. )
  14. // ApplyInfo get apply info
  15. // 检查顺序: 是否已通过->拒绝-> 已提交-> 开放申请-> 申请已满 -> 实名认证/封禁状态/绑定手机
  16. func (s *Service) ApplyInfo(c context.Context, mid int64) (res *model.Apply, err error) {
  17. if mid == 0 {
  18. if !s.setting.ApplyOpen {
  19. err = ecode.ArtApplyClose
  20. return
  21. }
  22. if s.checkApplyFull(c) {
  23. err = ecode.ArtApplyFull
  24. return
  25. }
  26. return
  27. }
  28. res = &model.Apply{}
  29. res.Forbid, _, _ = s.UserDisabled(c, mid)
  30. if res.Forbid {
  31. err = ecode.ArtApplyForbid
  32. return
  33. }
  34. if pass, _, _ := s.IsAuthor(c, mid); pass {
  35. err = ecode.ArtApplyPass
  36. return
  37. }
  38. var author *model.AuthorLimit
  39. if author, err = s.dao.RawAuthor(c, mid); err != nil {
  40. return
  41. }
  42. if author != nil {
  43. if author.State == model.AuthorStatePass {
  44. err = ecode.ArtApplyPass
  45. return
  46. } else if author.State == model.AuthorStateReject {
  47. if time.Now().Unix()-int64(author.Rtime) <= s.setting.ApplyFrozenDuration {
  48. err = ecode.ArtApplyReject
  49. return
  50. }
  51. } else if author.State == model.AuthorStatePending {
  52. err = ecode.ArtApplySubmit
  53. return
  54. }
  55. }
  56. if !s.setting.ApplyOpen {
  57. err = ecode.ArtApplyClose
  58. return
  59. }
  60. if s.checkApplyFull(c) {
  61. err = ecode.ArtApplyFull
  62. return
  63. }
  64. var identify *model.Identify
  65. if identify, err = s.dao.Identify(c, mid); err != nil {
  66. return
  67. }
  68. res.Verify = (identify.Identify == 0)
  69. res.Phone = identify.Phone
  70. if res.Phone == _identifyPhoneEmpty {
  71. err = ecode.ArtApplyPhone
  72. }
  73. return
  74. }
  75. func (s *Service) checkApplyFull(c context.Context) (full bool) {
  76. if count, err := s.dao.ApplyCount(c); err != nil {
  77. return
  78. } else if count > 0 {
  79. return count >= s.setting.ApplyLimit
  80. }
  81. return
  82. }
  83. // Apply add apply
  84. func (s *Service) Apply(c context.Context, mid int64, content, category string) (err error) {
  85. var res = &model.Apply{}
  86. if res, err = s.ApplyInfo(c, mid); err != nil {
  87. return
  88. } else if res.Phone == _identifyPhoneVirtual {
  89. err = ecode.ArtApplyPhoneVirtual
  90. return
  91. }
  92. err = s.dao.AddApply(c, mid, content, category)
  93. return
  94. }