adjust
This commit is contained in:
parent
0bde3a2312
commit
dc6f0981df
@ -1,22 +1,24 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto"
|
||||
"crypto/rsa"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"f5"
|
||||
"fmt"
|
||||
"io"
|
||||
"main/mt"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"q5"
|
||||
"time"
|
||||
|
||||
"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/services/fileuploader"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
||||
)
|
||||
@ -66,30 +68,98 @@ func (wp *wxpay) checkGameMediaId() {
|
||||
for {
|
||||
nowtime := f5.GetApp().GetRealSeconds()
|
||||
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 {
|
||||
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 {
|
||||
f5.GetSysLog().Error("file error:%s", filename)
|
||||
return false
|
||||
f5.GetSysLog().Error("file error1:%s", filename)
|
||||
return true
|
||||
}
|
||||
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)
|
||||
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)
|
||||
// }
|
||||
|
||||
return true
|
||||
})
|
||||
|
||||
uploadtime = nowtime
|
||||
}
|
||||
time.Sleep(time.Minute)
|
||||
|
||||
if wp.mediaInfo.GetSize() == 0 {
|
||||
time.Sleep(time.Second)
|
||||
} else {
|
||||
time.Sleep(time.Minute)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user