Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

add short id switch

aichy12647fc6caf9147
diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.go
index ec17b64b..b420813f 100644
--- a/internal/schema/siteinfo_schema.go
+++ b/internal/schema/siteinfo_schema.go
@@ -15,6 +15,8 @@ import (
const PermaLinkQuestionIDAndTitle = 1
const PermaLinkQuestionID = 2
+const PermaLinkQuestionIDAndTitleByShortID = 3
+const PermaLinkQuestionIDByShortID = 4
// SiteGeneralReq site general request
type SiteGeneralReq struct {
diff --git a/internal/service/siteinfo_common/siteinfo_service.go b/internal/service/siteinfo_common/siteinfo_service.go
index b61e59ff..d690e5fd 100644
--- a/internal/service/siteinfo_common/siteinfo_service.go
+++ b/internal/service/siteinfo_common/siteinfo_service.go
@@ -7,6 +7,8 @@ import (
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/schema"
+ "github.com/answerdev/answer/pkg/uid"
+ "github.com/segmentfault/pacman/log"
)
//go:generate mockgen -source=./siteinfo_service.go -destination=../mock/siteinfo_repo_mock.go -package=mock
@@ -22,9 +24,18 @@ type SiteInfoCommonService struct {
// NewSiteInfoCommonService new site info common service
func NewSiteInfoCommonService(siteInfoRepo SiteInfoRepo) *SiteInfoCommonService {
- return &SiteInfoCommonService{
+ siteInfo := &SiteInfoCommonService{
siteInfoRepo: siteInfoRepo,
}
+ seoinfo, err := siteInfo.GetSiteSeo(context.Background())
+ if err != nil {
+ log.Error("seoinfo error", err)
+ }
+ if seoinfo.PermaLink == schema.PermaLinkQuestionIDAndTitleByShortID || seoinfo.PermaLink == schema.PermaLinkQuestionIDByShortID {
+ uid.ShortIDSwitch = true
+ }
+
+ return siteInfo
}
// GetSiteGeneral get site info general
diff --git a/pkg/uid/sid.go b/pkg/uid/sid.go
index e3151bd2..a8880d6b 100644
--- a/pkg/uid/sid.go
+++ b/pkg/uid/sid.go
@@ -6,6 +6,8 @@ import (
const salt = int64(0)
+var ShortIDSwitch = false
+
var AlphanumericSet = []rune{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
@@ -84,12 +86,16 @@ func ShortIDToNum(code string) int64 {
}
func EnShortID(id string) string {
- num, err := strconv.ParseInt(id, 10, 64)
- if err != nil {
- return ""
+ if ShortIDSwitch {
+ num, err := strconv.ParseInt(id, 10, 64)
+ if err != nil {
+ return ""
+ }
+ return NumToShortID(num)
}
- return NumToShortID(num)
+ return id
}
+
func DeShortID(sid string) string {
num, err := strconv.ParseInt(sid, 10, 64)
if err != nil {