admin_backend/controller/user.go
2026-05-07 14:39:24 +08:00

202 lines
4.8 KiB
Go

package controller
import (
"backend/model"
"backend/store"
"backend/util"
"fmt"
"log"
"slices"
"github.com/gin-gonic/gin"
)
func UserInfo(c *gin.Context) {
Token := util.GetToken(c)
info, _ := store.GetTokenInfo(Token)
if info == nil {
c.JSON(401, gin.H{"code": 401, "message": "Unauthorized"})
return
}
homePath := "/dashboard"
// 从 DB 查询用户 ID 及是否 super
db := util.MPool.GetGameDB()
if db == nil {
c.JSON(500, gin.H{"code": 500, "message": "db unavailable"})
return
}
var adminRow struct {
ID int `db:"id"`
Role int `db:"role"`
}
if err := db.Get(&adminRow, "SELECT id, role FROM admin WHERE username = ?", info.UserName); err != nil {
db.Close()
log.Printf("UserInfo: query admin failed: %v", err)
c.JSON(500, gin.H{"code": 500, "message": "query failed"})
return
}
db.Close()
isSuperAdmin := util.GetRole(adminRow.Role) == util.RoleSuper
// 更新最后登录 IP 和时间
clientIP := c.ClientIP()
now := int(util.Now())
db2 := util.MPool.GetGameDB()
if db2 != nil {
db2.Exec("UPDATE admin SET lastLoginIp=?, lastLoginTime=? WHERE id=?", clientIP, now, adminRow.ID)
db2.Close()
}
effectiveInfo, err := model.GetUserEffectiveInfo(adminRow.ID, isSuperAdmin)
if err != nil {
log.Printf("UserInfo: GetUserEffectiveInfo failed: %v", err)
// 降级:只返回 token 里的静态角色
effectiveInfo = &model.UserEffectiveInfo{
Roles: []string{info.Role},
Permissions: []string{},
}
}
// super 在 roles 中补充标记,供前端路由守卫识别
roles := effectiveInfo.Roles
if isSuperAdmin || slices.Contains(roles, "R_SUPER") {
roles = []string{util.RoleSuper}
}
if len(roles) == 1 && slices.Contains(roles, "R_LANGUAGE") {
homePath = "/language"
}
c.JSON(200, gin.H{
"code": 0,
"data": map[string]interface{}{
"userId": fmt.Sprintf("%d", adminRow.ID),
"username": info.UserName,
"realName": info.UserName,
"avatar": "",
"homePath": homePath,
"roles": roles,
"permissions": effectiveInfo.Permissions,
},
"message": "ok",
})
}
// 获取玩家列表
func UserList(c *gin.Context) {
var request struct {
Id int `json:"Id"`
Uid int `json:"Uid"`
ServerId int `json:"ServerId"`
PageSize int `json:"pageSize"`
CurrentPage int `json:"currentPage"`
StartTime int `json:"StartTime"`
EndTime int `json:"EndTime"`
Nickname string `json:"Nickname"`
Username string `json:"Username"`
DeviceId string `json:"DeviceId"`
}
err := c.BindJSON(&request)
if err != nil {
fmt.Print(err)
failed(c, err.Error())
return
}
user, total, err := model.GetUserList(request.Id, request.ServerId, request.PageSize, request.CurrentPage, request.Uid, request.StartTime, request.EndTime, request.Nickname, request.Username, request.DeviceId)
if err != nil {
fmt.Print(err)
failed(c, err.Error())
return
}
success(c, map[string]interface{}{
"total": total,
"data": user,
})
}
func UserDetail(c *gin.Context) {
var request struct {
Uid int `json:"Id"`
Node int `json:"Node"`
AppId int `json:"AppId"`
}
err := c.BindJSON(&request)
if err != nil {
fmt.Print(err)
failed(c, err.Error())
return
}
user, err := model.UserDetail2(request.AppId, request.Uid, request.Node)
if err != nil {
fmt.Print(err)
failed(c, err.Error())
return
}
for _, v := range user.Order {
for _, chessInfo := range v.Chess {
chess_url := util.GetChessURL(util.String(chessInfo.GetId()))
chessInfo.Icon = chess_url
}
v.ChessId = v.Chess
}
for _, v := range user.FriendList {
face_url := util.GetFaceURL(util.Int(v.Avatar))
v.AvatarUrl = face_url
v.OnlineStatus = util.Int(v.LoginTime) > util.Int(v.LogoutTime)
}
success(c, user)
}
func UserGM(c *gin.Context) {
var request struct {
AppId int `json:"AppId"`
ServerId int `json:"ServerId"`
Uid int `json:"Uid"`
Command string `json:"Command"`
}
err := c.BindJSON(&request)
if err != nil {
fmt.Print(err)
failed(c, err.Error())
return
}
if request.Command == "" || request.AppId == 0 || request.AppId == 3 {
failed(c, "参数错误")
}
r, err := model.UserGM(request.AppId, request.ServerId, request.Uid, request.Command)
if err != nil {
fmt.Print(err)
failed(c, err.Error())
return
}
success(c, r)
}
func UserBan(c *gin.Context) {
var request struct {
AppId int `json:"AppId"`
ServerId int `json:"ServerId"`
Uid int `json:"Uid"`
Day int `json:"Day"`
Reason string `json:"Reason"`
}
err := c.BindJSON(&request)
if err != nil {
fmt.Print(err)
failed(c, err.Error())
return
}
r, err := model.UserBan(request.AppId, request.ServerId, request.Uid, request.Day, request.Reason)
if err != nil {
fmt.Print(err)
failed(c, err.Error())
return
}
success(c, r)
}