mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-07-15 23:13:32 +08:00
59 lines
3.1 KiB
C#
59 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using Flurl;
|
|
using Flurl.Http;
|
|
|
|
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
|
{
|
|
/// <summary>
|
|
/// 为 <see cref="WechatTenpayClient"/> 提供图片上传(营销专用)相关的 API 扩展方法。
|
|
/// </summary>
|
|
public static class WechatTenpayClientExecuteMarketingMediaExtensions
|
|
{
|
|
/// <summary>
|
|
/// <para>异步调用 [POST] /marketing/favor/media/image-upload 接口。</para>
|
|
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter9_0_1.shtml </para>
|
|
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter9_0_1.shtml </para>
|
|
/// </summary>
|
|
/// <param name="client"></param>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public static async Task<Models.UploadMarketingMediaImageResponse> ExecuteUploadMarketingMediaImageAsync(this WechatTenpayClient client, Models.UploadMarketingMediaImageRequest request, CancellationToken cancellationToken = default)
|
|
{
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
if (string.IsNullOrEmpty(request.FileName))
|
|
request.FileName = Guid.NewGuid().ToString("N").ToLower() + ".png";
|
|
|
|
if (string.IsNullOrEmpty(request.FileHash))
|
|
request.FileHash = Security.SHA256Utility.Hash(request.FileBytes).ToLower();
|
|
|
|
if (string.IsNullOrEmpty(request.FileContentType))
|
|
request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForImage(request.FileName!) ?? "image/png";
|
|
|
|
IFlurlRequest flurlReq = client
|
|
.CreateRequest(request, HttpMethod.Post, "marketing", "favor", "media", "image-upload");
|
|
|
|
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
|
|
using var fileContent = new ByteArrayContent(request.FileBytes);
|
|
using var metaContent = new ByteArrayContent(Encoding.UTF8.GetBytes(client.JsonSerializer.Serialize(request)));
|
|
using var httpContent = new MultipartFormDataContent(boundary);
|
|
httpContent.Add(metaContent, $"\"{Constants.FormDataFields.FORMDATA_META}\"");
|
|
httpContent.Add(fileContent, "\"file\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
|
|
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
|
|
metaContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
|
|
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
|
|
|
|
return await client.SendRequestAsync<Models.UploadMarketingMediaImageResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
|
}
|
|
}
|
|
}
|