|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- 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"
- "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 {
- redisConn := cache.GetPool().Get()
- sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn)
- sysCfg := sysCfgDb.SysCfgFindWithDb("oss_domain", "oss_bucket_scheme")
- http := sysCfg["oss_bucket_scheme"]
- return http + "://" + sysCfg["oss_domain"] + "/"
- }
- func GetOssUrl(img string) string {
- redisConn := cache.GetPool().Get()
- sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn)
- 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 {
- redisConn := cache.GetPool().Get()
- sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn)
- return sysCfgDb.SysCfgGetWithDb(key)
-
- }
-
- //存入用户的设备
-
- func UserImeiAdd(c *gin.Context, uid int64) {
- var data = model.UserImei{
- Uid: int(uid),
- Imei: c.GetHeader("imei"),
- CreateAt: time.Now(),
- Ip: c.ClientIP(),
- }
- db.Db.Insert(&data)
- 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 = c.ClientIP()
- 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")
- }
|