invite.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package model
  2. import (
  3. "net"
  4. "strconv"
  5. accmdl "go-common/app/service/main/account/model"
  6. xtime "go-common/library/time"
  7. )
  8. const (
  9. // StatusOK status ok
  10. StatusOK = 0
  11. // StatusUsed status used
  12. StatusUsed = 1
  13. // StatusExpires status expires
  14. StatusExpires = 2
  15. )
  16. // Invite invite.
  17. type Invite struct {
  18. Status int64 `json:"status"`
  19. Mid int64 `json:"mid"`
  20. Code string `json:"invite_code"`
  21. IP uint32 `json:"-"` // legacy IP field
  22. IPng []byte `json:"-"`
  23. Ctime xtime.Time `json:"buy_time"`
  24. Expires int64 `json:"expires"`
  25. Imid int64 `json:"invited_mid,omitempty"`
  26. UsedAt int64 `json:"used_at,omitempty"`
  27. }
  28. // BuyIPString is
  29. func (inv *Invite) BuyIPString() string {
  30. if inv.IP != 0 {
  31. return inetNtoA(inv.IP)
  32. }
  33. return net.IP(inv.IPng).String()
  34. }
  35. func inetNtoA(sum uint32) string {
  36. ip := make(net.IP, net.IPv4len)
  37. ip[0] = byte((sum >> 24) & 0xFF)
  38. ip[1] = byte((sum >> 16) & 0xFF)
  39. ip[2] = byte((sum >> 8) & 0xFF)
  40. ip[3] = byte(sum & 0xFF)
  41. return ip.String()
  42. }
  43. // FillStatus fill status.
  44. func (inv *Invite) FillStatus(now int64) {
  45. if inv.Used() {
  46. inv.Status = StatusUsed
  47. return
  48. }
  49. if inv.Expired(now) {
  50. inv.Status = StatusExpires
  51. return
  52. }
  53. inv.Status = StatusOK
  54. }
  55. // Used check if used.
  56. func (inv *Invite) Used() bool {
  57. return inv.UsedAt > 0 && inv.Imid > 0
  58. }
  59. // Expired check if expired.
  60. func (inv *Invite) Expired(now int64) bool {
  61. return now > inv.Expires
  62. }
  63. // RichInvite rich invite with invitee info.
  64. type RichInvite struct {
  65. Status int64 `json:"status"`
  66. Mid int64 `json:"mid"`
  67. Code string `json:"invite_code"`
  68. BuyIP string `json:"buy_ip"`
  69. Ctime xtime.Time `json:"buy_time"`
  70. Expires int64 `json:"expires"`
  71. Invitee *Invitee `json:"invitee,omitempty"`
  72. UsedAt int64 `json:"used_at,omitempty"`
  73. }
  74. // NewRichInvite new a rich invite.
  75. func NewRichInvite(inv *Invite, info *accmdl.Info) *RichInvite {
  76. if inv == nil {
  77. return nil
  78. }
  79. var invt *Invitee
  80. if inv.Used() {
  81. if info != nil {
  82. invt = &Invitee{
  83. Mid: inv.Imid,
  84. Uname: info.Name,
  85. Face: info.Face,
  86. }
  87. } else {
  88. invt = &Invitee{
  89. Mid: inv.Imid,
  90. Uname: "用户" + strconv.FormatInt(inv.Imid, 10),
  91. Face: "http://static.hdslb.com/images/member/noface.gif",
  92. }
  93. }
  94. }
  95. return &RichInvite{
  96. Status: inv.Status,
  97. Mid: inv.Mid,
  98. Code: inv.Code,
  99. Ctime: inv.Ctime,
  100. Expires: inv.Expires,
  101. Invitee: invt,
  102. UsedAt: inv.UsedAt,
  103. BuyIP: inv.BuyIPString(),
  104. }
  105. }
  106. // Invitee invited.
  107. type Invitee struct {
  108. Mid int64 `json:"mid"`
  109. Uname string `json:"uname"`
  110. Face string `json:"face"`
  111. }