예제 #1
0
        /// <summary>
        /// 上传素材(已上传则直接返回)
        /// </summary>
        /// <param name="type"></param>
        /// <param name="fileId"></param>
        /// <returns></returns>
        public WeChatSuccessUploadResponse CreateData(MaterialType type, string fileId)
        {
            var file = new SysFileService().GetData(fileId);

            AssertUtil.CheckBoolean <SpException>(file == null, $"根据fileid:{fileId}未找到记录", "36B5F5C9-ED65-4CAC-BE60-712278056EA9");

            // 检查素材库是否已经上传
            var data = Broker.Retrieve <wechat_material>("select * from wechat_material where sys_fileid = @sys_fileid", new Dictionary <string, object>()
            {
                { "@sys_fileid", file.sys_fileId }
            });

            if (data != null)
            {
                return(new WeChatSuccessUploadResponse()
                {
                    media_id = data.media_id,
                    url = data.url
                });
            }

            // 获取文件流
            var config = StoreConfig.Config;
            var stream = ServiceContainer.Resolve <IStoreStrategy>(config?.Type).GetStream(fileId);
            var media  = WeChatApi.AddMaterial(type, stream, file.name, file.content_type);

            stream.Dispose();

            // 创建素材记录
            var material = new wechat_material()
            {
                wechat_materialId = Guid.NewGuid().ToString(),
                media_id          = media.media_id,
                url        = media.url,
                sys_fileid = fileId,
                local_url  = SysFileService.GetDownloadUrl(fileId),
                name       = file.name,
                type       = type.ToMaterialTypeString()
            };

            CreateData(material);
            return(media);
        }
예제 #2
0
        /// <summary>
        /// 新增永久素材(音乐、视频、图片)
        /// </summary>
        /// <param name="type"></param>
        /// <param name="stream"></param>
        /// <param name="fileName"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public static WeChatSuccessUploadResponse AddMaterial(MaterialType type, Stream stream, string fileName, string contentType = "application/octet-stream")
        {
            var url = string.Format(WeChatApiConfig.GetValue("AddMaterialAPi"), WeChatService.AccessToken, type.ToMaterialTypeString());

            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            var postData = new UploadFile
            {
                Name        = "media",
                Filename    = fileName,
                ContentType = contentType,
                Data        = bytes
            };
            var result     = HttpUtil.Post(url, postData);
            var resultJson = JObject.Parse(result);

            if (resultJson.GetValue("errcode") != null && resultJson.GetValue("errcode").ToString() != "0")
            {
                var error = JsonConvert.DeserializeObject <WeChatErrorResponse>(result);
                throw new SpException("添加素材失败:" + error.errmsg);
            }
            else
            {
                return(JsonConvert.DeserializeObject <WeChatSuccessUploadResponse>(result));
            }
        }