Beispiel #1
0
        /// <summary>
        /// 根据微信SiteCode获取相应的菜单
        /// </summary>
        /// <param name="strSiteCode"></param>
        /// <returns></returns>
        public List <Menu> GetWeiXinMenuBySiteCode(string strSiteCode)
        {
            List <Menu> res = null;
            WXConfigDAL dal = new WXConfigDAL();
            //WXConfig wxConfig = dal.GetWXConfigByID(strSiteCode);
            WXConfig wxConfig = dal.GetWXConfigBySiteCode(strSiteCode);

            if (wxConfig != null)
            {
                if (!string.IsNullOrEmpty(wxConfig.ID))
                {
                    StringBuilder sql = new StringBuilder();
                    sql.AppendFormat("SELECT * FROM [{0}]", TABLE_NAME);
                    sql.AppendFormat(" WHERE WXConfigID=@WXConfigID");
                    sql.AppendFormat(" AND Enabled=1");
                    IList <System.Data.SqlClient.SqlParameter> paras = new List <System.Data.SqlClient.SqlParameter>()
                    {
                        new System.Data.SqlClient.SqlParameter("@WXConfigID", wxConfig.ID)
                    };
                    DataSet ds = DbHelperSQL.Query(sql.ToString(), paras.ToArray());
                    res = ds.ConvertToList <Menu>();
                }
            }
            return(res);
        }
Beispiel #2
0
        /// <summary>
        /// 根据站点配置获取微信配置
        /// </summary>
        /// <param name="siteCode"></param>
        /// <returns></returns>
        public WXConfig GetWXConfigBySiteCode(string siteCode)
        {
            WXConfig res = null;

            if (!string.IsNullOrEmpty(siteCode))
            {
                string sql = "SELECT * FROM WX_WXConfig WHERE SiteCode = @SiteCode AND [State] = 1 ";
                IList <System.Data.SqlClient.SqlParameter> paras = new List <System.Data.SqlClient.SqlParameter>()
                {
                    new System.Data.SqlClient.SqlParameter("@SiteCode", siteCode)
                };
                DataSet ds = DbHelperSQL.Query(sql, paras.ToArray());
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    res = new WXConfig()
                    {
                        ID             = dr.GetColumnValue("ID", string.Empty),
                        SiteCode       = dr.GetColumnValue("SiteCode", string.Empty),
                        WXID           = dr.GetColumnValue("WXID", string.Empty),
                        WXName         = dr.GetColumnValue("WXName", string.Empty),
                        WXToken        = dr.GetColumnValue("WXToken", string.Empty),
                        WXAppID        = dr.GetColumnValue("WXAppID", string.Empty),
                        WXAppSecret    = dr.GetColumnValue("WXAppSecret", string.Empty),
                        State          = dr.GetColumnValue("State", 1),
                        EncryptMode    = dr.GetColumnValue("EncryptMode", 0),
                        EncodingAESKey = dr.GetColumnValue("EncodingAESKey", string.Empty)
                    };
                }
            }
            return(res);
        }
