123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- package mpwechat
-
- import (
- base_ctl "SCRM/controllers"
- "SCRM/enums"
- "SCRM/models"
- "SCRM/service/wechat_service"
- "bytes"
- "encoding/json"
- "mime/multipart"
- "sync"
- "time"
-
- "io"
- "io/ioutil"
-
- "github.com/astaxie/beego"
- "github.com/astaxie/beego/httplib"
- )
-
- func MpMaterialCtlRegistRouters() {
- beego.Router("/api/mpwechat/media/add_material", &MpMaterialAPIController{}, "Post:AddMaterial")
- beego.Router("/api/mpwechat/media/material", &MpMaterialAPIController{}, "Get:GetMaterials")
- }
-
- type MpMaterialAPIController struct {
- base_ctl.BaseAuthAPIController
- }
-
- type MultipartFormField struct {
- IsFile bool
- Name string
- FileName string
- Value io.Reader
- }
-
- var mediaBufferPool = sync.Pool{
- New: func() interface{} {
- return bytes.NewBuffer(make([]byte, 0, 10<<20)) // 10MB
- },
- }
-
- func (c MpMaterialAPIController) AddMaterial() {
-
- adminUserInfo := c.GetAdminUserInfo()
-
- authorization, err := wechat_service.GetAuthorizationByOrgID(adminUserInfo.CurrentOrgId)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDataException, "上传失败:("+err.Error()+")")
- return
- }
- if authorization == nil || authorization.AuthorizerStatus != 1 {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "上传失败:(公众号未授权)")
- return
- }
-
- file, head, err := c.GetFile("file")
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "上传失败:("+err.Error()+")")
- return
- }
- defer file.Close()
-
- var fields = []MultipartFormField{
- {
- IsFile: true,
- Name: "media",
- FileName: head.Filename,
- Value: file,
- },
- }
-
- buffer := mediaBufferPool.Get().(*bytes.Buffer)
- buffer.Reset()
- defer mediaBufferPool.Put(buffer)
-
- multipartWriter := multipart.NewWriter(buffer)
- for i := 0; i < len(fields); i++ {
- if field := &fields[i]; field.IsFile {
- partWriter, err3 := multipartWriter.CreateFormFile(field.Name, field.FileName)
- if err3 != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "上传失败:("+err3.Error()+")")
- return
- }
- if _, err3 = io.Copy(partWriter, field.Value); err3 != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "上传失败:("+err3.Error()+")")
- return
- }
- } else {
- partWriter, err3 := multipartWriter.CreateFormField(field.Name)
- if err3 != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "上传失败:("+err3.Error()+")")
- return
- }
- if _, err3 = io.Copy(partWriter, field.Value); err3 != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "上传失败:("+err3.Error()+")")
- return
- }
- }
- }
-
- if err = multipartWriter.Close(); err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "上传失败:("+err.Error()+")")
- return
- }
- requestBodyBytes := buffer.Bytes()
- requestBodyType := multipartWriter.FormDataContentType()
- url := "https://api.weixin.qq.com/cgi-bin/material/add_material?type=image&access_token=" + authorization.AuthorizerAccessToken
- res := httplib.Post(url)
- res.Header("Content-Type", requestBodyType)
- response, err := res.Body(requestBodyBytes).Response()
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "上传失败:("+err.Error()+")")
- return
- }
- respBody, err := ioutil.ReadAll(response.Body)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "上传失败:("+err.Error()+")")
- return
- }
-
- var resMaterial wechat_service.ResAddMaterial
- err = json.Unmarshal(respBody, &resMaterial)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "上传失败:("+err.Error()+")")
- return
- }
- if resMaterial.ErrCode != 0 {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "上传失败:("+resMaterial.ErrMsg+")")
- return
- }
- mediaID := resMaterial.MediaID
- mediaUrl := resMaterial.URL
- var media models.WechatMedias
- media.UserOrgId = adminUserInfo.CurrentOrgId
- media.MediaId = mediaID
- media.MediaUrl = mediaUrl
- media.Msgtype = "image"
- media.MediaTitle = head.Filename
- media.CreatedTime = time.Now().Unix()
- media.UpdatedTime = time.Now().Unix()
- media.MediaStatus = 1
-
- err = wechat_service.SaveMaterial(&media)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBCreate, "上传失败:("+err.Error()+")")
- return
- }
-
- returnData := make(map[string]interface{}, 0)
- returnData["media"] = media
- c.ServeSuccessJSON(returnData)
- return
- }
-
- func (c *MpMaterialAPIController) GetMaterials() {
- page, _ := c.GetInt64("page", 1)
- limit, _ := c.GetInt64("limit", 10)
- mediaType := c.GetString("media_type")
- if page <= 0 {
- page = 1
- }
- if limit <= 0 {
- limit = 10
- }
-
- adminUserInfo := c.GetAdminUserInfo()
- medias, total, err := wechat_service.GetPageMaterials(adminUserInfo.CurrentOrgId, page, limit, mediaType)
- if err != nil {
- c.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "拉取失败:("+err.Error()+")")
- return
- }
-
- returnData := make(map[string]interface{}, 0)
- returnData["medias"] = medias
- returnData["total"] = total
- c.ServeSuccessJSON(returnData)
- return
- }
|