蛋蛋星球-客户端
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

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