Beispiel #3
0
        public void ProcessRequest(HttpContext context)
        {
            siteCode = context.Request.QueryString["id"];

            if (!string.IsNullOrEmpty(siteCode))
            {
                WXConfigDAL dal = new WXConfigDAL();
                wxConfig = dal.GetWXConfigBySiteCode(siteCode);
                if (wxConfig != null)
                {
                    WeiXinCore.Models.WeiXinConfig weixinConfig = new WeiXinCore.Models.WeiXinConfig()
                    {
                        ID        = wxConfig.WXID,
                        Name      = wxConfig.WXName,
                        Token     = wxConfig.WXToken,
                        AppId     = wxConfig.WXAppID,
                        AppSecret = wxConfig.WXAppSecret
                    };
                    weixin = new WeiXinCore.WeiXin(weixinConfig);
                    weixin.OnMsgReceiving     = MsgReceiving;
                    weixin.OnTextMsgReceived  = TextMsgReceive;
                    weixin.OnImageMsgReceived = ImageMsgReceive;
                    weixin.OnSubscribeEvent   = SubscribeProcess;
                    weixin.OnMenuClick        = MenuClickProcess;

                    //MenuDAL.CreateWeiXinMenu(wxConfig.ID);
                    //string json=WeiXinHelper.GetCustomMenu(weixin.GetAccessToken().Access_Token);
                    //string fileDir = HttpContext.Current.Server.MapPath("/HP_PHOTO/");
                    //if (!Directory.Exists(fileDir))
                    //{
                    //    Directory.CreateDirectory(fileDir);
                    //}
                    //string fileName = weixin.SaveAsMedia("WVNXJOLqpySbvX1T7zGCKYjboBbcv2x4YOgz9e0Avbq_xks5mGujx10avfPQ3ZlW", fileDir);
                    //string file = HttpContext.Current.Server.MapPath("/HP_PHOTO/");
                    //if (!Directory.Exists(file))
                    //{
                    //    Directory.CreateDirectory(file);
                    //}
                    //string tt=weixin.SaveAsMedia("d3VvIIQU7kaxd-NPw8a5qmDPkxLLY1NlBPLmCGNA_40K9DmDN4bbBEBJywfx2JFx",
                    //   file);
                    //List<UserInfo> list= weixin.GetUserInfos();

                    context.Response.ContentType = "text/plain";
                    context.Response.Write(weixin.AcceptMsg());
                    return;
                }
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write("ERROR");
        }
Beispiel #4
0
        public static WXConfig GetWXConfig(string url)
        {
            WXConfig wxConfig = new WXConfig();

            wxConfig.appId     = "wxb8ea569b2499e60f";
            wxConfig.timestamp = DateToUnix();
            wxConfig.nonceStr  = getNoncestr();
            wxConfig.signature = Sign(GetJsApiTicket(), wxConfig.nonceStr, wxConfig.timestamp, url);
            wxConfig.jsApiList = new List <string>();
            wxConfig.jsApiList.Add("onMenuShareTimeline");
            wxConfig.jsApiList.Add("onMenuShareAppMessage");

            return(wxConfig);
        }
Beispiel #5
0
 /// <summary>
 /// 同步微信用户信息
 /// </summary>
 public static void SyncWXUserBySiteCode(string siteCode)
 {
     if (!string.IsNullOrEmpty(siteCode))
     {
         WXConfigDAL dal      = new WXConfigDAL();
         WXConfig    wxConfig = dal.GetWXConfigBySiteCode(siteCode);
         if (wxConfig != null)
         {
             WeiXinCore.Models.WeiXinConfig weixinConfig = new WeiXinCore.Models.WeiXinConfig()
             {
                 ID        = wxConfig.WXID,
                 Name      = wxConfig.WXName,
                 Token     = wxConfig.WXToken,
                 AppId     = wxConfig.WXAppID,
                 AppSecret = wxConfig.WXAppSecret
             };
             WeiXinCore.WeiXin weixin = new WeiXinCore.WeiXin(weixinConfig);
             List <UserInfo>   users  = weixin.GetUserInfos();
             if (users != null)
             {
                 UserDAL dal2 = CreateInstance();
                 dal2.ClearUser(wxConfig.ID);
                 foreach (UserInfo user in users)
                 {
                     User info = new User()
                     {
                         WXConfigID    = wxConfig.ID,
                         OpenID        = user.OpenId,
                         Subscribe     = user.Subscribe,
                         NickName      = user.NickName,
                         Sex           = user.Sex,
                         Language      = user.Language,
                         City          = user.City,
                         Province      = user.Province,
                         Country       = user.Country,
                         HeadImgUrl    = user.Headimgurl,
                         SubscribeTime = user.Subscribe_Time,
                     };
                     dal2.Insert(info);
                 }
             }
         }
     }
 }
Beispiel #6
0
        /// <summary>
        /// 根据wxConfigId创建微信对象
        /// </summary>
        /// <param name="wxConfigId"></param>
        /// <returns></returns>
        public static WeiXinCore.WeiXin CreateWeiXinInstanceBySiteCode(string siteCode)
        {
            WeiXinCore.WeiXin res      = null;
            WXConfigDAL       dal      = new WXConfigDAL();
            WXConfig          wxConfig = dal.GetWXConfigBySiteCode(siteCode);

            if (wxConfig != null)
            {
                WeiXinCore.Models.WeiXinConfig weixinConfig = new WeiXinCore.Models.WeiXinConfig()
                {
                    ID        = wxConfig.WXID,
                    Name      = wxConfig.WXName,
                    Token     = wxConfig.WXToken,
                    AppId     = wxConfig.WXAppID,
                    AppSecret = wxConfig.WXAppSecret
                };
                res = new WeiXinCore.WeiXin(weixinConfig);
            }
            return(res);
        }
Beispiel #7
0
        /// <summary>
        /// 创建微信菜单
        /// </summary>
        /// <param name="siteCode"></param>
        public static void CreateWeiXinMenu(string wxConfigID)
        {
            if (!string.IsNullOrEmpty(wxConfigID))
            {
                WXConfigDAL dal      = new WXConfigDAL();
                WXConfig    wxConfig = dal.GetWXConfigByID(wxConfigID);
                if (wxConfig != null)
                {
                    WeiXinCore.Models.WeiXinConfig weixinConfig = new WeiXinCore.Models.WeiXinConfig()
                    {
                        ID        = wxConfig.WXID,
                        Name      = wxConfig.WXName,
                        Token     = wxConfig.WXToken,
                        AppId     = wxConfig.WXAppID,
                        AppSecret = wxConfig.WXAppSecret
                    };
                    WeiXinCore.WeiXin weixin   = new WeiXinCore.WeiXin(weixinConfig);
                    MenuDAL           dal2     = CreateInstance();
                    List <Menu>       menus    = dal2.GetWeiXinMenu(wxConfig.ID);
                    MenuInfo          menuInfo = new MenuInfo();
                    var buttons = from x in menus
                                  where x.MenuType.ToLower() == "button"
                                  orderby x.OrderNum ascending
                                  select x;
                    foreach (Menu button in buttons)
                    {
                        if (!string.IsNullOrEmpty(button.ButtonType))
                        {
                            switch (button.ButtonType.ToLower())
                            {
                            case "view":
                                string url = button.AccessLink;
                                if (!string.IsNullOrEmpty(button.RedirectScope))
                                {
                                    url = WeiXinHelper.GenerateAuthorizeUrl(weixinConfig.AppId, HttpUtility.UrlEncode(url), button.RedirectScope, button.RedirectState);
                                }
                                menuInfo.Buttons.Add(new MenuViewBtton()
                                {
                                    Name = button.ButtonName,
                                    Type = button.ButtonType,
                                    Url  = url
                                });
                                break;

                            case "click":
                                menuInfo.Buttons.Add(new MenuClickButton()
                                {
                                    Name = button.ButtonName,
                                    Type = button.ButtonType,
                                    Key  = button.ButtonKey
                                });
                                break;

                            default:
                                menuInfo.Buttons.Add(new MenuEventButton()
                                {
                                    Name = button.ButtonName,
                                    Type = button.ButtonType,
                                    Key  = button.ButtonKey
                                });
                                break;
                            }
                        }
                        else
                        {
                            MenuSubButton subButton = new MenuSubButton()
                            {
                                Name = button.ButtonName
                            };
                            var subButtons = from x in menus
                                             where x.MenuType.ToLower() == "sub_button" &&
                                             x.ParentMenuID == button.ID
                                             orderby x.OrderNum ascending
                                             select x;
                            foreach (Menu subButton1 in subButtons)
                            {
                                switch (subButton1.ButtonType.ToLower())
                                {
                                case "view":
                                    string url = subButton1.AccessLink;
                                    if (!string.IsNullOrEmpty(subButton1.RedirectScope))
                                    {
                                        url = WeiXinHelper.GenerateAuthorizeUrl(weixinConfig.AppId, HttpUtility.UrlEncode(url), subButton1.RedirectScope, subButton1.RedirectState);
                                    }
                                    subButton.SubButtons.Add(new MenuViewBtton()
                                    {
                                        Name = subButton1.ButtonName,
                                        Type = subButton1.ButtonType,
                                        Url  = url
                                    });
                                    break;

                                case "click":
                                    subButton.SubButtons.Add(new MenuClickButton()
                                    {
                                        Name = subButton1.ButtonName,
                                        Type = subButton1.ButtonType,
                                        Key  = subButton1.ButtonKey
                                    });
                                    break;

                                default:
                                    subButton.SubButtons.Add(new MenuEventButton()
                                    {
                                        Name = subButton1.ButtonName,
                                        Type = subButton1.ButtonType,
                                        Key  = subButton1.ButtonKey
                                    });
                                    break;
                                }
                            }
                            menuInfo.Buttons.Add(subButton);
                        }
                    }
                    string strJosn = menuInfo.ToJson();
                    weixin.CreateCustomMenu(menuInfo);
                }
            }
        }