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 GetWeixinUser()
 {
     try
     {
         WxBaseConfigEntity weixin = config.GetDefaultConfig();
         AccessTokenResult  token  = AccessTokenContainer.GetAccessTokenResult(weixin.AppId, true);
         if (token.errcode != Senparc.Weixin.ReturnCode.请求成功)
         {
             return(Error("更新失败,获取token失败:" + token.errmsg));
         }
         //更新时间,作为一个标记
         DateTime updateTime = DateTime.Now;
         try
         {
             string ret = SysPersonFun(0, weixin.uuId, token.access_token, "", updateTime);
             //查找此次为同步到的,则为已经跑路的
             IList <UsersEntity> userlist = app.GetList2(updateTime);
             if (userlist != null && userlist.Count > 0)
             {
                 for (int i = 0; i < userlist.Count; i++)
                 {
                     userlist[i].Status = 2;
                     app.SubmitForm(userlist[i], userlist[i].OpenId);
                 }
             }
             return(Success("更新成功"));
         }
         catch (Exception ex)
         {
             return(Error("更新失败:" + ex.Message));
         }
     }
     catch (Exception ex)
     {
         return(Error("更新失败:" + ex.Message));
     }
 }
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
        }
Exemplo n.º 4
0
        public ActionResult SubmitForm(WxMenu entity, string keyValue)
        {
            try
            {
                MenuManage         mMrg   = new MenuManage();
                WxBaseConfigEntity weixin = app.GetDefaultConfig();
                AccessTokenResult  token  = AccessTokenContainer.GetAccessTokenResult(weixin.AppId);
                if (token.errcode != Senparc.Weixin.ReturnCode.请求成功)
                {
                    return(Error(token.errmsg));
                }

                //重新整理按钮信息
                ButtonGroup          bg      = new ButtonGroup();
                IList <BaseButton>   topList = new List <BaseButton>();
                IList <SingleButton> subList = new List <SingleButton>();
                #region 菜单设置
                for (int i = 0; i < 3; i++)
                {
                    string txtName = (string)ForeachClass.GetValue <WxMenu>(entity, "Top" + (i + 1));
                    string txtKey  = (string)ForeachClass.GetValue <WxMenu>(entity, "Top" + (i + 1) + "Key");
                    string txtUrl  = (string)ForeachClass.GetValue <WxMenu>(entity, "Top" + (i + 1) + "Url");
                    if (string.IsNullOrEmpty(txtName))
                    {
                        // 如果名称为空,则忽略该菜单,以及下面的子菜单
                        continue;
                    }
                    subList = new List <SingleButton>();
                    string txtSubName;
                    string txtSubKey;
                    string txtSubUrl;
                    for (int j = 0; j < 5; j++)
                    {
                        #region 子菜单的设置
                        txtSubName = (string)ForeachClass.GetValue <WxMenu>(entity, "Menu" + (i + 1) + (j + 1));
                        txtSubKey  = (string)ForeachClass.GetValue <WxMenu>(entity, "Menu" + (i + 1) + (j + 1) + "Key");
                        txtSubUrl  = (string)ForeachClass.GetValue <WxMenu>(entity, "Menu" + (i + 1) + (j + 1) + "Url");
                        if (string.IsNullOrEmpty(txtSubName))
                        {
                            continue;
                        }

                        if (!string.IsNullOrEmpty(txtSubUrl))
                        {
                            SingleViewButton sub = new SingleViewButton();
                            sub.name = txtSubName.Trim();
                            sub.url  = Utils.UrlDecode(txtSubUrl.Trim());
                            subList.Add(sub);
                        }
                        else if (!string.IsNullOrEmpty(txtSubKey))
                        {
                            SingleClickButton sub = new SingleClickButton();
                            sub.name = txtSubName.Trim();
                            sub.key  = txtSubKey.Trim();
                            subList.Add(sub);
                        }
                        else
                        {
                            //报错 :子菜单必须有key和name
                            return(Error("二级菜单的名称和key或者url必填!"));

                            ;
                        }
                        #endregion
                    } //子菜单循环结束


                    if (subList != null && subList.Count > 0)
                    {
                        //有子菜单, 该一级菜单是SubButton
                        if (subList.Count < 1)
                        {
                            return(Error("子菜单个数为2~5个!"));
                        }
                        SubButton topButton = new SubButton(Utils.CutString(txtName.Trim(), 16));
                        topButton.sub_button.AddRange(subList);
                        topList.Add(topButton);
                    }
                    else
                    {
                        // 无子菜单
                        if (string.IsNullOrEmpty(txtKey) && string.IsNullOrEmpty(txtUrl))
                        {
                            return(Error("若无子菜单,必须填写key或者url值!"));
                        }

                        if (!string.IsNullOrEmpty(txtUrl))
                        {  //view 页面跳转
                            SingleViewButton topSingleButton = new SingleViewButton();
                            topSingleButton.name = txtName.Trim();
                            topSingleButton.url  = Utils.UrlDecode(txtUrl.Trim());
                            topList.Add(topSingleButton);
                        }
                        else if (!string.IsNullOrEmpty(txtKey))
                        {
                            SingleClickButton topSingleButton = new SingleClickButton();
                            topSingleButton.name = txtName.Trim();
                            topSingleButton.key  = txtKey.Trim();
                            topList.Add(topSingleButton);
                        }
                    }
                }
                #endregion

                bg.button.AddRange(topList);

                var result = mMrg.CreateMenu(token.access_token, bg);
                return(Success("菜单提交成功!"));
            }
            catch (Exception ex)
            {
                return(Error("报错:" + ex.Message));
            }
        }