// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
namespace Admin.NET.Core.Service;
///
/// 微信公众号服务
///
[ApiDescriptionSettings(Order = 230)]
public class SysWechatService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _sysWechatUserRep;
private readonly SysConfigService _sysConfigService;
private readonly WechatApiClientFactory _wechatApiClientFactory;
private readonly WechatApiClient _wechatApiClient;
public SysWechatService(SqlSugarRepository sysWechatUserRep,
SysConfigService sysConfigService,
WechatApiClientFactory wechatApiClientFactory)
{
_sysWechatUserRep = sysWechatUserRep;
_sysConfigService = sysConfigService;
_wechatApiClientFactory = wechatApiClientFactory;
_wechatApiClient = wechatApiClientFactory.CreateWechatClient();
}
///
/// 生成网页授权Url
///
///
///
[AllowAnonymous]
[DisplayName("生成网页授权Url")]
public string GenAuthUrl(GenAuthUrlInput input)
{
return _wechatApiClient.GenerateParameterizedUrlForConnectOAuth2Authorize(input.RedirectUrl, input.Scope, input.State);
}
///
/// 获取微信用户OpenId
///
///
[AllowAnonymous]
[DisplayName("获取微信用户OpenId")]
public async Task SnsOAuth2([FromQuery] WechatOAuth2Input input)
{
var reqOAuth2 = new SnsOAuth2AccessTokenRequest()
{
Code = input.Code,
};
var resOAuth2 = await _wechatApiClient.ExecuteSnsOAuth2AccessTokenAsync(reqOAuth2);
if (resOAuth2.ErrorCode != (int)WechatReturnCodeEnum.请求成功)
throw Oops.Oh(resOAuth2.ErrorMessage + " " + resOAuth2.ErrorCode);
var wxUser = await _sysWechatUserRep.GetFirstAsync(p => p.OpenId == resOAuth2.OpenId);
if (wxUser == null)
{
var reqUserInfo = new SnsUserInfoRequest()
{
OpenId = resOAuth2.OpenId,
AccessToken = resOAuth2.AccessToken,
};
var resUserInfo = await _wechatApiClient.ExecuteSnsUserInfoAsync(reqUserInfo);
wxUser = resUserInfo.Adapt();
wxUser.Avatar = resUserInfo.HeadImageUrl;
wxUser.NickName = resUserInfo.Nickname;
wxUser = await _sysWechatUserRep.AsInsertable(wxUser).ExecuteReturnEntityAsync();
}
else
{
wxUser.AccessToken = resOAuth2.AccessToken;
wxUser.RefreshToken = resOAuth2.RefreshToken;
await _sysWechatUserRep.AsUpdateable(wxUser).IgnoreColumns(true).ExecuteCommandAsync();
}
return resOAuth2.OpenId;
}
///
/// 微信用户登录OpenId
///
///
///
[AllowAnonymous]
[DisplayName("微信用户登录OpenId")]
public async Task OpenIdLogin(WechatUserLogin input)
{
var wxUser = await _sysWechatUserRep.GetFirstAsync(p => p.OpenId == input.OpenId);
if (wxUser == null)
throw Oops.Oh("微信用户登录OpenId错误");
var tokenExpire = await _sysConfigService.GetTokenExpire();
return new
{
wxUser.Avatar,
accessToken = JWTEncryption.Encrypt(new Dictionary
{
{ ClaimConst.UserId, wxUser.Id },
{ ClaimConst.NickName, wxUser.NickName },
{ ClaimConst.LoginMode, LoginModeEnum.APP },
}, tokenExpire)
};
}
///
/// 获取配置签名参数(wx.config)
///
///
[DisplayName("获取配置签名参数(wx.config)")]
public async Task GenConfigPara(SignatureInput input)
{
var resCgibinToken = await _wechatApiClient.ExecuteCgibinTokenAsync(new CgibinTokenRequest());
var request = new CgibinTicketGetTicketRequest()
{
AccessToken = resCgibinToken.AccessToken
};
var response = await _wechatApiClient.ExecuteCgibinTicketGetTicketAsync(request);
if (!response.IsSuccessful())
throw Oops.Oh(response.ErrorMessage);
return _wechatApiClient.GenerateParametersForJSSDKConfig(response.Ticket, input.Url);
}
///
/// 获取模板列表
///
[DisplayName("获取模板列表")]
public async Task GetMessageTemplateList()
{
var accessToken = await GetCgibinToken();
var reqTemplate = new CgibinTemplateGetAllPrivateTemplateRequest()
{
AccessToken = accessToken
};
var resTemplate = await _wechatApiClient.ExecuteCgibinTemplateGetAllPrivateTemplateAsync(reqTemplate);
if (resTemplate.ErrorCode != (int)WechatReturnCodeEnum.请求成功)
throw Oops.Oh(resTemplate.ErrorMessage + " " + resTemplate.ErrorCode);
return resTemplate.TemplateList;
}
///
/// 发送模板消息
///
///
///
[DisplayName("发送模板消息")]
public async Task SendTemplateMessage(MessageTemplateSendInput input)
{
var dataInfo = input.Data.ToDictionary(k => k.Key, k => k.Value);
var messageData = new Dictionary();
foreach (var item in dataInfo)
{
messageData.Add(item.Key, new CgibinMessageTemplateSendRequest.Types.DataItem() { Value = "" + item.Value.Value.ToString() + "" });
}
var accessToken = await GetCgibinToken();
var reqMessage = new CgibinMessageTemplateSendRequest()
{
AccessToken = accessToken,
TemplateId = input.TemplateId,
ToUserOpenId = input.ToUserOpenId,
Url = input.Url,
MiniProgram = new CgibinMessageTemplateSendRequest.Types.MiniProgram
{
AppId = _wechatApiClientFactory._wechatOptions.WechatAppId,
PagePath = input.MiniProgramPagePath,
},
Data = messageData
};
var resMessage = await _wechatApiClient.ExecuteCgibinMessageTemplateSendAsync(reqMessage);
return resMessage;
}
///
/// 删除模板
///
///
///
[ApiDescriptionSettings(Name = "DeleteMessageTemplate"), HttpPost]
[DisplayName("删除模板")]
public async Task DeleteMessageTemplate(DeleteMessageTemplateInput input)
{
var accessToken = await GetCgibinToken();
var reqMessage = new CgibinTemplateDeletePrivateTemplateRequest()
{
AccessToken = accessToken,
TemplateId = input.TemplateId
};
var resTemplate = await _wechatApiClient.ExecuteCgibinTemplateDeletePrivateTemplateAsync(reqMessage);
return resTemplate;
}
///
/// 获取Access_token
///
private async Task GetCgibinToken()
{
var reqCgibinToken = new CgibinTokenRequest();
var resCgibinToken = await _wechatApiClient.ExecuteCgibinTokenAsync(reqCgibinToken);
if (resCgibinToken.ErrorCode != (int)WechatReturnCodeEnum.请求成功)
throw Oops.Oh(resCgibinToken.ErrorMessage + " " + resCgibinToken.ErrorCode);
return resCgibinToken.AccessToken;
}
}