蛋蛋星球-客户端
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

svc_common.go 2.9 KiB

1 tydzień temu
1 tydzień temu
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  70. }
  71. // GetYearsAndWeekStr 获取年份和周数
  72. func GetYearsAndWeekStr(esIndexName string) (string, string) {
  73. parts := strings.Split(esIndexName, "_")
  74. return parts[len(parts)-1][:4], parts[len(parts)-1][4:]
  75. }
  76. // GetWeekInfo 获取周数、开始时间、结束时间
  77. func GetWeekInfo(dateStr string) (string, string, string, string) {
  78. date := utils.TimeParseStd(dateStr)
  79. year, week := date.ISOWeek()
  80. location, _ := time.LoadLocation("Asia/Shanghai")
  81. // 计算给定年份1月1日是星期几
  82. startOfYear := time.Date(year, time.January, 1, 0, 0, 0, 0, location)
  83. daysOffset := int(startOfYear.Weekday()) - int(time.Monday) + 1
  84. // 计算给定年份的第一周的开始日期
  85. firstWeekStart := startOfYear.AddDate(0, 0, -daysOffset+1)
  86. // 计算给定周的开始日期
  87. weekStart := firstWeekStart.AddDate(0, 0, (week-1)*7)
  88. weekEnd := weekStart.AddDate(0, 0, 6)
  89. return utils.IntToStr(year), utils.IntToStr(week), weekStart.Format("2006-01-02"), weekEnd.Format("2006-01-02")
  90. }