aa
This commit is contained in:
@@ -7,6 +7,11 @@ const (
|
||||
|
||||
ticketType = "wx_card"
|
||||
authType = 0 //Int 是 授权类型,0:开票授权,1:填写字段开票授权,2:领票授权
|
||||
//图片(image)、视频(video)、语音 (voice)、图文(news)
|
||||
MaterialTypeImage = "image"
|
||||
MaterialTypeVideo = "video"
|
||||
MaterialTypeVoice = "voice"
|
||||
MaterialTypeNews = "news"
|
||||
)
|
||||
|
||||
type CBUserInfo struct {
|
||||
@@ -115,3 +120,52 @@ func (a *API) CBGetTicketInfo() (ticketInfo *CBTicketInfo, err error) {
|
||||
}
|
||||
return ticketInfo, err
|
||||
}
|
||||
|
||||
type CBBatchgetMaterialResult struct {
|
||||
Item []struct {
|
||||
MediaID string `json:"media_id"`
|
||||
Content struct {
|
||||
NewsItem []struct {
|
||||
Title string `json:"title"`
|
||||
Author string `json:"author"`
|
||||
Digest string `json:"digest"`
|
||||
Content string `json:"content"`
|
||||
ContentSourceURL string `json:"content_source_url"`
|
||||
ThumbMediaID string `json:"thumb_media_id"`
|
||||
ShowCoverPic int `json:"show_cover_pic"`
|
||||
URL string `json:"url"`
|
||||
ThumbURL string `json:"thumb_url"`
|
||||
NeedOpenComment int `json:"need_open_comment"`
|
||||
OnlyFansCanComment int `json:"only_fans_can_comment"`
|
||||
} `json:"news_item"`
|
||||
CreateTime int `json:"create_time"`
|
||||
UpdateTime int `json:"update_time"`
|
||||
} `json:"content"`
|
||||
UpdateTime int `json:"update_time"`
|
||||
} `json:"item"`
|
||||
TotalCount int `json:"total_count"`
|
||||
ItemCount int `json:"item_count"`
|
||||
}
|
||||
|
||||
//获取素材列表
|
||||
func (a *API) CBBatchgetMaterial(mediaType string, offset, count int) (ticketInfo *CBBatchgetMaterialResult, err error) {
|
||||
bodyJson := map[string]interface{}{
|
||||
"type": mediaType,
|
||||
"offset": offset,
|
||||
"count": count,
|
||||
}
|
||||
result, err := a.AccessAPI("cgi-bin/material/batchget_material", nil, string(utils.MustMarshal(bodyJson)))
|
||||
if err == nil {
|
||||
err = utils.Map2StructByJson(result, &ticketInfo, false)
|
||||
}
|
||||
return ticketInfo, err
|
||||
}
|
||||
|
||||
//上传图片
|
||||
func (a *API) CBUploadImg(data []byte, fileName, contentType string) (url string, err error) {
|
||||
result, err := a.AccessAPIUpload(data, fileName, contentType)
|
||||
if err == nil {
|
||||
return result["url"].(string), err
|
||||
}
|
||||
return url, err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
package weixinapi
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
@@ -58,3 +63,35 @@ func TestCBGetTicketInfo(t *testing.T) {
|
||||
}
|
||||
t.Log(utils.Format4Output(userInfo, false))
|
||||
}
|
||||
|
||||
func TestCBBatchgetMaterial(t *testing.T) {
|
||||
userInfo, err := api.CBBatchgetMaterial(MaterialTypeNews, 19, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(utils.Format4Output(userInfo, false))
|
||||
}
|
||||
|
||||
func TestCBUploadImg(t *testing.T) {
|
||||
data, _, _ := DownloadFileByURL("https://image.jxc4.com/noGoodsImg.jpg")
|
||||
result, err := api.CBUploadImg(data, "noGoodsImg.jpg", "image/jpeg")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
|
||||
func DownloadFileByURL(fileURL string) (bodyData []byte, fileMD5 string, err error) {
|
||||
response, err := http.Get(fileURL)
|
||||
if err == nil {
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode == http.StatusOK {
|
||||
if bodyData, err = ioutil.ReadAll(response.Body); err == nil {
|
||||
fileMD5 = fmt.Sprintf("%X", md5.Sum(bodyData))
|
||||
}
|
||||
} else {
|
||||
err = platformapi.ErrHTTPCodeIsNot200
|
||||
}
|
||||
}
|
||||
return bodyData, fileMD5, err
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package weixinapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -17,6 +21,8 @@ const (
|
||||
const (
|
||||
ResponseCodeBusy = -1
|
||||
ResponseCodeSuccess = 0
|
||||
|
||||
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
)
|
||||
|
||||
type TokenInfo struct {
|
||||
@@ -129,3 +135,78 @@ func (a *API) AccessAPI(action string, params map[string]interface{}, body strin
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func RandStringBytes(n int) string {
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func getBoundary() (boundary string) {
|
||||
boundary = "----WebKitFormBoundary"
|
||||
boundary += RandStringBytes(16)
|
||||
return boundary
|
||||
}
|
||||
|
||||
func (a *API) AccessAPIUpload(data []byte, fileName, contentType string) (retVal map[string]interface{}, err error) {
|
||||
params2 := make(map[string]interface{})
|
||||
accessToken := a.CBGetToken()
|
||||
if accessToken == "" {
|
||||
panic("token is empty!")
|
||||
}
|
||||
params2["access_token"] = accessToken
|
||||
fullURL := utils.GenerateGetURL(prodURL, "cgi-bin/media/uploadimg", params2)
|
||||
// baseapi.SugarLogger.Debug(fullURL)
|
||||
// 实例化multipart
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
writer.SetBoundary(getBoundary())
|
||||
// 创建multipart 文件字段
|
||||
h := make(textproto.MIMEHeader)
|
||||
h.Set("Content-Disposition",
|
||||
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
|
||||
"media", fileName))
|
||||
h.Set("Content-Type", contentType)
|
||||
part, err := writer.CreatePart(h)
|
||||
// 写入文件数据到multipart,和读取本地文件方法的唯一区别
|
||||
_, err = part.Write(data)
|
||||
//将额外参数也写入到multipart
|
||||
//_ = writer.WriteField("media", fileName)
|
||||
err = writer.Close()
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
request, _ := http.NewRequest(http.MethodPost, fullURL, body)
|
||||
request.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
var errInfo *ErrorInfo
|
||||
// 微信的返回值,在错误与正常情况下,结构是完全不一样的
|
||||
if errCode, ok := jsonResult1["errcode"]; ok {
|
||||
errInfo = &ErrorInfo{
|
||||
ErrCode: int(utils.MustInterface2Int64(errCode)),
|
||||
ErrMsg: jsonResult1["errmsg"].(string),
|
||||
}
|
||||
if errInfo.ErrCode == 0 {
|
||||
retVal = jsonResult1
|
||||
}
|
||||
} else {
|
||||
retVal = jsonResult1
|
||||
}
|
||||
if retVal != nil {
|
||||
return platformapi.ErrLevelSuccess, nil
|
||||
}
|
||||
newErr := utils.NewErrorIntCode(errInfo.ErrMsg, errInfo.ErrCode)
|
||||
if errInfo.ErrCode == ResponseCodeBusy {
|
||||
return platformapi.ErrLevelRecoverableErr, newErr
|
||||
}
|
||||
return platformapi.ErrLevelCodeIsNotOK, newErr
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
@@ -23,11 +23,11 @@ func init() {
|
||||
// api = New("wxbf235770edaabc5c", "ba32b269a068a5b72486a0beafd171e8")
|
||||
|
||||
// // prod
|
||||
// api = New("wx2bb99eb5d2c9b82c", "6bbbed1443cc062c20a015a64c07a531")
|
||||
api = New("wx2bb99eb5d2c9b82c", "6bbbed1443cc062c20a015a64c07a531")
|
||||
|
||||
//weixinapp
|
||||
api = New("wx18111a41fd17f24f", "c79ac6e1b2d6d7968e72a9658a8b6715")
|
||||
// api.CBSetToken("43_ZotOV6kJJjqc4H9blBhwxbXpSmY1LMOv13lxYRL44FQ1icX0nnnAGt5-pNWDVSuaVK3IrYoxR1xY6AGeY5XHsjCgwymwT009y6uDyo7mB8suHrSUP-2vCWHk8Q0Ff8FQHVsTnxBo8NicLeUAPYJjAEASBM")
|
||||
//api = New("wx18111a41fd17f24f", "c79ac6e1b2d6d7968e72a9658a8b6715")
|
||||
api.CBSetToken("46_Cj1HI_V-qSBMFLvS0L-Xot-mYJEFhKK5apzHURNV83IPCGMj66X2TGo4Zqtr4nkwpZY3bBNaQhPzKjseBXL9JcyTp0h3i-idLHPCukqz7QO-kV7zycv_Cp6HZyhpsxyyFwobd4DIONbfYTQrCSNaAEAGWR")
|
||||
}
|
||||
|
||||
func handleError(t *testing.T, err error) {
|
||||
|
||||
Reference in New Issue
Block a user