using Furion.DependencyInjection; using Furion.DynamicApiController; using Furion.FriendlyException; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; namespace GDZZ.Core.Service; /// /// OAuth服务 /// [ApiDescriptionSettings(Name = "OAuth", Order = 159)] [AllowAnonymous] public class SysOauthService : ISysOauthService, IDynamicApiController, ITransient { private readonly HttpContext _httpContext; private readonly WechatOAuth _wechatOAuth; public SysOauthService(IHttpContextAccessor httpContextAccessor, WechatOAuth wechatOAuth) { _httpContext = httpContextAccessor.HttpContext; _wechatOAuth = wechatOAuth; } /// /// 微信登录授权 /// [HttpGet("oauth/wechat")] public Task WechatLogin() { _httpContext.Response.Redirect(_wechatOAuth.GetAuthorizeUrl("GDZZ")); return Task.CompletedTask; } /// /// 微信登录授权回调 /// /// /// /// /// [HttpGet("oauth/wechatcallback")] public async Task WechatLoginCallback([FromQuery] string code, [FromQuery] string state, [FromQuery] string error_description = "") { if (!string.IsNullOrEmpty(error_description)) throw Oops.Oh(error_description); var accessTokenModel = await _wechatOAuth.GetAccessTokenAsync(code, state); //var userInfoModel = await _wechatOAuth.GetUserInfoAsync(accessTokenModel.AccessToken, accessTokenModel.OpenId); await _httpContext.Response.WriteAsJsonAsync(accessTokenModel); } /// /// 获取微信用户基本信息 /// /// /// /// [HttpGet("oauth/wechat/user")] public async Task GetWechatUserInfo([FromQuery] string token, [FromQuery] string openId) { return await _wechatOAuth.GetUserInfoAsync(token, openId); } }