package svc import ( "applet/app/db" "applet/app/svc/sys_cfg" "applet/app/utils" "applet/app/utils/logx" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "github.com/gin-gonic/gin" "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 GetOssDomain() string { sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) sysCfg := sysCfgDb.SysCfgFindWithDb("oss_domain", "oss_bucket_scheme") http := sysCfg["oss_bucket_scheme"] return http + "://" + sysCfg["oss_domain"] + "/" } func GetOssUrl(img string) string { sysCfgDb := sys_cfg.NewSysCfgDb(db.Db) sysCfg := sysCfgDb.SysCfgFindWithDb("oss_domain", "oss_bucket_scheme") if strings.Contains(img, "http") == false && img != "" { http := sysCfg["oss_bucket_scheme"] 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 { return sys_cfg.NewSysCfgDb(db.Db).SysCfgGetWithDb(key) } // 是否实名 func CheckRealName(uid int64) string { IsRealName := "0" NewUserRealNameAuthDb := implement.NewUserRealNameAuthDb(db.Db) realName, _ := NewUserRealNameAuthDb.GetRealNameAuth(int(uid)) if realName != nil && realName.State == 1 { IsRealName = "1" } return IsRealName } //存入用户的设备 func UserImeiAdd(c *gin.Context, uid int64) { var tmp model.UserImei get, _ := db.Db.Where("uid=? and imei=? and ip=?", uid, c.GetHeader("imei"), utils.GetIP(c.Request)).Get(&tmp) if get == false { var data = model.UserImei{ Uid: int(uid), Imei: c.GetHeader("imei"), CreateAt: time.Now(), UpdateAt: time.Now(), Ip: utils.GetIP(c.Request), } db.Db.Insert(&data) } else { tmp.UpdateAt = time.Now() db.Db.Where("id=?", tmp.Id).Cols("update_at").Update(&tmp) } NewUserDb := implement.NewUserDb(db.Db) user, _ := NewUserDb.GetUser(uid) if user != nil { user.LastLoginAt = time.Now().Format("2006-01-02 15:04:05") user.LastLoginIp = utils.GetIP(c.Request) db.Db.Where("id=?", user.Id).Cols("last_login_at,last_login_ip").Update(user) UpdateUserTime(user.Id, "login") } } func UpdateUserTime(uid int64, types string) { count, _ := db.Db.Where("uid=?", uid).Count(&model.UserNoticeTime{}) if count == 0 { tmp := &model.UserNoticeTime{Uid: int(uid)} if types == "login" { tmp.LoginTime = int(time.Now().Unix()) } else { tmp.SignTime = int(time.Now().Unix()) } db.Db.Insert(tmp) } else { tmp := &model.UserNoticeTime{} str := "" if types == "login" { str = "login_time" tmp.LoginTime = int(time.Now().Unix()) } else { str = "sign_time" tmp.SignTime = int(time.Now().Unix()) } db.Db.Where("uid=?", uid).Cols(str).Update(tmp) } } // 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") }