蛋蛋星球-客户端
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.
 
 
 
 
 

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