Exemple #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                switch (helper.GetParam("action").ToLower())
                {
                case "getnewcount":
                    int count = db.count <Wlniao.WeChat.Model.Fans>("Subscribe=1 and IsNewFans=1");
                    if (count > 0)
                    {
                        helper.Response(string.Format("document.write('<span class=\"label label-important tip-bottom\" title=\"{0} 位新增订阅者\">{0}</span>');", count));
                    }
                    else
                    {
                        this.Response.ContentType = "application/x-javascript";
                        this.Response.Write("document.write('');");
                        this.Response.End();
                    }
                    break;

                case "setnickname":
                    Wlniao.WeChat.Model.Fans fansNickName = Wlniao.WeChat.Model.Fans.findByField("StrGuid", helper.GetParam("Guid"));
                    if (fansNickName == null)
                    {
                        helper.Result.Add("Sorry,要更新的订阅者不存在或已删除!");
                    }
                    else
                    {
                        fansNickName.NickName = helper.GetParam("NickName");
                    }
                    if (fansNickName != null && fansNickName.Id > 0)
                    {
                        try
                        {
                            fansNickName.update("NickName");
                        }
                        catch
                        {
                            helper.Result.Add("Sorry,更新订阅者信息失败!");
                        }
                    }
                    helper.ResponseResult();
                    break;

                case "setallowtest":
                    Wlniao.WeChat.Model.Fans fansAllowtest = Wlniao.WeChat.Model.Fans.findByField("StrGuid", helper.GetParam("Guid"));
                    if (fansAllowtest == null)
                    {
                        helper.Result.Add("Sorry,要更新的订阅者不存在或已删除!");
                    }
                    else
                    {
                        try
                        {
                            fansAllowtest.AllowTest = Convert.ToInt32(helper.GetParam("AllowTest"));
                        }
                        catch { }
                    }
                    if (fansAllowtest != null && fansAllowtest.Id > 0)
                    {
                        try
                        {
                            fansAllowtest.update("AllowTest");
                        }
                        catch
                        {
                            helper.Result.Add("Sorry,更新订阅者信息失败!");
                        }
                    }
                    helper.ResponseResult();
                    break;

                case "getlist":
                    int pageIndex = 0;
                    int pageSize  = int.MaxValue;
                    try
                    {
                        pageIndex = int.Parse(helper.GetParam("pageIndex"));
                        pageSize  = int.Parse(helper.GetParam("pageSize"));
                    }
                    catch { }
                    string where = "Subscribe=1";
                    if (!string.IsNullOrEmpty(helper.GetParam("key")))
                    {
                        where += " and NickName like'%" + helper.GetParam("key") + "%'";
                    }
                    System.DataPage <Wlniao.WeChat.Model.Fans> items = db.findPage <Wlniao.WeChat.Model.Fans>(where, pageIndex, pageSize);

                    try
                    {
                        foreach (Wlniao.WeChat.Model.Fans fans in items.Results)
                        {
                            try
                            {
                                fans.Sid = string.IsNullOrEmpty(fans.Sid) ? "" : "已绑定";
                            }
                            catch { }
                        }
                    }
                    catch { }
                    List <Wlniao.WeChat.Model.Fans> list = null;
                    try
                    {
                        //删除新订阅标记
                        list = db.findPage <Wlniao.WeChat.Model.Fans>("IsNewFans=1", 0, int.MaxValue).Results;
                        foreach (Wlniao.WeChat.Model.Fans fans in list)
                        {
                            fans.IsNewFans = 0;
                            fans.update("IsNewFans");
                        }
                    }
                    catch { };
                    list = items.Results;
                    if (list == null)
                    {
                        list = new List <Wlniao.WeChat.Model.Fans>();
                    }
                    helper.Response("{total:" + items.RecordCount + ",data:" + Json.ToStringList(items.Results) + "}");
                    break;
                }
            }
        }
