This commit is contained in:
yangduo 2025-02-24 13:59:58 +08:00
parent dc6f0981df
commit 04521ba59a
2 changed files with 89 additions and 89 deletions

View File

@ -24,11 +24,16 @@ type expireSession struct {
SessionTime int64
Time int64
}
type mediaidInfo struct {
mediaId string
time int64
}
type wxpay struct {
gamesGoods q5.ConcurrentMap[int64, map[int64]GoodsInfo] //[gameid, [goodsid]goodsinfo]
accessTokens q5.ConcurrentMap[int64, TokenInfo] //[gameid, TokenInfo]
expireInfo q5.ConcurrentMap[string, *expireSession] //[accountid, expireSession]
mediaInfo q5.ConcurrentMap[int64, string] //[gameid, mediaid]
mediaInfo q5.ConcurrentMap[int64, mediaidInfo] //[gameid, mediainfo]
refreshflag bool
ctx context.Context
client *core.Client

View File

@ -63,99 +63,94 @@ func (wp *wxpay) initMch() {
}
func (wp *wxpay) checkGameMediaId() {
wp.mediaInfo = q5.ConcurrentMap[int64, string]{}
uploadtime := int64(0)
wp.mediaInfo = q5.ConcurrentMap[int64, mediaidInfo]{}
gamecout := 0
mt.Table.Wxconfig.Traverse(func(w *mt.Wxconfig) bool {
gamecout++
return true
})
for {
nowtime := f5.GetApp().GetRealSeconds()
if wp.mediaInfo.GetSize() == 0 || q5.GetDaySeconds(uploadtime, 8) != q5.GetDaySeconds(nowtime, 8) {
// uploader := fileuploader.ImageUploader{Client: wp.client}
mt.Table.Wxconfig.Traverse(func(w *mt.Wxconfig) bool {
filename := q5.SafeToString(w.GetGameid()) + ".jpg"
fullfilename := fmt.Sprintf("../res/gamepics/%s", filename)
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
accesstoken, ok := wp.accessTokens.Load(w.GetGameid())
if !ok || len(accesstoken.Token) == 0 {
return true
}
file, err := os.Open(fullfilename)
if err != nil {
f5.GetSysLog().Error("file error1:%s", filename)
return true
}
defer file.Close()
part, err := writer.CreateFormFile("media", fullfilename)
if err != nil {
f5.GetSysLog().Debug("createFormFile err:%v", err.Error())
return true
}
_, err = io.Copy(part, file)
if err != nil {
f5.GetSysLog().Debug("io.copy err:%v", err.Error())
return true
}
writer.Close()
refreshed := false
f5.GetHttpCliMgr().SendGoStylePost(
"https://api.weixin.qq.com/cgi-bin/media/upload",
map[string]string{
"access_token": accesstoken.Token,
"type": "image",
},
writer.FormDataContentType(),
body.String(),
func(hcr f5.HttpCliResponse) {
if hcr.GetErr() != nil {
f5.GetSysLog().Debug("upload err:%s", hcr.GetErr().Error())
return
}
f5.GetSysLog().Debug("upload rsp:%s", hcr.GetRawData())
rspObj := struct {
Type string `json:"type"`
MediaId string `json:"media_id"`
Time int64 `json:"created_at"`
}{}
if json.Unmarshal([]byte(hcr.GetRawData()), &rspObj) != nil || len(rspObj.MediaId) == 0 {
return
}
wp.mediaInfo.Store(w.GetGameid(), rspObj.MediaId)
f5.GetSysLog().Debug("upload refresh media ok:%s, %s", filename, rspObj.MediaId)
refreshed = true
})
if refreshed {
return true
}
// file, err := os.Open(fullfilename)
// if err != nil {
// f5.GetSysLog().Error("file error:%s", filename)
// return false
// }
// defer file.Close()
// resp, result, err := uploader.Upload(wp.ctx, file, filename, consts.ImageJPG)
// if err == nil {
// wp.mediaInfo.Store(w.GetGameid(), *resp.MediaId)
// f5.GetSysLog().Debug("refresh media ok:%s, %s", filename, *resp.MediaId)
// } else {
// f5.GetSysLog().Error("refresh media err:%s, %s", filename, result.Response.Status)
// }
mt.Table.Wxconfig.Traverse(func(w *mt.Wxconfig) bool {
info, _ := wp.mediaInfo.Load(w.GetGameid())
if info == nil {
info = &mediaidInfo{}
}
if q5.GetDaySeconds(info.time, 8) == q5.GetDaySeconds(nowtime, 8) && len(info.mediaId) != 0 {
return true
})
}
uploadtime = nowtime
}
filename := q5.SafeToString(w.GetGameid()) + ".jpg"
fullfilename := fmt.Sprintf("../res/gamepics/%s", filename)
if wp.mediaInfo.GetSize() == 0 {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
accesstoken, ok := wp.accessTokens.Load(w.GetGameid())
if !ok || len(accesstoken.Token) == 0 {
return true
}
file, err := os.Open(fullfilename)
if err != nil {
f5.GetSysLog().Error("file error1:%s", filename)
return true
}
defer file.Close()
part, err := writer.CreateFormFile("media", fullfilename)
if err != nil {
f5.GetSysLog().Debug("createFormFile err:%v", err.Error())
return true
}
_, err = io.Copy(part, file)
if err != nil {
f5.GetSysLog().Debug("io.copy err:%v", err.Error())
return true
}
writer.Close()
desurl := "https://api.weixin.qq.com/cgi-bin/media/upload"
urls := mt.Table.Config.GetWxUrl()
if len(urls) > 0 {
desurl = fmt.Sprintf("https://%s/cgi-bin/media/upload", urls[0])
}
f5.GetHttpCliMgr().SendGoStylePost(
desurl,
map[string]string{
"access_token": accesstoken.Token,
"type": "image",
},
writer.FormDataContentType(),
body.String(),
func(hcr f5.HttpCliResponse) {
if hcr.GetErr() != nil {
f5.GetSysLog().Debug("upload err:%s", hcr.GetErr().Error())
return
}
f5.GetSysLog().Debug("upload rsp:%s", hcr.GetRawData())
rspObj := struct {
Type string `json:"type"`
MediaId string `json:"media_id"`
Time int64 `json:"created_at"`
}{}
if json.Unmarshal([]byte(hcr.GetRawData()), &rspObj) != nil || len(rspObj.MediaId) == 0 {
return
}
info.mediaId = rspObj.MediaId
info.time = nowtime
wp.mediaInfo.Store(w.GetGameid(), *info)
f5.GetSysLog().Debug("upload refresh media ok:%s, %s", filename, rspObj.MediaId)
})
return true
})
if wp.mediaInfo.GetSize() != gamecout {
time.Sleep(time.Second)
} else {
time.Sleep(time.Minute)