|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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)
- }
-
- }
-
- // 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")
- }
|