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.

75 lines
1.8 KiB

package internal
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"
"net/url"
"strings"
)
type NoticeBody struct {
Time int `json:"time"`
Type int `json:"type"`
Content string `json:"content"`
ModuleType string `json:"moduleType"`
Uid string `json:"uid"`
}
type MqttNotice struct {
ReqId int64 `json:"reqId"`
ModuleId string `json:"moduleId"`
Body NoticeBody `json:"body"`
}
func BackStageMessage(ctx context.Context, serverId int, ownerId string, content string, code int) (err error) {
srcCharset := "UTF-8"
g.Client().SetHeader("Content-Type", "application/json;charset=UTF-8")
errdata := ""
owners := strings.Split(ownerId, ";")
owner := ""
if ownerId != "" {
for _, v := range owners {
uid, _ := GetStringIdToUid(ctx, v)
if uid == "" {
errdata += "uid:" + v + "不存在!"
continue
}
if len(owner) > 0 {
owner += ";"
}
owner += uid
}
if len(owner) == 0 {
return
}
}
g.Log().Info(ctx, "SendToGame - ServerConfig: ", gjson.MustEncodeString(ServerConfig))
ip := ServerConfig[fmt.Sprint(serverId)]
//g.Log().Info(ctx, "sendMail - ServerConfig: ", v, gjson.MustEncodeString(ServerConfig))
url := "http://" + ip + "/BackStageMessage?ids=" + owner + "&context=" + url.QueryEscape(content) +
"&code=" + fmt.Sprint(code)
g.Log().Info(ctx, "SendToGame - req: ", url)
bytes := g.Client().GetBytes(ctx, url)
src := string(bytes)
if g.IsEmpty(src) {
return
}
tmp, _ := gcharset.ToUTF8(srcCharset, src)
json, err1 := gjson.DecodeToJson(tmp)
if err1 != nil {
return err1
}
if json.Get("Error").Int() != 200 {
return
}
g.Log().Info(ctx, "SendToGame - errdata2: ", url, errdata)
return errors.New(errdata)
}