Пример #1
0
    /// <summary>
    /// 系统事件,如订阅等处理
    /// </summary>
    public void WxEventHandler(M_WxTextMsg reqMod)
    {
        M_WX_User userMod = null;

        errmsg += ",事件:" + reqMod.Event;
        switch (reqMod.Event.ToLower())
        {
        case "subscribe":
            userMod       = api.GetWxUserModel(reqMod.FromUserName);
            userMod.CDate = DateTime.Now;
            userMod.AppId = api.AppId.ID;
            wxuserBll.Insert(userMod);
            M_WxImgMsg msgmod = JsonConvert.DeserializeObject <M_WxImgMsg>(api.AppId.WelStr);
            msgmod.ToUserName   = reqMod.FromUserName;
            msgmod.FromUserName = reqMod.ToUserName;

            int       pid      = DataConvert.CLng(reqMod.EventKey.Replace("qrscene_", ""));
            M_UserAPP uappMod1 = new M_UserAPP();

            M_UserInfo mu1      = new M_UserInfo();
            M_WX_User  wuserMod = new M_WX_User();
            M_Uinfo    mubase   = new M_Uinfo();
            uappMod1 = uappBll.SelModelByOpenID(reqMod.FromUserName, "wechat");
            wuserMod = api.GetWxUserModel(reqMod.FromUserName);
            if (uappMod1 != null)
            {
                mu1 = buser.GetUserByUserID(uappMod1.UserID);
                if (mu1 != null && mu1.UserID > 0)
                {
                    mu1.TrueName = wuserMod.Name;
                    try
                    {
                        string vpath = SiteConfig.SiteOption.UploadDir + "User/" + mu1.UserName + mu1.UserID + "/wxheadimg.jpg";
                        HttpHelper.DownFile(wuserMod.HeadImgUrl, vpath);
                        mu1.UserFace = vpath;
                    }
                    catch (Exception ex)
                    {
                        ZLLog.L("微信更新头像错误,原因:" + ex.Message);
                    }

                    mu1.GroupID = 1;
                    buser.UpdateByID(mu1);
                }
                else
                {
                    mu1.UserName = "******" + DateTime.Now.ToString("yyyyMMddHHmmss") + function.GetRandomString(2).ToLower();
                    mu1.UserPwd  = StringHelper.MD5(function.GetRandomString(6));
                    mu1.Email    = function.GetRandomString(10) + "@wx.com";
                    mu1.Question = function.GetRandomString(5);
                    mu1.Answer   = function.GetRandomString(5);
                    mu1.TrueName = wuserMod.Name;
                    mu1.GroupID  = 1;
                    try
                    {
                        string vpath = SiteConfig.SiteOption.UploadDir + "User/" + mu1.UserName + mu1.UserID + "/wxheadimg.jpg";
                        HttpHelper.DownFile(wuserMod.HeadImgUrl, vpath);
                        mu1.UserFace = vpath;
                    }
                    catch (Exception ex)
                    {
                        ZLLog.L("微信更新头像错误,原因:" + ex.Message);
                    }

                    mu1.UserID = buser.Add(mu1);

                    uappMod1.UserID     = mu1.UserID;
                    uappMod1.SourcePlat = "wechat";
                    uappMod1.OpenID     = reqMod.FromUserName;
                    uappBll.UpdateByID(uappMod1);
                    mubase.UserId = mu1.UserID;
                    buser.AddBase(mubase);
                }
            }
            else
            {
                uappMod1     = new M_UserAPP();
                mu1.UserName = "******" + DateTime.Now.ToString("yyyyMMddHHmmss") + function.GetRandomString(2).ToLower();
                mu1.UserPwd  = StringHelper.MD5(function.GetRandomString(6));
                mu1.Email    = function.GetRandomString(10) + "@wx.com";
                mu1.Question = function.GetRandomString(5);
                mu1.Answer   = function.GetRandomString(5);
                mu1.TrueName = wuserMod.Name;
                mu1.GroupID  = 1;
                try
                {
                    string vpath = SiteConfig.SiteOption.UploadDir + "User/" + mu1.UserName + mu1.UserID + "/wxheadimg.jpg";
                    HttpHelper.DownFile(wuserMod.HeadImgUrl, vpath);
                    mu1.UserFace = vpath;
                }
                catch (Exception ex)
                {
                    ZLLog.L("微信更新头像错误,原因:" + ex.Message);
                }

                mu1.UserID = buser.Add(mu1);

                uappMod1.UserID     = mu1.UserID;
                uappMod1.SourcePlat = "wechat";
                uappMod1.OpenID     = reqMod.FromUserName;
                uappBll.Insert(uappMod1);
                mubase.UserId = mu1.UserID;
                buser.AddBase(mubase);
            }

            if (string.IsNullOrEmpty(msgmod.Articles[0].PicUrl))     //如未设置图片则以纯文本格式发送,纯文本支持内置超链接
            {
                M_WxTextMsg textMod = ImageToText(msgmod);
                RepToClient(textMod.ToXML());
            }
            else
            {
                RepToClient(msgmod.ToXML());
            }
            //关注时发送多条信息
            if (pid > 0)
            {
                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(SendMsg), reqMod);
            }
            //关注时发送多条信息
            //System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(SendMsg), reqMod);
            break;

        case "unsubscribe":
            wxuserBll.DelByOpenid(api.AppId.ID, reqMod.FromUserName);
            break;

        case "location":    //用户进入公众号(包含定位信息)
            break;

        case "scan":
            M_UserAPP   uappMod   = new M_UserAPP();
            M_UserInfo  mu        = new M_UserInfo();
            M_WxTextMsg textMod2  = new M_WxTextMsg();
            M_WX_User   wuserMod1 = api.GetWxUserModel(reqMod.FromUserName);
            M_Uinfo     mubase1   = new M_Uinfo();

            textMod2.FromUserName = reqMod.ToUserName;
            textMod2.ToUserName   = reqMod.FromUserName;
            textMod2.MsgType      = "text";
            textMod2.Content      = "";
            uappMod = uappBll.SelModelByOpenID(reqMod.FromUserName, "wechat");
            if (uappMod != null)
            {
                mu = buser.GetUserByUserID(uappMod.UserID);
                M_UserInfo pmu = buser.GetUserByUserID(DataConvert.CLng(reqMod.EventKey));
                if (mu != null && mu.UserID > 0)
                {
                    if (DataConvert.CLng(mu.ParentUserID) > 0)
                    {
                        textMod2.Content = "您已绑定了推荐人不能重复绑定!";
                        RepToClient(textMod2.ToXML());
                    }
                    else
                    {
                        if (mu.UserID == DataConvert.CLng(reqMod.EventKey))
                        {
                            textMod2.Content = "您扫描的是您自己的二维码!";
                        }
                        else
                        {
                            mu.ParentUserID = DataConvert.CLng(reqMod.EventKey).ToString();
                            if (pmu != null && pmu.UserID > 0)
                            {
                                string err = "";
                                try
                                {
                                    if (CheckParentUser(pmu.UserID, mu.UserID, ref err))
                                    {
                                        mu.ParentUserID = pmu.UserID.ToString();
                                        if (buser.UpdateByID(mu))
                                        {
                                            textMod2.Content = "您成功绑定了推荐人:" + pmu.TrueName + "!";
                                        }

                                        else
                                        {
                                            textMod2.Content = "绑定推荐人失败!";
                                        }
                                    }
                                    else
                                    {
                                        textMod2.Content = "绑定推荐失败,错误信息:" + err;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    textMod2.Content = "绑定推荐失败,错误信息:" + pmu.TrueName + "的推荐关系有循环!";
                                }
                            }
                            else
                            {
                                textMod2.Content = "绑定推荐失败,推荐人不存在!";
                            }
                        }
                        RepToClient(textMod2.ToXML());
                    }
                }
            }
            else
            {
                uappMod1    = new M_UserAPP();
                mu.UserName = "******" + DateTime.Now.ToString("yyyyMMddHHmmss") + function.GetRandomString(2).ToLower();
                mu.UserPwd  = StringHelper.MD5(function.GetRandomString(6));
                mu.Email    = function.GetRandomString(10) + "@wx.com";
                mu.Question = function.GetRandomString(5);
                mu.Answer   = function.GetRandomString(5);
                mu.TrueName = wuserMod1.Name;
                mu.GroupID  = 1;
                try
                {
                    string vpath = SiteConfig.SiteOption.UploadDir + "User/" + mu.UserName + mu.UserID + "/wxheadimg.jpg";
                    HttpHelper.DownFile(wuserMod1.HeadImgUrl, vpath);
                    mu.UserFace = vpath;
                }
                catch (Exception ex)
                {
                    ZLLog.L("微信更新头像错误,原因:" + ex.Message);
                }

                M_UserInfo pmu = buser.GetUserByUserID(DataConvert.CLng(reqMod.EventKey));
                if (pmu != null && pmu.UserID > 0)
                {
                    mu.ParentUserID = reqMod.EventKey;
                }
                mu.UserID = buser.Add(mu);

                uappMod1.UserID     = mu.UserID;
                uappMod1.SourcePlat = "wechat";
                uappMod1.OpenID     = reqMod.FromUserName;
                uappBll.Insert(uappMod1);
                mubase1.UserId = mu.UserID;
                buser.AddBase(mubase1);

                if (pmu != null && pmu.UserID > 0)
                {
                    textMod2.Content = "您成功绑定了推荐人:" + pmu.TrueName + "!";
                    RepToClient(textMod2.ToXML());
                }
            }
            break;

        default:
            break;
        }
    }
