feat(wxapi): 新增视频号小店上传图片接口

This commit is contained in:
Fu Diwei
2022-10-31 22:32:42 +08:00
parent 0c23713561
commit dd722a9899
3 changed files with 143 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
namespace SKIT.FlurlHttpClient.Wechat.Api
{
public static class WechatApiClientExecuteChannelsExtensions
{
#region ECBasics
/// <summary>
/// <para>异步调用 [POST] /channels/ec/basics/img/upload 接口。</para>
/// <para>REF: https://developers.weixin.qq.com/doc/channels/API/basics/img_upload.html </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.ChannelsECBasicsImageUploadResponse> ExecuteChannelsECBasicsImageUploadAsync(this WechatApiClient client, Models.ChannelsECBasicsImageUploadRequest request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "channels", "ec", "basics", "img", "upload")
.SetQueryParam("access_token", request.AccessToken)
.SetQueryParam("resp_type", request.ResponseType);
if (request.ImageUrl != null)
{
flurlReq.SetQueryParam("upload_type", 1)
.SetQueryParam("img_url", request.ImageUrl);
return await client.SendRequestWithJsonAsync<Models.ChannelsECBasicsImageUploadResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
}
else
{
flurlReq.SetQueryParam("upload_type", 0)
.SetQueryParam("height", request.Height)
.SetQueryParam("width", request.Width);
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
using var httpContent = new MultipartFormDataContent(boundary);
using var fileContent = new ByteArrayContent(request.ImageFileBytes ?? Array.Empty<byte>());
httpContent.Add(fileContent, "\"media\"", "\"image.png\"");
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/png");
fileContent.Headers.ContentLength = request.ImageFileBytes?.Length;
return await client.SendRequestAsync<Models.ChannelsECBasicsImageUploadResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
}
}
#endregion
}
}

View File

@@ -0,0 +1,43 @@
namespace SKIT.FlurlHttpClient.Wechat.Api.Models
{
/// <summary>
/// <para>表示 [POST] /channels/ec/basics/img/upload 接口的请求。</para>
/// </summary>
public class ChannelsECBasicsImageUploadRequest : WechatApiRequest, IInferable<ChannelsECBasicsImageUploadRequest, ChannelsECBasicsImageUploadResponse>
{
/// <summary>
/// 获取或设置返回数据类型。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public int ResponseType { get; set; }
/// <summary>
/// 获取或设置图片文件字节数组。与字段 <see cref="ImageUrl"/> 二选一。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public byte[]? ImageFileBytes { get; set; }
/// <summary>
/// 获取或设置图片高度(单位:像素)。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public int? Height { get; set; }
/// <summary>
/// 获取或设置图片宽度(单位:像素)。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public int? Width { get; set; }
/// <summary>
/// 获取或设置图片文件 URL。与字段 <see cref="ImageFileBytes"/> 二选一。
/// </summary>
[Newtonsoft.Json.JsonProperty("img_url")]
[System.Text.Json.Serialization.JsonPropertyName("img_url")]
public string? ImageUrl { get; set; }
}
}

View File

@@ -0,0 +1,42 @@
namespace SKIT.FlurlHttpClient.Wechat.Api.Models
{
/// <summary>
/// <para>表示 [POST] /channels/ec/basics/img/upload 接口的响应。</para>
/// </summary>
public class ChannelsECBasicsImageUploadResponse : WechatApiResponse
{
public static class Types
{
public class PictureFile
{
/// <summary>
/// 获取或设置 MediaId。
/// </summary>
[Newtonsoft.Json.JsonProperty("media_id")]
[System.Text.Json.Serialization.JsonPropertyName("media_id")]
public string? MediaId { get; set; }
/// <summary>
/// 获取或设置支付专用 MediaId。
/// </summary>
[Newtonsoft.Json.JsonProperty("pay_media_id")]
[System.Text.Json.Serialization.JsonPropertyName("pay_media_id")]
public string? PayMediaId { get; set; }
/// <summary>
/// 获取或设置图片临时 URL。
/// </summary>
[Newtonsoft.Json.JsonProperty("temp_img_url")]
[System.Text.Json.Serialization.JsonPropertyName("temp_img_url")]
public string? TempImageUrl { get; set; }
}
}
/// <summary>
/// 获取或设置图片文件信息。
/// </summary>
[Newtonsoft.Json.JsonProperty("pic_file")]
[System.Text.Json.Serialization.JsonPropertyName("pic_file")]
public Types.PictureFile PictureFile { get; set; } = default!;
}
}