蛋蛋星球-客户端
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

137 Zeilen
4.0 KiB

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