You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
420 lines
15 KiB
420 lines
15 KiB
package serviceGame |
|
|
|
import ( |
|
"context" |
|
"errors" |
|
"fmt" |
|
"github.com/gogf/gf/v2/encoding/gcharset" |
|
"github.com/gogf/gf/v2/encoding/gjson" |
|
"github.com/gogf/gf/v2/frame/g" |
|
"log" |
|
"strings" |
|
"tyj_admin/api/v1/game" |
|
"tyj_admin/internal/consts" |
|
"tyj_admin/internal/dao" |
|
"tyj_admin/internal/model/entity" |
|
"tyj_admin/internal/service" |
|
"tyj_admin/internal/serviceGame/internal" |
|
"tyj_admin/library/libUtils" |
|
"tyj_admin/library/liberr" |
|
"tyj_admin/utils" |
|
) |
|
|
|
type IGameLoginUrl interface { |
|
GetLoginUrl(ctx context.Context, req *game.GetLoginUrlReq) (res *game.GetLoginUrlRes, err error) |
|
GetServerConfigGeneral(ctx context.Context, req *game.GetServerConfigGeneralReq) (res *game.GetServerConfigGeneralRes, err error) |
|
AddServerConfigGeneral(ctx context.Context, req *game.AddServerConfigGeneralReq) (res *game.AddServerConfigGeneralRes, err error) |
|
GetAllLoginUrl(ctx context.Context, req *game.GetAllLoginUrlReq) (res *game.GetAllLoginUrlRes, err error) |
|
AddLoginUrl(ctx context.Context, req *game.AddLoginUrlReq) (res *game.AddLoginUrlRes, err error) |
|
DelLoginUrl(ctx context.Context, req *game.DelLoginUrlReq) (res *game.DelLoginUrlRes, err error) |
|
GetGameLoginUrl(ctx context.Context, req *game.GetGameLoginUrlReq) (res *game.GetGameLoginUrlRes, err error) |
|
ReConfigServer(ctx context.Context, req *game.ReConfigServerReq) (res *game.ReConfigServerRes, err error) |
|
GetAllCcdUrl(ctx context.Context, req *game.GetAllCcdUrlReq) (res *game.GetAllCcdUrlRes, err error) |
|
AddServerConfig(ctx context.Context, req *game.AddServerConfigReq) (res *game.AddServerConfigRes, err error) |
|
GetRouter(ctx context.Context, req *game.GetRouterReq) (res *game.GetRouterRes, err error) |
|
GetServerVersionList(ctx context.Context, req *game.GetServerVersionListReq) (res *game.GetServerVersionListRes, err error) |
|
UpdateRouter(ctx context.Context, req *game.AddRouterReq) (res *game.AddRouterRes, err error) |
|
ServerUpdateMsg(ctx context.Context, req *game.ServerUpdateMsgReq) (res *game.ServerUpdateMsgRes, err error) |
|
ServerReloadDll(ctx context.Context, req *game.ServerReloadDllReq) (res *game.ServerReloadDllRes, err error) |
|
} |
|
|
|
type gameLoginUrlImpl struct { |
|
} |
|
|
|
var gameLoginUrlService = gameLoginUrlImpl{} |
|
|
|
func GameLoginUrl() IGameLoginUrl { |
|
return &gameLoginUrlService |
|
} |
|
func (c *gameLoginUrlImpl) GetLoginUrl(ctx context.Context, req *game.GetLoginUrlReq) (res *game.GetLoginUrlRes, err error) { |
|
g.Try(ctx, func(ctx context.Context) { |
|
res = new(game.GetLoginUrlRes) |
|
model := dao.GameLoginUrl.Ctx(ctx) |
|
res.Total, err = model.Count() |
|
liberr.ErrIsNil(ctx, err, "mysql err") |
|
model = model.Page(req.PageNum, req.PageSize) |
|
err = model.Scan(&res.LoginUrlList) |
|
|
|
//service.Email().SendEmail(ctx, &system.SendEmailReq{}) |
|
}) |
|
return |
|
} |
|
func (c *gameLoginUrlImpl) GetServerConfigGeneral(ctx context.Context, req *game.GetServerConfigGeneralReq) (res *game.GetServerConfigGeneralRes, err error) { |
|
g.Try(ctx, func(ctx context.Context) { |
|
res = new(game.GetServerConfigGeneralRes) |
|
err = dao.GameServerJsonGeneral.Ctx(ctx).Scan(&res.List) |
|
liberr.ErrIsNil(ctx, err, "mysql err") |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) AddServerConfigGeneral(ctx context.Context, req *game.AddServerConfigGeneralReq) (res *game.AddServerConfigGeneralRes, err error) { |
|
g.Try(ctx, func(ctx context.Context) { |
|
res = new(game.AddServerConfigGeneralRes) |
|
_, err = dao.GameServerJsonGeneral.Ctx(ctx).Where("id=?", req.Id).Update(g.Map{ |
|
dao.GameServerJsonGeneral.Columns().BackstageRechargeUrl: req.BackstageRechargeUrl, |
|
dao.GameServerJsonGeneral.Columns().IdentityCheckAddress: req.IdentityCheckAddress, |
|
dao.GameServerJsonGeneral.Columns().UniqueAddress: req.UniqueAddress, |
|
dao.GameServerJsonGeneral.Columns().RechargeWhiteListIps: req.RechargeWhiteListIps, |
|
dao.GameServerJsonGeneral.Columns().GmWhiteListUrl: req.GmWhiteListUrl, |
|
dao.GameServerJsonGeneral.Columns().SdkAddress: req.SdkAddress, |
|
}) |
|
liberr.ErrIsNil(ctx, err, "mysql err") |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) GetAllLoginUrl(ctx context.Context, req *game.GetAllLoginUrlReq) (res *game.GetAllLoginUrlRes, err error) { |
|
g.Try(ctx, func(ctx context.Context) { |
|
res = new(game.GetAllLoginUrlRes) |
|
res.LoginUrlList = internal.ServerList |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) AddLoginUrl(ctx context.Context, req *game.AddLoginUrlReq) (res *game.AddLoginUrlRes, err error) { |
|
res = new(game.AddLoginUrlRes) |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
server := []entity.GameLoginUrl{} |
|
model := dao.GameLoginUrl.Ctx(ctx) |
|
model.WherePri(req.Id).Scan(&server) |
|
if len(server) > 0 { |
|
data := g.Map{ |
|
dao.GameLoginUrl.Columns().Id: req.Id, |
|
} |
|
if req.IsNew != "" { |
|
data[dao.GameLoginUrl.Columns().IsNew] = req.IsNew |
|
} |
|
if req.Recommend != "" { |
|
data[dao.GameLoginUrl.Columns().Recommend] = req.Recommend |
|
} |
|
if req.State != "" { |
|
data[dao.GameLoginUrl.Columns().State] = req.State |
|
} |
|
if req.Host != "" { |
|
data[dao.GameLoginUrl.Columns().Host] = req.Host |
|
} |
|
if req.Port != 0 { |
|
data[dao.GameLoginUrl.Columns().Port] = req.Port |
|
} |
|
_, err = model.WherePri(req.Id).Update(data) |
|
} |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) DelLoginUrl(ctx context.Context, req *game.DelLoginUrlReq) (res *game.DelLoginUrlRes, err error) { |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
if req.Id == 0 { |
|
liberr.ErrIsNil(ctx, err, "错误的id") |
|
return |
|
} |
|
_, e := dao.GameLoginUrl.Ctx(ctx).Where("id=", req.Id).Delete() |
|
if e != nil { |
|
liberr.ErrIsNil(ctx, e, "删除通知失败") |
|
return |
|
} |
|
}) |
|
|
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) GetGameLoginUrl(ctx context.Context, req *game.GetGameLoginUrlReq) (res *game.GetGameLoginUrlRes, err error) { |
|
res = new(game.GetGameLoginUrlRes) |
|
ip := libUtils.GetClientIp(ctx) |
|
list, _ := internal.GetWhiteList(ctx) |
|
blackList, _ := internal.GetBlackList(ctx) |
|
account := req.Account |
|
if req.Channel == consts.Channel_Editor && !strings.Contains(account, req.Channel) { |
|
account = req.Channel + account |
|
} |
|
|
|
g.Log().Debugf(ctx, "GetWhiteList -> ", req.Account, gjson.MustEncodeString(list), ip) |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
var LoginUrlList []*entity.GameLoginUrl |
|
err = dao.GameLoginUrl.Ctx(ctx).Scan(&LoginUrlList) |
|
var unitData []*entity.GameUnit |
|
if req.Account != "" { |
|
err = dao.GameUnit.Ctx(ctx).Where("channel=?", req.Channel).Where("account=?", account).Scan(&unitData) |
|
} |
|
|
|
if err != nil || len(LoginUrlList) == 0 { |
|
liberr.ErrIsNil(ctx, err, "获取服务器失败") |
|
return |
|
} |
|
|
|
if req.Channel == "000005000000" && req.Version == "1.0.1" && req.VersionName != "" { |
|
req.Version = "1.0.2" |
|
} |
|
|
|
logTime := int64(0) |
|
last := 0 |
|
channel := ";" |
|
if internal.LoginUrlChannelList(ctx, req.Channel+req.Version) == 1 { |
|
channel += req.Channel + req.Version |
|
} else if internal.LoginUrlChannelList(ctx, req.Channel) == 1 { |
|
channel += req.Channel |
|
} else if internal.LoginUrlChannelList(ctx, req.Version) == 1 { |
|
channel += req.Version |
|
} |
|
channel += ";" |
|
for _, v := range LoginUrlList { |
|
if !strings.Contains(v.Channel, channel) { |
|
continue |
|
} |
|
if v.IsShow != 1 && !(utils.ContainsWhiteList(list, ip) || utils.ContainsWhiteList(list, req.Channel)) { |
|
continue |
|
} |
|
|
|
var data = game.LoginUrlData{Id: v.Id, Host: v.Host, Port: v.Port, Difficulty: v.Difficulty, IsNew: v.IsNew, |
|
Recommend: v.Recommend, Name: v.Name, State: v.State, Area: v.Area, CreateTime: v.CDate.Unix()} |
|
if data.State == consts.Login_URL_STATE_Maintenance { |
|
data.Host = consts.DEFAULT_HOST |
|
} else if data.State == consts.Login_URL_STATE_WHITE { |
|
if utils.ContainsWhiteList(list, ip) || utils.ContainsWhiteList(list, req.Channel) { |
|
data.State = consts.Login_URL_STATE_NORMAL |
|
} else { |
|
data.Host = consts.DEFAULT_HOST |
|
data.State = consts.Login_URL_STATE_Maintenance |
|
} |
|
} else if data.State == consts.Login_URL_STATE_BLACK { |
|
if utils.ContainsBlackList(blackList, req.Channel) || utils.ContainsBlackList(blackList, ip) { |
|
data.Host = consts.DEFAULT_HOST |
|
data.State = consts.Login_URL_STATE_Maintenance |
|
} else { |
|
data.State = consts.Login_URL_STATE_NORMAL |
|
} |
|
} |
|
for _, v1 := range unitData { |
|
if v1.Server == data.Id { |
|
data.AccountName = v1.Name |
|
data.Scale = v1.Scale |
|
if logTime < v1.LogTime { |
|
logTime = v1.LogTime |
|
last = v1.Server |
|
} |
|
} |
|
} |
|
res.UrlStr = append(res.UrlStr, data) |
|
} |
|
for i := 0; i < len(res.UrlStr); i++ { |
|
if res.UrlStr[i].Id == last { |
|
res.UrlStr[i].Last = 1 |
|
break |
|
} |
|
} |
|
|
|
g.Log().Debugf(ctx, "GetGameLoginUrl -> ", req.Account, gjson.MustEncodeString(res), last) |
|
return |
|
}) |
|
|
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) ReConfigServer(ctx context.Context, req *game.ReConfigServerReq) (res *game.ReConfigServerRes, err error) { |
|
if req.SType == 2 { |
|
service.AliYunSms().ConfigInit() |
|
service.Email().ConfigInit() |
|
service.TencentSms().ConfigInit() |
|
internal.InitChannel(ctx) |
|
internal.WebHookInit(ctx) |
|
} else { |
|
internal.InitServer(ctx) |
|
internal.InitRechargeServer(ctx) |
|
internal.InitRouterServer(ctx) |
|
internal.MongoInit(ctx) |
|
} |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) GetAllCcdUrl(ctx context.Context, req *game.GetAllCcdUrlReq) (res *game.GetAllCcdUrlRes, err error) { |
|
res = new(game.GetAllCcdUrlRes) |
|
g.Try(ctx, func(ctx context.Context) { |
|
err = dao.GameCcdVersion.Ctx(ctx).Scan(&res.List) |
|
log.Printf("ccdBox: %v", res.List) |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) GetRouter(ctx context.Context, req *game.GetRouterReq) (res *game.GetRouterRes, err error) { |
|
g.Try(ctx, func(ctx context.Context) { |
|
res = new(game.GetRouterRes) |
|
model := dao.GameRouter.Ctx(ctx) |
|
res.Total, err = model.Count() |
|
liberr.ErrIsNil(ctx, err, "mysql err") |
|
model = model.Page(req.PageNum, req.PageSize) |
|
err = model.Scan(&res.List) |
|
liberr.ErrIsNil(ctx, err, "mysql err") |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) AddServerConfig(ctx context.Context, req *game.AddServerConfigReq) (res *game.AddServerConfigRes, err error) { |
|
res = new(game.AddServerConfigRes) |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
server := []entity.GameLoginUrl{} |
|
model := dao.GameLoginUrl.Ctx(ctx) |
|
model.WherePri(req.Id).Scan(&server) |
|
if len(server) > 0 { |
|
_, err = model.WherePri(req.Id).Update(g.Map{ |
|
dao.GameLoginUrl.Columns().Id: req.Id, |
|
dao.GameLoginUrl.Columns().GameDbUrl: req.GameDbUrl, |
|
dao.GameLoginUrl.Columns().GameDbName: req.GameDbName, |
|
dao.GameLoginUrl.Columns().Platform: req.Platform, |
|
dao.GameLoginUrl.Columns().InnerIp: req.InnerIp, |
|
dao.GameLoginUrl.Columns().CreateTime: req.CreateTime, |
|
dao.GameLoginUrl.Columns().Difficulty: req.Difficulty, |
|
dao.GameLoginUrl.Columns().Channel: req.Channel, |
|
}) |
|
} |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) UpdateRouter(ctx context.Context, req *game.AddRouterReq) (res *game.AddRouterRes, err error) { |
|
res = new(game.AddRouterRes) |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
server := []entity.GameRouter{} |
|
model := dao.GameRouter.Ctx(ctx) |
|
model.WherePri(req.Id).Scan(&server) |
|
if len(server) > 0 { |
|
_, err = model.WherePri(req.Id).Update(g.Map{ |
|
dao.GameRouter.Columns().Id: req.Id, |
|
dao.GameRouter.Columns().RealmIp: req.RealmIp, |
|
dao.GameRouter.Columns().RealmPort: req.RealmPort, |
|
dao.GameRouter.Columns().RealmNum: req.RealmNum, |
|
dao.GameRouter.Columns().RealmStep: req.RealmStep, |
|
dao.GameRouter.Columns().RouterIp: req.RouterIp, |
|
dao.GameRouter.Columns().RouterPort: req.RouterPort, |
|
dao.GameRouter.Columns().RouterNum: req.RouterNum, |
|
dao.GameRouter.Columns().RouterStep: req.RouterStep, |
|
}) |
|
} else { |
|
_, err = model.Insert(g.Map{ |
|
dao.GameRouter.Columns().Id: req.Id, |
|
dao.GameRouter.Columns().RealmIp: req.RealmIp, |
|
dao.GameRouter.Columns().RealmPort: req.RealmPort, |
|
dao.GameRouter.Columns().RealmNum: req.RealmNum, |
|
dao.GameRouter.Columns().RealmStep: req.RealmStep, |
|
dao.GameRouter.Columns().RouterIp: req.RouterIp, |
|
dao.GameRouter.Columns().RouterPort: req.RouterPort, |
|
dao.GameRouter.Columns().RouterNum: req.RouterNum, |
|
dao.GameRouter.Columns().RouterStep: req.RouterStep, |
|
}) |
|
} |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) GetServerVersionList(ctx context.Context, req *game.GetServerVersionListReq) (res *game.GetServerVersionListRes, err error) { |
|
res = new(game.GetServerVersionListRes) |
|
ip := internal.ServerConfig[fmt.Sprint(req.Server)] |
|
url := "http://" + ip + "/get_version" |
|
bytes := g.Client().GetBytes(ctx, url) |
|
//fmt.Println("GetOnlineList: ", url, bytes) |
|
src := string(bytes) |
|
if g.IsEmpty(src) { |
|
res.Version = "获取失败" |
|
return |
|
} |
|
|
|
srcCharset := "UTF-8" |
|
tmp, _ := gcharset.ToUTF8(srcCharset, src) |
|
json, err := gjson.DecodeToJson(tmp) |
|
if err != nil { |
|
res.Version = "获取失败" |
|
return |
|
} |
|
//fmt.Println("GetOnlineList - json: ", tmp, gjson.MustEncodeString(json.Get("Accounts").Array())) |
|
if json.Get("Error").Int() == 200 { |
|
res.Version = json.Get("Message").String() |
|
} else { |
|
res.Version = "获取失败" |
|
} |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) ServerUpdateMsg(ctx context.Context, req *game.ServerUpdateMsgReq) (res *game.ServerUpdateMsgRes, err error) { |
|
url := internal.Webhook |
|
if len(url) == 0 { |
|
return nil, errors.New("地址为空!") |
|
} |
|
|
|
normalV2 := map[string]interface{}{"default": "normal", "pc": "normal", "mobile": "heading"} |
|
textSize := map[string]interface{}{"normal_v2": normalV2} |
|
style := map[string]interface{}{"text_size": textSize} |
|
config := map[string]interface{}{"update_multi": true, "style": style} |
|
elements := []map[string]interface{}{{"tag": "markdown", "content": req.Content, "text_align": "left", "text_size": "normal_v2", "margin": "0px 0px 0px 0px"}} |
|
body1 := map[string]interface{}{"direction": "vertical", "padding": "12px 12px 12px 12px", "elements": elements} |
|
title := map[string]interface{}{"tag": "plain_text", "content": req.Title} |
|
subtitle := map[string]interface{}{"tag": "plain_text", "content": req.Subtitle} |
|
header := map[string]interface{}{"title": title, "template": "blue", "subtitle": subtitle, "padding": "12px 12px 12px 12px"} |
|
card := map[string]interface{}{"schema": "2.0", "config": config, "body": body1, "header": header} |
|
body := map[string]interface{}{"msg_type": "interactive", "card": card} |
|
|
|
fmt.Println("GetOnlineList - body: ", gjson.MustEncodeString(body)) |
|
bytes := g.Client().PostBytes(ctx, url, gjson.MustEncodeString(body)) |
|
src := string(bytes) |
|
if g.IsEmpty(src) { |
|
return |
|
} |
|
|
|
srcCharset := "UTF-8" |
|
tmp, _ := gcharset.ToUTF8(srcCharset, src) |
|
json, err := gjson.DecodeToJson(tmp) |
|
if err != nil { |
|
return |
|
} |
|
fmt.Println("GetOnlineList - json: ", tmp) |
|
if json.Get("code").Int() != 0 { |
|
err = errors.New(json.Get("msg").String()) |
|
} |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) ServerReloadDll(ctx context.Context, req *game.ServerReloadDllReq) (res *game.ServerReloadDllRes, err error) { |
|
res = new(game.ServerReloadDllRes) |
|
res.Message = "发送失败" |
|
ip := internal.ServerConfig[fmt.Sprint(req.Server)] |
|
url := "http://" + ip + "/ReloadDll" |
|
bytes := g.Client().GetBytes(ctx, url) |
|
fmt.Println("ServerReloadDll: ", url, bytes) |
|
src := string(bytes) |
|
if g.IsEmpty(src) { |
|
fmt.Println("ServerReloadDll: IsEmpty(src) err", url) |
|
return |
|
} |
|
|
|
srcCharset := "UTF-8" |
|
tmp, _ := gcharset.ToUTF8(srcCharset, src) |
|
json, err := gjson.DecodeToJson(tmp) |
|
if err != nil { |
|
fmt.Println("ServerReloadDll: DecodeToJson(tmp) err", url) |
|
return |
|
} |
|
fmt.Println("ReloadDll - json: ", tmp) |
|
if json.Get("Error").Int() == 200 { |
|
res.Message = "发送成功" |
|
} |
|
return |
|
}
|
|
|