This commit is contained in:
JACKYMYPERSON
2025-11-04 23:00:01 +08:00
parent 334752373e
commit 76b863b99d
10 changed files with 12 additions and 57 deletions

View File

@@ -9,21 +9,16 @@ import (
func AVALogin(cfg *config.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 核心:获取请求头中的 session_id与前端发送的头名称一致
sessionID := r.Header.Get("session_id")
// 处理头不存在的情况(返回空字符串)
if sessionID == "" {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"code":401,"msg":"未获取到 session_id请登录"}`))
return
}
// 后续逻辑:验证 sessionID 有效性...
// 例如:查询数据库、缓存判断 session 是否有效
fmt.Printf("获取到的 session_id%s\n", sessionID)
// 响应成功(示例)
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"code":0,"msg":"验证成功"}`))
}

View File

@@ -23,7 +23,6 @@ func AdminLogin(cfg *config.Config) http.HandlerFunc {
return
}
// 验证必填参数
if req.Username == "" {
httpx.ErrorCtx(r.Context(), w, errors.New("用户名不能为空"))
return
@@ -43,7 +42,7 @@ func AdminLogin(cfg *config.Config) http.HandlerFunc {
mysqlCfg.Database,
mysqlCfg.Charset,
)
fmt.Println("接收到articlePost请求")
conn := sqlx.NewSqlConn("mysql", dsn)
AdminModel := model.NewAdminModel(conn)
@@ -60,10 +59,8 @@ func AdminLogin(cfg *config.Config) http.HandlerFunc {
}
func GetClientIP(r *http.Request) string {
// 1. 优先从代理头获取如果经过反向代理如Nginx
ip := r.Header.Get("X-Forwarded-For")
if ip != "" {
// X-Forwarded-For 可能包含多个IP客户端IP, 代理1IP, 代理2IP...),取第一个
parts := strings.Split(ip, ",")
if len(parts) > 0 {
ip = strings.TrimSpace(parts[0])
@@ -73,13 +70,11 @@ func GetClientIP(r *http.Request) string {
}
}
// 2. 其次从 X-Real-IP 获取(部分代理会设置此头)
ip = r.Header.Get("X-Real-IP")
if ip != "" {
return ip
}
// 3. 最后从 RemoteAddr 获取原始客户端IP可能包含端口
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
// 若解析失败,直接返回 RemoteAddr可能包含端口

View File

@@ -33,7 +33,6 @@ func CreateAdminHandler(cfg *config.Config) http.HandlerFunc {
mysqlCfg.Database,
mysqlCfg.Charset,
)
fmt.Println("接收到articlePost请求")
conn := sqlx.NewSqlConn("mysql", dsn)
AdminModel := model.NewAdminModel(conn)

View File

@@ -33,7 +33,6 @@ func DeleteAdminHandler(cfg *config.Config) http.HandlerFunc {
mysqlCfg.Database,
mysqlCfg.Charset,
)
fmt.Println("接收到articlePost请求")
conn := sqlx.NewSqlConn("mysql", dsn)
AdminModel := model.NewAdminModel(conn)

View File

@@ -33,7 +33,6 @@ func GetAdminHandler(cfg *config.Config) http.HandlerFunc {
mysqlCfg.Database,
mysqlCfg.Charset,
)
fmt.Println("接收到articlePost请求")
conn := sqlx.NewSqlConn("mysql", dsn)
AdminModel := model.NewAdminModel(conn)

View File

@@ -33,7 +33,6 @@ func ListAdminHandler(cfg *config.Config) http.HandlerFunc {
mysqlCfg.Database,
mysqlCfg.Charset,
)
fmt.Println("接收到articlePost请求")
conn := sqlx.NewSqlConn("mysql", dsn)
AdminModel := model.NewAdminModel(conn)

View File

@@ -33,7 +33,6 @@ func UpdateAdminHandler(cfg *config.Config) http.HandlerFunc {
mysqlCfg.Database,
mysqlCfg.Charset,
)
fmt.Println("接收到articlePost请求")
conn := sqlx.NewSqlConn("mysql", dsn)
AdminModel := model.NewAdminModel(conn)

View File

@@ -35,7 +35,6 @@ func NewLoginAdminLogic(ctx context.Context, cfg *config.Config, model model.Adm
}
func (l *LoginAdminLogic) LoginAdmin(req *types.LoginReq, ip string) (resp *types.LoginResp, err error) {
// 1. 验证请求参数
if err := l.validateReq(req); err != nil {
return &types.LoginResp{
Code: 1,
@@ -43,17 +42,14 @@ func (l *LoginAdminLogic) LoginAdmin(req *types.LoginReq, ip string) (resp *type
}, nil
}
// 2. 从数据库查询用户(通过 admin 表模型查询,符合 go-zero 分层规范)
admin, err := l.model.FindOneByUsername(l.ctx, req.Username)
if err != nil {
// 处理查询错误:区分"用户不存在"和"数据库异常"
if err == model.ErrNotFound {
return &types.LoginResp{
Code: 1,
Msg: "用户名或密码错误",
}, nil
}
// 数据库异常(记录日志,返回通用错误)
l.Logger.Errorf("查询管理员失败: %v, username: %s", err, req.Username)
return &types.LoginResp{
Code: 500,
@@ -61,7 +57,6 @@ func (l *LoginAdminLogic) LoginAdmin(req *types.LoginReq, ip string) (resp *type
}, nil
}
// 3. 检查账号状态1=启用0=禁用,与表结构一致)
if admin.Status != 1 {
return &types.LoginResp{
Code: 1,
@@ -69,7 +64,6 @@ func (l *LoginAdminLogic) LoginAdmin(req *types.LoginReq, ip string) (resp *type
}, nil
}
// 4. 验证密码(数据库存储 bcrypt 加密后的密码)
if _, err := VerifyPassword(req.Password, admin.Password); err != nil {
return &types.LoginResp{
Code: 1,
@@ -96,7 +90,6 @@ func (l *LoginAdminLogic) LoginAdmin(req *types.LoginReq, ip string) (resp *type
}, nil
}
// 验证请求参数
func (l *LoginAdminLogic) validateReq(req *types.LoginReq) error {
if req.Username == "" {
return errors.New("用户名不能为空")
@@ -113,11 +106,11 @@ func VerifyPassword(plainPassword, hashedPassword string) (bool, error) {
err := bcrypt.CompareHashAndPassword(hashedBytes, plainBytes)
if err == nil {
return true, nil // 匹配成功(密码正确)
return true, nil
} else if err == bcrypt.ErrMismatchedHashAndPassword {
return false, nil // 匹配失败(密码错误)
return false, nil
} else {
return false, err // 其他异常(如哈希格式错误、内存不足等)
return false, err
}
}