蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

122 lines
3.5 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/utils"
  5. "applet/app/utils/cache"
  6. "applet/app/utils/logx"
  7. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  9. "github.com/gin-gonic/gin"
  10. "strings"
  11. "time"
  12. )
  13. // 简单的recover
  14. func Rev() {
  15. if err := recover(); err != nil {
  16. _ = logx.Error(err)
  17. }
  18. }
  19. func UserImg(user *model.User) *model.User {
  20. if user.Avatar == "" {
  21. user.Avatar = GetSysCfgStr("default_avatar")
  22. }
  23. return user
  24. }
  25. func GetOssDomain() string {
  26. redisConn := cache.GetPool().Get()
  27. sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn)
  28. sysCfg := sysCfgDb.SysCfgFindWithDb("oss_domain", "oss_bucket_scheme")
  29. http := sysCfg["oss_bucket_scheme"]
  30. return http + "://" + sysCfg["oss_domain"] + "/"
  31. }
  32. func GetOssUrl(img string) string {
  33. redisConn := cache.GetPool().Get()
  34. sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn)
  35. sysCfg := sysCfgDb.SysCfgFindWithDb("oss_domain", "oss_bucket_scheme")
  36. if strings.Contains(img, "http") == false && img != "" {
  37. http := sysCfg["oss_bucket_scheme"]
  38. img = http + "://" + sysCfg["oss_domain"] + "/" + img
  39. }
  40. return img
  41. }
  42. func NumFormat(num int) string {
  43. str := utils.IntToStr(num)
  44. if num >= 10000 {
  45. str = utils.Float64ToStrPrec1(float64(int(float64(num)/10000*10))/10) + "w"
  46. }
  47. return str
  48. }
  49. func GetSysCfgStr(key string) string {
  50. redisConn := cache.GetPool().Get()
  51. sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn)
  52. return sysCfgDb.SysCfgGetWithDb(key)
  53. }
  54. //存入用户的设备
  55. func UserImeiAdd(c *gin.Context, uid int64) {
  56. var data = model.UserImei{
  57. Uid: int(uid),
  58. Imei: c.GetHeader("imei"),
  59. CreateAt: time.Now(),
  60. Ip: c.ClientIP(),
  61. }
  62. db.Db.Insert(&data)
  63. NewUserDb := implement.NewUserDb(db.Db)
  64. user, _ := NewUserDb.GetUser(uid)
  65. if user != nil {
  66. user.LastLoginAt = time.Now().Format("2006-01-02 15:04:05")
  67. user.LastLoginIp = c.ClientIP()
  68. db.Db.Where("id=?", user.Id).Cols("last_login_at,last_login_ip").Update(user)
  69. UpdateUserTime(user.Id, "login")
  70. }
  71. }
  72. func UpdateUserTime(uid int64, types string) {
  73. count, _ := db.Db.Where("uid=?", uid).Count(&model.UserNoticeTime{})
  74. if count == 0 {
  75. tmp := &model.UserNoticeTime{Uid: int(uid)}
  76. if types == "login" {
  77. tmp.LoginTime = int(time.Now().Unix())
  78. } else {
  79. tmp.SignTime = int(time.Now().Unix())
  80. }
  81. db.Db.Insert(tmp)
  82. } else {
  83. tmp := &model.UserNoticeTime{}
  84. str := ""
  85. if types == "login" {
  86. str = "login_time"
  87. tmp.LoginTime = int(time.Now().Unix())
  88. } else {
  89. str = "sign_time"
  90. tmp.SignTime = int(time.Now().Unix())
  91. }
  92. db.Db.Where("uid=?", uid).Cols(str).Update(tmp)
  93. }
  94. }
  95. // GetYearsAndWeekStr 获取年份和周数
  96. func GetYearsAndWeekStr(esIndexName string) (string, string) {
  97. parts := strings.Split(esIndexName, "_")
  98. return parts[len(parts)-1][:4], parts[len(parts)-1][4:]
  99. }
  100. // GetWeekInfo 获取周数、开始时间、结束时间
  101. func GetWeekInfo(dateStr string) (string, string, string, string) {
  102. date := utils.TimeParseStd(dateStr)
  103. year, week := date.ISOWeek()
  104. location, _ := time.LoadLocation("Asia/Shanghai")
  105. // 计算给定年份1月1日是星期几
  106. startOfYear := time.Date(year, time.January, 1, 0, 0, 0, 0, location)
  107. daysOffset := int(startOfYear.Weekday()) - int(time.Monday) + 1
  108. // 计算给定年份的第一周的开始日期
  109. firstWeekStart := startOfYear.AddDate(0, 0, -daysOffset+1)
  110. // 计算给定周的开始日期
  111. weekStart := firstWeekStart.AddDate(0, 0, (week-1)*7)
  112. weekEnd := weekStart.AddDate(0, 0, 6)
  113. return utils.IntToStr(year), utils.IntToStr(week), weekStart.Format("2006-01-02"), weekEnd.Format("2006-01-02")
  114. }