|
@@ -24,6 +24,12 @@ import (
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
+
|
|
|
+ "github.com/beego/beego/v2/server/web"
|
|
|
+ "github.com/beego/beego/v2/server/web/context"
|
|
|
+ "github.com/go-redis/redis/v8"
|
|
|
+ "github.com/rdlucklib/rdluck_tools/file"
|
|
|
+ "github.com/rdlucklib/rdluck_tools/http"
|
|
|
)
|
|
|
|
|
|
// AddReportAndChapter
|
|
@@ -2151,3 +2157,80 @@ func processMap(data map[string]interface{}, opType string, tokenMap map[string]
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+// GetReportShareUrlToken 获取报告分享链接token
|
|
|
+func GetReportShareUrlToken(req models.ReportShartUrlReq, adminId int) (linkToken string, err error) {
|
|
|
+ cacheLinkKey := utils.CACHE_REPORT_SHARE_SHORT_Url + strconv.Itoa(req.ReportId) + "userId:" + strconv.Itoa(adminId)
|
|
|
+ linkToken, _ = utils.Rc.RedisString(cacheLinkKey)
|
|
|
+ if linkToken != "" && utils.Rc.IsExist(utils.CACHE_REPORT_SHARE_ORIGIN_Url+linkToken) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var ok bool
|
|
|
+ // 冲突检测
|
|
|
+ for i := 0; i < 3; i++ {
|
|
|
+ linkToken = req.Url
|
|
|
+ if i > 0 {
|
|
|
+ linkToken += "_" + utils.GetRandDigit(3)
|
|
|
+ }
|
|
|
+ hashUrl := utils.MurmurHash64([]byte(linkToken))
|
|
|
+ linkToken = utils.ConvertNumToBase62(hashUrl)
|
|
|
+ // 拼上报告标题
|
|
|
+ linkToken = fmt.Sprintf("%s %s", linkToken, req.Title)
|
|
|
+
|
|
|
+ ok = utils.Rc.IsExist(utils.CACHE_REPORT_SHARE_ORIGIN_Url + linkToken)
|
|
|
+ if !ok {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !ok {
|
|
|
+ after := time.Now().AddDate(0, 0, 7)
|
|
|
+ err = utils.Rc.Put(cacheLinkKey, linkToken, time.Until(after))
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = utils.Rc.Put(utils.CACHE_REPORT_SHARE_ORIGIN_Url+linkToken, req.Url, time.Until(after))
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ linkToken = ""
|
|
|
+ err = errors.New("生成链接失败")
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func TransfromToOriginUrl(linkToken string) (originLink string, msg string, err error) {
|
|
|
+ cacheLinkKey := utils.CACHE_REPORT_SHARE_ORIGIN_Url + linkToken
|
|
|
+ originLink, err = utils.Rc.RedisString(cacheLinkKey)
|
|
|
+ if err != nil {
|
|
|
+ if err == redis.Nil {
|
|
|
+ msg = "链接已失效, 请重新获取"
|
|
|
+ return
|
|
|
+ }
|
|
|
+ msg = "获取链接失败"
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if originLink == "" {
|
|
|
+ msg = "链接已失效, 请重新获取"
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func FilterShareUrl() web.FilterFunc {
|
|
|
+ return func(c *context.Context) {
|
|
|
+ path := c.Input.Context.Request.URL.Path
|
|
|
+ tokenArr := strings.Split(path, "/")
|
|
|
+ token := tokenArr[len(tokenArr)-1]
|
|
|
+
|
|
|
+ newPath := "/adminapi/report/share/link"
|
|
|
+ q := c.Input.Context.Request.URL.Query()
|
|
|
+ q.Add("Token", token)
|
|
|
+ c.Input.Context.Request.URL.Path = newPath
|
|
|
+ c.Input.Context.Request.URL.RawQuery = q.Encode()
|
|
|
+
|
|
|
+ utils.ApiLog.Info(fmt.Sprintf("原始请求为:%s, 已修改请求路径为:%s?%s", path, newPath, q.Encode()))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|