コード例 #1
0
        /// <summary>
        /// 分析消息结构
        /// </summary>
        /// <param name="MsgList"></param>
        private static void AnalyseMsg(List <AddMsgListItemEntity> MsgList)
        {
            try
            {
                //遍历所有消息项
                for (int index = 0; index < MsgList.Count; index++)
                {
                    AddMsgListItemEntity ali = MsgList[index];  //消息体
                    MsgEntity            me  = new MsgEntity(); //二次封装
                    me.IsCanAutoReply = true;
                    //  WebwxStatusNotify(ali.FromUserName, ali.ToUserName);
                    if (ali.FromUserName == CommonDefine.BaseContact.User.UserName)
                    {
                        return;
                    }
                    //取得消息来源联系人信息
                    var CustomName = from item in CommonDefine.ContactsList.MemberList where item.UserName == ali.FromUserName select item;
                    if (CustomName == null || CustomName.ToList().Count <= 0)
                    {
                        //当前联系人列表中找不到此用户
                        try
                        {
                            //此处进行联系人刷新
                            RefreshContacts();
                            CustomName = from item in CommonDefine.ContactsList.MemberList where item.UserName == ali.FromUserName select item;
                            if (CustomName == null || CustomName.ToList().Count <= 0)//未找到消息来源
                            {
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            LogWriter.Write("刷新联系人发生异常,异常信息是:" + ex.Message, LogPathDefine.ExceptionLogPath);
                        }
                    }
                    MemberListItemEntity Mlie = CustomName.ToList().SingleOrDefault();
                    try
                    {
                        if (Mlie.UserName.Contains("@@"))
                        {
                            //群消息处理
                            GroupMemberEntity Gme = SelectGroupMumber(ali);
                            if (Gme == null)
                            {
                                //刷新群成员
                                GetGroupContactsMethod(CommonDefine.ContactsList.MemberList);
                                Gme = SelectGroupMumber(ali);
                            }
                            if (ali.Content.Contains(":"))
                            {
                                string[] a = ali.Content.Split(':');
                                ali.Content = Gme.NickName + ":" + a[1].Replace("<br/>", "");
                            }
                            me.GroupMember = Gme;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogWriter.Write("替换群成员名称失败,异常信息为:" + ex.Message, LogPathDefine.ExceptionLogPath);
                    }
                    //消息列表实体
                    me.MsgOwer        = Mlie;
                    me.MsgContent     = ali.Content;
                    me.MsgOwerType    = MsgOwerTypeEnum.AccepterMsg;
                    me.IsCanAutoReply = Mlie.IsCanAutoReply;

                    #region 储存消息

                    if (ali.MsgType == (int)MsgTypeEnum.Voice)
                    {
                        me.MsgType = MsgTypeEnum.Voice;
                        //语音消息
                        string Url    = string.Format(UrlDefine.VoiceUrl, ali.MsgId, CommonDefine.LoginResult.skey);
                        string FileId = MethodsHelper.MsgSaveFile(DirectoryDefine.VoiceMsgPath);//获取FileId
                        //根据FileID创建本地的语音对象
                        List <byte> list = HttpMethods.GetFile(Url, Environment.CurrentDirectory + "\\" + DirectoryDefine.VoiceMsgPath + "\\" + FileId + ".mp3", CommonDefine.Cookies).ContentData as List <byte>;
                        me.MsgTime  = DateTime.Now;
                        me.FileId   = FileId;
                        me.FilePath = Environment.CurrentDirectory + "\\" + DirectoryDefine.VoiceMsgPath + "\\" + me.FileId + ".mp3";
                    }
                    else if (ali.MsgType == (int)MsgTypeEnum.Picture)
                    {
                        me.MsgType = MsgTypeEnum.Picture;
                        //图片消息
                        string Url    = string.Format(UrlDefine.ImgUrlBig, ali.MsgId, CommonDefine.LoginResult.skey);
                        string FileId = MethodsHelper.MsgSaveFile(DirectoryDefine.ImageMsgPath);//获取FileId
                        //根据FileID创建本地的语音对象
                        List <byte> list = HttpMethods.GetFile(Url, Environment.CurrentDirectory + "\\" + DirectoryDefine.ImageMsgPath + "\\" + FileId + ".jpg", CommonDefine.Cookies).ContentData as List <byte>;
                        //获取缩略图
                        HttpMethods.GetFile(string.Format(UrlDefine.ImgUrl, ali.MsgId, CommonDefine.LoginResult.skey), Environment.CurrentDirectory + "\\" + DirectoryDefine.ImageMsgTempPath + "\\" + FileId + ".jpg", CommonDefine.Cookies);
                        //将下载下来的MP3文件转成Base64进行储存
                        //string Base64Str = Convert.ToBase64String(list.ToArray());
                        me.MsgTime      = DateTime.Now;
                        me.FileId       = FileId;
                        me.FilePath     = Environment.CurrentDirectory + "\\" + DirectoryDefine.ImageMsgPath + "\\" + FileId + ".jpg";//
                        me.FileTempPath = Environment.CurrentDirectory + "\\" + DirectoryDefine.ImageMsgTempPath + "\\" + FileId + ".jpg";
                    }
                    else if (ali.MsgType == (int)MsgTypeEnum.Gif)
                    {
                        //TODO:GIF消息
                        me.MsgType = MsgTypeEnum.Gif;
                        //语音消息
                        string Url    = string.Format(UrlDefine.ImgUrlBig, ali.MsgId, CommonDefine.LoginResult.skey);
                        string FileId = MethodsHelper.MsgSaveFile(DirectoryDefine.ImageMsgGifPath);//获取FileId
                        HttpMethods.GetFile(Url, Environment.CurrentDirectory + "\\" + DirectoryDefine.ImageMsgGifPath + "\\" + FileId + ".gif", CommonDefine.Cookies);
                        HttpMethods.GetFile(string.Format(UrlDefine.ImgUrl, ali.MsgId, CommonDefine.LoginResult.skey), Environment.CurrentDirectory + "\\" + DirectoryDefine.ImageMsgTempPath + "\\" + FileId + ".jpg", CommonDefine.Cookies);
                        me.MsgTime      = DateTime.Now;
                        me.FileId       = FileId;
                        me.FilePath     = Environment.CurrentDirectory + "\\" + DirectoryDefine.ImageMsgGifPath + "\\" + FileId + ".gif";//
                        me.FileTempPath = Environment.CurrentDirectory + "\\" + DirectoryDefine.ImageMsgTempPath + "\\" + FileId + ".jpg";
                    }
                    else if (ali.MsgType == (int)MsgTypeEnum.Video)
                    {
                        me.MsgType = MsgTypeEnum.Video;
                        //视频消息
                        string Url    = string.Format(UrlDefine.VideoUrl, ali.MsgId, HttpUtility.UrlEncode(CommonDefine.LoginResult.skey));
                        string FileId = MethodsHelper.MsgSaveFile(DirectoryDefine.VideoMsgPath);//获取FileId
                        HttpMethods.GetFile(string.Format(UrlDefine.ImgUrl, ali.MsgId, CommonDefine.LoginResult.skey), Environment.CurrentDirectory + "\\" + DirectoryDefine.ImageMsgTempPath + "\\" + FileId + ".jpg", CommonDefine.Cookies);
                        //根据FileID创建本地的语音对象
                        List <byte> list = HttpMethods.GetVideo(Url, Environment.CurrentDirectory + "\\" + DirectoryDefine.VideoMsgPath + "\\" + FileId + ".mp4", CommonDefine.Cookies);
                        me.MsgTime      = DateTime.Now;
                        me.FileId       = FileId;
                        me.FilePath     = Environment.CurrentDirectory + "\\" + DirectoryDefine.VideoMsgPath + "\\" + FileId + ".mp4";//
                        me.FileTempPath = Environment.CurrentDirectory + "\\" + DirectoryDefine.ImageMsgTempPath + "\\" + FileId + ".jpg";
                    }
                    else if (ali.MsgType == 1 && !string.IsNullOrWhiteSpace(ali.Url))
                    {
                        //地图消息
                        me.MsgType = MsgTypeEnum.Map;
                        me.MsgUrl  = ali.Url;
                        //地图消息
                        string Url = string.Format(UrlDefine.MapUrl, ali.MsgId);
                        //HttpHelper.ContentType = "";
                        string FileId = MethodsHelper.MsgSaveFile(DirectoryDefine.MapImageMsgPath);//获取FileId
                        //根据FileID创建本地的语音对象
                        HttpMethods.GetFile(Url, Environment.CurrentDirectory + "\\" + DirectoryDefine.MapImageMsgPath + "\\" + FileId + ".jpg", CommonDefine.Cookies);
                        me.FilePath = Environment.CurrentDirectory + "\\" + DirectoryDefine.MapImageMsgPath + "\\" + FileId + ".jpg";
                        me.MsgTime  = DateTime.Now;
                    }
                    else if (ali.MsgType == (int)MsgTypeEnum.SystemMsg)
                    {
                        me.MsgType = MsgTypeEnum.SystemMsg;
                        me.MsgTime = DateTime.Now;
                    }
                    else
                    {
                        me.MsgType = MsgTypeEnum.Text;
                        me.MsgTime = DateTime.Now;
                    }
                    LogWriter.Write(me.MsgContent.ToString(), LogPathDefine.WeChatLogPath);
                    CommonMethodCallBackHandlers.OnReceivedMsgAnalyseMsgCompleted(me);
                    SetAutoRepate(CommonDefine.WhiteUserList, LoadAutoReplyConfig(ConfigDefine.WeChatAutoReplyPath), me.MsgOwer.UserName, me);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogWriter.Write(ex.Message, LogPathDefine.ExceptionLogPath);
            }
        }