蛋蛋星球-客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

svc_common.go 4.0 KiB

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