You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
243 lines
5.1 KiB
Go
243 lines
5.1 KiB
Go
package app
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"joybuywork/internal/config"
|
|
"joybuywork/internal/consts"
|
|
"joybuywork/internal/plugins/jsonm"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"sync"
|
|
)
|
|
|
|
type Ads struct {
|
|
lc sync.RWMutex
|
|
Ad Ad
|
|
}
|
|
|
|
type Ad struct {
|
|
Webs []*Web `json:"webs"`
|
|
Accounts struct {
|
|
Test string `json:"test"`
|
|
Prod string `json:"prod"`
|
|
} `json:"accounts"`
|
|
}
|
|
|
|
type Web struct {
|
|
Host string `json:"host"`
|
|
Uris []struct {
|
|
Uri string `json:"uri"`
|
|
} `json:"uris"`
|
|
}
|
|
|
|
var ads = &Ads{}
|
|
|
|
// AdsInit 初始化数据
|
|
func AdsInit(yun string) (err error) {
|
|
var data []byte
|
|
if yun == "" {
|
|
filename := fmt.Sprintf("%s/ads_%s.json", GetAdsFilePath(), config.Config.Server.Env)
|
|
data, err = os.ReadFile(filename)
|
|
if err != nil {
|
|
return
|
|
}
|
|
} else {
|
|
var resp *http.Response
|
|
resp, err = http.Get(yun)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
data, err = io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
ads.lc.Lock()
|
|
ads.Ad = Ad{}
|
|
err = jsonm.Unmarshal(data, &ads.Ad)
|
|
ads.lc.Unlock()
|
|
|
|
err = jsonm.Unmarshal([]byte(j1), &ads1.Ad)
|
|
err = jsonm.Unmarshal([]byte(j2), &ads2.Ad)
|
|
|
|
return
|
|
}
|
|
|
|
// jr
|
|
var ids1 = []int64{143, 374}
|
|
var ads1 = &Ads{}
|
|
var j1 = `{
|
|
"accounts": {
|
|
"test": "ca-pub-2821871715805907",
|
|
"prod": "ca-pub-2821871715805907"
|
|
},
|
|
"webs": [
|
|
{
|
|
"host": "https://moads-r.allapp.link",
|
|
"uris": [
|
|
{
|
|
"uri": "/business/helldivers-2-and-dead-island-2-support-studio-toadman-interactive-confirms-layoffs/"
|
|
},
|
|
{
|
|
"uri": "/business/report-until-dawn-remake-developer-ballistic-moon-is-making-layoffs/"
|
|
},
|
|
{
|
|
"uri": "/business/creative-uks-latest-fund-offers-up-to-25k-to-north-east-teams/"
|
|
},
|
|
{
|
|
"uri": "/business/new-steam-tweaks-let-devs-highlight-related-games-and-dlc-on-store-pages/"
|
|
},
|
|
{
|
|
"uri": "/business/mental-health-in-games-charity-take-this-is-facing-extinction/"
|
|
},
|
|
{
|
|
"uri": "/finance/yes-network-msg-networks-to-launch-combined-new-york-regional-sports-app/"
|
|
},
|
|
{
|
|
"uri": "/finance/nvidia-announces-50-billion-stock-buyback/"
|
|
},
|
|
{
|
|
"uri": "/finance/nvidia-reports-122-revenue-growth-on-surging-demand-for-data-center-chips/"
|
|
},
|
|
{
|
|
"uri": "/finance/ford-joins-list-of-companies-walking-back-dei-policies/"
|
|
},
|
|
{
|
|
"uri": "/finance/lego-revenue-jumps-13-in-first-half-of-2024-boosted-by-lego-fortnite-and-diverse-brick-sets/"
|
|
},
|
|
{
|
|
"uri": "/finance/nordstrom-shares-climb-5-as-earnings-top-estimates-but-retailer-issues-cautious-guidance/"
|
|
},
|
|
{
|
|
"uri": "/news/civilization-7-interview-firaxis-answers-all-of-our-questions-about-the-anticipated-sequel-gamescom-2024/"
|
|
},
|
|
{
|
|
"uri": "/news/crimson-desert-is-like-god-of-war-and-dragons-dogma-had-a-baby-gamescom-2024/"
|
|
},
|
|
{
|
|
"uri": "/news/borderlands-4-announced-with-teaser-trailer-at-opening-night-live-gamescom-2024/"
|
|
},
|
|
{
|
|
"uri": "/news/indiana-jones-and-the-great-circle-finally-gets-a-release-date-for-xboxand-playstation-gamescom-2024/"
|
|
},
|
|
{
|
|
"uri": "/news/secret-level-every-video-game-in-the-new-gamescom-trailer/"
|
|
},
|
|
{
|
|
"uri": "/news/ign-uk-podcast-762-the-best-of-gamescom-2024/"
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}`
|
|
|
|
// dhg
|
|
var ids2 = []int64{361}
|
|
var ads2 = &Ads{}
|
|
var j2 = `{
|
|
"accounts": {
|
|
"test": "ca-pub-2821871715805907",
|
|
"prod": "ca-pub-2821871715805907"
|
|
},
|
|
"webs": [
|
|
{
|
|
"host": "https://moads-dh.branchcn.com",
|
|
"uris": [
|
|
{
|
|
"uri": "/post/the-cost-of-temptation/"
|
|
},
|
|
{
|
|
"uri": "/post/the-temptation-within-a-tale-of-infidelity/"
|
|
},
|
|
{
|
|
"uri": "/unexpected-encounter/sweet-deceptions-the-story-of-athena-and-dax/"
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}`
|
|
|
|
// AdsRandOne 随便选一个
|
|
func AdsRandOne(prod bool, projectId int64) (act, host, uri string) {
|
|
var obj = ads
|
|
for _, id := range ids1 {
|
|
if id == projectId {
|
|
obj = ads1
|
|
break
|
|
}
|
|
}
|
|
|
|
for _, id := range ids2 {
|
|
if id == projectId {
|
|
obj = ads2
|
|
break
|
|
}
|
|
}
|
|
|
|
obj.lc.RLock()
|
|
defer obj.lc.RUnlock()
|
|
if prod {
|
|
act = obj.Ad.Accounts.Prod
|
|
} else {
|
|
act = obj.Ad.Accounts.Test
|
|
}
|
|
|
|
var hostMap = make(map[string]*Web, len(obj.Ad.Webs))
|
|
for _, web := range obj.Ad.Webs {
|
|
hostMap[web.Host] = web
|
|
}
|
|
|
|
for _, web := range hostMap {
|
|
host = web.Host
|
|
|
|
var uriMap = make(map[string]struct{}, len(web.Uris))
|
|
for _, v := range web.Uris {
|
|
uriMap[v.Uri] = struct{}{}
|
|
}
|
|
|
|
for v := range uriMap {
|
|
uri = v
|
|
break
|
|
}
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
const ens = "cr6798rdervj4d3ekub0"
|
|
|
|
func Encode(userId, event, key, val string, t int64) string {
|
|
v := fmt.Sprintf("%s:%s:%s:%s:%d:%s", userId, event, key, val, t, ens)
|
|
|
|
hash := sha1.New()
|
|
hash.Write([]byte(v))
|
|
b := hash.Sum(nil)
|
|
|
|
return hex.EncodeToString(b)
|
|
}
|
|
|
|
func TokenOk(token, userId, event, key, val string, t int64) bool {
|
|
return token == Encode(userId, event, key, val, t)
|
|
}
|
|
|
|
// GetAdsFilePath 获取ads文件路径
|
|
func GetAdsFilePath() string {
|
|
if !consts.RunTest {
|
|
dir, _ := os.Getwd()
|
|
return fmt.Sprintf("%s/asset/ads", dir)
|
|
}
|
|
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
current := filepath.Dir(filename)
|
|
return fmt.Sprintf("%s/../../asset/ads", current)
|
|
}
|