package svc import ( "applet/app/db" "applet/app/utils" "applet/app/utils/cache" "applet/app/utils/logx" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "strings" "time" ) // 简单的recover func Rev() { if err := recover(); err != nil { _ = logx.Error(err) } } func UserImg(user *model.User) *model.User { if user.Avatar == "" { user.Avatar = GetSysCfgStr("default_avatar") } return user } func GetOssUrl(img string) string { redisConn := cache.GetPool().Get() sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) sysCfg := sysCfgDb.SysCfgFindWithDb("oss_domain") if strings.Contains(img, "http") == false && img != "" { http := "" if strings.Contains(sysCfg["oss_domain"], "http") == false { http = "http://" } img = http + sysCfg["oss_domain"] + img } return img } func NumFormat(num int) string { str := utils.IntToStr(num) if num >= 10000 { str = utils.Float64ToStrPrec1(float64(int(float64(num)/10000*10))/10) + "w" } return str } func GetSysCfgStr(key string) string { redisConn := cache.GetPool().Get() sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn) return sysCfgDb.SysCfgGetWithDb(key) } // GetYearsAndWeekStr 获取年份和周数 func GetYearsAndWeekStr(esIndexName string) (string, string) { parts := strings.Split(esIndexName, "_") return parts[len(parts)-1][:4], parts[len(parts)-1][4:] } // GetWeekInfo 获取周数、开始时间、结束时间 func GetWeekInfo(dateStr string) (string, string, string, string) { date := utils.TimeParseStd(dateStr) year, week := date.ISOWeek() location, _ := time.LoadLocation("Asia/Shanghai") // 计算给定年份1月1日是星期几 startOfYear := time.Date(year, time.January, 1, 0, 0, 0, 0, location) daysOffset := int(startOfYear.Weekday()) - int(time.Monday) + 1 // 计算给定年份的第一周的开始日期 firstWeekStart := startOfYear.AddDate(0, 0, -daysOffset+1) // 计算给定周的开始日期 weekStart := firstWeekStart.AddDate(0, 0, (week-1)*7) weekEnd := weekStart.AddDate(0, 0, 6) return utils.IntToStr(year), utils.IntToStr(week), weekStart.Format("2006-01-02"), weekEnd.Format("2006-01-02") }