/// <summary> /// <![CDATA[上报地理位置]]> /// </summary> /// <param name="context"></param> /// <returns></returns> public virtual Task OnLocation(WeiXinContext context) { if (Options.OnLocationAsync != null) { return(Options.OnLocationAsync(context)); } return(Task.Delay(0)); }
/// <summary> /// <![CDATA[取消关注]]> /// </summary> /// <param name="context"></param> /// <returns></returns> public virtual Task OnUnsubscribe(WeiXinContext context) { if (Options.OnUnsubscribeAsync != null) { return(Options.OnUnsubscribeAsync(context)); } return(Task.Delay(0)); }
public override void ProcessWeiXin(WeiXinContext context) { XElement result = new XElement("xml", new XElement("ToUserName", context.Request.FromUserName), new XElement("FromUserName", context.Request.ToUserName), new XElement("CreateTime", DateTime.Now.GetInt()), new XElement("MsgType", WeiXinMsgType.Text.ToString().ToLower()), new XElement("Content", "为什么不爱我?")); context.Response.Write(result); }
public override void ProcessWeiXin(WeiXinContext context) { WeiXinTextMessageEntity requestModel = context.Request.GetRequestModel <WeiXinTextMessageEntity>(); WeiXinTextMessageEntity responseModel = new WeiXinTextMessageEntity { ToUserName = requestModel.FromUserName, Content = string.Format("你请求的是text类型消息!执行的控制器是:{0},实现:{1}", this.GetType().FullName, this.GetType().GetInterface("IWeiXinHandler").FullName), MsgType = requestModel.MsgType }; context.Response.Write(responseModel); }
public override void ProcessWeiXin(WeiXinContext context) { WeiXinImageMessageEntity requestModel = context.Request.GetRequestModel <WeiXinImageMessageEntity>(); WeiXinImageMessageEntity responseModel = new WeiXinImageMessageEntity { ToUserName = requestModel.FromUserName, MsgType = WeiXinMsgType.Image.ToString().ToLower() }; XElement xElement = responseModel.GetXElement(); xElement.Add(new XElement("Image", new XElement("MediaId", requestModel.MediaId))); context.Response.Write(xElement); }
/// <summary> /// /// </summary> /// <param name="app"></param> /// <param name="env"></param> public void Configure(IApplicationBuilder app, IHostingEnvironment env, IOptions <WeixinSetting> settings, IServiceProvider serviceProvider) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } using (var scope = app.ApplicationServices.CreateScope()) { var services = scope.ServiceProvider; try { services.GetRequiredService <AppsettingsUtility>();//生成全局单利 var context = services.GetRequiredService <DbContext>(); context.Database.EnsureCreated(); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred creating the DB."); } } app.UseCors(builder => { builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials(); }); app.UseMiddleware(typeof(ErrorHandlingMiddleware)); //Microsoft.AspNetCore.Authentication.AuthenticationMiddleware //启动配置权限管道 //UseAuthentication方法注册了AuthenticationMiddleware中间件 app.UseAuthentication(); WeiXinContext.RegisterWX(settings, serviceProvider); app.UseSwagger(); app.UseSwaggerUI(options => { //访问swagger options.SwaggerEndpoint("/swagger/v1/swagger.json", "MsSystem API V1"); }); app.UseSession(); app.UseMvc(); }
/// <summary> /// 使用AccessToken进行操作时,如果遇到AccessToken错误的情况,重新获取AccessToken一次,并重试。 /// 使用此方法之前必须在startup注册 WeiXinContext.RegisterWX /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fun"></param> /// <param name="fun"></param> /// <returns></returns> public static T TryCommonApi <T>(Func <string, T> fun, bool retryIfFaild = true) where T : WxJsonResult, new() { var access_token = WeiXinContext.AccessToken; var result = fun(access_token); if (result.ReturnCode == ReturnCode.请求成功) { return(result); } if (result.ReturnCode == ReturnCode.获取access_token时AppSecret错误或者access_token无效) { if (retryIfFaild) { WeiXinContext.ClearAccessToken(); return(TryCommonApi <T>(fun, false)); } } throw new Exception(result.ToString()); }
/// <summary> /// <![CDATA[虚方法,接收消息后处理]]> /// </summary> /// <param name="context"></param> /// <returns></returns> public virtual Task OnRecieve(HttpContext context) { if (Options.OnRecieveAsync != null) { return(Options.OnRecieveAsync(context)); } string strRecieveBody = null;//接收消息 using (System.IO.StreamReader streamReader = new System.IO.StreamReader(context.Request.Body)) { strRecieveBody = streamReader.ReadToEndAsync().GetAwaiter().GetResult(); logger.Info($"接收内容:{strRecieveBody}"); strRecieveBody = Utils.ClearXmlHeader(strRecieveBody); } //反序列化 var recieve = _XmlSerializer.Deserialize(strRecieveBody) as WeiXinMessage; logger.Info($"接收者信息:FromUserName:{recieve.FromUserName} ToUserName:{recieve.ToUserName} MsgType:{recieve.MsgType} Event:{recieve.Event} EventKey:{recieve.EventKey}"); //事件消息 if (recieve.MsgType == Constants.MSG_TYPE.EVENT) { var weiXinContext = new WeiXinContext(recieve, context); var actionName = recieve.Event.ToLower(); actionName = actionName.First().ToString().ToUpper() + actionName.Substring(1); var action = this.GetType().GetMethod($"On{actionName}"); if (action != null) { return((Task)action.Invoke(this, new object[] { weiXinContext })); } } //被动接收消息 else { return(OnRecieveMessage(context)); } return(Task.Delay(0)); }
public static Dictionary <string, object> TryCommonApi(Func <string, Dictionary <string, object> > fun, bool retryIfFaild = true) { var access_token = WeiXinContext.AccessToken; var result = fun(access_token); if (!result.ContainsKey("errcode")) { return(result); } if (result["errcode"].ToString() == (int)ReturnCode.请求成功 + "") { return(result); } if (result["errcode"].ToString() == (int)ReturnCode.获取access_token时AppSecret错误或者access_token无效 + "") { if (retryIfFaild) { WeiXinContext.ClearAccessToken(); return(TryCommonApi(fun, false)); } } throw new Exception(result.ToString()); }
public TextRequest(WeiXinContext weiXin) : base(weiXin) { }
public ValidResponse(WeiXinContext weiXin) : base(weiXin) { }
public ValidCommand(WeiXinContext weiXin, WeixinSettings weixinSettings) : base(weiXin) { _weixinSettings = weixinSettings; }
public NewsResponse(WeiXinContext weiXin) : base(weiXin) { }
public abstract void ProcessWeiXin(WeiXinContext context);
public DataFieldDataSource(OTTCommand command, WeiXinContext weiXin) : base(command, weiXin) { }
public EventRequest(WeiXinContext weiXin) : base(weiXin) { }
public DataSourceBase(OTTCommand command, WeiXinContext weiXin) { _command = command; _weiXin = weiXin; }
public WeiXinRequest(WeiXinContext weiXin) { WeiXin = weiXin; }
public ValidRequest(WeiXinContext weiXin) : base(weiXin) { }
public CommandBase(WeiXinContext weiXin) { this.WeiXin = weiXin; }
public override void ProcessWeiXin(WeiXinContext context) { string userInfo = string.Empty; string nick_name = UtilityAccessToken.GetUserInfo(context.Request.FromUserName); UtilityMenu.DeleteMenu(); string menu = "{" + "\"button\": [" + "{" + "\"name\": \"关于我们\"," + "\"sub_button\": [" + "{" + "\"type\": \"view\"," + "\"name\": \"关于我们\"," + "\"url\": \"http://www.soyisoft.cn/WebUI/pages/a5001.aspx\"" + "}," + "{" + "\"type\": \"view\"," + "\"name\": \"信息中心\"," + "\"url\": \"http://www.soyisoft.cn/WebUI/pages/a1001.aspx\"" + "}" + "]" + "}," + "{" + "\"name\": \"产品方案\"," + "\"sub_button\": [" + "{" + "\"type\": \"view\"," + "\"name\": \"产品列表\"," + "\"url\": \"http://www.soyisoft.cn/WeiXin/Ui/Products/ShowProduct.aspx?shop_id=1\"" + "}," + "{" + "\"type\": \"view\"," + "\"name\": \"店铺列表\"," + "\"url\": \"http://www.soyisoft.cn/WeiXin/Ui/Shop/ShopPreview.htm?shop_id=1\"" + "}," + "{" + "\"type\": \"view\"," + "\"name\": \"活动列表\"," + "\"url\": \"http://www.soyisoft.cn/WeiXin/Ui/Activity/ActViewList.aspx?shop_id=1\"" + "}" + "]" + "}," + "{" + "\"name\": \"沃尔沃\"," + "\"sub_button\": [" + "{" + "\"type\": \"view\"," + "\"name\": \"报修登记\"," + "\"url\": \"http://t.vdis.cn/BI/Ui/Wx/RepairEdit.aspx\"" + "}," + "{" + "\"type\": \"view\"," + "\"name\": \"报修查询\"," + "\"url\": \"http://t.vdis.cn/BI/Ui/Wx/RepairList.aspx\"" + "}," + "{" + "\"type\": \"view\"," + "\"name\": \"经验分享\"," + "\"url\": \"http://t.vdis.cn/BI/Ui/Wx/ShareList.aspx\"" + "}" + "]" + "}" + "]" + "}"; menu = menu.Replace(" ", ""); UtilityMenu.CreateMenu(menu); //context.Response.Write(menu); //s_Log.Info(this.GetType().ToString() + ":" + menu); XElement result = new XElement("xml", new XElement("ToUserName", context.Request.FromUserName), new XElement("FromUserName", context.Request.ToUserName), new XElement("CreateTime", DateTime.Now.GetInt()), new XElement("MsgType", WeiXinMsgType.Text.ToString().ToLower()), new XElement("Content", nick_name + " 您好:欢迎关注索一软件微信订阅号。")); context.Response.Write(result); }
public TextResponse(WeiXinContext weiXin) : base(weiXin) { }
public WeiXinResponse(WeiXinContext weiXin) { WeiXin = weiXin; }