Senparc.Weixin.MP.MVC Senparc.Weixin.WxOpen
"SenparcWeixinSetting": { //微信全局 "IsDebug": true, //公衆號 "Token": "#Token#", "EncodingAESKey": "#EncodingAESKey#", "WeixinAppId": "#WeixinAppId#", "WeixinAppSecret": "#WeixinAppSecret#", "Items": { "小程序1": { "WxOpenAppId": "#WxOpenAppId#", "WxOpenAppSecret": "#WxOpenAppSecret#", "WxOpenToken": "#WxOpenToken#", "WxOpenEncodingAESKey": "#WxOpenEncodingAESKey#" }, "小程B": { "WxOpenAppId": "#WxOpenAppId#", "WxOpenAppSecret": "#WxOpenAppSecret#", "WxOpenToken": "#WxOpenToken#", "WxOpenEncodingAESKey": "#WxOpenEncodingAESKey#" } } }
services.AddSenparcGlobalServices(Configuration)
.AddSenparcWeixinServices(Configuration);
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IOptions<SenparcSetting> senparcSetting, IOptions<SenparcWeixinSetting> senparcWeixinSetting) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseSwaggerUIV2(); } app.UseStaticHttpContext(); app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); #region 公衆號,小程序 IRegisterService register = RegisterService.Start(env, senparcSetting.Value).UseSenparcGlobal(false, null); register.UseSenparcWeixin(senparcWeixinSetting.Value, senparcSetting.Value); //配置 var bInfo = senparcWeixinSetting.Value.Items["appid"]; AccessTokenContainer.RegisterAsync(bInfo.WxOpenAppId, bInfo.WxOpenAppSecret, bInfo.WxOpenAppId); //公衆號 AccessTokenContainer.RegisterAsync(senparcWeixinSetting.Value.WeixinAppId, senparcWeixinSetting.Value.WeixinAppSecret, senparcWeixinSetting.Value.WeixinAppId); #endregion }
#region static /// <summary> /// 當前小程序的AppId /// </summary> public static readonly string AppId = Senparc.Weixin.Config.SenparcWeixinSetting.Items["appid"].WxOpenAppId; /// <summary> /// /// </summary> public static readonly string Token = Senparc.Weixin.Config.SenparcWeixinSetting.Items[AppId].WxOpenToken; /// <summary> /// /// </summary> public static readonly string EncodingAESKey = Senparc.Weixin.Config.SenparcWeixinSetting.Items[AppId].WxOpenEncodingAESKey; #endregion
[HttpGet] [ActionName("Index")] public ActionResult Get(PostModel postModel, string echostr) { if (CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token)) { return Content(echostr); //返回隨機字符串則表示驗證經過 } else { return Content("failed:" + postModel.Signature + "," + Senparc.Weixin.MP.CheckSignature.GetSignature(postModel.Timestamp, postModel.Nonce, Token) + "。" + "若是你在瀏覽器中看到這句話,說明此地址能夠被做爲微信小程序後臺的Url,請注意保持Token一致。1"); } }
[HttpPost] [ActionName("Index")] public ActionResult Post(PostModel postModel) { if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token)) { return Content("參數錯誤!"); } try { postModel.Token = Token;//根據本身後臺的設置保持一致 postModel.EncodingAESKey = EncodingAESKey;//根據本身後臺的設置保持一致 postModel.AppId = AppId;//根據本身後臺的設置保持一致(必須提供) var maxRecordCount = 10; var messageHandler = new CustomWxOpenMessageHandler(Request.GetRequestMemoryStream(), postModel, maxRecordCount); messageHandler.Execute();//執行微信處理過程(關鍵) var result = new FixWeixinBugWeixinResult(messageHandler); return result; } catch (Exception ex) { Logger.Info("異常消息:" + ex.Message); return Content(""); } }
public CustomWxOpenMessageHandler(Stream inputStream, PostModel postModel, int maxRecordCount = 0) : base(inputStream, postModel, maxRecordCount) { //這裏設置僅用於測試,實際開發能夠在外部更全局的地方設置, //好比MessageHandler<MessageContext>.GlobalGlobalMessageContext.ExpireMinutes = 3。 GlobalMessageContext.ExpireMinutes = 3; if (!string.IsNullOrEmpty(postModel.AppId)) { appId = postModel.AppId;//經過第三方開放平臺發送過來的請求 } //在指定條件下,不使用消息去重 base.OmitRepeatedMessageFunc = requestMessage => { var textRequestMessage = requestMessage as RequestMessageText; if (textRequestMessage != null && textRequestMessage.Content == "容錯") { return false; } return true; }; }
public override IResponseMessageBase OnEvent_UserEnterTempSessionRequest(RequestMessageEvent_UserEnterTempSession requestMessage) { //進入客服 var msg = @"歡迎您!這條消息來自服務器"; Senparc.Weixin.WxOpen.AdvancedAPIs.CustomApi.SendText(appId, OpenId, msg); return DefaultResponseMessage(requestMessage); }
public override IResponseMessageBase OnImageRequest(RequestMessageImage requestMessage) { //發來圖片,進行處理 Task.Factory.StartNew(async () => { await Senparc.Weixin.WxOpen.AdvancedAPIs.CustomApi.SendTextAsync(appId, OpenId, "剛纔您發送了這張圖片:"); await Senparc.Weixin.WxOpen.AdvancedAPIs.CustomApi.SendImageAsync(appId, OpenId, requestMessage.MediaId); }); return DefaultResponseMessage(requestMessage); }
public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage) { if (contentUpper == "1") { var uploadResult = Senparc.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryMedia(appId, UploadMediaFileType.image, ServerUtility.ContentRootMapPath("~/wwwroot/imgs/fwh.jpg")); Senparc.Weixin.WxOpen.AdvancedAPIs.CustomApi.SendImage(appId, OpenId, uploadResult.media_id); } else { var msg = "親,回覆「1」,關注服務號。"; Senparc.Weixin.WxOpen.AdvancedAPIs.CustomApi.SendText(appId, OpenId, msg); } return new SuccessResponseMessage(); }
public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage) { return new SuccessResponseMessage(); }
做者:Dylangit
公衆號:dotNET名人堂(sharecore)github
微信:tangguo_9669小程序
QQ:.NET Core 技術交流(18362376)微信小程序
出處:https://www.cnblogs.com/hailang8/api