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

162 lines
4.4 KiB

  1. package comm
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/md/comm"
  8. "applet/app/svc"
  9. "applet/app/svc/sys_cfg"
  10. "applet/app/utils/cache"
  11. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  12. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  13. enum2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  14. "fmt"
  15. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  16. "github.com/gin-gonic/gin"
  17. "os"
  18. "time"
  19. )
  20. // GetOssUrl
  21. // @Summary 通用请求-对象存储-上传许可链接(获取)
  22. // @Tags 对象存储
  23. // @Description 上传许可链接(获取)
  24. // @Accept json
  25. // @Produce json
  26. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  27. // @Param req body comm.ImgReqUploadReq true "签名上传url"
  28. // @Success 200 {string} "许可链接"
  29. // @Failure 400 {object} md.Response "具体错误"
  30. // @Router /api/v1/comm/getOssUrl [POST]
  31. func GetOssUrl(c *gin.Context) {
  32. var args comm.ImgReqUploadReq
  33. err := c.ShouldBindJSON(&args)
  34. if err != nil {
  35. err = svc.HandleValidateErr(err)
  36. err1 := err.(e.E)
  37. e.OutErr(c, err1.Code, err1.Error())
  38. return
  39. }
  40. sysCfgDb := sys_cfg.NewSysCfgDb(db.Db)
  41. sysCfgs, err := sysCfgDb.SysCfgGetAll()
  42. if err != nil {
  43. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  44. return
  45. }
  46. if sysCfgs == nil {
  47. e.OutErr(c, e.ERR_CFG_CACHE, nil)
  48. return
  49. }
  50. cfgMap := make(map[string]string, len(*sysCfgs))
  51. for _, cfg := range *sysCfgs {
  52. cfgMap[cfg.Key] = cfg.Val
  53. }
  54. endpoint := cfgMap[enum2.AliyunOssEndpoint]
  55. bucketName := cfgMap[enum2.AliyunOssBucketName]
  56. ossBucketScheme := cfgMap[enum2.AliyunOssBucketScheme]
  57. accessKeyID := cfgMap[enum2.AliyunOssAccessKeyID]
  58. accessKeySecret := cfgMap[enum2.AliyunOssAccessKeySecret]
  59. if cfg.Prd {
  60. accessKeyID = os.Getenv(enum2.AliyunOssAccessKeyID)
  61. accessKeySecret = os.Getenv(enum2.AliyunOssAccessKeySecret)
  62. }
  63. // 创建OSSClient实例。
  64. client, err := oss.New(ossBucketScheme+"://"+endpoint, accessKeyID, accessKeySecret)
  65. if err != nil {
  66. e.OutErr(c, e.ERR, err.Error())
  67. return
  68. }
  69. // 获取存储空间。
  70. bucket, err := client.Bucket(bucketName)
  71. if err != nil {
  72. e.OutErr(c, e.ERR, err.Error())
  73. return
  74. }
  75. options := []oss.Option{
  76. oss.ContentType(args.ContentType),
  77. }
  78. signedURL, err := bucket.SignURL(args.FileName, oss.HTTPPut, 60*5, options...)
  79. if err != nil {
  80. e.OutErr(c, e.ERR_AES_ENCODE, err.Error())
  81. return
  82. }
  83. e.OutSuc(c, signedURL, nil)
  84. }
  85. // AccessRecords
  86. // @Summary 通用请求-访问记录-页面记录
  87. // @Tags 访问记录
  88. // @Description 页面记录
  89. // @Accept json
  90. // @Produce json
  91. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  92. // @Param req body comm.AccessRecordsReq true "页面标识"
  93. // @Success 200 {string} "success"
  94. // @Success 200 {string} "data exist"
  95. // @Failure 400 {object} md.Response "具体错误"
  96. // @Router /api/v1/comm/accessRecords [POST]
  97. func AccessRecords(c *gin.Context) {
  98. var req *comm.AccessRecordsReq
  99. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  100. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  101. return
  102. }
  103. user := svc.GetUser(c)
  104. date := time.Now().Format("2006-01-02")
  105. cacheKey := fmt.Sprintf(md.UserAccessRecordsKey, date, req.Index)
  106. if !cache.Exists(cacheKey) {
  107. ttl := 24 * 60 * 60 // 记录保存24小时
  108. _, err := cache.SetEx(cacheKey, nil, ttl)
  109. if err != nil {
  110. e.OutErr(c, e.ERR, err.Error())
  111. return
  112. }
  113. }
  114. get, err1 := cache.GetBit(cacheKey, user.Id)
  115. if err1 != nil || get == 0 {
  116. // 更新页面记录
  117. recordsDb := implement.NewEggAccessRecordsDb(db.Db)
  118. exist, err := recordsDb.EggAccessRecordsExist(user.Id, date, req.Index)
  119. if err != nil {
  120. e.OutErr(c, e.ERR_DB_ORM, nil)
  121. }
  122. if !exist {
  123. m := model.EggAccessRecords{
  124. Uid: user.Id,
  125. PageIndex: req.Index,
  126. Date: date,
  127. }
  128. _, err = recordsDb.EggAccessRecordsInsert(&m)
  129. if err != nil {
  130. e.OutErr(c, e.ERR_DB_ORM, nil)
  131. return
  132. }
  133. }
  134. _, err = cache.SetBit(cacheKey, user.Id, 1)
  135. if err != nil {
  136. e.OutErr(c, e.ERR, err.Error())
  137. return
  138. }
  139. // 更新浏览记录总表
  140. year, week := time.Now().ISOWeek()
  141. totalDb := implement.NewEggAccessTotalDb(db.Db)
  142. _, err = totalDb.EggAccessTotalAddOrDefault(user.Id, year, week)
  143. if err != nil {
  144. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  145. return
  146. }
  147. }
  148. e.OutSuc(c, "success", nil)
  149. }