Exemplo n.º 1
0
        public static void SendTemplateMessage(string openId, string templateId, object data, string url = null)
        {
            WxBaseConfigApp    app    = new WxBaseConfigApp();
            WxBaseConfigEntity weixin = app.GetDefaultConfig();
            AccessTokenResult  token  = AccessTokenContainer.GetAccessTokenResult(weixin.AppId, true);

            if (token.errcode != Senparc.Weixin.ReturnCode.请求成功)
            {
                throw new Exception(token.errmsg);
            }
            if (!string.IsNullOrEmpty(templateId))
            {
                //获取AccessToken
                try
                {
                    var result = TemplateApi.SendTemplateMessage(token.access_token, openId, templateId, url, data);
                    //new BLL.wx_message_template_sendlog().AddLog(AppId, templateId, temp_title, openId, string.Format("【微信公众号发送模板消息接口调用】返回参数:errcode:{0},erromsg:{1},misgid:{2}", result.errcode, result.errmsg, result.msgid));
                }
                catch (Exception ex)
                {
                    throw new Exception("微信模板消息:" + ex.Message);
                    //new BLL.wx_message_template_sendlog().AddLog(AppId, templateId, temp_title, openId, string.Format("【微信公众号发送模板消息接口调用】错误:{0}", ex.Message));
                }
            }
        }
Exemplo n.º 2
0
        public ActionResult Get(PostModel postModel, string echostr)
        {
            string          Token = "";//与微信公众账号后台的Token设置保持一致,区分大小写。
            WxBaseConfigApp app   = new WxBaseConfigApp();
            string          apiid = Request["apiid"];

            if (string.IsNullOrEmpty(apiid))
            {
                Response.Output.Write("参数非法");
            }
            Token = app.GetWeiXinToken(apiid);
            if (Token == null || string.IsNullOrEmpty(Token))
            {
                Response.Output.Write("不存在该微信号或账号已过期或已被禁用!");
                Response.End();
            }
            //get method - 仅在微信后台填写URL验证时触发
            if (CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token))
            {
                return(Content(echostr)); //返回随机字符串则表示验证通过
            }
            else
            {
                return(Content("failed:" + postModel.Signature + "," + CheckSignature.GetSignature(postModel.Timestamp, postModel.Nonce, Token) + "。" +
                               "如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。"));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 启动应用程序
        /// </summary>
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            // 开启Log4Net日志配置
            LogFactory.LogFactoryConfig();

            //注册定时作业任务
            SystemTask.PublicSystemTask t = new SystemTask.PublicSystemTask();
            t.StartUp();

#if 使用RegisterServices方式注册微信
            //设置全局 Debug 状态
            var isGLobalDebug = false;
            //全局设置参数,将被储存到 Senparc.CO2NET.Config.SenparcSetting
            var senparcSetting = SenparcSetting.BuildFromWebConfig(isGLobalDebug);
            //也可以通过这种方法在程序任意位置设置全局 Debug 状态:
            //Senparc.CO2NET.Config.IsDebug = isGLobalDebug;

            //CO2NET 全局注册,必须!!
            IRegisterService register = RegisterService.Start(senparcSetting).UseSenparcGlobal();


            //--------注册公众号--------------
            //设置微信 Debug 状态
            var isWeixinDebug = true;
            //全局设置参数,将被储存到 Senparc.Weixin.Config.SenparcWeixinSetting
            var senparcWeixinSetting = SenparcWeixinSetting.BuildFromWebConfig(isWeixinDebug);
            //微信全局注册,必须!!
            register.UseSenparcWeixin(senparcWeixinSetting, senparcSetting).RegisterMpAccount(senparcWeixinSetting, "我的公众号名称");
            //可以在程序任意地方注册公众号或小程序:
            //AccessTokenContainer.Register(weixin.T_AppId, weixin.T_AppSecret, weixin.T_WxName);
            WxBaseConfigApp app    = new WxBaseConfigApp();
            var             weixin = app.GetDefaultConfig();
            AccessTokenContainer.RegisterAsync(weixin.AppId, weixin.AppSecret, weixin.WxName);
#endif
        }