material.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package material
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/silenceper/wechat/context"
  7. "github.com/silenceper/wechat/util"
  8. )
  9. const (
  10. addNewsURL = "https://api.weixin.qq.com/cgi-bin/material/add_news"
  11. addMaterialURL = "https://api.weixin.qq.com/cgi-bin/material/add_material"
  12. )
  13. //Material 素材管理
  14. type Material struct {
  15. *context.Context
  16. }
  17. //NewMaterial init
  18. func NewMaterial(context *context.Context) *Material {
  19. material := new(Material)
  20. material.Context = context
  21. return material
  22. }
  23. //Article 永久图文素材
  24. type Article struct {
  25. ThumbMediaID string `json:"thumb_media_id"`
  26. Author string `json:"author"`
  27. Digest string `json:"digest"`
  28. ShowCoverPic int `json:"show_cover_pic"`
  29. Content string `json:"content"`
  30. ContentSourceURL string `json:"content_source_url"`
  31. }
  32. //reqArticles 永久性图文素材请求信息
  33. type reqArticles struct {
  34. Articles []*Article `json:"articles"`
  35. }
  36. //resArticles 永久性图文素材返回结果
  37. type resArticles struct {
  38. util.CommonError
  39. MediaID string `json:"media_id"`
  40. }
  41. //AddNews 新增永久图文素材
  42. func (material *Material) AddNews(articles []*Article) (mediaID string, err error) {
  43. req := &reqArticles{articles}
  44. var accessToken string
  45. accessToken, err = material.GetAccessToken()
  46. if err != nil {
  47. return
  48. }
  49. uri := fmt.Sprintf("%s?access_token=%s", addNewsURL, accessToken)
  50. responseBytes, err := util.PostJSON(uri, req)
  51. var res resArticles
  52. err = json.Unmarshal(responseBytes, res)
  53. if err != nil {
  54. return
  55. }
  56. mediaID = res.MediaID
  57. return
  58. }
  59. //resAddMaterial 永久性素材上传返回的结果
  60. type resAddMaterial struct {
  61. util.CommonError
  62. MediaID string `json:"media_id"`
  63. URL string `json:"url"`
  64. }
  65. //AddMaterial 上传永久性素材(处理视频需要单独上传)
  66. func (material *Material) AddMaterial(mediaType MediaType, filename string) (mediaID string, url string, err error) {
  67. if mediaType == MediaTypeVideo {
  68. err = errors.New("永久视频素材上传使用 AddVideo 方法")
  69. }
  70. var accessToken string
  71. accessToken, err = material.GetAccessToken()
  72. if err != nil {
  73. return
  74. }
  75. uri := fmt.Sprintf("%s?access_token=%s&type=%s", addMaterialURL, accessToken, mediaType)
  76. var response []byte
  77. response, err = util.PostFile("media", filename, uri)
  78. if err != nil {
  79. return
  80. }
  81. var resMaterial resAddMaterial
  82. err = json.Unmarshal(response, &resMaterial)
  83. if err != nil {
  84. return
  85. }
  86. if resMaterial.ErrCode != 0 {
  87. err = fmt.Errorf("AddMaterial error : errcode=%v , errmsg=%v", resMaterial.ErrCode, resMaterial.ErrMsg)
  88. return
  89. }
  90. mediaID = resMaterial.MediaID
  91. url = resMaterial.URL
  92. return
  93. }
  94. type reqVideo struct {
  95. Title string `json:"title"`
  96. Introduction string `json:"introduction"`
  97. }
  98. //AddVideo 永久视频素材文件上传
  99. func (material *Material) AddVideo(filename, title, introduction string) (mediaID string, url string, err error) {
  100. var accessToken string
  101. accessToken, err = material.GetAccessToken()
  102. if err != nil {
  103. return
  104. }
  105. uri := fmt.Sprintf("%s?access_token=%s&type=video", addMaterialURL, accessToken)
  106. videoDesc := &reqVideo{
  107. Title: title,
  108. Introduction: introduction,
  109. }
  110. var fieldValue []byte
  111. fieldValue, err = json.Marshal(videoDesc)
  112. if err != nil {
  113. return
  114. }
  115. fields := []util.MultipartFormField{
  116. {
  117. IsFile: true,
  118. Fieldname: "video",
  119. Filename: filename,
  120. },
  121. {
  122. IsFile: true,
  123. Fieldname: "description",
  124. Value: fieldValue,
  125. },
  126. }
  127. var response []byte
  128. response, err = util.PostMultipartForm(fields, uri)
  129. if err != nil {
  130. return
  131. }
  132. var resMaterial resAddMaterial
  133. err = json.Unmarshal(response, &resMaterial)
  134. if err != nil {
  135. return
  136. }
  137. if resMaterial.ErrCode != 0 {
  138. err = fmt.Errorf("AddMaterial error : errcode=%v , errmsg=%v", resMaterial.ErrCode, resMaterial.ErrMsg)
  139. return
  140. }
  141. mediaID = resMaterial.MediaID
  142. url = resMaterial.URL
  143. return
  144. }