feat(theme): site info add theme options

This commit is contained in:
LinkinStar 2022-12-13 17:44:24 +08:00
parent e3f8e37647
commit c946b08b23
6 changed files with 45 additions and 11 deletions

View File

@ -63,7 +63,6 @@ func BindAndCheck(ctx *gin.Context, data interface{}) bool {
// BindAndCheckReturnErr bind request and check
func BindAndCheckReturnErr(ctx *gin.Context, data interface{}) (errFields []*validator.FormErrorField) {
lang := GetLang(ctx)
ctx.Set(constant.AcceptLanguageFlag, lang)
if err := ctx.ShouldBind(data); err != nil {
log.Errorf("http_handle BindAndCheck fail, %s", err.Error())
HandleResponse(ctx, myErrors.New(http.StatusBadRequest, reason.RequestFormatError), nil)

View File

@ -0,0 +1,13 @@
package middleware
import (
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/handler"
"github.com/gin-gonic/gin"
)
// ExtractAndSetAcceptLanguage extract accept language from header and set to context
func ExtractAndSetAcceptLanguage(ctx *gin.Context) {
lang := handler.GetLang(ctx)
ctx.Set(constant.AcceptLanguageFlag, lang)
}

View File

@ -41,7 +41,7 @@ func NewHTTPServer(debug bool,
gin.SetMode(gin.ReleaseMode)
}
r := gin.New()
r.Use(brotli.Brotli(brotli.DefaultCompression))
r.Use(brotli.Brotli(brotli.DefaultCompression), middleware.ExtractAndSetAcceptLanguage)
r.GET("/healthz", func(ctx *gin.Context) { ctx.String(200, "OK") })
viewRouter.Register(r)

View File

@ -1,8 +1,12 @@
package schema
import (
"context"
"fmt"
"net/url"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/translator"
)
const PermaLinkQuestionIDAndTitle = 1
@ -118,7 +122,28 @@ type SiteLoginResp SiteLoginReq
type SiteCustomCssHTMLResp SiteCustomCssHTMLReq
// SiteThemeResp site theme response
type SiteThemeResp SiteThemeReq
type SiteThemeResp struct {
ThemeOptions []*ThemeOption `json:"theme_options"`
Theme string `json:"theme"`
ThemeConfig map[string]interface{} `json:"theme_config"`
}
func (s *SiteThemeResp) TrTheme(ctx context.Context) {
la := handler.GetLangByCtx(ctx)
for _, option := range s.ThemeOptions {
tr := translator.GlobalTrans.Tr(la, option.Value)
// if tr is equal the option value means not found translation, so use the original label
if tr != option.Value {
option.Label = tr
}
}
}
// ThemeOption get label option
type ThemeOption struct {
Label string `json:"label"`
Value string `json:"value"`
}
// SiteWriteResp site write response
type SiteWriteResp SiteWriteReq

View File

@ -1,12 +1,6 @@
package schema
// GetThemeOption get label option
type GetThemeOption struct {
Label string `json:"label"`
Value string `json:"value"`
}
var GetThemeOptions = []*GetThemeOption{
var GetThemeOptions = []*ThemeOption{
{
Label: "Default",
Value: "default",

View File

@ -91,10 +91,13 @@ func (s *SiteInfoCommonService) GetSiteCustomCssHTML(ctx context.Context) (resp
// GetSiteTheme get site theme
func (s *SiteInfoCommonService) GetSiteTheme(ctx context.Context) (resp *schema.SiteThemeResp, err error) {
resp = &schema.SiteThemeResp{}
resp = &schema.SiteThemeResp{
ThemeOptions: schema.GetThemeOptions,
}
if err = s.getSiteInfoByType(ctx, constant.SiteTypeTheme, resp); err != nil {
return nil, err
}
resp.TrTheme(ctx)
return resp, nil
}