feat(wxapi): 新增小程序重置登录态接口

This commit is contained in:
Fu Diwei
2024-06-20 16:14:22 +08:00
parent d05fc8437c
commit 2a50fafa0a
5 changed files with 126 additions and 2 deletions

View File

@@ -13,6 +13,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
/// <para>异步调用 [GET] /wxa/checksession 接口。</para> /// <para>异步调用 [GET] /wxa/checksession 接口。</para>
/// <para> /// <para>
/// REF: <br/> /// REF: <br/>
/// <![CDATA[ https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/checkSessionKey.html ]]>
/// <![CDATA[ https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/login/auth.checkSessionKey.html ]]> /// <![CDATA[ https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/login/auth.checkSessionKey.html ]]>
/// </para> /// </para>
/// </summary> /// </summary>
@@ -35,6 +36,33 @@ namespace SKIT.FlurlHttpClient.Wechat.Api
return await client.SendFlurlRequestAsJsonAsync<Models.WxaCheckSessionResponse>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false); return await client.SendFlurlRequestAsJsonAsync<Models.WxaCheckSessionResponse>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
} }
/// <summary>
/// <para>异步调用 [GET] /wxa/resetusersessionkey 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/ResetUserSessionKey.html ]]> <br/>
/// <![CDATA[ https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/login/auth.ResetUserSessionKey.html ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.WxaResetUserSessionKeyResponse> ExecuteWxaResetUserSessionKeyAsync(this WechatApiClient client, Models.WxaResetUserSessionKeyRequest 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
.CreateFlurlRequest(request, HttpMethod.Get, "wxa", "resetusersessionkey")
.SetQueryParam("access_token", request.AccessToken)
.SetQueryParam("openid", request.OpenId)
.SetQueryParam("signature", request.Signature)
.SetQueryParam("sig_method", request.SignMethod);
return await client.SendFlurlRequestAsJsonAsync<Models.WxaResetUserSessionKeyResponse>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary> /// <summary>
/// <para>异步调用 [GET] /wxa/getpaidunionid 接口。</para> /// <para>异步调用 [GET] /wxa/getpaidunionid 接口。</para>
/// <para> /// <para>

View File

@@ -1,4 +1,6 @@
namespace SKIT.FlurlHttpClient.Wechat.Api.Models using System;
namespace SKIT.FlurlHttpClient.Wechat.Api.Models
{ {
/// <summary> /// <summary>
/// <para>表示 [GET] /wxa/checksession 接口的请求。</para> /// <para>表示 [GET] /wxa/checksession 接口的请求。</para>
@@ -21,10 +23,27 @@
/// <summary> /// <summary>
/// 获取或设置用户登录态签名的哈希方法。 /// 获取或设置用户登录态签名的哈希方法。
/// <para>默认值hmac_sha256</para> /// <para>默认值:"hmac_sha256"</para>
/// </summary> /// </summary>
[Newtonsoft.Json.JsonIgnore] [Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore] [System.Text.Json.Serialization.JsonIgnore]
public string SignMethod { get; set; } = "hmac_sha256"; public string SignMethod { get; set; } = "hmac_sha256";
public void SetSignatureBySessionKey(string sessionKey, string signMethod = "hmac_sha256")
{
if (sessionKey is null) throw new ArgumentNullException(nameof(sessionKey));
if (signMethod is null) throw new ArgumentNullException(nameof(signMethod));
switch (signMethod)
{
case "hmac_sha256":
Signature = Utilities.HMACUtility.HashWithSHA256(sessionKey, string.Empty).Value!.ToLower();
SignMethod = signMethod;
break;
default:
throw new NotSupportedException();
}
}
} }
} }

View File

@@ -0,0 +1,49 @@
using System;
namespace SKIT.FlurlHttpClient.Wechat.Api.Models
{
/// <summary>
/// <para>表示 [GET] /wxa/resetusersessionkey 接口的请求。</para>
/// </summary>
public class WxaResetUserSessionKeyRequest : WechatApiRequest, IInferable<WxaResetUserSessionKeyRequest, WxaResetUserSessionKeyResponse>
{
/// <summary>
/// 获取或设置用户唯一标识。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public string OpenId { get; set; } = string.Empty;
/// <summary>
/// 获取或设置用户登录态签名。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public string Signature { get; set; } = string.Empty;
/// <summary>
/// 获取或设置用户登录态签名的哈希方法。
/// <para>默认值:"hmac_sha256"</para>
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public string SignMethod { get; set; } = "hmac_sha256";
public void SetSignatureBySessionKey(string sessionKey, string signMethod = "hmac_sha256")
{
if (sessionKey is null) throw new ArgumentNullException(nameof(sessionKey));
if (signMethod is null) throw new ArgumentNullException(nameof(signMethod));
switch (signMethod)
{
case "hmac_sha256":
Signature = Utilities.HMACUtility.HashWithSHA256(sessionKey, string.Empty).Value!.ToLower();
SignMethod = signMethod;
break;
default:
throw new NotSupportedException();
}
}
}
}

View File

@@ -0,0 +1,22 @@
namespace SKIT.FlurlHttpClient.Wechat.Api.Models
{
/// <summary>
/// <para>表示 [GET] /wxa/resetusersessionkey 接口的响应。</para>
/// </summary>
public class WxaResetUserSessionKeyResponse : WechatApiResponse
{
/// <summary>
/// 获取或设置用户唯一标识。
/// </summary>
[Newtonsoft.Json.JsonProperty("openid")]
[System.Text.Json.Serialization.JsonPropertyName("openid")]
public string OpenId { get; set; } = default!;
/// <summary>
/// 获取或设置重置后的会话密钥。
/// </summary>
[Newtonsoft.Json.JsonProperty("session_key")]
[System.Text.Json.Serialization.JsonPropertyName("session_key")]
public string SessionKey { get; set; } = default!;
}
}

View File

@@ -0,0 +1,6 @@
{
"errcode": 0,
"errmsg": "ok",
"openid": "xxxxxxx",
"session_key": "xxxxxxxx"
}