Пример #2
0
    /// <summary>
    /// 系统事件,如订阅等处理
    /// </summary>
    public void WxEventHandler(M_WxTextMsg reqMod)
    {
        M_WX_User userMod = null;

        switch (reqMod.Event.ToLower())
        {
        case "subscribe":
            userMod       = api.GetWxUserModel(reqMod.FromUserName);
            userMod.CDate = DateTime.Now;
            userMod.AppId = api.AppId.ID;
            wxuserBll.Insert(userMod);
            M_WxImgMsg msgmod = JsonConvert.DeserializeObject <M_WxImgMsg>(api.AppId.WelStr);
            msgmod.ToUserName   = reqMod.FromUserName;
            msgmod.FromUserName = reqMod.ToUserName;

            int        pid      = DataConvert.CLng(reqMod.EventKey.Replace("qrscene_", ""));
            Appinfo    uappMod1 = new Appinfo();
            B_UserAPP  uappBll1 = new B_UserAPP();
            M_UserInfo mu1      = new M_UserInfo();
            M_WX_User  wuserMod = new M_WX_User();
            M_Uinfo    mubase   = new M_Uinfo();
            uappMod1 = uappBll1.SelModelByOpenID(reqMod.FromUserName, "wechat");
            wuserMod = api.GetWxUserModel(reqMod.FromUserName);
            if (uappMod1 != null)
            {
                mu1 = buser.GetUserByUserID(uappMod1.UserID);
                if (mu1 != null && mu1.UserID > 0)
                {
                    mu1.TrueName = wuserMod.Name;
                    mu1.GroupID  = 1;
                    buser.UpdateByID(mu1);
                }
                else
                {
                    mu1.UserName = "******" + DateTime.Now.ToString("yyyyMMddHHmmss") + function.GetRandomString(2).ToLower();
                    mu1.UserPwd  = StringHelper.MD5(function.GetRandomString(6));
                    mu1.Email    = function.GetRandomString(10) + "@wx.com";
                    mu1.Question = function.GetRandomString(5);
                    mu1.Answer   = function.GetRandomString(5);
                    mu1.TrueName = wuserMod.Name;
                    mu1.GroupID  = 1;
                    mu1.UserID   = buser.Add(mu1);

                    uappMod1.UserID     = mu1.UserID;
                    uappMod1.SourcePlat = "wechat";
                    uappMod1.OpenID     = reqMod.FromUserName;
                    uappBll1.UpdateByID(uappMod1);
                    mubase.UserId = mu1.UserID;
                    buser.AddBase(mubase);
                }
            }
            else
            {
                uappMod1     = new Appinfo();
                mu1.UserName = "******" + DateTime.Now.ToString("yyyyMMddHHmmss") + function.GetRandomString(2).ToLower();
                mu1.UserPwd  = StringHelper.MD5(function.GetRandomString(6));
                mu1.Email    = function.GetRandomString(10) + "@wx.com";
                mu1.Question = function.GetRandomString(5);
                mu1.Answer   = function.GetRandomString(5);
                mu1.TrueName = wuserMod.Name;
                mu1.GroupID  = 1;
                mu1.UserID   = buser.Add(mu1);

                uappMod1.UserID     = mu1.UserID;
                uappMod1.SourcePlat = "wechat";
                uappMod1.OpenID     = reqMod.FromUserName;
                uappBll1.Insert(uappMod1);
                mubase.UserId = mu1.UserID;
                buser.AddBase(mubase);
            }

            if (string.IsNullOrEmpty(msgmod.Articles[0].PicUrl))     //如未设置图片则以纯文本格式发送,纯文本支持内置超链接
            {
                M_WxTextMsg textMod = ImageToText(msgmod);
                RepToClient(textMod.ToXML());
            }
            else
            {
                RepToClient(msgmod.ToXML());
            }
            //关注时发送多条信息
            if (pid > 0)
            {
                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(SendMsg), reqMod);
            }
            //关注时发送多条信息
            //System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(SendMsg), reqMod);
            break;

        case "unsubscribe":
            wxuserBll.DelByOpenid(api.AppId.ID, reqMod.FromUserName);
            break;

        case "location":    //用户进入公众号(包含定位信息)
            M_WxImgMsg msgmod1 = new M_WxImgMsg();
            msgmod1.ToUserName   = reqMod.FromUserName;
            msgmod1.FromUserName = reqMod.ToUserName;
            M_WX_ReplyMsg rpMod = new M_WX_ReplyMsg();
            rpMod = rpBll.SelByFileAndDef(appBll.GetAppByWxNo(reqMod.ToUserName).ID, "头图");
            if (rpMod != null)
            {
                M_WXImgItem item = JsonConvert.DeserializeObject <M_WXImgItem>(rpMod.Content);
                msgmod1.Articles.Add(new M_WXImgItem()
                {
                    Title = item.Title, Url = item.Url, Description = item.Description, PicUrl = item.PicUrl
                });
            }
//                rpMod = rpBll.SelByFileAndDef(appBll.GetAppByWxNo(reqMod.ToUserName).ID, "推送1");
//                {
//                    M_WXImgItem item = JsonConvert.DeserializeObject<M_WXImgItem>(rpMod.Content);
//                    msgmod1.Articles.Add(new M_WXImgItem() { Title = item.Title, Url = item.Url, Description = item.Description, PicUrl = item.PicUrl });
//                }
//                rpMod = rpBll.SelByFileAndDef(appBll.GetAppByWxNo(reqMod.ToUserName).ID, "推送2");
//                {
//                    M_WXImgItem item = JsonConvert.DeserializeObject<M_WXImgItem>(rpMod.Content);
//                    msgmod1.Articles.Add(new M_WXImgItem() { Title = item.Title, Url = item.Url, Description = item.Description, PicUrl = item.PicUrl });
//                }
//                rpMod = rpBll.SelByFileAndDef(appBll.GetAppByWxNo(reqMod.ToUserName).ID, "推送3");
//                {
//                    M_WXImgItem item = JsonConvert.DeserializeObject<M_WXImgItem>(rpMod.Content);
//                    msgmod1.Articles.Add(new M_WXImgItem() { Title = item.Title, Url = item.Url, Description = item.Description, PicUrl = item.PicUrl });
//                }
//                rpMod = rpBll.SelByFileAndDef(appBll.GetAppByWxNo(reqMod.ToUserName).ID, "推送4");
//                {
//                    M_WXImgItem item = JsonConvert.DeserializeObject<M_WXImgItem>(rpMod.Content);
//                    msgmod1.Articles.Add(new M_WXImgItem() { Title = item.Title, Url = item.Url, Description = item.Description, PicUrl = item.PicUrl });
//                }
            RepToClient(msgmod1.ToXML());
            break;

        case "scan":
            Appinfo     uappMod  = new Appinfo();
            B_UserAPP   uappBll  = new B_UserAPP();
            M_UserInfo  mu       = new M_UserInfo();
            M_WxTextMsg textMod2 = new M_WxTextMsg();
            textMod2.FromUserName = reqMod.ToUserName;
            textMod2.ToUserName   = reqMod.FromUserName;
            textMod2.MsgType      = "text";
            textMod2.Content      = "";
            uappMod = uappBll.SelModelByOpenID(reqMod.FromUserName, "wechat");
            if (uappMod != null)
            {
                mu = buser.GetUserByUserID(uappMod.UserID);
                M_UserInfo pmu = buser.GetUserByUserID(DataConvert.CLng(reqMod.EventKey));
                if (mu != null && mu.UserID > 0)
                {
                    if (DataConvert.CLng(mu.ParentUserID) > 0)
                    {
                        textMod2.Content = "您已绑定了推荐人不能重复绑定!";
                        RepToClient(textMod2.ToXML());
                    }
                    else
                    {
                        if (mu.UserID == DataConvert.CLng(reqMod.EventKey))
                        {
                            textMod2.Content = "您扫描的是您自己的二维码!";
                        }
                        else
                        {
                            mu.ParentUserID = DataConvert.CLng(reqMod.EventKey).ToString();
                            if (pmu != null && pmu.UserID > 0)
                            {
                                string err = "";
                                if (CheckParentUser(pmu.UserID, mu.UserID, ref err))
                                {
                                    mu.ParentUserID = pmu.UserID.ToString();
                                    if (buser.UpdateByID(mu))
                                    {
                                        textMod2.Content = "您成功绑定了推荐人:" + pmu.TrueName + "!";
                                    }

                                    else
                                    {
                                        textMod2.Content = "绑定推荐人失败!";
                                    }
                                }
                                else
                                {
                                    textMod2.Content = "绑定推荐失败,错误信息:" + err;
                                }
                            }
                            else
                            {
                                textMod2.Content = "绑定推荐失败,推荐人不存在!";
                            }
                        }
                        RepToClient(textMod2.ToXML());
                    }
                }
            }
            break;

        default:
            break;
        }
    }