mobile.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package mobile
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/app-wall/model"
  6. "go-common/app/interface/main/app-wall/model/mobile"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _mobileKey = "mobile"
  12. )
  13. // InOrdersSync insert OrdersSync
  14. func (s *Service) InOrdersSync(c context.Context, ip string, u *mobile.MobileXML, now time.Time) (err error) {
  15. if !s.iplimit(_mobileKey, ip) {
  16. err = ecode.AccessDenied
  17. return
  18. }
  19. var result int64
  20. if result, err = s.dao.InOrdersSync(c, u); err != nil || result == 0 {
  21. log.Error("mobile_s.dao.OrdersSync (%v) error(%v) or result==0", u, err)
  22. }
  23. return
  24. }
  25. // FlowSync update OrdersSync
  26. func (s *Service) FlowSync(c context.Context, u *mobile.MobileXML, ip string) (err error) {
  27. if !s.iplimit(_mobileKey, ip) {
  28. err = ecode.AccessDenied
  29. return
  30. }
  31. var result int64
  32. if result, err = s.dao.FlowSync(c, u); err != nil || result == 0 {
  33. log.Error("mobile_s.dao.OrdersSync(%v) error(%v) or result==0", u, err)
  34. }
  35. return
  36. }
  37. // Activation
  38. func (s *Service) Activation(c context.Context, usermob string, now time.Time) (msg string, err error) {
  39. rows := s.mobileInfo(c, usermob, now)
  40. res, ok := rows[usermob]
  41. if !ok {
  42. err = ecode.NothingFound
  43. msg = "该卡号尚未开通哔哩哔哩专属免流服务"
  44. return
  45. }
  46. for _, u := range res {
  47. if u.Actionid == 1 || (u.Actionid == 2 && now.Unix() <= int64(u.Expiretime)) {
  48. return
  49. }
  50. }
  51. err = ecode.NotModified
  52. msg = "该卡号哔哩哔哩专属免流服务已退订且已过期"
  53. return
  54. }
  55. // Activation
  56. func (s *Service) MobileState(c context.Context, usermob string, now time.Time) (res *mobile.Mobile) {
  57. data := s.mobileInfo(c, usermob, now)
  58. res = s.userState(data, usermob, now)
  59. return
  60. }
  61. // UserFlowState
  62. func (s *Service) UserMobileState(c context.Context, usermob string, now time.Time) (res *mobile.Mobile) {
  63. data := s.mobileInfo(c, usermob, now)
  64. if rows, ok := data[usermob]; ok {
  65. for _, res = range rows {
  66. if res.Actionid == 1 || (res.Actionid == 2 && now.Unix() <= int64(res.Expiretime)) {
  67. res.MobileType = 2
  68. return
  69. }
  70. }
  71. res = &mobile.Mobile{MobileType: 1}
  72. }
  73. res = &mobile.Mobile{MobileType: 1}
  74. return
  75. }
  76. // userState
  77. func (s *Service) userState(user map[string][]*mobile.Mobile, usermob string, now time.Time) (res *mobile.Mobile) {
  78. if rows, ok := user[usermob]; !ok {
  79. res = &mobile.Mobile{MobileType: 1}
  80. } else {
  81. for _, res = range rows {
  82. if res.Actionid == 2 && now.Unix() <= int64(res.Expiretime) {
  83. res.MobileType = 4
  84. break
  85. } else if res.Actionid == 1 {
  86. res.MobileType = 2
  87. break
  88. }
  89. }
  90. if res.MobileType == 0 {
  91. res.MobileType = 3
  92. }
  93. }
  94. log.Info("mobile_state_type:%v mobile_state_usermob:%v", res.MobileType, usermob)
  95. return
  96. }
  97. // mobileInfo
  98. func (s *Service) mobileInfo(c context.Context, usermob string, now time.Time) (res map[string][]*mobile.Mobile) {
  99. var (
  100. err error
  101. m []*mobile.Mobile
  102. tmps []*mobile.Mobile
  103. row = map[string][]*mobile.Mobile{}
  104. )
  105. res = map[string][]*mobile.Mobile{}
  106. if m, err = s.dao.MobileCache(c, usermob); err == nil && len(m) > 0 {
  107. row[usermob] = m
  108. s.pHit.Incr("mobile_cache")
  109. } else {
  110. row, err = s.dao.OrdersUserFlow(c, usermob, now)
  111. if err != nil {
  112. log.Error("mobile_s.dao.OrdersUserFlow error(%v)", err)
  113. return
  114. }
  115. s.pMiss.Incr("mobile_cache")
  116. if user, ok := row[usermob]; ok && len(user) > 0 {
  117. if err = s.dao.AddMobileCache(c, usermob, user); err != nil {
  118. log.Error("s.dao.AddMobileCache error(%v)", err)
  119. return
  120. }
  121. }
  122. }
  123. if ms, ok := row[usermob]; ok && len(ms) > 0 {
  124. for _, m := range ms {
  125. tmp := &mobile.Mobile{}
  126. *tmp = *m
  127. tmp.Productid = ""
  128. tmps = append(tmps, tmp)
  129. }
  130. res[usermob] = tmps
  131. }
  132. return
  133. }
  134. // IsMobileIP is mobile ip
  135. func (s *Service) IsMobileIP(ipUint uint32, ipStr, usermob string) (res *mobile.MobileUserIP) {
  136. res = &mobile.MobileUserIP{
  137. IPStr: ipStr,
  138. IsValide: false,
  139. }
  140. if !model.IsIPv4(ipStr) {
  141. return
  142. }
  143. for _, u := range s.mobileIpCache {
  144. if u.IPStartUint <= ipUint && u.IPEndUint >= ipUint {
  145. res.IsValide = true
  146. return
  147. }
  148. }
  149. log.Error("mobile_user_ip:%v mobile_ip_usermob:%v", ipStr, usermob)
  150. return
  151. }
  152. // mobileIp ip limit
  153. func (s *Service) iplimit(k, ip string) bool {
  154. return true
  155. }