Exemple #2
0
        void WeChatApi_Load(object sender, EventArgs e)
        {
            Response.Clear();
            if (string.IsNullOrEmpty(Request.QueryString["echostr"]))
            {
                string toUser   = Request.QueryString["toUser"];
                string fromUser = Request.QueryString["fromUser"];
                string MsgType  = Request.QueryString["MsgType"];
                string Event    = Request.QueryString["Event"];
                string Content  = Request.QueryString["Content"];
                string MsgId    = "";
                try
                {
                    //声明一个XMLDoc文档对象,LOAD()xml字符串
                    if (string.IsNullOrEmpty(Content))
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(new StreamReader(Request.InputStream).ReadToEnd());
                        toUser   = doc.GetElementsByTagName("ToUserName")[0].InnerText;
                        fromUser = doc.GetElementsByTagName("FromUserName")[0].InnerText;
                        MsgType  = doc.GetElementsByTagName("MsgType")[0].InnerText;
                        try
                        {
                            MsgId = doc.GetElementsByTagName("MsgId")[0].InnerText;
                        }
                        catch { }
                        try
                        {
                            Content = doc.GetElementsByTagName("Content")[0].InnerText;
                        }
                        catch { }
                        try
                        {
                            Event = doc.GetElementsByTagName("Event")[0].InnerText;
                            if (string.IsNullOrEmpty(Content) && !string.IsNullOrEmpty(Event))
                            {
                                Content = Event;
                            }
                        }
                        catch { }
                    }

                    Wlniao.WeChat.Model.Fans fans = Wlniao.WeChat.BLL.Fans.Check(fromUser, Content);
                    switch (MsgType.ToLower())
                    {
                    case "event":
                    case "text":
                        Wlniao.WeChat.Model.Rules rule = null;
                        string msgArgs = "";
                        try
                        {
                            rule = Wlniao.WeChat.BLL.Rules.GetRule(Content, fromUser);
                        }
                        catch { }
                        if (rule != null)
                        {
                            if (Content.StartsWith(rule.GoOnCmd))
                            {
                                msgArgs = Content.Substring(rule.GoOnCmd.Length).Trim();
                            }
                            Wlniao.WeChat.BLL.Fans.SetSession(fromUser, rule.GoOnCmd, rule.DoMethod, msgArgs, rule.CallBackText);
                            if (!string.IsNullOrEmpty(rule.DoMethod))
                            {
                                rule.ReContent = RunMethod(rule.DoMethod, fromUser, toUser, Content, msgArgs);
                            }
                            else if (string.IsNullOrEmpty(rule.ReContent))
                            {
                                string where = "RuleGuid='" + rule.Guid + "'";
                                if (fans.AllowTest == 1)
                                {
                                    where += " and (ContentStatus='normal' or ContentStatus='test')";
                                }
                                else
                                {
                                    where += " and ContentStatus='normal'";
                                }

                                List <Wlniao.WeChat.Model.RuleContent> listAll     = Wlniao.WeChat.Model.RuleContent.find(where + " order by LastStick desc").list();
                                List <Wlniao.WeChat.Model.RuleContent> listText    = Wlniao.WeChat.Model.RuleContent.find(where + " and ContentType='text' order by LastStick desc").list();
                                List <Wlniao.WeChat.Model.RuleContent> listPicText = Wlniao.WeChat.Model.RuleContent.find(where + " and ContentType='pictext' order by LastStick desc").list();
                                List <Wlniao.WeChat.Model.RuleContent> listMusic   = Wlniao.WeChat.Model.RuleContent.find(where + " and ContentType='music' order by LastStick desc").list();
                                if (rule.SendMode == "sendgroup" && listPicText != null && listPicText.Count > 0)
                                {
                                    rule.ReContent = ResponsePicTextMsg(fromUser, toUser, listPicText);
                                }
                                else if (listAll.Count > 0)
                                {
                                    int i = 0;
                                    if (rule.SendMode == "sendrandom")
                                    {
                                        i = new Random().Next(0, listAll.Count);
                                    }
                                    if (listAll[i].ContentType == "text")
                                    {
                                        rule.ReContent = listAll[i].TextContent;
                                        try
                                        {
                                            //更新推送次数
                                            listAll[i].PushCount++;
                                            listAll[i].update("PushCount");
                                        }
                                        catch { }
                                    }
                                    else if (listAll[i].ContentType == "music")
                                    {
                                        rule.ReContent = ResponseMusicMsg(fromUser, toUser, listAll[i].Title, listAll[i].TextContent, listAll[i].MusicUrl, listAll[i].MusicUrl);
                                        try
                                        {
                                            //更新推送次数
                                            listAll[i].PushCount++;
                                            listAll[i].update("PushCount");
                                        }
                                        catch { }
                                    }
                                    else if (listAll[i].ContentType == "pictext")
                                    {
                                        List <Wlniao.WeChat.Model.RuleContent> listTemp = new List <Model.RuleContent>();
                                        listTemp.Add(listAll[i]);
                                        rule.ReContent = ResponsePicTextMsg(fromUser, toUser, listTemp);
                                    }
                                }
                            }
                            Response.Write(ResponseTextMsg(fromUser, toUser, rule.ReContent));
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(fans.DoMethod) || fans.LastCmdTime < DateTools.GetNow().AddSeconds(0 - BLL.Rules.SessionTimeOut))
                            {
                                Response.Write(ResponseTextMsg(fromUser, toUser, RunDefaultMethod(fromUser, toUser, Content)));
                            }
                            else
                            {
                                Response.Write(ResponseTextMsg(fromUser, toUser, RunMethod(fans.DoMethod, fromUser, toUser, (fans.GoOnCmd + BLL.Rules.Separation[0] + Content).Trim(), Content)));
                            }
                        }
                        break;

                    default:
                        break;
                    }
                }
                catch { }
            }
            else if (CheckSignature(Context))
            {
                Response.Write(Request.QueryString["echostr"]);
            }
            Response.End();
        }