PictureApi.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:OrderApi.cs
  17. 文件功能描述:微小店图片接口
  18. 创建标识:Senparc - 20150827
  19. 修改标识:Senparc - 20160721
  20. 修改描述:增加UploadImg的异步方法
  21. ----------------------------------------------------------------*/
  22. /*
  23. 微小店接口,官方API:http://mp.weixin.qq.com/wiki/index.php?title=%E5%BE%AE%E4%BF%A1%E5%B0%8F%E5%BA%97%E6%8E%A5%E5%8F%A3
  24. */
  25. using System.Threading.Tasks;
  26. using Senparc.CO2NET.Extensions;
  27. using Senparc.CO2NET.Helpers;
  28. using Senparc.CO2NET.HttpUtility;
  29. using Senparc.NeuChar;
  30. using Senparc.Weixin.Helpers;
  31. using Senparc.Weixin.HttpUtility;
  32. namespace Senparc.Weixin.MP.AdvancedAPIs.MerChant
  33. {
  34. /// <summary>
  35. /// 微小店图片接口
  36. /// </summary>
  37. public static class PictureApi
  38. {
  39. #region 同步方法
  40. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "PictureApi.UploadImg", true)]
  41. public static PictureResult UploadImg(string accessToken, string fileName)
  42. {
  43. var urlFormat = Config.ApiMpHost + "/merchant/common/upload_img?access_token={0}&filename={1}";
  44. var url = string.IsNullOrEmpty(accessToken) ? urlFormat : string.Format(urlFormat, accessToken.AsUrlData(), fileName.AsUrlData());
  45. var json = new PictureResult();
  46. using (var fs = FileHelper.GetFileStream(fileName))
  47. {
  48. var jsonText = RequestUtility.HttpPost(url, null, fs);
  49. json = Senparc.Weixin.HttpUtility.Post.GetResult<PictureResult>(jsonText);
  50. }
  51. return json;
  52. }
  53. #endregion
  54. #if !NET35 && !NET40
  55. #region 异步方法
  56. [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "PictureApi.UploadImgAsync", true)]
  57. public static async Task<PictureResult> UploadImgAsync(string accessToken, string fileName)
  58. {
  59. var urlFormat = Config.ApiMpHost + "/merchant/common/upload_img?access_token={0}&filename={1}";
  60. var url = string.IsNullOrEmpty(accessToken) ? urlFormat : string.Format(urlFormat, accessToken.AsUrlData(), fileName.AsUrlData());
  61. var json = new PictureResult();
  62. using (var fs = FileHelper.GetFileStream(fileName))
  63. {
  64. var jsonText = await RequestUtility.HttpPostAsync(url, null, fs);
  65. json = Senparc.Weixin.HttpUtility.Post.GetResult<PictureResult>(jsonText);
  66. }
  67. return json;
  68. }
  69. #endregion
  70. #endif
  71. }
  72. }