Ejemplo n.º 1
0
        private void backgroundWorkerSync_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //MsgHandle handle = new MsgHandle();
            string state = e.UserState as string;

            if (string.IsNullOrEmpty(state))
            {
                AddMsgList msg = e.UserState as AddMsgList;
                if (msg != null)
                {
                    LogOut(string.Format("消息类型:{0},FromUserName:{1},ToUserName:{2},消息内容:{3},\r\n NotifyUserName:{4},时间:{5}",
                                         msg.MsgType, msg.FromUserName, msg.ToUserName, msg.Content, msg.StatusNotifyUserName, DateTime.Now));

                    MsgHandle handle  = new MsgHandle();
                    int       msgType = msg.MsgType;
                    switch (msgType)
                    {
                    case (int)MsgTypeEnum.系统消息:
                        handle.InitWxUser(msg);
                        break;

                    case (int)MsgTypeEnum.加好友:
                        if (buyService.HaveUser(Global.robot.Id, msg.RecommendInfo.NickName))
                        {
                            var result = protocol.VerifyUser(msg.RecommendInfo.UserName, msg.RecommendInfo.Ticket, Global.robot.Cookie, Global.robot.CookieStr);
                            LogOut("加好友" + result.ErrMsg);
                            //加完好友之后增加到机器人的好友表中
                            //wx_friend friend = new wx_friend();
                            //friend.UserName = msg.RecommendInfo.UserName;
                            //friend.NickName = msg.RecommendInfo.NickName;
                            //friend.AttrStatus = msg.RecommendInfo.AttrStatus;
                            //ContactService service = new ContactService();
                            //service.AddContent(friend);
                            Global.robot.Contact = protocol.GetContact(Global.robot.Cookie, Global.robot.CookieStr).MemberList.ToList();
                            Task.Run(() =>
                            {
                                protocol.StatusNotify(Global.robot.Cookie, Global.robot.CookieStr, Global.robot.UserName);
                            });

                            //发送消息
                            protocol.SendMsg("请输入验证码,如未购买,请访问", msg.RecommendInfo.UserName, 1, Global.robot.Cookie, Global.robot.CookieStr, Global.robot.UserName);
                        }
                        break;

                    case (int)MsgTypeEnum.群通知:
                        //好友已添加,被拉进群
                        if (msg.FromUserName.StartsWith("@@"))
                        {
                            //拉进群
                            //初始化
                            //拉别人进群也会有此通知
                            //判断群id是否存在,不存在初始化
                            var user = Global.robot.WxUserList.Where(u => u.UserName == msg.FromUserName).FirstOrDefault();
                            if (user == null)
                            {
                                protocol.StatusNotify(Global.robot.Cookie, Global.robot.CookieStr, Global.robot.UserName);
                            }
                            //protocol.SendMsg("群绑定成功,机器人开始为您服务", msg.FromUserName, 1, Global.robot.Cookie, Global.robot.CookieStr, Global.robot.UserName);
                        }
                        else
                        {
                            //好友
                        }
                        break;

                    default:
                        if (msg.FromUserName.StartsWith("@@") && msg.ToUserName.StartsWith("@"))
                        {
                            //群消息对个人
                            string fromAlias = WechatCommon.GetTrueId(msg.FromUserName);
                            bool   isBind    = buyService.IsBind(fromAlias);
                            string content   = msg.Content;

                            if (isBind)
                            {
                                //聊天记录
                                wx_group_chat chat = new wx_group_chat();
                                chat.Createtime    = DateTime.Now;
                                chat.MsgType       = msg.MsgType;
                                chat.GroupUserName = msg.FromUserName;
                                chat.GroupId       = WechatCommon.GetTrueId(msg.FromUserName);

                                if (content.StartsWith("@"))
                                {
                                    int    startIndex   = content.IndexOf(':');
                                    string fromUserName = content.Substring(0, startIndex);
                                    chat.SendAlias      = WechatCommon.GetTrueId(fromUserName);
                                    chat.SendUserName   = fromUserName;
                                    chat.Content        = content.Substring(startIndex + 6);
                                    chat.SendNickName   = WechatCommon.GetNickNameFromGroup(msg.FromUserName, fromUserName);
                                    chat.SendAttrStatus = WechatCommon.GetAttrStatusFromGroup(msg.FromUserName, fromUserName);
                                }
                                else
                                {
                                    chat.Content = content;
                                }
                                msgService.InsertMsg(chat);
                            }
                            //判断是否是开通群空间
                            if (content.StartsWith("@"))
                            {
                                int    startIndex   = content.IndexOf(':');
                                string fromUserName = content.Substring(0, startIndex);
                                string sendAlias    = WechatCommon.GetTrueId(fromUserName);
                                string message      = content.Substring(startIndex + 6);

                                string[] c = message.Split(' ');
                                if (c != null && c.Length == 2)
                                {
                                    if (c[0] == "@" + Global.robot.NickName && c[1] == "开通群空间")
                                    {
                                        //判断已经开通的群无法再次开通
                                        if (!isBind)
                                        {
                                            int recordId = buyService.GetBuyRecordId(Global.robot.Id, sendAlias);
                                            if (recordId > 0)
                                            {
                                                int result = buyService.BindGroup(recordId, fromAlias);
                                                if (result > 0)
                                                {
                                                    protocol.SendMsg("群绑定成功,机器人开始为您服务", msg.FromUserName, 1, Global.robot.Cookie, Global.robot.CookieStr, Global.robot.UserName);
                                                    //群空间开通功能,捞取人员信息存入数据库等操作

                                                    var groupInfo = Global.robot.GroupInfo.Where(n => n.UserName == msg.FromUserName).FirstOrDefault();
                                                    if (groupInfo == null)
                                                    {
                                                        BatchGetContactResult contactResult = protocol.BatchGetContact(new List <string>()
                                                        {
                                                            msg.FromUserName
                                                        }, Global.robot.Cookie, Global.robot.CookieStr);
                                                        if (contactResult != null)
                                                        {
                                                            groupInfo = contactResult.ContactList[0];
                                                        }
                                                    }
                                                    //存数据库
                                                    Task.Run(() => {
                                                        GroupService groupService = new GroupService();
                                                        groupService.SyncGroup(groupInfo);
                                                    });
                                                }
                                                else
                                                {
                                                    protocol.SendMsg("群绑定失败", msg.FromUserName, 1, Global.robot.Cookie, Global.robot.CookieStr, Global.robot.UserName);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            protocol.SendMsg("群已经被绑定", msg.FromUserName, 1, Global.robot.Cookie, Global.robot.CookieStr, Global.robot.UserName);
                                        }
                                    }
                                }
                            }
                        }
                        else if (msg.FromUserName.StartsWith("@") && msg.ToUserName.StartsWith("@@"))
                        {
                            //个人发群消息
                        }
                        else if (msg.FromUserName.StartsWith("@") && msg.ToUserName.StartsWith("@"))
                        {
                            //个人对个人
                            if (msg.ToUserName == Global.robot.UserName)
                            {
                                //判断内容是否跟验证码符合
                                string nickName = Global.robot.Contact.Where(n => n.UserName == msg.FromUserName).Select(n => n.NickName).FirstOrDefault();
                                int    recordId = buyService.VaildCode(Global.robot.Id, nickName, msg.Content);
                                if (recordId > 0)
                                {
                                    //绑定成功
                                    string alias = WechatCommon.GetTrueId(msg.FromUserName);
                                    if (buyService.UseCode(recordId, alias) > 0)
                                    {
                                        protocol.SendMsg("验证成功,请添加到群中", msg.FromUserName, 1, Global.robot.Cookie, Global.robot.CookieStr, Global.robot.UserName);
                                    }
                                    else
                                    {
                                        protocol.SendMsg("验证失败,请重新输入", msg.FromUserName, 1, Global.robot.Cookie, Global.robot.CookieStr, Global.robot.UserName);
                                    }
                                }
                                else
                                {
                                    protocol.SendMsg("验证码错误,如未购买,请访问", msg.FromUserName, 1, Global.robot.Cookie, Global.robot.CookieStr, Global.robot.UserName);
                                }
                            }
                        }
                        break;
                    }

                    //if (msg.MsgType == (int)MsgTypeEnum.加好友)
                    //{
                    //    //加好友
                    //    var result = protocol.VerifyUser(msg.RecommendInfo.UserName, msg.RecommendInfo.Ticket, Global.robot.Cookie, Global.robot.CookieStr);
                    //    LogOut("加好友" + result.ErrMsg);
                    //}
                    //else if (msg.MsgType == (int)MsgTypeEnum.系统消息&&msg.ToUserName.StartsWith("@@"))
                    //{
                    //    //群
                    //    //protocol.SendMsg("欢迎加入群聊天", msg.ToUserName, 1, Global.robot.Cookie, Global.robot.CookieStr,Global.robot.UserName);
                    //    //string html = protocol.BatchGetContact(msg.ToUserName, Global.robot.Cookie, Global.robot.CookieStr);
                    //}
                }
            }
            else if (state == "1100")
            {
                LogOut("拉取消息请求超时,请重新登录" + DateTime.Now);
                syncRun = false;
            }
            else if (state == "正常")
            {
                LogOut("0正常捞取" + DateTime.Now);
            }
            else
            {
                LogOut(state + "未知异常,请重新登录" + DateTime.Now);
                syncRun = false;
            }
        }
Ejemplo n.º 2
0
 public void InsertMsg(wx_group_chat chat)
 {
     tobotDB.GetInstance().Insert(chat);
 }