using System.Text; using System.Xml; using JNPF.Common.Extension; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.Extras.Thirdparty.WeChat; using JNPF.Extras.Thirdparty.WeChat.Internal; using JNPF.Logging.Attributes; using JNPF.Message.Entitys.Entity; using JNPF.Systems.Entitys.Permission; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Senparc.Weixin.MP; using SqlSugar; namespace JNPF.Message.Service; /// /// 公众号. /// 版 本:V3.2 /// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com) /// 日 期:2021-06-01. /// [ApiDescriptionSettings(Tag = "Message", Name = "WechatOpen", Order = 240)] [Route("api/message/[controller]")] public class WechatOpenService : IDynamicApiController, ITransient { private readonly ISqlSugarRepository _repository; public WechatOpenService(ISqlSugarRepository repository) { _repository = repository; } /// /// 验证. /// /// 实体对象. /// [HttpGet("token")] [AllowAnonymous] [IgnoreLog] [NonUnify] public dynamic CheckToken([FromQuery] string signature, [FromQuery] string timestamp, [FromQuery] string nonce, [FromQuery] string echostr) { var token = "WEIXINJNPF"; var hashCode = CheckSignature.GetSignature(timestamp, nonce, token); if (hashCode == signature) { Console.WriteLine("验证成功"); return echostr; } else { Console.WriteLine("验证失败"); return string.Empty; } } /// /// 新建. /// /// 实体对象. /// [HttpPost("token")] [AllowAnonymous] [IgnoreLog] //[NonUnify] public async Task Create([FromQuery] string signature, [FromQuery] string timestamp, [FromQuery] string nonce, [FromQuery] string openid) { var input = await GetWechatMPEvent(); var weChatMp = new WeChatMPUtil(); if (weChatMp.CheckToken(signature, timestamp, nonce)) { var messageAccountEntity = await _repository.AsSugarClient().Queryable().FirstAsync(x => x.AppKey == input.ToUserName && x.DeleteMark == null); var wechatUser = new WeChatMPUtil(messageAccountEntity.AppId, messageAccountEntity.AppSecret).GetWeChatUserInfo(openid); if (wechatUser.IsNotEmptyOrNull() && wechatUser.unionid.IsNotEmptyOrNull()) { var socialsUser = await _repository.AsSugarClient().Queryable().FirstAsync(x => x.SocialId == wechatUser.unionid && x.SocialType == "WECHAT_OPEN" && x.DeleteMark == null); var entity = await _repository.GetFirstAsync(x => x.GzhId == input.ToUserName && x.UserId == socialsUser.UserId && x.OpenId == openid); if (entity.IsNullOrEmpty()) { entity = new MessageWechatUserEntity { GzhId = input.ToUserName, UserId = socialsUser.UserId, OpenId = openid, CloseMark = 1 }; await _repository.AsInsertable(entity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync(); } else { entity.CloseMark = input.Event.Equals("subscribe") ? 1 : 0; await _repository.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandHasChangeAsync(); } } return string.Empty; } else { return string.Empty; } } /// /// 获取xml参数. /// /// private async Task GetWechatMPEvent() { var request = App.HttpContext.Request; var buffer = new byte[Convert.ToInt32(request.ContentLength)]; await request.Body.ReadAsync(buffer, 0, buffer.Length); var body = Encoding.UTF8.GetString(buffer); XmlDocument doc = new XmlDocument(); doc.LoadXml(body); var input = new WechatMPEvent(); input.ToUserName = doc.DocumentElement.SelectSingleNode("ToUserName").InnerText.Trim(); input.FromUserName = doc.DocumentElement.SelectSingleNode("FromUserName").InnerText.Trim(); input.Event = doc.DocumentElement.SelectSingleNode("Event").InnerText.Trim(); return input; } }