web_info.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package member
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "strconv"
  7. "strings"
  8. "go-common/app/interface/main/account/model"
  9. cmodel "go-common/app/service/main/coin/model"
  10. lmodel "go-common/app/service/main/location/model"
  11. mmodel "go-common/app/service/main/member/model"
  12. pmodel "go-common/app/service/main/passport/model"
  13. smodel "go-common/app/service/main/secure/model"
  14. "go-common/library/log"
  15. "go-common/library/net/metadata"
  16. "go-common/library/time"
  17. )
  18. // UpdateSettings Update Settings
  19. func (s *Service) UpdateSettings(c context.Context, mid int64, settings *model.Settings) (err error) {
  20. var (
  21. mb *mmodel.Member
  22. ip = metadata.String(c, metadata.RemoteIP)
  23. )
  24. if mb, err = s.memRPC.Member(c, &mmodel.ArgMemberMid{Mid: mid, RemoteIP: ip}); err != nil {
  25. log.Error("s.Member(%d) error(%v)", mid, err)
  26. return
  27. }
  28. if settings.Sex != sexName(mb.Sex) {
  29. if err = s.accDao.UpdateSex(c, mid, sexID(settings.Sex), ip); err != nil {
  30. return
  31. }
  32. }
  33. if settings.Uname != mb.Name {
  34. if err = s.updateName(c, mid, settings.Uname, ip); err != nil {
  35. return
  36. }
  37. }
  38. if settings.Sign != mb.Sign {
  39. if err = s.UpdateSign(c, mid, settings.Sign); err != nil {
  40. return
  41. }
  42. }
  43. if settings.Birthday != mb.Birthday.Time().Format("2006-01-02") {
  44. if err = s.accDao.UpdateBirthday(c, mid, ip, settings.Birthday); err != nil {
  45. return
  46. }
  47. }
  48. return
  49. }
  50. // SettingsInfo Settings Info
  51. func (s *Service) SettingsInfo(c context.Context, mid int64) (user *model.User, err error) {
  52. var (
  53. mb *mmodel.Member
  54. nickFree *model.NickFree
  55. userID string
  56. ip = metadata.String(c, metadata.RemoteIP)
  57. )
  58. user = &model.User{}
  59. if mb, err = s.memRPC.Member(c, &mmodel.ArgMemberMid{Mid: mid, RemoteIP: ip}); err != nil {
  60. log.Error("s.Member(%d) error(%v)", mid, err)
  61. return
  62. }
  63. if nickFree, err = s.NickFree(c, mid); err != nil {
  64. return
  65. }
  66. if userID, err = s.passDao.UserID(c, mid, ip); err != nil {
  67. return
  68. }
  69. user.Userid = userID
  70. user.Mid = mb.Mid
  71. user.Uname = mb.Name
  72. user.Birthday = mb.Birthday.Time().Format("2006-01-02")
  73. user.NickFree = nickFree.NickFree
  74. user.Sign = mb.Sign
  75. user.Sex = sexName(mb.Sex)
  76. // 尝试清理一次缓存减少用户反馈
  77. s.NotityPurgeCache(c, mid, "updateUname")
  78. return
  79. }
  80. // LogCoin Log Money
  81. func (s *Service) LogCoin(c context.Context, mid int64) (logCoin *model.LogCoins, err error) {
  82. var (
  83. logs []*cmodel.Log
  84. )
  85. if logs, err = s.coinRPC.UserLog(c, &cmodel.ArgLog{Mid: mid, Recent: true, Translate: true}); err != nil {
  86. return
  87. }
  88. logCoin = &model.LogCoins{}
  89. logCoin.Count = len(logs)
  90. logCoin.List = make([]*model.LogCoin, 0)
  91. for _, l := range logs {
  92. s := fmt.Sprintf("%0.1f", l.To-l.From)
  93. delta, _ := strconv.ParseFloat(s, 64)
  94. model := &model.LogCoin{Time: time.Time(l.TimeStamp).Time().Format("2006-01-02 15:04:05"), Reason: l.Desc, Delta: delta}
  95. logCoin.List = append(logCoin.List, model)
  96. }
  97. return
  98. }
  99. // Coin coin.
  100. func (s *Service) Coin(c context.Context, mid int64) (logCoin *model.Coin, err error) {
  101. var (
  102. coin float64
  103. )
  104. if coin, err = s.coinRPC.UserCoins(c, &cmodel.ArgCoinInfo{Mid: mid}); err != nil {
  105. return
  106. }
  107. logCoin = &model.Coin{Money: coin}
  108. return
  109. }
  110. // LogMoral Log Moral
  111. func (s *Service) LogMoral(c context.Context, mid int64) (logMorals *model.LogMorals, err error) {
  112. var (
  113. logs []*mmodel.UserLog
  114. moral *mmodel.Moral
  115. toMoral, fromMoral, origin int64
  116. )
  117. if logs, err = s.memRPC.MoralLog(c, &mmodel.ArgMemberMid{Mid: mid}); err != nil {
  118. log.Error("s.memRPC.MoralLog(%v) error(%v)", mid, err)
  119. return
  120. }
  121. if moral, err = s.memRPC.Moral(c, &mmodel.ArgMemberMid{Mid: mid}); err != nil {
  122. log.Error("s.memRPC.Moral(%v) error(%v)", mid, err)
  123. return
  124. }
  125. logMorals = &model.LogMorals{Count: len(logs), Moral: moral.Moral / 100}
  126. logMorals.List = make([]*model.LogMoral, 0)
  127. for _, l := range logs {
  128. ml := &model.LogMoral{}
  129. ml.Reason = l.Content["reason"]
  130. if origin, err = strconv.ParseInt(l.Content["origin"], 10, 64); err != nil {
  131. log.Error("strconv.ParseInt(%v) error(%v)", l.Content["origin"], err)
  132. continue
  133. }
  134. ml.Origin = model.Origin[origin]
  135. ml.Time = time.Time(l.TS).Time().Format("2006-01-02 15:04:05")
  136. if toMoral, err = strconv.ParseInt(l.Content["to_moral"], 10, 64); err != nil {
  137. log.Error("strconv.ParseInt(%v) error(%v)", l.Content["to_moral"], err)
  138. continue
  139. }
  140. if fromMoral, err = strconv.ParseInt(l.Content["from_moral"], 10, 64); err != nil {
  141. log.Error("strconv.ParseInt(%v) error(%v)", l.Content["from_moral"], err)
  142. continue
  143. }
  144. delta := float64(toMoral-fromMoral) / float64(100)
  145. if ml.Delta, err = strconv.ParseFloat(fmt.Sprintf("%0.2f", delta), 64); err != nil {
  146. log.Error("strconv.ParseFloat(%v) error(%v)", delta, err)
  147. continue
  148. }
  149. logMorals.List = append(logMorals.List, ml)
  150. }
  151. return
  152. }
  153. // LogExp Log Exp
  154. func (s *Service) LogExp(c context.Context, mid int64) (logExp *model.LogExps, err error) {
  155. var (
  156. logs []*mmodel.UserLog
  157. toExp, fromExp float64
  158. ip = metadata.String(c, metadata.RemoteIP)
  159. )
  160. logExp = &model.LogExps{}
  161. if logs, err = s.memRPC.Log(c, &mmodel.ArgMid2{Mid: mid, RealIP: ip}); err != nil {
  162. log.Error("s.memRPC.Log(%v) error(%v)", mid, err)
  163. return
  164. }
  165. logExp.Count = len(logs)
  166. logExp.List = make([]*model.LogExp, 0)
  167. for _, l := range logs {
  168. expLog := &model.LogExp{}
  169. expLog.Time = time.Time(l.TS).Time().Format("2006-01-02 15:04:05")
  170. expLog.Reason = l.Content["reason"]
  171. if toExp, err = strconv.ParseFloat(l.Content["to_exp"], 10); err != nil {
  172. log.Error("strconv.ParseFloat(%v) error(%v)", l.Content["to_exp"], err)
  173. continue
  174. }
  175. if fromExp, err = strconv.ParseFloat(l.Content["from_exp"], 10); err != nil {
  176. log.Error("strconv.ParseFloat(%v) error(%v)", l.Content["from_exp"], err)
  177. continue
  178. }
  179. expLog.Delta = toExp - fromExp
  180. logExp.List = append(logExp.List, expLog)
  181. }
  182. return
  183. }
  184. // LogLogin logLogin
  185. func (s *Service) LogLogin(c context.Context, mid int64) (logLogins *model.LogLogins, err error) {
  186. var (
  187. logs []*pmodel.LoginLog
  188. ips []string
  189. locations map[string]*lmodel.Info
  190. excLogs []*smodel.Expection
  191. )
  192. logLogins = &model.LogLogins{}
  193. if logs, err = s.passRPC.LoginLogs(c, &pmodel.ArgLoginLogs{Mid: mid, Limit: 30}); err != nil {
  194. log.Error("s.passRPC.LoginLogs(%v) error(%v)", mid, err)
  195. return
  196. }
  197. if excLogs, err = s.secureRPC.ExpectionLoc(c, &smodel.ArgSecure{Mid: mid}); err != nil {
  198. log.Error("s.secureRPC.ExpectionLoc(%v) error(%v)", mid, err)
  199. return
  200. }
  201. logLogins.Count = len(logs)
  202. logLogins.List = make([]*model.LogLogin, 0)
  203. beRemoved := func(sip string) bool {
  204. ip := net.ParseIP(sip)
  205. for _, cidr := range s.removeLoginLogCIDR {
  206. if cidr.Contains(ip) {
  207. return true
  208. }
  209. }
  210. lip, ierr := s.locRPC.Info(c, &lmodel.ArgIP{IP: sip})
  211. if ierr != nil || lip == nil {
  212. log.Error("Failed to get ip info with ip: %s: %+v", sip, ierr)
  213. return false
  214. }
  215. // 过滤局域网登录
  216. if lip.Country == "局域网" {
  217. return true
  218. }
  219. return false
  220. }
  221. for _, l := range logs {
  222. sip := string(int64ToIP(l.LoginIP).String())
  223. if beRemoved(sip) {
  224. continue
  225. }
  226. nl := &model.LogLogin{}
  227. nl.Status = true
  228. nl.Time = l.Timestamp
  229. nl.TimeAt = time.Time(l.Timestamp).Time().Format("2006-01-02 15:04:05")
  230. if len(excLogs) != 0 {
  231. for _, e := range excLogs {
  232. if int64(e.IP) == l.LoginIP && time.Time(l.Timestamp) == e.Time {
  233. nl.Status = false
  234. nl.Type = int64(e.FeedBack)
  235. }
  236. }
  237. }
  238. nl.IP = sip
  239. ips = append(ips, nl.IP)
  240. logLogins.List = append(logLogins.List, nl)
  241. }
  242. if locations, err = s.locRPC.Infos(c, ips); err != nil {
  243. log.Error("s.locRPC.Infos(%v) error(%v)", ips, err)
  244. return
  245. }
  246. for _, log := range logLogins.List {
  247. if addr, ok := locations[log.IP]; ok {
  248. log.Geo = addr.Country + addr.Province + addr.City + addr.ISP
  249. }
  250. log.IP = vagueIP(log.IP)
  251. }
  252. return
  253. }
  254. // Reward exp reward.
  255. func (s *Service) Reward(c context.Context, mid int64) (reward *model.Reward, err error) {
  256. var (
  257. expStat *mmodel.ExpStat
  258. todayExp int64
  259. ip = metadata.String(c, metadata.RemoteIP)
  260. )
  261. if expStat, err = s.memRPC.Stat(c, &mmodel.ArgMid2{Mid: mid, RealIP: ip}); err != nil {
  262. log.Error("s.s.memRPC.Stat(%d) error(%v)", mid, err)
  263. return
  264. }
  265. if todayExp, err = s.coinRPC.TodayExp(c, &cmodel.ArgMid{Mid: mid, RealIP: ip}); err != nil {
  266. log.Error("s.coinRPC.TodayExp(%d) error(%v)", mid, err)
  267. return
  268. }
  269. if todayExp > 50 {
  270. todayExp = 50
  271. }
  272. reward = &model.Reward{}
  273. reward.Login = expStat.Login
  274. reward.Share = expStat.Share
  275. reward.Watch = expStat.Watch
  276. reward.Coin = todayExp
  277. return
  278. }
  279. func sexID(sex string) int64 {
  280. switch sex {
  281. case "男":
  282. return 1
  283. case "女":
  284. return 2
  285. default:
  286. return 0
  287. }
  288. }
  289. func sexName(sex int64) string {
  290. switch sex {
  291. case 1:
  292. return "男"
  293. case 2:
  294. return "女"
  295. default:
  296. return "保密"
  297. }
  298. }
  299. func vagueIP(ip string) string {
  300. strs := strings.Split(ip, ".")
  301. if len(strs) != 4 {
  302. log.Error("error ip (%v)", ip)
  303. return ""
  304. }
  305. strs[2] = "*"
  306. strs[3] = "*"
  307. return strs[0] + "." + strs[1] + "." + strs[2] + "." + strs[3]
  308. }
  309. func int64ToIP(ipnr int64) net.IP {
  310. var bytes [4]byte
  311. bytes[0] = byte(ipnr & 0xFF)
  312. bytes[1] = byte((ipnr >> 8) & 0xFF)
  313. bytes[2] = byte((ipnr >> 16) & 0xFF)
  314. bytes[3] = byte((ipnr >> 24) & 0xFF)
  315. return net.IPv4(bytes[3], bytes[2], bytes[1], bytes[0])
  316. }