AssetManager.API/AssetManager.Services/Services/WechatService.cs
niannian zheng b5499ef7fe refactor: 将模型属性改为可为空类型以增强健壮性
- 修改ApiResponse、RiskParityConfig等DTO类的属性为可空类型
- 在策略计算器中添加空值检查逻辑
- 更新服务层代码处理可能的空值情况
- 添加发布配置文件FolderProfile.pubxml
2026-03-06 15:51:59 +08:00

37 lines
1.2 KiB
C#

using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
namespace AssetManager.Services.Services;
public class WechatService
{
private readonly HttpClient _httpClient;
private readonly string _appId;
private readonly string _appSecret;
public WechatService(HttpClient httpClient)
{
_httpClient = httpClient;
_appId = "wx245f0f3ebcfcf5a7"; // 替换为实际的微信小程序AppId
_appSecret = "809c740129bc8b434177ce12ef292dd0"; // 替换为实际的微信小程序AppSecret
}
public async Task<WechatAuthResult> GetOpenIdAsync(string code)
{
var url = $"https://api.weixin.qq.com/sns/jscode2session?appid={_appId}&secret={_appSecret}&js_code={code}&grant_type=authorization_code";
var response = await _httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<WechatAuthResult>(content) ?? new WechatAuthResult();
}
}
public class WechatAuthResult
{
public string? OpenId { get; set; }
public string? SessionKey { get; set; }
public string? UnionId { get; set; }
public int Errcode { get; set; }
public string? Errmsg { get; set; }
}