using Furion.DependencyInjection; using Furion.DynamicApiController; using Furion.FriendlyException; using GDZZ.Core.OAuth; 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; private readonly MiniProgramUtil miniProgram; public SysOauthService(IHttpContextAccessor httpContextAccessor, WechatOAuth wechatOAuth) { _httpContext = httpContextAccessor.HttpContext; _wechatOAuth = wechatOAuth; this.miniProgram = new MiniProgramUtil(); } /// /// 微信登录授权获取微信access_token /// [HttpGet("oauth/wechat")] public Task WechatLogin() { var token = _wechatOAuth.GetAuthorizeUrl("GDZZ"); _httpContext.Response.Redirect(token); return Task.CompletedTask; } /// /// 微信登录换取jscode授权回调 /// /// /// /// /// [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); return accessTokenModel; } /// /// 获取微信用户基本信息 /// /// /// /// [HttpGet("oauth/wechat/user")] public async Task GetWechatUserInfo([FromQuery] string token, [FromQuery] string openId) { return await _wechatOAuth.GetUserInfoAsync(token, openId); } }