完成全部后端和管理系统
This commit is contained in:
30
server/internal/admin/handler/admin/AvaLoginhandler.go
Normal file
30
server/internal/admin/handler/admin/AvaLoginhandler.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
)
|
||||
|
||||
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":"验证成功"}`))
|
||||
}
|
||||
}
|
||||
89
server/internal/admin/handler/admin/adminloginhandler.go
Normal file
89
server/internal/admin/handler/admin/adminloginhandler.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/admin/internal/logic/admin"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/admin/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/admin/internal/types"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func AdminLogin(cfg *config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.LoginReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, fmt.Errorf("参数解析失败:%v", err))
|
||||
return
|
||||
}
|
||||
|
||||
// 验证必填参数
|
||||
if req.Username == "" {
|
||||
httpx.ErrorCtx(r.Context(), w, errors.New("用户名不能为空"))
|
||||
return
|
||||
}
|
||||
if req.Password == "" {
|
||||
httpx.ErrorCtx(r.Context(), w, errors.New("密码不能为空"))
|
||||
return
|
||||
}
|
||||
|
||||
mysqlCfg := cfg.MySQL
|
||||
dsn := fmt.Sprintf(
|
||||
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=true&loc=Local",
|
||||
mysqlCfg.Username,
|
||||
mysqlCfg.Password,
|
||||
mysqlCfg.Host,
|
||||
mysqlCfg.Port,
|
||||
mysqlCfg.Database,
|
||||
mysqlCfg.Charset,
|
||||
)
|
||||
fmt.Println("接收到articlePost请求")
|
||||
conn := sqlx.NewSqlConn("mysql", dsn)
|
||||
AdminModel := model.NewAdminModel(conn)
|
||||
|
||||
l := admin.NewLoginAdminLogic(r.Context(), cfg, AdminModel)
|
||||
|
||||
clientIP := GetClientIP(r)
|
||||
resp, err := l.LoginAdmin(&req, clientIP)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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])
|
||||
if ip != "" {
|
||||
return ip
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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(可能包含端口)
|
||||
return r.RemoteAddr
|
||||
}
|
||||
return ip
|
||||
}
|
||||
Reference in New Issue
Block a user