You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.2 KiB

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;
/// <summary>
/// OAuth服务
/// </summary>
[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();
}
/// <summary>
2 years ago
/// 微信登录授权获取微信access_token
/// </summary>
[HttpGet("oauth/wechat")]
public Task WechatLogin()
{
var token = _wechatOAuth.GetAuthorizeUrl("GDZZ");
_httpContext.Response.Redirect(token);
return Task.CompletedTask;
}
/// <summary>
/// 微信登录换取jscode授权回调
/// </summary>
/// <param name="code"></param>
/// <param name="state"></param>
/// <param name="error_description"></param>
/// <returns></returns>
[HttpGet("oauth/wechatcallback")]
public async Task<dynamic> 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;
}
/// <summary>
/// 获取微信用户基本信息
/// </summary>
/// <param name="token"></param>
/// <param name="openId"></param>
/// <returns></returns>
[HttpGet("oauth/wechat/user")]
public async Task<dynamic> GetWechatUserInfo([FromQuery] string token, [FromQuery] string openId)
{
return await _wechatOAuth.GetUserInfoAsync(token, openId);
}
}