answer/internal/controller/siteinfo_controller.go

107 lines
3.0 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package controller
import (
"net/http"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/schema"
2022-11-03 11:12:20 +08:00
"github.com/answerdev/answer/internal/service/siteinfo_common"
2022-09-27 17:59:05 +08:00
"github.com/gin-gonic/gin"
2022-11-14 11:50:00 +08:00
"github.com/segmentfault/pacman/log"
2022-09-27 17:59:05 +08:00
)
type SiteinfoController struct {
2022-11-03 11:12:20 +08:00
siteInfoService *siteinfo_common.SiteInfoCommonService
2022-09-27 17:59:05 +08:00
}
// NewSiteinfoController new siteinfo controller.
2022-11-03 11:12:20 +08:00
func NewSiteinfoController(siteInfoService *siteinfo_common.SiteInfoCommonService) *SiteinfoController {
2022-09-27 17:59:05 +08:00
return &SiteinfoController{
siteInfoService: siteInfoService,
}
}
2022-11-03 10:33:48 +08:00
// GetSiteInfo get site info
// @Summary get site info
// @Description get site info
2022-09-27 17:59:05 +08:00
// @Tags site
// @Produce json
// @Success 200 {object} handler.RespBody{data=schema.SiteGeneralResp}
// @Router /answer/api/v1/siteinfo [get]
2022-11-03 10:33:48 +08:00
func (sc *SiteinfoController) GetSiteInfo(ctx *gin.Context) {
var err error
resp := &schema.SiteInfoResp{}
resp.General, err = sc.siteInfoService.GetSiteGeneral(ctx)
2022-09-27 17:59:05 +08:00
if err != nil {
2022-11-14 11:50:00 +08:00
log.Error(err)
2022-09-27 17:59:05 +08:00
}
2022-11-14 11:50:00 +08:00
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)
}
resp.Login, err = sc.siteInfoService.GetSiteLogin(ctx)
if err != nil {
log.Error(err)
}
2022-11-14 11:50:00 +08:00
handler.HandleResponse(ctx, nil, resp)
2022-09-27 17:59:05 +08:00
}
// GetSiteLegalInfo get site legal info
// @Summary get site legal info
// @Description get site legal info
// @Tags site
// @Param info_type query string true "legal information type" Enums(tos, privacy)
// @Produce json
// @Success 200 {object} handler.RespBody{data=schema.GetSiteLegalInfoResp}
// @Router /answer/api/v1/siteinfo/legal [get]
func (sc *SiteinfoController) GetSiteLegalInfo(ctx *gin.Context) {
req := &schema.GetSiteLegalInfoReq{}
if handler.BindAndCheck(ctx, req) {
return
}
siteLegal, err := sc.siteInfoService.GetSiteLegal(ctx)
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
resp := &schema.GetSiteLegalInfoResp{}
if req.IsTOS() {
resp.TermsOfServiceOriginalText = siteLegal.TermsOfServiceOriginalText
resp.TermsOfServiceParsedText = siteLegal.TermsOfServiceParsedText
} else if req.IsPrivacy() {
resp.PrivacyPolicyOriginalText = siteLegal.PrivacyPolicyOriginalText
resp.PrivacyPolicyParsedText = siteLegal.PrivacyPolicyParsedText
}
handler.HandleResponse(ctx, nil, resp)
}
// GetManifestJson get manifest.json
func (sc *SiteinfoController) GetManifestJson(ctx *gin.Context) {
resp := &schema.GetManifestJsonResp{
ShortName: "Answer",
Name: "Answer.dev",
Icons: map[string]string{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon",
},
StartUrl: ".",
Display: "standalone",
ThemeColor: "#000000",
BackgroundColor: "#ffffff",
}
branding, err := sc.siteInfoService.GetSiteBranding(ctx)
if err != nil {
log.Error(err)
} else if len(branding.Favicon) > 0 {
resp.Icons["scr"] = branding.Favicon
}
ctx.JSON(http.StatusOK, resp)
}