mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-05 16:56:22 +00:00
⚡️ feat: add main page layout with navigation and footer
feat: create random labels page feat: implement login page with OpenID Connect support feat: add Gravatar component for user avatars feat: create Navbar component with navigation menu chore: create Sidebar component placeholder feat: implement login form with OIDC and email/password options feat: add reusable button component feat: create card component for structured content display feat: implement input component for forms feat: create label component for form labels feat: add navigation menu component for site navigation chore: add configuration file for site metadata feat: implement device context for responsive design feat: add utility functions for class name management feat: define OIDC configuration model feat: define base response model for API responses feat: define user model for user data feat: implement i18n for internationalization support feat: add English and Chinese translations for login chore: create index for locale resources chore: add blog home view placeholder
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
||||
"github.com/snowykami/neo-blog/internal/service"
|
||||
"github.com/snowykami/neo-blog/pkg/errs"
|
||||
"github.com/snowykami/neo-blog/pkg/resps"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type AdminController struct {
|
||||
@ -22,7 +23,7 @@ func NewAdminController() *AdminController {
|
||||
func (cc *AdminController) CreateOidc(ctx context.Context, c *app.RequestContext) {
|
||||
var adminCreateOidcReq dto.AdminOidcConfigDto
|
||||
if err := c.BindAndValidate(&adminCreateOidcReq); err != nil {
|
||||
c.JSON(400, map[string]string{"error": "Invalid parameters"})
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
err := cc.service.CreateOidcConfig(&adminCreateOidcReq)
|
||||
@ -77,12 +78,23 @@ func (cc *AdminController) ListOidc(ctx context.Context, c *app.RequestContext)
|
||||
}
|
||||
|
||||
func (cc *AdminController) UpdateOidc(ctx context.Context, c *app.RequestContext) {
|
||||
var adminUpdateOidcReq dto.AdminOidcConfigDto
|
||||
if err := c.BindAndValidate(&adminUpdateOidcReq); err != nil {
|
||||
c.JSON(400, map[string]string{"error": "Invalid parameters"})
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
err := cc.service.UpdateOidcConfig(&adminUpdateOidcReq)
|
||||
idInt, err := strconv.Atoi(id)
|
||||
if err != nil || idInt <= 0 {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
var adminUpdateOidcReq dto.AdminOidcConfigDto
|
||||
if err := c.BindAndValidate(&adminUpdateOidcReq); err != nil {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
adminUpdateOidcReq.ID = uint(idInt)
|
||||
err = cc.service.UpdateOidcConfig(&adminUpdateOidcReq)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
|
@ -3,10 +3,14 @@ package v1
|
||||
import (
|
||||
"context"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/snowykami/neo-blog/internal/ctxutils"
|
||||
"github.com/snowykami/neo-blog/internal/dto"
|
||||
"github.com/snowykami/neo-blog/internal/service"
|
||||
"github.com/snowykami/neo-blog/pkg/constant"
|
||||
"github.com/snowykami/neo-blog/pkg/errs"
|
||||
"github.com/snowykami/neo-blog/pkg/resps"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type PostController struct {
|
||||
@ -24,7 +28,7 @@ func (p *PostController) Create(ctx context.Context, c *app.RequestContext) {
|
||||
if err := c.BindAndValidate(&req); err != nil {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
}
|
||||
if err := p.service.CreatePost(&req); err != nil {
|
||||
if err := p.service.CreatePost(ctx, &req); err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
@ -47,13 +51,63 @@ func (p *PostController) Delete(ctx context.Context, c *app.RequestContext) {
|
||||
}
|
||||
|
||||
func (p *PostController) Get(ctx context.Context, c *app.RequestContext) {
|
||||
// TODO: Impl
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
post, err := p.service.GetPost(ctx, id)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
if post == nil {
|
||||
resps.NotFound(c, resps.ErrNotFound)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, post)
|
||||
}
|
||||
|
||||
func (p *PostController) Update(ctx context.Context, c *app.RequestContext) {
|
||||
// TODO: Impl
|
||||
var req dto.CreateOrUpdatePostReq
|
||||
if err := c.BindAndValidate(&req); err != nil {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
if err := p.service.UpdatePost(ctx, id, &req); err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, nil)
|
||||
}
|
||||
|
||||
func (p *PostController) List(ctx context.Context, c *app.RequestContext) {
|
||||
// TODO: Impl
|
||||
pagination := ctxutils.GetPaginationParams(c)
|
||||
if pagination.OrderedBy != "" && !slices.Contains(constant.OrderedByEnumPost, pagination.OrderedBy) {
|
||||
resps.BadRequest(c, "无效的排序字段")
|
||||
return
|
||||
}
|
||||
keywords := c.Query("keywords")
|
||||
keywordsArray := strings.Split(keywords, ",")
|
||||
req := &dto.ListPostReq{
|
||||
Keywords: keywordsArray,
|
||||
Page: pagination.Page,
|
||||
Size: pagination.Size,
|
||||
OrderedBy: pagination.OrderedBy,
|
||||
Reverse: pagination.Reverse,
|
||||
}
|
||||
resp, err := p.service.ListPosts(ctx, req)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, resp)
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/common/utils"
|
||||
"github.com/snowykami/neo-blog/internal/ctxutils"
|
||||
@ -82,8 +83,13 @@ func (u *UserController) OidcList(ctx context.Context, c *app.RequestContext) {
|
||||
|
||||
func (u *UserController) OidcLogin(ctx context.Context, c *app.RequestContext) {
|
||||
name := c.Param("name")
|
||||
code := c.Param("code")
|
||||
state := c.Param("state")
|
||||
code := c.Query("code")
|
||||
state := c.Query("state")
|
||||
redirectUri := c.Query("redirect_back") // 前端路由登录前的重定向地址
|
||||
if redirectUri == "" {
|
||||
redirectUri = "/"
|
||||
}
|
||||
fmt.Println("redirectBack:", redirectUri)
|
||||
oidcLoginReq := &dto.OidcLoginReq{
|
||||
Name: name,
|
||||
Code: code,
|
||||
@ -96,22 +102,14 @@ func (u *UserController) OidcLogin(ctx context.Context, c *app.RequestContext) {
|
||||
return
|
||||
}
|
||||
ctxutils.SetTokenAndRefreshTokenCookie(c, resp.Token, resp.RefreshToken)
|
||||
resps.Ok(c, resps.Success, map[string]any{
|
||||
"token": resp.Token,
|
||||
"user": resp.User,
|
||||
})
|
||||
resps.Redirect(c, redirectUri) // 重定向到前端路由
|
||||
}
|
||||
|
||||
func (u *UserController) GetUser(ctx context.Context, c *app.RequestContext) {
|
||||
userID := c.Param("id")
|
||||
if userID == "" {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
userIDInt, err := strconv.Atoi(userID)
|
||||
if err != nil || userIDInt <= 0 {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
userIDInt = int(ctxutils.GetCurrentUserID(ctx))
|
||||
}
|
||||
|
||||
resp, err := u.service.GetUser(&dto.GetUserReq{UserID: uint(userIDInt)})
|
||||
@ -142,7 +140,7 @@ func (u *UserController) UpdateUser(ctx context.Context, c *app.RequestContext)
|
||||
updateUserReq.ID = uint(userIDInt)
|
||||
currentUser := ctxutils.GetCurrentUser(ctx)
|
||||
if currentUser == nil {
|
||||
resps.UnAuthorized(c, resps.ErrUnauthorized)
|
||||
resps.Unauthorized(c, resps.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
if currentUser.ID != updateUserReq.ID {
|
||||
|
Reference in New Issue
Block a user