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

150 line
4.0 KiB

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