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

svc_common.go 3.9 KiB

1 month ago
1 week ago
1 month ago
1 week ago
1 week ago
1 week ago
3 weeks ago
3 weeks ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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"), c.ClientIP()).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: c.ClientIP(),
  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 = c.ClientIP()
  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. }