// 获取微信小程序OpenID
private async Task<string> GetWxOpenIdAsync(string code)
{
    try
    {
        var appId = configuration["WeChat:AppId"];
        var appSecret = configuration["WeChat:AppSecret"];
        
        if (string.IsNullOrEmpty(appId) || string.IsNullOrEmpty(appSecret))
        {
            throw new Exception("微信小程序配置缺失");
        }

        var httpClient = _httpClientFactory.CreateClient();
        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 responseContent = await response.Content.ReadAsStringAsync();
        var jsonDocument = JsonDocument.Parse(responseContent);
        
        if (jsonDocument.RootElement.TryGetProperty("openid", out var openidElement))
        {
            return openidElement.GetString();
        }
        else
        {
            // 如果有错误信息，抛出异常
            if (jsonDocument.RootElement.TryGetProperty("errcode", out var errcodeElement) && 
                jsonDocument.RootElement.TryGetProperty("errmsg", out var errmsgElement))
            {
                throw new Exception($"获取OpenID失败: {errcodeElement.GetInt32()} - {errmsgElement.GetString()}");
            }
            throw new Exception("获取OpenID失败: 响应中未包含openid");
        }
    }
    catch (Exception ex)
    {
        throw new Exception($"获取微信OpenID时发生错误: {ex.Message}");
    }
}