蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

145 lines
4.2 KiB

  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. defer redisConn.Close()
  28. sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn)
  29. sysCfg := sysCfgDb.SysCfgFindWithDb("oss_domain", "oss_bucket_scheme")
  30. http := sysCfg["oss_bucket_scheme"]
  31. return http + "://" + sysCfg["oss_domain"] + "/"
  32. }
  33. func GetOssUrl(img string) string {
  34. redisConn := cache.GetPool().Get()
  35. defer redisConn.Close()
  36. sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn)
  37. sysCfg := sysCfgDb.SysCfgFindWithDb("oss_domain", "oss_bucket_scheme")
  38. if strings.Contains(img, "http") == false && img != "" {
  39. http := sysCfg["oss_bucket_scheme"]
  40. img = http + "://" + sysCfg["oss_domain"] + "/" + img
  41. }
  42. return img
  43. }
  44. func NumFormat(num int) string {
  45. str := utils.IntToStr(num)
  46. if num >= 10000 {
  47. str = utils.Float64ToStrPrec1(float64(int(float64(num)/10000*10))/10) + "w"
  48. }
  49. return str
  50. }
  51. func GetSysCfgStr(key string) string {
  52. redisConn := cache.GetPool().Get()
  53. defer redisConn.Close()
  54. sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn)
  55. return sysCfgDb.SysCfgGetWithDb(key)
  56. }
  57. // 是否实名
  58. func CheckRealName(uid int64) string {
  59. IsRealName := "0"
  60. NewUserRealNameAuthDb := implement.NewUserRealNameAuthDb(db.Db)
  61. realName, _ := NewUserRealNameAuthDb.GetRealNameAuth(int(uid))
  62. if realName != nil && realName.State == 1 {
  63. IsRealName = "1"
  64. }
  65. return IsRealName
  66. }
  67. //存入用户的设备
  68. func UserImeiAdd(c *gin.Context, uid int64) {
  69. var tmp model.UserImei
  70. get, _ := db.Db.Where("uid=? and imei=? and ip=?", uid, c.GetHeader("imei"), c.ClientIP()).Get(&tmp)
  71. if get == false {
  72. var data = model.UserImei{
  73. Uid: int(uid),
  74. Imei: c.GetHeader("imei"),
  75. CreateAt: time.Now(),
  76. UpdateAt: time.Now(),
  77. Ip: c.ClientIP(),
  78. }
  79. db.Db.Insert(&data)
  80. } else {
  81. tmp.UpdateAt = time.Now()
  82. db.Db.Where("id=?", tmp.Id).Cols("update_at").Update(&tmp)
  83. }
  84. NewUserDb := implement.NewUserDb(db.Db)
  85. user, _ := NewUserDb.GetUser(uid)
  86. if user != nil {
  87. user.LastLoginAt = time.Now().Format("2006-01-02 15:04:05")
  88. user.LastLoginIp = c.ClientIP()
  89. db.Db.Where("id=?", user.Id).Cols("last_login_at,last_login_ip").Update(user)
  90. UpdateUserTime(user.Id, "login")
  91. }
  92. }
  93. func UpdateUserTime(uid int64, types string) {
  94. count, _ := db.Db.Where("uid=?", uid).Count(&model.UserNoticeTime{})
  95. if count == 0 {
  96. tmp := &model.UserNoticeTime{Uid: int(uid)}
  97. if types == "login" {
  98. tmp.LoginTime = int(time.Now().Unix())
  99. } else {
  100. tmp.SignTime = int(time.Now().Unix())
  101. }
  102. db.Db.Insert(tmp)
  103. } else {
  104. tmp := &model.UserNoticeTime{}
  105. str := ""
  106. if types == "login" {
  107. str = "login_time"
  108. tmp.LoginTime = int(time.Now().Unix())
  109. } else {
  110. str = "sign_time"
  111. tmp.SignTime = int(time.Now().Unix())
  112. }
  113. db.Db.Where("uid=?", uid).Cols(str).Update(tmp)
  114. }
  115. }
  116. // GetYearsAndWeekStr 获取年份和周数
  117. func GetYearsAndWeekStr(esIndexName string) (string, string) {
  118. parts := strings.Split(esIndexName, "_")
  119. return parts[len(parts)-1][:4], parts[len(parts)-1][4:]
  120. }
  121. // GetWeekInfo 获取周数、开始时间、结束时间
  122. func GetWeekInfo(dateStr string) (string, string, string, string) {
  123. date := utils.TimeParseStd(dateStr)
  124. year, week := date.ISOWeek()
  125. location, _ := time.LoadLocation("Asia/Shanghai")
  126. // 计算给定年份1月1日是星期几
  127. startOfYear := time.Date(year, time.January, 1, 0, 0, 0, 0, location)
  128. daysOffset := int(startOfYear.Weekday()) - int(time.Monday) + 1
  129. // 计算给定年份的第一周的开始日期
  130. firstWeekStart := startOfYear.AddDate(0, 0, -daysOffset+1)
  131. // 计算给定周的开始日期
  132. weekStart := firstWeekStart.AddDate(0, 0, (week-1)*7)
  133. weekEnd := weekStart.AddDate(0, 0, 6)
  134. return utils.IntToStr(year), utils.IntToStr(week), weekStart.Format("2006-01-02"), weekEnd.Format("2006-01-02")
  135. }