蛋蛋星球-客户端
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.
 
 
 
 
 

133 lines
3.8 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 CheckRealName(uid int64) string {
  56. IsRealName := "0"
  57. NewUserRealNameAuthDb := implement.NewUserRealNameAuthDb(db.Db)
  58. realName, _ := NewUserRealNameAuthDb.GetRealNameAuth(int(uid))
  59. if realName != nil && realName.State == 1 {
  60. IsRealName = "1"
  61. }
  62. return IsRealName
  63. }
  64. //存入用户的设备
  65. func UserImeiAdd(c *gin.Context, uid int64) {
  66. var data = model.UserImei{
  67. Uid: int(uid),
  68. Imei: c.GetHeader("imei"),
  69. CreateAt: time.Now(),
  70. Ip: c.ClientIP(),
  71. }
  72. db.Db.Insert(&data)
  73. NewUserDb := implement.NewUserDb(db.Db)
  74. user, _ := NewUserDb.GetUser(uid)
  75. if user != nil {
  76. user.LastLoginAt = time.Now().Format("2006-01-02 15:04:05")
  77. user.LastLoginIp = c.ClientIP()
  78. db.Db.Where("id=?", user.Id).Cols("last_login_at,last_login_ip").Update(user)
  79. UpdateUserTime(user.Id, "login")
  80. }
  81. }
  82. func UpdateUserTime(uid int64, types string) {
  83. count, _ := db.Db.Where("uid=?", uid).Count(&model.UserNoticeTime{})
  84. if count == 0 {
  85. tmp := &model.UserNoticeTime{Uid: int(uid)}
  86. if types == "login" {
  87. tmp.LoginTime = int(time.Now().Unix())
  88. } else {
  89. tmp.SignTime = int(time.Now().Unix())
  90. }
  91. db.Db.Insert(tmp)
  92. } else {
  93. tmp := &model.UserNoticeTime{}
  94. str := ""
  95. if types == "login" {
  96. str = "login_time"
  97. tmp.LoginTime = int(time.Now().Unix())
  98. } else {
  99. str = "sign_time"
  100. tmp.SignTime = int(time.Now().Unix())
  101. }
  102. db.Db.Where("uid=?", uid).Cols(str).Update(tmp)
  103. }
  104. }
  105. // GetYearsAndWeekStr 获取年份和周数
  106. func GetYearsAndWeekStr(esIndexName string) (string, string) {
  107. parts := strings.Split(esIndexName, "_")
  108. return parts[len(parts)-1][:4], parts[len(parts)-1][4:]
  109. }
  110. // GetWeekInfo 获取周数、开始时间、结束时间
  111. func GetWeekInfo(dateStr string) (string, string, string, string) {
  112. date := utils.TimeParseStd(dateStr)
  113. year, week := date.ISOWeek()
  114. location, _ := time.LoadLocation("Asia/Shanghai")
  115. // 计算给定年份1月1日是星期几
  116. startOfYear := time.Date(year, time.January, 1, 0, 0, 0, 0, location)
  117. daysOffset := int(startOfYear.Weekday()) - int(time.Monday) + 1
  118. // 计算给定年份的第一周的开始日期
  119. firstWeekStart := startOfYear.AddDate(0, 0, -daysOffset+1)
  120. // 计算给定周的开始日期
  121. weekStart := firstWeekStart.AddDate(0, 0, (week-1)*7)
  122. weekEnd := weekStart.AddDate(0, 0, 6)
  123. return utils.IntToStr(year), utils.IntToStr(week), weekStart.Format("2006-01-02"), weekEnd.Format("2006-01-02")
  124. }