Ejemplo n.º 1
0
        public virtual async Task <string> UploadVideoAndGetMediaID(int mpid, string fileUrl, MpMessageType type, string title, string introduction)
        {
            var account = await _mpAccountAppService.Get(new Abp.Application.Services.Dto.EntityDto <int> {
                Id = mpid
            });

            var access_token = await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret);

            UploadForeverMediaResult responseModel = null;
            var fileName = fileUrl.Substring(fileUrl.LastIndexOf("/") + 1);
            Dictionary <string, string> content = new Dictionary <string, string>()
            {
                { "description", JsonConvert.SerializeObject(new { title = title, introduction = introduction }) }
            };


            try
            {
                responseModel = JsonConvert.DeserializeObject <UploadForeverMediaResult>(await HttpHelper.HttpPostVideo($"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={access_token}&type={type.ToString()}", fileUrl, content.First(), fileName));
            }
            catch
            {
                access_token = await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true);

                try
                {
                    responseModel = JsonConvert.DeserializeObject <UploadForeverMediaResult>(await HttpHelper.HttpPostVideo($"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={access_token}&type={type.ToString()}", fileUrl, content.First(), fileName));
                }
                catch (Exception e)
                {
                    throw new UserFriendlyException(e.Message);
                }
            }
            return(responseModel != null ? responseModel.media_id : "");
        }
Ejemplo n.º 2
0
        public virtual async Task <string> UploadArtImageAndGetMediaID(int mpid, string fileUrl, MpMessageType type)
        {
            var account = await _mpAccountAppService.Get(new Abp.Application.Services.Dto.EntityDto <int> {
                Id = mpid
            });

            var access_token = await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret);

            UploadImgResult responseModel = null;
            var             fileName      = fileUrl.Substring(fileUrl.LastIndexOf("/") + 1);

            try
            {
                responseModel = JsonConvert.DeserializeObject <UploadImgResult>(await HttpHelper.HttpPost($"https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={access_token}&type={type.ToString()}", fileUrl, fileName));
            }
            catch
            {
                access_token = await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true);

                try
                {
                    responseModel = JsonConvert.DeserializeObject <UploadImgResult>(await HttpHelper.HttpPost($"https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={access_token}&type={type.ToString()}", fileUrl, fileName));
                }
                catch (Exception e)
                {
                    throw new UserFriendlyException(e.Message);
                }
            }
            return(responseModel != null ? responseModel.url : "");
        }
        /// <summary>
        /// 上传微信临时素材
        /// </summary>
        /// <param name="mpid"></param>
        /// <param name="path"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private async Task <string> UploadMedia(int mpid, string path, MpMessageType type)
        {
            var account = await _mpAccountAppService.Get(new EntityDto <int>() { Id = mpid });

            if (account != null)
            {
                var retString = "";
                try
                {
                    var url = string.Format("https://api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), type.ToString());
                    retString = await HttpHelper.HttpPost(url, path, timeOut : Config.TIME_OUT);
                }
                catch (Exception ex)
                {
                    Logger.Error(string.Format("上传MpID为{0},地址{1}的临时素材报错 {2}", mpid, path, type.ToString()), ex);
                    var url = string.Format("https://api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), type.ToString());
                    retString = await HttpHelper.HttpPost(url, path, timeOut : Config.TIME_OUT);
                }
                var returndata = JsonConvert.DeserializeObject <UploadTemporaryMediaResult>(retString);
                if (returndata.errcode == ReturnCode.请求成功)
                {
                    return(returndata.media_id);
                }
                else
                {
                    Logger.Error(string.Format("上传MpID为{0},地址{1}的临时素材报错:{2}", mpid, path, JsonConvert.SerializeObject(returndata)));
                }
            }
            return(string.Empty);
        }