Files
stat_center/service/versionSer/h5config.go
T
2026-05-20 16:13:58 +08:00

142 lines
3.5 KiB
Go

package versionSer
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
"regexp"
"stat/api/proto"
"stat/common/utils"
downloadinfo "stat/models/downloadinfo"
"stat/models/versionMod"
"stat/service/cacheSer"
"stat/statg"
"time"
"github.com/xxjwxc/public/myglobal/snowflake"
)
func GenerateConfigFile(origin, target, configFile, DC, PC string) ([]byte, string) {
if len(DC) > 0 && len(PC) > 0 {
node, err := snowflake.NewNode(1)
if err == nil {
id := node.Generate()
target = fmt.Sprintf("%s?dc=%s&pc=%s&UID=%d", target, DC, PC, id)
}
} else if len(DC) > 0 {
node, err := snowflake.NewNode(1)
if err == nil {
id := node.Generate()
target = fmt.Sprintf("%s?dc=%s&UID=%d", target, DC, id)
}
} else if len(PC) > 0 {
node, err := snowflake.NewNode(1)
if err == nil {
id := node.Generate()
target = fmt.Sprintf("%s?pc=%s&UID=%d", target, PC, id)
}
} else {
node, err := snowflake.NewNode(1)
if err == nil {
id := node.Generate()
target = fmt.Sprintf("%s?UID=%d", target, id)
}
}
target = fmt.Sprintf("%s&isLight=true", target)
output, _, err := readFile("conf/"+configFile, origin, target)
if err != nil {
output, _ = os.ReadFile("conf/" + configFile)
}
return output, configFile
}
func GetAppConfig(appId int32) (origin, target, config string) {
endpoint := statg.GlobalAppIdEndpoints[appId]
if endpoint != nil {
origin = endpoint.H5Origin
target = endpoint.H5Target
config = endpoint.H5Config
}
return
}
// PageButtonClick 落地页点击下载返回最新的下载链接
func GetH5Config(ctx context.Context, req *proto.H5ConfigReq, userAgent string, ip string) ([]byte, string) {
req.DC = utils.RepairDC(req.DC)
downloadInfo := &downloadinfo.DownloadInfoModel{
AppId: req.AppId,
IP: ip,
PC: req.PC,
DC: req.DC,
SysType: "light",
PackageType: versionMod.Light,
PeerIP: ip,
Domain: req.Domain,
UserAgent: userAgent,
CreatedAt: time.Now(),
}
cacheSer.Writers.LandPageClickWriter.Add(downloadInfo)
origin, target, config := GetAppConfig(req.AppId)
if origin == "" || target == "" || config == "" {
return nil, "madou.mobileconfig"
}
return GenerateConfigFile(origin, target, config, req.DC, req.PC)
}
func readFile(filePath string, origin, target string) ([]byte, bool, error) {
var err error
var data []byte
cacheKey := fmt.Sprintf("h5config#%s", filePath)
if cache, exists := cacheSer.Cache.Get(cacheKey); exists {
if item, ok := cache.([]byte); ok {
data = item
}
}
if data == nil {
data, err = os.ReadFile(filePath)
if err != nil {
return nil, false, err
}
cacheSer.Cache.Set(cacheKey, data, 0)
}
reader := bufio.NewReader(bytes.NewReader(data))
needHandle := false
output := make([]byte, 0)
for {
line, _, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
return output, needHandle, nil
}
return nil, needHandle, err
}
if ok, _ := regexp.Match(origin, line); ok {
reg := regexp.MustCompile(origin)
newByte := reg.ReplaceAll(line, []byte(target))
output = append(output, newByte...)
output = append(output, []byte("\n")...)
if !needHandle {
needHandle = true
}
} else {
output = append(output, line...)
output = append(output, []byte("\n")...)
}
}
return output, needHandle, nil
}
func GetH5ConfigV2(ctx context.Context, req *proto.H5ConfigReq, ip string) proto.H5ConfigV2Resp {
if req.AppId == 694 {
return proto.H5ConfigV2Resp{
URL: "http://awll.yangjiaolian.top/91seman.mobileconfig",
}
}
return proto.H5ConfigV2Resp{}
}