mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-17 15:02:25 +08:00
init commit
This commit is contained in:
75
service/gm-hk4e/controller/controller.go
Normal file
75
service/gm-hk4e/controller/controller.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"flswld.com/common/config"
|
||||
"flswld.com/light"
|
||||
waterAuth "flswld.com/water-api/auth"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
rpcWaterAuthConsumer *light.Consumer
|
||||
rpcHk4eGatewayConsumer *light.Consumer
|
||||
}
|
||||
|
||||
func NewController(rpcWaterAuthConsumer *light.Consumer, rpcHk4eGatewayConsumer *light.Consumer) (r *Controller) {
|
||||
r = new(Controller)
|
||||
r.rpcWaterAuthConsumer = rpcWaterAuthConsumer
|
||||
r.rpcHk4eGatewayConsumer = rpcHk4eGatewayConsumer
|
||||
go r.registerRouter()
|
||||
return r
|
||||
}
|
||||
|
||||
func (c *Controller) getAccessToken(context *gin.Context) string {
|
||||
accessToken := context.GetHeader("Authorization")
|
||||
divIndex := strings.Index(accessToken, " ")
|
||||
if divIndex > 0 {
|
||||
payload := accessToken[divIndex+1:]
|
||||
return payload
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// access_token鉴权
|
||||
func (c *Controller) authorize() gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
valid, err := waterAuth.WaterVerifyAccessToken(c.rpcWaterAuthConsumer, c.getAccessToken(context))
|
||||
if err == nil && valid == true {
|
||||
// 验证通过
|
||||
context.Next()
|
||||
return
|
||||
}
|
||||
// 验证不通过
|
||||
context.Abort()
|
||||
context.JSON(http.StatusOK, gin.H{
|
||||
"code": "10001",
|
||||
"msg": "没有访问权限",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) registerRouter() {
|
||||
if config.CONF.Logger.Level == "DEBUG" {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
} else {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
engine := gin.Default()
|
||||
// gacha
|
||||
engine.GET("/gm/gacha", c.gacha)
|
||||
engine.GET("/gm/gacha/details", c.gachaDetails)
|
||||
engine.Use(c.authorize())
|
||||
// gate
|
||||
engine.POST("/gm/gate/state", c.changeGateState)
|
||||
engine.POST("/gm/gate/kick", c.kickPlayer)
|
||||
engine.GET("/gm/gate/online", c.getOnlineUser)
|
||||
engine.POST("/gm/gate/forbid", c.forbidUser)
|
||||
engine.POST("/gm/gate/forbid/cancel", c.unForbidUser)
|
||||
port := strconv.FormatInt(int64(config.CONF.HttpPort), 10)
|
||||
portStr := ":" + port
|
||||
_ = engine.Run(portStr)
|
||||
}
|
||||
65
service/gm-hk4e/controller/gacha_controller.go
Normal file
65
service/gm-hk4e/controller/gacha_controller.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"flswld.com/common/entity/dto"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type UserInfo struct {
|
||||
UserId uint32 `json:"userId"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func (c *Controller) gacha(context *gin.Context) {
|
||||
jwtStr := context.Query("jwt")
|
||||
token, err := jwt.ParseWithClaims(jwtStr, new(UserInfo), func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte("flswld"), nil
|
||||
})
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10005, "验签失败", nil))
|
||||
return
|
||||
}
|
||||
if !token.Valid {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10005, "验签失败", nil))
|
||||
return
|
||||
}
|
||||
info, ok := token.Claims.(*UserInfo)
|
||||
if !ok {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10005, "验签失败", nil))
|
||||
return
|
||||
}
|
||||
gachaType := context.Query("gachaType")
|
||||
rsp := map[string]any{
|
||||
"uid": info.UserId,
|
||||
"gachaType": gachaType,
|
||||
}
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(0, "成功", rsp))
|
||||
}
|
||||
|
||||
func (c *Controller) gachaDetails(context *gin.Context) {
|
||||
jwtStr := context.Query("jwt")
|
||||
token, err := jwt.ParseWithClaims(jwtStr, new(UserInfo), func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte("flswld"), nil
|
||||
})
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10005, "验签失败", nil))
|
||||
return
|
||||
}
|
||||
if !token.Valid {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10005, "验签失败", nil))
|
||||
return
|
||||
}
|
||||
info, ok := token.Claims.(*UserInfo)
|
||||
if !ok {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10005, "验签失败", nil))
|
||||
return
|
||||
}
|
||||
scheduleId := context.Query("scheduleId")
|
||||
rsp := map[string]any{
|
||||
"uid": info.UserId,
|
||||
"scheduleId": scheduleId,
|
||||
}
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(0, "成功", rsp))
|
||||
}
|
||||
161
service/gm-hk4e/controller/gate_controller.go
Normal file
161
service/gm-hk4e/controller/gate_controller.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"flswld.com/common/entity/dto"
|
||||
"flswld.com/gate-hk4e-api/gm"
|
||||
waterAuth "flswld.com/water-api/auth"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (c *Controller) changeGateState(context *gin.Context) {
|
||||
accessToken := c.getAccessToken(context)
|
||||
user, err := waterAuth.WaterQueryUserByAccessToken(c.rpcWaterAuthConsumer, accessToken)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(1001, "服务器内部错误", nil))
|
||||
return
|
||||
}
|
||||
if !user.IsAdmin {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10001, "没有访问权限", nil))
|
||||
return
|
||||
}
|
||||
stateStr := context.Query("state")
|
||||
state, err := strconv.ParseBool(stateStr)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10003, "参数错误", nil))
|
||||
return
|
||||
}
|
||||
var res bool
|
||||
ok := c.rpcHk4eGatewayConsumer.CallFunction("RpcManager", "ChangeGateOpenState", &state, &res)
|
||||
if ok == true && res == true {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(0, "操作成功", nil))
|
||||
} else {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(-1, "操作失败", nil))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) kickPlayer(context *gin.Context) {
|
||||
accessToken := c.getAccessToken(context)
|
||||
user, err := waterAuth.WaterQueryUserByAccessToken(c.rpcWaterAuthConsumer, accessToken)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(1001, "服务器内部错误", nil))
|
||||
return
|
||||
}
|
||||
if !user.IsAdmin {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10001, "没有访问权限", nil))
|
||||
return
|
||||
}
|
||||
uidStr := context.Query("uid")
|
||||
reasonStr := context.Query("reason")
|
||||
uid, err := strconv.ParseInt(uidStr, 10, 64)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10003, "参数错误", nil))
|
||||
return
|
||||
}
|
||||
reason, err := strconv.ParseInt(reasonStr, 10, 64)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10003, "参数错误", nil))
|
||||
return
|
||||
}
|
||||
info := new(gm.KickPlayerInfo)
|
||||
info.UserId = uint32(uid)
|
||||
info.Reason = uint32(reason)
|
||||
var result bool
|
||||
ok := c.rpcHk4eGatewayConsumer.CallFunction("RpcManager", "KickPlayer", &info, &result)
|
||||
if ok == true && result == true {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(0, "操作成功", nil))
|
||||
} else {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(-1, "操作失败", nil))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) getOnlineUser(context *gin.Context) {
|
||||
accessToken := c.getAccessToken(context)
|
||||
user, err := waterAuth.WaterQueryUserByAccessToken(c.rpcWaterAuthConsumer, accessToken)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(1001, "服务器内部错误", nil))
|
||||
return
|
||||
}
|
||||
if !user.IsAdmin {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10001, "没有访问权限", nil))
|
||||
return
|
||||
}
|
||||
uidStr := context.Query("uid")
|
||||
uid, err := strconv.ParseInt(uidStr, 10, 64)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10003, "参数错误", nil))
|
||||
return
|
||||
}
|
||||
list := new(gm.OnlineUserList)
|
||||
list.UserList = make([]*gm.OnlineUserInfo, 0)
|
||||
userId := uint32(uid)
|
||||
ok := c.rpcHk4eGatewayConsumer.CallFunction("RpcManager", "GetOnlineUser", &userId, &list)
|
||||
if ok {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(0, "查询成功", list))
|
||||
} else {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(-1, "查询失败", nil))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) forbidUser(context *gin.Context) {
|
||||
accessToken := c.getAccessToken(context)
|
||||
user, err := waterAuth.WaterQueryUserByAccessToken(c.rpcWaterAuthConsumer, accessToken)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(1001, "服务器内部错误", nil))
|
||||
return
|
||||
}
|
||||
if !user.IsAdmin {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10001, "没有访问权限", nil))
|
||||
return
|
||||
}
|
||||
uidStr := context.Query("uid")
|
||||
uid, err := strconv.ParseInt(uidStr, 10, 64)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10003, "参数错误", nil))
|
||||
return
|
||||
}
|
||||
endTimeStr := context.Query("endTime")
|
||||
endTime, err := strconv.ParseInt(endTimeStr, 10, 64)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10003, "参数错误", nil))
|
||||
return
|
||||
}
|
||||
info := new(gm.ForbidUserInfo)
|
||||
info.UserId = uint32(uid)
|
||||
info.ForbidEndTime = uint64(endTime)
|
||||
var result bool
|
||||
ok := c.rpcHk4eGatewayConsumer.CallFunction("RpcManager", "ForbidUser", &info, &result)
|
||||
if ok == true && result == true {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(0, "操作成功", nil))
|
||||
} else {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(-1, "操作失败", nil))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) unForbidUser(context *gin.Context) {
|
||||
accessToken := c.getAccessToken(context)
|
||||
user, err := waterAuth.WaterQueryUserByAccessToken(c.rpcWaterAuthConsumer, accessToken)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(1001, "服务器内部错误", nil))
|
||||
return
|
||||
}
|
||||
if !user.IsAdmin {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10001, "没有访问权限", nil))
|
||||
return
|
||||
}
|
||||
uidStr := context.Query("uid")
|
||||
uid, err := strconv.ParseInt(uidStr, 10, 64)
|
||||
if err != nil {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(10003, "参数错误", nil))
|
||||
return
|
||||
}
|
||||
userId := uint32(uid)
|
||||
var result bool
|
||||
ok := c.rpcHk4eGatewayConsumer.CallFunction("RpcManager", "UnForbidUser", &userId, &result)
|
||||
if ok == true && result == true {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(0, "操作成功", nil))
|
||||
} else {
|
||||
context.JSON(http.StatusOK, dto.NewResponseResult(-1, "操作失败", nil))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user