优化菜单加载

This commit is contained in:
2025-11-04 10:08:50 +08:00
parent ca1d265e34
commit 34035fb007
13 changed files with 351 additions and 35 deletions
+43 -2
View File
@@ -3,6 +3,7 @@ package controllers
import (
"encoding/json"
"server/models"
"strings"
"time"
"github.com/beego/beego/v2/client/orm"
@@ -81,10 +82,50 @@ func (c *AuthController) Login() {
"data": nil,
}
} else {
// 登录成功,写当前时间到last_login_time,并增加login_count
// 登录成功,写当前时间到last_login_time获取IP写入last_login_ip并增加login_count
loginTime := time.Now()
// 获取客户端IP地址
clientIP := c.Ctx.Input.IP()
// 优先从X-Forwarded-For获取真实IP(适用于代理环境)
forwardedFor := c.Ctx.Input.Header("X-Forwarded-For")
if forwardedFor != "" {
// X-Forwarded-For可能包含多个IP,取第一个
ips := strings.Split(forwardedFor, ",")
if len(ips) > 0 {
ip := strings.TrimSpace(ips[0])
// 过滤掉本地地址
if ip != "" && ip != "::1" && ip != "127.0.0.1" && !strings.HasPrefix(ip, "192.168.") && !strings.HasPrefix(ip, "10.") && !strings.HasPrefix(ip, "172.16.") {
clientIP = ip
} else if ip != "" {
clientIP = ip
}
}
}
// 如果X-Forwarded-For没有有效IP,尝试从X-Real-IP获取
if clientIP == "" || clientIP == "::1" || clientIP == "127.0.0.1" {
realIP := c.Ctx.Input.Header("X-Real-IP")
if realIP != "" {
ip := strings.TrimSpace(realIP)
if ip != "::1" && ip != "127.0.0.1" {
clientIP = ip
}
}
}
// 如果获取到的是IPv6的localhost,转换为IPv4格式显示
if clientIP == "::1" {
clientIP = "127.0.0.1"
}
// 如果仍然没有获取到IP,使用默认值
if clientIP == "" {
clientIP = "unknown"
}
o := orm.NewOrm()
_, _ = o.Raw("UPDATE yz_users SET last_login_time = ?, login_count = IFNULL(login_count,0)+1 WHERE id = ?", loginTime, user.Id).Exec()
_, _ = o.Raw("UPDATE yz_users SET last_login_time = ?, last_login_ip = ?, login_count = IFNULL(login_count,0)+1 WHERE id = ?", loginTime, clientIP, user.Id).Exec()
c.Data["json"] = map[string]interface{}{
"code": 0,
+24
View File
@@ -78,6 +78,7 @@ func (c *UserController) GetTenantUsers() {
"status": user.Status,
"role": user.Role,
"last_login_time": user.LastLoginTime,
"last_login_ip": user.LastLoginIp,
})
}
@@ -364,6 +365,29 @@ func (c *UserController) DeleteUser() {
return
}
// 先查询用户信息,检查是否为admin账号
user, err := models.GetUserInfo(userId, "", 0)
if err != nil {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "查询用户失败: " + err.Error(),
"data": nil,
}
c.ServeJSON()
return
}
// 禁止删除admin账号
if user.Username == "admin" {
c.Data["json"] = map[string]interface{}{
"code": 1,
"message": "admin账号不允许删除",
"data": nil,
}
c.ServeJSON()
return
}
// 调用模型层方法删除用户
err = models.DeleteUser(userId)