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

183 lines
5.3 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. "encoding/json"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "math/rand"
  13. "strings"
  14. "time"
  15. )
  16. // 简单的recover
  17. func Rev() {
  18. if err := recover(); err != nil {
  19. _ = logx.Error(err)
  20. }
  21. }
  22. func UserImg(user *model.User) *model.User {
  23. if user.Avatar == "" {
  24. user.Avatar = GetSysCfgStr("default_avatar")
  25. }
  26. user.Avatar = GetOssUrl(user.Avatar)
  27. return user
  28. }
  29. func GetOssDomain() string {
  30. sysCfgDb := sys_cfg.NewSysCfgDb(db.Db)
  31. sysCfg := sysCfgDb.SysCfgFindWithDb("oss_domain", "oss_bucket_scheme")
  32. http := sysCfg["oss_bucket_scheme"]
  33. return http + "://" + sysCfg["oss_domain"] + "/"
  34. }
  35. func GetOssUrl(img string) string {
  36. sysCfgDb := sys_cfg.NewSysCfgDb(db.Db)
  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. return sys_cfg.NewSysCfgDb(db.Db).SysCfgGetWithDb(key)
  53. }
  54. // 是否实名
  55. func CheckRealName(uid int64) string {
  56. IsRealName := "0"
  57. NewUserRealNameAuthDb := implement.NewUserRealNameAuthDb(db.Db)
  58. realName, _ := NewUserRealNameAuthDb.GetRealNameAuth(int(uid))
  59. if realName != nil && realName.State == 1 {
  60. IsRealName = "1"
  61. }
  62. return IsRealName
  63. }
  64. //存入用户的设备
  65. func UserImeiAdd(c *gin.Context, uid int64) {
  66. var tmp model.UserImei
  67. get, _ := db.Db.Where("uid=? and imei=? and ip=?", uid, c.GetHeader("imei"), utils.GetIP(c.Request)).Get(&tmp)
  68. if get == false {
  69. var data = model.UserImei{
  70. Uid: int(uid),
  71. Imei: c.GetHeader("imei"),
  72. CreateAt: time.Now(),
  73. UpdateAt: time.Now(),
  74. Ip: utils.GetIP(c.Request),
  75. }
  76. db.Db.Insert(&data)
  77. } else {
  78. tmp.UpdateAt = time.Now()
  79. db.Db.Where("id=?", tmp.Id).Cols("update_at").Update(&tmp)
  80. }
  81. NewUserDb := implement.NewUserDb(db.Db)
  82. user, _ := NewUserDb.GetUser(uid)
  83. if user != nil {
  84. user.LastLoginAt = time.Now().Format("2006-01-02 15:04:05")
  85. user.LastLoginIp = utils.GetIP(c.Request)
  86. db.Db.Where("id=?", user.Id).Cols("last_login_at,last_login_ip").Update(user)
  87. UpdateUserTime(user.Id, "login")
  88. }
  89. }
  90. func UpdateUserTime(uid int64, types string) {
  91. count, _ := db.Db.Where("uid=?", uid).Count(&model.UserNoticeTime{})
  92. if count == 0 {
  93. tmp := &model.UserNoticeTime{Uid: int(uid)}
  94. if types == "login" {
  95. tmp.LoginTime = int(time.Now().Unix())
  96. } else {
  97. tmp.SignTime = int(time.Now().Unix())
  98. }
  99. db.Db.Insert(tmp)
  100. } else {
  101. tmp := &model.UserNoticeTime{}
  102. str := ""
  103. if types == "login" {
  104. str = "login_time"
  105. tmp.LoginTime = int(time.Now().Unix())
  106. } else {
  107. str = "sign_time"
  108. tmp.SignTime = int(time.Now().Unix())
  109. }
  110. db.Db.Where("uid=?", uid).Cols(str).Update(tmp)
  111. }
  112. }
  113. // GetYearsAndWeekStr 获取年份和周数
  114. func GetYearsAndWeekStr(esIndexName string) (string, string) {
  115. parts := strings.Split(esIndexName, "_")
  116. return parts[len(parts)-1][:4], parts[len(parts)-1][4:]
  117. }
  118. // GetWeekInfo 获取周数、开始时间、结束时间
  119. func GetWeekInfo(dateStr string) (string, string, string, string) {
  120. date := utils.TimeParseStd(dateStr)
  121. year, week := date.ISOWeek()
  122. location, _ := time.LoadLocation("Asia/Shanghai")
  123. // 计算给定年份1月1日是星期几
  124. startOfYear := time.Date(year, time.January, 1, 0, 0, 0, 0, location)
  125. daysOffset := int(startOfYear.Weekday()) - int(time.Monday) + 1
  126. // 计算给定年份的第一周的开始日期
  127. firstWeekStart := startOfYear.AddDate(0, 0, -daysOffset+1)
  128. // 计算给定周的开始日期
  129. weekStart := firstWeekStart.AddDate(0, 0, (week-1)*7)
  130. weekEnd := weekStart.AddDate(0, 0, 6)
  131. return utils.IntToStr(year), utils.IntToStr(week), weekStart.Format("2006-01-02"), weekEnd.Format("2006-01-02")
  132. }
  133. func DownUrl(c *gin.Context) (string, string, string) {
  134. downDomainStr := GetSysCfgStr("down_web_domain")
  135. registerDomainStr := GetSysCfgStr("down_register_domain")
  136. var downDomainArr = make([]string, 0)
  137. json.Unmarshal([]byte(downDomainStr), &downDomainArr)
  138. downDomain := ""
  139. registerDomain := ""
  140. if len(downDomainArr) > 0 {
  141. rand.Seed(time.Now().UnixNano())
  142. num := rand.Intn(len(downDomainArr))
  143. if num == 0 {
  144. num = 1
  145. }
  146. num--
  147. downDomain = downDomainArr[num]
  148. }
  149. var registerDomainArr = make([]string, 0)
  150. json.Unmarshal([]byte(registerDomainStr), &registerDomainArr)
  151. if len(registerDomainArr) > 0 {
  152. rand.Seed(time.Now().UnixNano())
  153. num := rand.Intn(len(registerDomainArr))
  154. if num == 0 {
  155. num = 1
  156. }
  157. num--
  158. registerDomain = registerDomainArr[num]
  159. }
  160. downUrl := ""
  161. registerUrl := ""
  162. registerUrl1 := ""
  163. if downDomain != "" {
  164. downUrl = fmt.Sprintf("%s%s%s", "https://", downDomain, "/#/pages/down-page/down-page?type=download")
  165. }
  166. if registerDomain != "" {
  167. registerUrl = fmt.Sprintf("%s%s%s", "https://", registerDomain, "/#/pages/down-page/down-page?type=")
  168. }
  169. if registerDomain != "" {
  170. registerUrl1 = fmt.Sprintf("%s%s%s", "https://", registerDomain, "/#/pages/register-page/register-page?type=")
  171. }
  172. return downUrl, registerUrl, registerUrl1
  173. }