feat: add site info about branding

This commit is contained in:
LinkinStar 2022-11-14 11:50:00 +08:00
parent cfcf4ba02f
commit 6f815fb860
8 changed files with 93 additions and 14 deletions

View File

@ -53,4 +53,5 @@ var (
const (
SiteTypeGeneral = "general"
SiteTypeInterface = "interface"
SiteTypeBranding = "branding"
)

View File

@ -5,6 +5,7 @@ import (
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service/siteinfo_common"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/log"
)
type SiteinfoController struct {
@ -30,9 +31,16 @@ func (sc *SiteinfoController) GetSiteInfo(ctx *gin.Context) {
resp := &schema.SiteInfoResp{}
resp.General, err = sc.siteInfoService.GetSiteGeneral(ctx)
if err != nil {
handler.HandleResponse(ctx, err, resp)
return
log.Error(err)
}
resp.Face, err = sc.siteInfoService.GetSiteInterface(ctx)
handler.HandleResponse(ctx, err, resp)
resp.Interface, err = sc.siteInfoService.GetSiteInterface(ctx)
if err != nil {
log.Error(err)
}
resp.Branding, err = sc.siteInfoService.GetSiteBranding(ctx)
if err != nil {
log.Error(err)
}
handler.HandleResponse(ctx, nil, resp)
}

View File

@ -7,11 +7,12 @@ import (
"github.com/gin-gonic/gin"
)
// SiteInfoController site info controller
type SiteInfoController struct {
siteInfoService *siteinfo.SiteInfoService
}
// NewSiteInfoController new siteinfo controller.
// NewSiteInfoController new site info controller
func NewSiteInfoController(siteInfoService *siteinfo.SiteInfoService) *SiteInfoController {
return &SiteInfoController{
siteInfoService: siteInfoService,
@ -44,6 +45,19 @@ func (sc *SiteInfoController) GetInterface(ctx *gin.Context) {
handler.HandleResponse(ctx, err, resp)
}
// GetBranding get site interface
// @Summary get site interface
// @Description get site interface
// @Security ApiKeyAuth
// @Tags admin
// @Produce json
// @Success 200 {object} handler.RespBody{data=schema.SiteBrandingResp}
// @Router /answer/admin/api/siteinfo/branding [get]
func (sc *SiteInfoController) GetBranding(ctx *gin.Context) {
resp, err := sc.siteInfoService.GetSiteInterface(ctx)
handler.HandleResponse(ctx, err, resp)
}
// UpdateGeneral update site general information
// @Summary update site general information
// @Description update site general information
@ -80,6 +94,24 @@ func (sc *SiteInfoController) UpdateInterface(ctx *gin.Context) {
handler.HandleResponse(ctx, err, nil)
}
// UpdateBranding update site branding
// @Summary update site info branding
// @Description update site info branding
// @Security ApiKeyAuth
// @Tags admin
// @Produce json
// @Param data body schema.SiteInterfaceReq true "branding info"
// @Success 200 {object} handler.RespBody{}
// @Router /answer/admin/api/siteinfo/branding [put]
func (sc *SiteInfoController) UpdateBranding(ctx *gin.Context) {
req := &schema.SiteBrandingReq{}
if handler.BindAndCheck(ctx, req) {
return
}
err := sc.siteInfoService.SaveSiteBranding(ctx, req)
handler.HandleResponse(ctx, err, nil)
}
// GetSMTPConfig get smtp config
// @Summary GetSMTPConfig get smtp config
// @Description GetSMTPConfig get smtp config

View File

@ -224,8 +224,10 @@ func (a *AnswerAPIRouter) RegisterAnswerCmsAPIRouter(r *gin.RouterGroup) {
// siteinfo
r.GET("/siteinfo/general", a.siteInfoController.GetGeneral)
r.GET("/siteinfo/interface", a.siteInfoController.GetInterface)
r.GET("/siteinfo/branding", a.siteInfoController.GetBranding)
r.PUT("/siteinfo/general", a.siteInfoController.UpdateGeneral)
r.PUT("/siteinfo/interface", a.siteInfoController.UpdateInterface)
r.PUT("/siteinfo/branding", a.siteInfoController.UpdateBranding)
r.GET("/setting/smtp", a.siteInfoController.GetSMTPConfig)
r.PUT("/setting/smtp", a.siteInfoController.UpdateSMTPConfig)

View File

@ -68,24 +68,23 @@ func (a *UIRouter) Register(r *gin.Engine) {
// specify the not router for default routes and redirect
r.NoRoute(func(c *gin.Context) {
name := c.Request.URL.Path
urlPath := c.Request.URL.Path
filePath := ""
var file []byte
var err error
switch name {
switch urlPath {
case "/favicon.ico":
c.Header("content-type", "image/vnd.microsoft.icon")
filePath = UIRootFilePath + name
filePath = UIRootFilePath + urlPath
case "/manifest.json":
filePath = UIRootFilePath + name
filePath = UIRootFilePath + urlPath
case "/install":
// if answer is running by run command user can not access install page.
c.Redirect(http.StatusFound, "/")
return
default:
filePath = UIIndexFilePath
c.Header("content-type", "text/html;charset=utf-8")
}
file, err = ui.Build.ReadFile(filePath)
file, err := ui.Build.ReadFile(filePath)
if err != nil {
log.Error(err)
c.Status(http.StatusNotFound)

View File

@ -30,15 +30,27 @@ type SiteInterfaceReq struct {
TimeZone string `validate:"required,gt=1,lte=128" form:"time_zone" json:"time_zone"`
}
// SiteBrandingReq site branding request
type SiteBrandingReq struct {
MobileLogo string `validate:"omitempty,gt=0,lte=512" form:"mobile_logo" json:"mobile_logo"`
SquareIcon string `validate:"required,gt=0,lte=512" form:"square_icon" json:"square_icon"`
Favicon string `validate:"omitempty,gt=0,lte=512" form:"favicon" json:"favicon"`
}
// SiteGeneralResp site general response
type SiteGeneralResp SiteGeneralReq
// SiteInterfaceResp site interface response
type SiteInterfaceResp SiteInterfaceReq
// SiteBrandingResp site branding response
type SiteBrandingResp SiteBrandingReq
// SiteInfoResp get site info response
type SiteInfoResp struct {
General *SiteGeneralResp `json:"general"`
Face *SiteInterfaceResp `json:"interface"`
General *SiteGeneralResp `json:"general"`
Interface *SiteInterfaceResp `json:"interface"`
Branding *SiteBrandingResp `json:"branding"`
}
// UpdateSMTPConfigReq get smtp config request

View File

@ -109,6 +109,17 @@ func (s *SiteInfoService) SaveSiteInterface(ctx context.Context, req schema.Site
return
}
// SaveSiteBranding save site branding information
func (s *SiteInfoService) SaveSiteBranding(ctx context.Context, req *schema.SiteBrandingReq) (err error) {
content, _ := json.Marshal(req)
data := &entity.SiteInfo{
Type: constant.SiteTypeBranding,
Content: string(content),
Status: 1,
}
return s.siteInfoRepo.SaveByType(ctx, constant.SiteTypeBranding, data)
}
// GetSMTPConfig get smtp config
func (s *SiteInfoService) GetSMTPConfig(ctx context.Context) (
resp *schema.GetSMTPConfigResp, err error,

View File

@ -48,3 +48,17 @@ func (s *SiteInfoCommonService) GetSiteInterface(ctx context.Context) (resp *sch
_ = json.Unmarshal([]byte(siteInfo.Content), resp)
return resp, nil
}
// GetSiteBranding get site info branding
func (s *SiteInfoCommonService) GetSiteBranding(ctx context.Context) (resp *schema.SiteBrandingResp, err error) {
siteInfo, exist, err := s.siteInfoRepo.GetByType(ctx, constant.SiteTypeBranding)
if err != nil {
return nil, err
}
if !exist {
return nil, errors.BadRequest(reason.SiteInfoNotFound)
}
resp = &schema.SiteBrandingResp{}
_ = json.Unmarshal([]byte(siteInfo.Content), resp)
return resp, nil
}