feat(wxapi): 随官方更新自定义交易组件上传图片相关接口模型

This commit is contained in:
Fu Diwei
2022-01-21 17:11:05 +08:00
parent 211687ea62
commit 4004f30fc8
4 changed files with 48 additions and 14 deletions

View File

@@ -25,17 +25,30 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
IFlurlRequest flurlReq = client
.CreateRequest(request, HttpMethod.Post, "shop", "img", "upload")
.SetQueryParam("access_token", request.AccessToken);
.SetQueryParam("access_token", request.AccessToken)
.SetQueryParam("resp_type", request.ResponseType);
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
using var httpContent = new MultipartFormDataContent(boundary);
using var fileContent = new ByteArrayContent(request.ImageFileBytes ?? new byte[0]);
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;
if (request.ImageUrl != null)
{
flurlReq.SetQueryParam("upload_type", 1)
.SetQueryParam("img_url", request.ImageUrl);
return await client.SendRequestAsync<Models.ShopImageUploadResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
return await client.SendRequestWithJsonAsync<Models.ShopImageUploadResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
}
else
{
flurlReq.SetQueryParam("upload_type", 0);
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
using var httpContent = new MultipartFormDataContent(boundary);
using var fileContent = new ByteArrayContent(request.ImageFileBytes ?? new byte[0]);
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.ShopImageUploadResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
}
}
#region Register

View File

@@ -14,14 +14,14 @@
/// </summary>
[Newtonsoft.Json.JsonProperty("media_id")]
[System.Text.Json.Serialization.JsonPropertyName("media_id")]
public string MediaId { get; set; } = default!;
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; } = default!;
public string? PayMediaId { get; set; }
/// <summary>
/// 获取或设置图片临时 URL。

View File

@@ -6,10 +6,24 @@
public class ShopImageUploadRequest : WechatApiRequest
{
/// <summary>
/// 获取或设置图片文件字节数组
/// 获取或设置返回数据类型
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public byte[] ImageFileBytes { get; set; } = new byte[0];
public int ResponseType { get; set; }
/// <summary>
/// 获取或设置图片文件字节数组。与字段 <see cref="ImageUrl"/> 二选一。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public byte[]? ImageFileBytes { 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

@@ -14,7 +14,14 @@
/// </summary>
[Newtonsoft.Json.JsonProperty("media_id")]
[System.Text.Json.Serialization.JsonPropertyName("media_id")]
public string MediaId { get; set; } = default!;
public string? MediaId { get; set; }
/// <summary>
/// 获取或设置图片临时 URL。
/// </summary>
[Newtonsoft.Json.JsonProperty("temp_img_url")]
[System.Text.Json.Serialization.JsonPropertyName("temp_img_url")]
public string? TempImageUrl { get; set; }
}
}