diff --git a/platformapi/jdshopapi/store_page_test.go b/platformapi/jdshopapi/store_page_test.go index 1fe2ca9f..1fc9440f 100644 --- a/platformapi/jdshopapi/store_page_test.go +++ b/platformapi/jdshopapi/store_page_test.go @@ -125,7 +125,7 @@ func PKCS5UnPadding(decrypted []byte) []byte { } func TestShopDetail(t *testing.T) { - result, err := api.ShopDetail(1000070140) + result, err := api.ShopDetail(1000069719) if err != nil { t.Fatal(err) } diff --git a/platformapi/weixinapi/cgibin.go b/platformapi/weixinapi/cgibin.go index 0a98c1f3..001884de 100644 --- a/platformapi/weixinapi/cgibin.go +++ b/platformapi/weixinapi/cgibin.go @@ -12,6 +12,7 @@ const ( MaterialTypeVideo = "video" MaterialTypeVoice = "voice" MaterialTypeNews = "news" + MaterialTypeThumb = "thumb" ) type CBUserInfo struct { @@ -163,9 +164,61 @@ func (a *API) CBBatchgetMaterial(mediaType string, offset, count int) (ticketInf //上传图片 func (a *API) CBUploadImg(data []byte, fileName, contentType string) (url string, err error) { - result, err := a.AccessAPIUpload(data, fileName, contentType) + result, err := a.AccessAPIUpload("cgi-bin/media/uploadimg", data, fileName, contentType, "") if err == nil { return result["url"].(string), err } return url, err } + +//上传图片素材 +func (a *API) CBUploadThumb(data []byte, fileName, contentType string) (mediaID, url string, err error) { + result, err := a.AccessAPIUpload("cgi-bin/material/add_material", data, fileName, contentType, MaterialTypeThumb) + if err == nil { + return result["media_id"].(string), result["url"].(string), err + } + return mediaID, url, err +} + +type CBAddNewsParamF struct { + Articles []*CBAddNewsParam `json:"articles"` +} + +type CBAddNewsParam struct { + Title string `json:"title"` //是 标题 + ThumbMediaID string `json:"thumb_media_id"` //是 图文消息的封面图片素材id(必须是永久mediaID) + Author string `json:"author"` //否 作者 + Digest string `json:"digest"` //否 图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空。如果本字段为没有填写,则默认抓取正文前54个字。 + ShowCoverPic int `json:"show_cover_pic"` //是 是否显示封面,0为false,即不显示,1为true,即显示 + Content string `json:"content"` //是 图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS,涉及图片url必须来源 "上传图文消息内的图片获取URL"接口获取。外部图片url将被过滤。 + ContentSourceUrl string `json:"content_source_url"` //是 图文消息的原文地址,即点击“阅读原文”后的URL + NeedOpenComment int `json:"need_open_comment"` //否 Uint32 是否打开评论,0不打开,1打开 + OnlyFansCanComment int `json:"only_fans_can_comment"` //否 Uint32 是否粉丝才可评论,0所有人可评论,1粉丝才可评论 +} + +//新增永久图文素材 +func (a *API) CBAddNews(cbAddNewsParam *CBAddNewsParam) (mediaID string, err error) { + params := &CBAddNewsParamF{ + Articles: []*CBAddNewsParam{}, + } + params.Articles = append(params.Articles, cbAddNewsParam) + bodyJson := utils.Struct2FlatMap(params) + result, err := a.AccessAPI("cgi-bin/material/add_news", nil, string(utils.MustMarshal(bodyJson))) + if err == nil { + return result["media_id"].(string), err + } + return mediaID, err +} + +//根据OpenID列表群发【订阅号不可用,服务号认证后可用】 +func (a *API) CBMassSend(openIDs []string, mediaID string) (err error) { + bodyJson := map[string]interface{}{ + "touser": openIDs, + "mpnews": map[string]interface{}{ + "media_id": mediaID, + }, + "msgtype": "mpnews", + } + _, err = a.AccessAPI("cgi-bin/message/mass/send", nil, string(utils.MustMarshal(bodyJson))) + return err +} diff --git a/platformapi/weixinapi/cgibin_test.go b/platformapi/weixinapi/cgibin_test.go index b9628b15..65ab1067 100644 --- a/platformapi/weixinapi/cgibin_test.go +++ b/platformapi/weixinapi/cgibin_test.go @@ -81,6 +81,15 @@ func TestCBUploadImg(t *testing.T) { t.Log(utils.Format4Output(result, false)) } +func TestCBCBUploadThumb(t *testing.T) { + data, _, _ := DownloadFileByURL("https://image.jxc4.com/noGoodsImg.jpg") + result, _, err := api.CBUploadThumb(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 { @@ -95,3 +104,30 @@ func DownloadFileByURL(fileURL string) (bodyData []byte, fileMD5 string, err err } return bodyData, fileMD5, err } + +func TestCBAddNews(t *testing.T) { + result, err := api.CBAddNews(&CBAddNewsParam{ + Title: "测试素材", + ThumbMediaID: "cILZtVn68ncUY79JUE3tWFOGvL3GytjbKLOKiGbLK1I", + Author: "西西", + Digest: "测试摘要", + ShowCoverPic: 1, + Content: "测试内容:

测试段落

", + ContentSourceUrl: "", + NeedOpenComment: 0, + OnlyFansCanComment: 0, + }) + if err != nil { + t.Fatal(err) + } + //cILZtVn68ncUY79JUE3tWL4YE2er7eHJJPV_BGRieHE + t.Log(utils.Format4Output(result, false)) +} + +func TestCBMassSend(t *testing.T) { + err := api.CBMassSend([]string{"oYN_usv1RPvrSxCvo1WsbwI8lZa0", "oYN_ushJ8Ma2kppYRlCJKSZxtzMk"}, "cILZtVn68ncUY79JUE3tWL4YE2er7eHJJPV_BGRieHE") + if err != nil { + t.Fatal(err) + } + //t.Log(utils.Format4Output(result, false)) +} diff --git a/platformapi/weixinapi/weixinapi.go b/platformapi/weixinapi/weixinapi.go index d8a2ae90..ea559cae 100644 --- a/platformapi/weixinapi/weixinapi.go +++ b/platformapi/weixinapi/weixinapi.go @@ -150,14 +150,14 @@ func getBoundary() (boundary string) { return boundary } -func (a *API) AccessAPIUpload(data []byte, fileName, contentType string) (retVal map[string]interface{}, err error) { +func (a *API) AccessAPIUpload(url string, data []byte, fileName, contentType, materialType 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) + fullURL := utils.GenerateGetURL(prodURL, url, params2) // baseapi.SugarLogger.Debug(fullURL) // 实例化multipart body := &bytes.Buffer{} @@ -173,7 +173,9 @@ func (a *API) AccessAPIUpload(data []byte, fileName, contentType string) (retVal // 写入文件数据到multipart,和读取本地文件方法的唯一区别 _, err = part.Write(data) //将额外参数也写入到multipart - //_ = writer.WriteField("media", fileName) + if materialType != "" { + _ = writer.WriteField("type", materialType) + } err = writer.Close() err = platformapi.AccessPlatformAPIWithRetry(a.client, func() *http.Request { diff --git a/platformapi/weixinapi/weixinapi_test.go b/platformapi/weixinapi/weixinapi_test.go index a024bbb0..3da757bc 100644 --- a/platformapi/weixinapi/weixinapi_test.go +++ b/platformapi/weixinapi/weixinapi_test.go @@ -27,7 +27,7 @@ func init() { //weixinapp //api = New("wx18111a41fd17f24f", "c79ac6e1b2d6d7968e72a9658a8b6715") - api.CBSetToken("46_Cj1HI_V-qSBMFLvS0L-Xot-mYJEFhKK5apzHURNV83IPCGMj66X2TGo4Zqtr4nkwpZY3bBNaQhPzKjseBXL9JcyTp0h3i-idLHPCukqz7QO-kV7zycv_Cp6HZyhpsxyyFwobd4DIONbfYTQrCSNaAEAGWR") + api.CBSetToken("46_wVx1dpEvGz0nGoSl_4mngTxlmLMYXMkixtDpppskseIwPJCa0U9JclQWGVPRtEShpwEhZumF6SoLpwtQMwndEOWOaZnI9tbdC1nOggw594wTadHzco4Dl8qmkrvvaPDwwHuz43bBWtURKruyNCFfABAJYB") } func handleError(t *testing.T, err error) {