Merge branch 'feat/1.0.7/short-id' into test

This commit is contained in:
aichy126 2023-03-13 16:25:17 +08:00
commit dd54b6b9c8
3 changed files with 39 additions and 11 deletions

View File

@ -160,6 +160,14 @@ func (tc *TemplateController) QuestionInfoeRdirect(ctx *gin.Context, siteInfo *s
id := ctx.Param("id")
title := ctx.Param("title")
titleIsAnswerID := false
NeedChangeShortID := false
isShortID := uid.IsShortID(id)
if uid.ShortIDSwitch {
if !isShortID {
id = uid.EnShortID(id)
NeedChangeShortID = true
}
}
objectType, objectTypeerr := obj.GetObjectTypeStrByObjectID(uid.DeShortID(title))
if objectTypeerr == nil {
@ -167,21 +175,23 @@ func (tc *TemplateController) QuestionInfoeRdirect(ctx *gin.Context, siteInfo *s
titleIsAnswerID = true
}
}
url = fmt.Sprintf("%s/questions/%s", siteInfo.General.SiteUrl, id)
if siteInfo.SiteSeo.PermaLink == schema.PermaLinkQuestionID {
if len(ctx.Request.URL.Query()) > 0 {
url = fmt.Sprintf("%s?%s", url, ctx.Request.URL.RawQuery)
}
if NeedChangeShortID {
return true, url
}
//not have title
if titleIsAnswerID || len(title) == 0 {
return false, ""
}
if len(ctx.Request.URL.Query()) > 0 {
url = fmt.Sprintf("%s?%s", url, ctx.Request.URL.RawQuery)
}
return true, url
} else {
//have title
if len(title) > 0 && !titleIsAnswerID && correctTitle {
return false, ""
}
detail, err := tc.templateRenderController.QuestionDetail(ctx, id)
if err != nil {
tc.Page404(ctx)
@ -195,6 +205,13 @@ func (tc *TemplateController) QuestionInfoeRdirect(ctx *gin.Context, siteInfo *s
if len(ctx.Request.URL.Query()) > 0 {
url = fmt.Sprintf("%s?%s", url, ctx.Request.URL.RawQuery)
}
//have title
if len(title) > 0 && !titleIsAnswerID && correctTitle {
if NeedChangeShortID {
return true, url
}
return false, ""
}
return true, url
}
}

View File

@ -13,10 +13,10 @@ import (
"github.com/segmentfault/pacman/errors"
)
const PermaLinkQuestionIDAndTitle = 1
const PermaLinkQuestionID = 2
const PermaLinkQuestionIDAndTitleByShortID = 3
const PermaLinkQuestionIDByShortID = 4
const PermaLinkQuestionIDAndTitle = 1 // /questions/10010000000000001/post-title
const PermaLinkQuestionID = 2 // /questions/10010000000000001
const PermaLinkQuestionIDAndTitleByShortID = 3 // /questions/11/post-title
const PermaLinkQuestionIDByShortID = 4 // /questions/11
// SiteGeneralReq site general request
type SiteGeneralReq struct {

View File

@ -106,3 +106,14 @@ func DeShortID(sid string) string {
}
return sid
}
func IsShortID(id string) bool {
num, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return true
}
if num < 10000000000000000 {
return true
}
return false
}