改造成api

This commit is contained in:
2025-07-02 14:51:23 +08:00
parent c8a81d0f49
commit 39e91e85ba
27 changed files with 665 additions and 519 deletions

View File

@@ -1,31 +1,30 @@
package auth
import (
"log"
"management/internal/erpserver/model/system/request"
v1 "management/internal/erpserver/service/v1"
authv1 "management/internal/erpserver/service/v1/auth"
"management/internal/pkg/gin/gu"
"management/internal/pkg/mid"
"github.com/drhin/logger"
"github.com/gin-gonic/gin"
"github.com/zhang2092/browser"
)
type app struct {
log *logger.Logger
captchaService v1.CaptchaService
userService v1.UserService
authService *authv1.Auth
}
func newApp(
log *logger.Logger,
captchaService v1.CaptchaService,
userService v1.UserService,
authService *authv1.Auth,
) *app {
return &app{
log: log,
captchaService: captchaService,
userService: userService,
authService: authService,
@@ -35,6 +34,7 @@ func newApp(
func (a *app) login(c *gin.Context) {
var req request.Login
if err := c.ShouldBindJSON(&req); err != nil {
log.Println("captchaID: ", req.CaptchaID)
gu.ValidatorErrors(c, err)
return
}
@@ -48,7 +48,7 @@ func (a *app) login(c *gin.Context) {
req.Url = c.Request.URL.String()
req.Referrer = c.Request.Referer()
br, err := browser.NewBrowser(c.Request.UserAgent())
if err == nil {
if err == nil && br != nil {
req.Os = br.Platform().Name()
req.Browser = br.Name()
}
@@ -62,6 +62,109 @@ func (a *app) login(c *gin.Context) {
gu.Ok(c, risk)
}
type Menu struct {
ID int `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
Icon string `json:"icon"`
Child []Menu `json:"child"`
}
type User struct {
Name string `json:"name"`
Avatar string `json:"avatar"`
RoleID int32 `json:"role_id"`
}
type UserInfo struct {
User User `json:"user"`
Menus []Menu `json:"menus"`
}
func (a *app) getInfo(c *gin.Context) {
auth := mid.GetUser(c)
if auth == nil {
gu.Failed(c, "用户未登录")
return
}
user, err := a.userService.GetByUuid(c, auth.ID)
if err != nil {
gu.Failed(c, err.Error())
return
}
menus := []Menu{
{
ID: 1,
Name: "控制台",
Icon: "house",
Url: "",
Child: []Menu{
{
ID: 21,
Name: "后台首页",
Icon: "aim",
Url: "/",
},
},
},
{
ID: 1,
Name: "系统管理",
Icon: "setting",
Url: "",
Child: []Menu{
{
ID: 11,
Name: "菜单管理",
Icon: "menu",
Url: "/system/menu",
},
{
ID: 12,
Name: "角色管理",
Icon: "star",
Url: "/system/role",
},
{
ID: 12,
Name: "部门管理",
Icon: "clock",
Url: "/system/department",
},
{
ID: 13,
Name: "用户管理",
Icon: "user",
Url: "/system/user",
},
{
ID: 14,
Name: "登录日志",
Icon: "lock",
Url: "/loginlog/list",
},
{
ID: 15,
Name: "操作日志",
Icon: "lock",
Url: "/audit/list",
},
},
},
}
gu.Ok(c, UserInfo{
User: User{
Name: user.Username,
Avatar: user.Avatar,
RoleID: user.RoleID,
},
Menus: menus,
})
}
func (a *app) logout(c *gin.Context) {
gu.Ok(c, nil)
}

View File

@@ -17,9 +17,11 @@ type Config struct {
}
func Routes(public *gin.RouterGroup, private *gin.RouterGroup, cfg Config) {
app := newApp(cfg.Log, cfg.CaptchaService, cfg.UserService, cfg.AuthService)
app := newApp(cfg.CaptchaService, cfg.UserService, cfg.AuthService)
public.POST("/login", app.login)
private.POST("user_info", app.getInfo)
private.GET("/logout", app.logout)
}