This commit is contained in:
yangduo 2025-02-24 11:35:40 +08:00
parent 0bde3a2312
commit dc6f0981df

View File

@ -1,22 +1,24 @@
package service package service
import ( import (
"bytes"
"context" "context"
"crypto" "crypto"
"crypto/rsa" "crypto/rsa"
"crypto/sha256" "crypto/sha256"
"encoding/base64" "encoding/base64"
"encoding/json"
"f5" "f5"
"fmt" "fmt"
"io"
"main/mt" "main/mt"
"mime/multipart"
"os" "os"
"q5" "q5"
"time" "time"
"github.com/wechatpay-apiv3/wechatpay-go/core" "github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/core/consts"
"github.com/wechatpay-apiv3/wechatpay-go/core/option" "github.com/wechatpay-apiv3/wechatpay-go/core/option"
"github.com/wechatpay-apiv3/wechatpay-go/services/fileuploader"
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi" "github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
"github.com/wechatpay-apiv3/wechatpay-go/utils" "github.com/wechatpay-apiv3/wechatpay-go/utils"
) )
@ -66,31 +68,99 @@ func (wp *wxpay) checkGameMediaId() {
for { for {
nowtime := f5.GetApp().GetRealSeconds() nowtime := f5.GetApp().GetRealSeconds()
if wp.mediaInfo.GetSize() == 0 || q5.GetDaySeconds(uploadtime, 8) != q5.GetDaySeconds(nowtime, 8) { if wp.mediaInfo.GetSize() == 0 || q5.GetDaySeconds(uploadtime, 8) != q5.GetDaySeconds(nowtime, 8) {
uploader := fileuploader.ImageUploader{Client: wp.client} // uploader := fileuploader.ImageUploader{Client: wp.client}
mt.Table.Wxconfig.Traverse(func(w *mt.Wxconfig) bool { mt.Table.Wxconfig.Traverse(func(w *mt.Wxconfig) bool {
filename := q5.SafeToString(w.GetGameid()) + ".jpg" filename := q5.SafeToString(w.GetGameid()) + ".jpg"
file, err := os.Open("../res/gamepics/" + filename) 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 { if err != nil {
f5.GetSysLog().Error("file error:%s", filename) f5.GetSysLog().Error("file error1:%s", filename)
return false return true
} }
defer file.Close() defer file.Close()
resp, result, err := uploader.Upload(wp.ctx, file, filename, consts.ImageJPG) part, err := writer.CreateFormFile("media", fullfilename)
if err == nil { if err != nil {
wp.mediaInfo.Store(w.GetGameid(), *resp.MediaId) f5.GetSysLog().Debug("createFormFile err:%v", err.Error())
f5.GetSysLog().Debug("refresh media ok:%s, %s", filename, *resp.MediaId) return true
} else {
f5.GetSysLog().Error("refresh media err:%s, %s", filename, result.Response.Status)
} }
_, 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)
// }
return true return true
}) })
uploadtime = nowtime uploadtime = nowtime
} }
if wp.mediaInfo.GetSize() == 0 {
time.Sleep(time.Second)
} else {
time.Sleep(time.Minute) time.Sleep(time.Minute)
} }
}
} }
func (wp *wxpay) GenThumburl(gameid int64) (thumburl string) { func (wp *wxpay) GenThumburl(gameid int64) (thumburl string) {