AssetManager.API/AssetManager.Services/Services/WechatService.cs
fanfpy cd5c3aedbe feat: 初始化项目结构并添加基础功能
- 创建解决方案及各项目层
- 添加API基础控制器和DTO定义
- 实现JWT认证服务和微信登录服务
- 添加Swagger文档支持
- 配置项目依赖和构建文件
2026-02-18 20:36:09 +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 = "your-wechat-app-id"; // 替换为实际的微信小程序AppId
_appSecret = "your-wechat-app-secret"; // 替换为实际的微信小程序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);
}
}
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; }
}