Exemplo n.º 1
0
        public async Task <PartialViewResult> CreateOrEditModal(int id)
        {
            if (id != 0)
            {
                var output = await _mpMediaVideoAppService.Get(new EntityDto <int>(id));

                var viewModel = new CreateOrEditMpMediaVideoViewModel(output);
                return(PartialView("_CreateOrEditModal", viewModel));
            }
            else
            {
                var mpID = await _userMpAppService.GetDefaultMpId();

                var model = new CreateOrEditMpMediaVideoViewModel()
                {
                    MpID = mpID, MediaID = ""
                };
                return(PartialView("_CreateOrEditModal", model));
            }
        }
Exemplo n.º 2
0
        public void SendMessage()
        {
            SendResult          result      = null;
            var                 Messages    = _mpMessageAppService.GetNotYetSendContent().Result;
            List <MpMessageDto> successList = new List <MpMessageDto>();

            if (Messages != null && Messages.Count > 0)
            {
                foreach (var Message in Messages)
                {
                    var account = _mpAccountAppService.GetCache(Message.MpID).Result;

                    List <int> groupIds = new List <int>()
                    {
                        Message.GroupID ?? -1
                    };
                    var groups = _mpGroupAppService.GetListByIds(Message.MpID, groupIds).Result;

                    #region 修改后代码-按OPenID发送
                    #region 获取粉丝数据
                    var fans = _mpFanAppService.GetAllByMpIdAndGroupId(Message.MpID, groupIds).Result.Where(m => m.IsFans == true);

                    if (!string.IsNullOrEmpty(Message.Sex))
                    {
                        var sexes = Message.Sex.Split(',');
                        fans = fans.Where(c => sexes.Contains(c.Sex)).ToList();
                    }
                    if (!string.IsNullOrEmpty(Message.Country))
                    {
                        fans = fans.Where(c => Message.Country == c.Country).ToList();
                    }
                    if (!string.IsNullOrEmpty(Message.Province))
                    {
                        fans = fans.Where(c => Message.Province == c.Province).ToList();
                    }
                    if (!string.IsNullOrEmpty(Message.City))
                    {
                        var cities = Message.City.Split(',');
                        fans = fans.Where(c => cities.Contains(c.City)).ToList();
                    }
                    #endregion

                    #region 发送消息
                    if (Message.MessageType == MpMessageType.text.ToString())
                    {
                        try
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, GroupMessageType.text, Message.Content, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                        catch
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true).Result, GroupMessageType.text, Message.Content, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                        if (result.errcode != ReturnCode.请求成功)
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, GroupMessageType.text, Message.Content, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                    }
                    else if (Message.MessageType == MpMessageType.video.ToString())
                    {
                        var videoId = int.Parse(Message.VideoID.ToString());
                        var video   = _mpMediaVideoAppService.Get(new Abp.Application.Services.Dto.EntityDto <int> {
                            Id = videoId
                        }).Result;
                        if (video == null)
                        {
                            throw new Exception(string.Format("视频{0}不存在", Message.VideoName));
                        }
                        try
                        {
                            result = GroupMessageApi.SendVideoGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, video.Title, video.Description, Message.VideoMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                        catch
                        {
                            result = GroupMessageApi.SendVideoGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true).Result, video.Title, video.Description, Message.VideoMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                        if (result.errcode != ReturnCode.请求成功)
                        {
                            result = GroupMessageApi.SendVideoGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, video.Title, video.Description, Message.VideoMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                    }
                    else if (Message.MessageType == MpMessageType.voice.ToString())
                    {
                        try
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, GroupMessageType.voice, Message.VoiceMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                        catch
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true).Result, GroupMessageType.voice, Message.VoiceMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                        if (result.errcode != ReturnCode.请求成功)
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, GroupMessageType.voice, Message.VoiceMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                    }
                    else if (Message.MessageType == MpMessageType.image.ToString())
                    {
                        try
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, GroupMessageType.image, Message.ImageMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                        catch
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true).Result, GroupMessageType.image, Message.ImageMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                        if (result.errcode != ReturnCode.请求成功)
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, GroupMessageType.image, Message.ImageMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                    }
                    else if (Message.MessageType == MpMessageType.mpnews.ToString())
                    {
                        try
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, GroupMessageType.mpnews, Message.ArticleMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                        catch
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true).Result, GroupMessageType.mpnews, Message.ArticleMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                        if (result.errcode != ReturnCode.请求成功)
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, GroupMessageType.mpnews, Message.ArticleMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                    }
                    else if (Message.MessageType == MpMessageType.mpmultinews.ToString())
                    {
                        try
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, GroupMessageType.mpnews, Message.ArticleGroupMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                        catch
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true).Result, GroupMessageType.mpnews, Message.ArticleGroupMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                        if (result.errcode != ReturnCode.请求成功)
                        {
                            result = GroupMessageApi.SendGroupMessageByOpenId(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, GroupMessageType.mpnews, Message.ArticleGroupMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                        }
                    }
                    #endregion
                    #endregion

                    #region 修改前代码
                    ////按分组发送
                    //if (new string[] { Message.Country, Message.Province, Message.City, Message.Sex }.All(c => string.IsNullOrEmpty(c)))
                    //{

                    //    #region 发送全部分组
                    //    if (string.IsNullOrEmpty(Message.GroupIDs) || groups.Any(c => c.Name == "全部"))
                    //    {
                    //        if (Message.MessageType == MpMessageType.text.ToString())
                    //        {
                    //            try
                    //            {
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.Content, GroupMessageType.text, true);
                    //            }
                    //            catch (Exception ex)
                    //            {
                    //                accessTokenResult = GetAccessToken();
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.Content, GroupMessageType.text, true);
                    //            }
                    //            if (result.errcode != ReturnCode.请求成功)
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.Content, GroupMessageType.text, true);
                    //        }
                    //        else if (Message.MessageType == MpMessageType.video.ToString())
                    //        {
                    //            try
                    //            {
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.VideoMediaID, GroupMessageType.video, true);
                    //            }
                    //            catch (Exception ex)
                    //            {
                    //                accessTokenResult = GetAccessToken();
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.VideoMediaID, GroupMessageType.video, true);
                    //            }
                    //            if (result.errcode != ReturnCode.请求成功)
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.VideoMediaID, GroupMessageType.video, true);
                    //        }
                    //        else if (Message.MessageType == MpMessageType.voice.ToString())
                    //        {
                    //            try
                    //            {
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.VoiceMediaID, GroupMessageType.voice, true);
                    //            }
                    //            catch (Exception ex)
                    //            {
                    //                accessTokenResult = GetAccessToken();
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.VoiceMediaID, GroupMessageType.voice, true);
                    //            }
                    //            if (result.errcode != ReturnCode.请求成功)
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.VoiceMediaID, GroupMessageType.voice, true);
                    //        }
                    //        else if (Message.MessageType == MpMessageType.image.ToString())
                    //        {
                    //            try
                    //            {
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.ImageMediaID, GroupMessageType.image, true);
                    //            }
                    //            catch (Exception ex)
                    //            {
                    //                accessTokenResult = GetAccessToken();
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.ImageMediaID, GroupMessageType.image, true);
                    //            }
                    //            if (result.errcode != ReturnCode.请求成功)
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.ImageMediaID, GroupMessageType.image, true);
                    //        }
                    //        else if (Message.MessageType == MpMessageType.mpnews.ToString())
                    //        {
                    //            try
                    //            {
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.ArticleMediaID, GroupMessageType.mpnews, true);
                    //            }
                    //            catch (Exception ex)
                    //            {
                    //                accessTokenResult = GetAccessToken();
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.ArticleMediaID, GroupMessageType.mpnews, true);
                    //            }
                    //            if (result.errcode != ReturnCode.请求成功)
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.ArticleMediaID, GroupMessageType.mpnews, true);
                    //        }
                    //        else if (Message.MessageType == MpMessageType.mpmultinews.ToString())
                    //        {
                    //            try
                    //            {
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.ArticleGroupMediaID, GroupMessageType.mpnews, true);
                    //            }
                    //            catch (Exception ex)
                    //            {
                    //                accessTokenResult = GetAccessToken();
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.ArticleGroupMediaID, GroupMessageType.mpnews, true);
                    //            }

                    //            if (result.errcode != ReturnCode.请求成功)
                    //                result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, "", Message.ArticleGroupMediaID, GroupMessageType.mpnews, true);
                    //        }
                    //    }
                    //    #endregion

                    //    #region 发送特定分组
                    //    else
                    //    {
                    //        foreach (var groupid in groups.Where(c => c.WxGroupID != null).Select(c => c.WxGroupID.ToString()))
                    //        {
                    //            if (Message.MessageType == MpMessageType.text.ToString())
                    //            {
                    //                try
                    //                {
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.Content, GroupMessageType.text, true);
                    //                }
                    //                catch (Exception ex)
                    //                {
                    //                    accessTokenResult = GetAccessToken();
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.Content, GroupMessageType.text, true);
                    //                }
                    //                if (result.errcode != ReturnCode.请求成功)
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.Content, GroupMessageType.text, true);
                    //            }
                    //            else if (Message.MessageType == MpMessageType.video.ToString())
                    //            {
                    //                try
                    //                {
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.VideoMediaID, GroupMessageType.video, true);
                    //                }
                    //                catch (Exception ex)
                    //                {
                    //                    accessTokenResult = GetAccessToken();
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.VideoMediaID, GroupMessageType.video, true);
                    //                }
                    //                if (result.errcode != ReturnCode.请求成功)
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.VideoMediaID, GroupMessageType.video, true);
                    //            }
                    //            else if (Message.MessageType == MpMessageType.voice.ToString())
                    //            {
                    //                try
                    //                {
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.VoiceMediaID, GroupMessageType.voice, true);
                    //                }
                    //                catch (Exception ex)
                    //                {
                    //                    accessTokenResult = GetAccessToken();
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.VoiceMediaID, GroupMessageType.voice, true);
                    //                }
                    //                if (result.errcode != ReturnCode.请求成功)
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.VoiceMediaID, GroupMessageType.voice, true);
                    //            }
                    //            else if (Message.MessageType == MpMessageType.image.ToString())
                    //            {
                    //                try
                    //                {
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.ImageMediaID, GroupMessageType.image, true);
                    //                }
                    //                catch (Exception ex)
                    //                {
                    //                    accessTokenResult = GetAccessToken();
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.ImageMediaID, GroupMessageType.image, true);
                    //                }
                    //                if (result.errcode != ReturnCode.请求成功)
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.ImageMediaID, GroupMessageType.image, true);
                    //            }
                    //            else if (Message.MessageType == MpMessageType.mpnews.ToString())
                    //            {
                    //                try
                    //                {
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.ArticleMediaID, GroupMessageType.mpnews, true);
                    //                }
                    //                catch (Exception ex)
                    //                {
                    //                    accessTokenResult = GetAccessToken();
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.ArticleMediaID, GroupMessageType.mpnews, true);
                    //                }
                    //                if (result.errcode != ReturnCode.请求成功)
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.ArticleMediaID, GroupMessageType.mpnews, true);
                    //            }
                    //            else if (Message.MessageType == MpMessageType.mpmultinews.ToString())
                    //            {
                    //                try
                    //                {
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.ArticleGroupMediaID, GroupMessageType.mpnews, true);
                    //                }
                    //                catch (Exception ex)
                    //                {
                    //                    accessTokenResult = GetAccessToken();
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.ArticleGroupMediaID, GroupMessageType.mpnews, true);
                    //                }

                    //                if (result.errcode != ReturnCode.请求成功)
                    //                    result = GroupMessageApi.SendGroupMessageByGroupId(accessTokenResult.access_token, groupid, Message.ArticleGroupMediaID, GroupMessageType.mpnews, true);
                    //            }
                    //        }
                    //    }
                    //    #endregion
                    //}
                    ////按openid发送
                    //else
                    //{
                    //    #region 过滤粉丝
                    //    //var fans = entities.Set<MpFans>().Where(c => c.MpID == Message.MpID && c.IsFans == "1");
                    //    var fans = _mpFanAppService.GetAllByMpId(Message.MpID).Result.Where(m => m.IsFans == "1").ToList();
                    //    if (!string.IsNullOrEmpty(Message.GroupIDs) && !groups.Any(c => c.Name == "全部"))
                    //    {
                    //        fans = fans.Where(c => groupIds.Contains(c.GroupID)).ToList();
                    //    }
                    //    if (!string.IsNullOrEmpty(Message.Sex))
                    //    {
                    //        var sexes = Message.Sex.Split(',');
                    //        fans = fans.Where(c => sexes.Contains(c.Sex)).ToList();
                    //    }
                    //    if (!string.IsNullOrEmpty(Message.Country))
                    //    {
                    //        fans = fans.Where(c => Message.Country == c.Country).ToList();
                    //    }
                    //    if (!string.IsNullOrEmpty(Message.Province))
                    //    {
                    //        fans = fans.Where(c => Message.Province == c.Province).ToList();
                    //    }
                    //    if (!string.IsNullOrEmpty(Message.City))
                    //    {
                    //        var cities = Message.City.Split(',');
                    //        fans = fans.Where(c => cities.Contains(c.City)).ToList();
                    //    }
                    //    #endregion

                    //    #region 发送消息
                    //    if (Message.MessageType == MpMessageType.text.ToString())
                    //    {
                    //        try
                    //        {
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.text, Message.Content, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //        }
                    //        catch (Exception ex)
                    //        {
                    //            accessTokenResult = GetAccessToken();
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.text, Message.Content, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //        }
                    //        if (result.errcode != ReturnCode.请求成功)
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.text, Message.Content, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //    }
                    //    else if (Message.MessageType == MpMessageType.video.ToString())
                    //    {
                    //        //var video = entities.Set<MpMediaVideo>().Where(c => c.MpID == Message.MpID && c.ID == Message.VideoID).FirstOrDefault();
                    //        var videoId = int.Parse(Message.VideoID.ToString());
                    //        var video = _mpMediaVideoAppService.Get(new Abp.Application.Services.Dto.EntityDto<int> { Id = videoId }).Result;
                    //        if (video == null)
                    //            throw new Exception(string.Format("视频{0}不存在", Message.VideoName));
                    //        try
                    //        {
                    //            result = GroupMessageApi.SendVideoGroupMessageByOpenId(accessTokenResult.access_token, video.Title, video.Description, Message.VideoMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //        }
                    //        catch (Exception ex)
                    //        {
                    //            accessTokenResult = GetAccessToken();
                    //            result = GroupMessageApi.SendVideoGroupMessageByOpenId(accessTokenResult.access_token, video.Title, video.Description, Message.VideoMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //        }
                    //        if (result.errcode != ReturnCode.请求成功)
                    //            result = GroupMessageApi.SendVideoGroupMessageByOpenId(accessTokenResult.access_token, video.Title, video.Description, Message.VideoMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //    }
                    //    else if (Message.MessageType == MpMessageType.voice.ToString())
                    //    {
                    //        try
                    //        {
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.voice, Message.VoiceMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //        }
                    //        catch (Exception ex)
                    //        {
                    //            accessTokenResult = GetAccessToken();
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.voice, Message.VoiceMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //        }
                    //        if (result.errcode != ReturnCode.请求成功)
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.voice, Message.VoiceMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //    }
                    //    else if (Message.MessageType == MpMessageType.image.ToString())
                    //    {
                    //        try
                    //        {
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.image, Message.ImageMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //        }
                    //        catch (Exception ex)
                    //        {
                    //            accessTokenResult = GetAccessToken();
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.image, Message.ImageMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //        }
                    //        if (result.errcode != ReturnCode.请求成功)
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.image, Message.ImageMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //    }
                    //    else if (Message.MessageType == MpMessageType.mpnews.ToString())
                    //    {
                    //        try
                    //        {
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.mpnews, Message.ArticleMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //        }
                    //        catch (Exception ex)
                    //        {
                    //            accessTokenResult = GetAccessToken();
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.mpnews, Message.ArticleMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //        }
                    //        if (result.errcode != ReturnCode.请求成功)
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.mpnews, Message.ArticleMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //    }
                    //    else if (Message.MessageType == MpMessageType.mpmultinews.ToString())
                    //    {
                    //        try
                    //        {
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.mpnews, Message.ArticleGroupMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //        }
                    //        catch (Exception ex)
                    //        {
                    //            accessTokenResult = GetAccessToken();
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.mpnews, Message.ArticleGroupMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //        }
                    //        if (result.errcode != ReturnCode.请求成功)
                    //            result = GroupMessageApi.SendGroupMessageByOpenId(accessTokenResult.access_token, GroupMessageType.mpnews, Message.ArticleGroupMediaID, null, Senparc.Weixin.Config.TIME_OUT, fans.Select(c => c.OpenID).ToArray());
                    //    }
                    //    #endregion
                    //}
                    #endregion

                    if (result != null)
                    {
                        Message.WxMsgID = result.msg_id;
                        GetSendResult sendresult = null;
                        try
                        {
                            sendresult = GroupMessageApi.GetGroupMessageResult(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, result.msg_id);
                        }
                        catch
                        {
                            sendresult = GroupMessageApi.GetGroupMessageResult(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true).Result, result.msg_id);
                        }
                        if (result.errcode != ReturnCode.请求成功)
                        {
                            sendresult = GroupMessageApi.GetGroupMessageResult(_accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret).Result, result.msg_id);
                        }
                        Message.State      = sendresult.msg_status;
                        Message.FinishDate = DateTime.Now;

                        //if (result.errcode == ReturnCode.请求成功)
                        successList.Add(Message);
                    }
                }

                if (successList.Count > 0)
                {
                    _mpMessageAppService.UpdateSendState(successList);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 按openid群发
        /// </summary>
        /// <param name="account"></param>
        /// <param name="args"></param>
        /// <param name="openIds"></param>
        /// <returns></returns>
        private async Task <SendResult> SendByOpenIds(MpAccountDto account, MpMessageDto args, string[] openIds)
        {
            SendResult result = null;


            if (args.MessageType == MpMessageType.text.ToString())
            {
                try
                {
                    LogWriter.Info("群发消息开始 openid开始");
                    result = GroupMessageApi.SendGroupMessageByOpenId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), GroupMessageType.text, args.Content, $"MpMessage{args.Id}", Senparc.Weixin.Config.TIME_OUT, openIds);

                    LogWriter.Info("群发消息开始 openid结束");
                }
                catch (Exception ex)
                {
                    LogWriter.Error("群发消息开始 第一次报错", ex);
                    try
                    {
                        LogWriter.Info("群发消息开始 openid开始2");
                        result = GroupMessageApi.SendGroupMessageByOpenId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), GroupMessageType.text, args.Content, $"MpMessage{args.Id}", Senparc.Weixin.Config.TIME_OUT, openIds);

                        LogWriter.Info("群发消息开始 openid结束2");
                    }
                    catch (Exception ex2)
                    {
                        LogWriter.Error("群发消息开始 第二次报错", ex2);
                    }
                }
            }
            else if (args.MessageType == MpMessageType.video.ToString())
            {
                var videoId = int.Parse(args.VideoID.ToString());
                var video   = await _mpMediaVideoAppService.Get(new Abp.Application.Services.Dto.EntityDto <int> {
                    Id = videoId
                });

                if (video == null)
                {
                    throw new Exception(string.Format("视频{0}不存在", args.VideoName));
                }
                try
                {
                    result = GroupMessageApi.SendVideoGroupMessageByOpenId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), video.Title, video.Description, args.VideoMediaID, $"MpMessage{args.Id}", Senparc.Weixin.Config.TIME_OUT, openIds);
                }
                catch
                {
                    try
                    {
                        result = GroupMessageApi.SendVideoGroupMessageByOpenId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), video.Title, video.Description, args.VideoMediaID, $"MpMessage{args.Id}", Senparc.Weixin.Config.TIME_OUT, openIds);
                    }
                    catch
                    {
                    }
                }
            }
            else if (args.MessageType == MpMessageType.voice.ToString())
            {
                try
                {
                    result = GroupMessageApi.SendGroupMessageByOpenId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), GroupMessageType.voice, args.VoiceMediaID, $"MpMessage{args.Id}", Senparc.Weixin.Config.TIME_OUT, openIds);
                }
                catch
                {
                    try
                    {
                        result = GroupMessageApi.SendGroupMessageByOpenId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), GroupMessageType.voice, args.VoiceMediaID, $"MpMessage{args.Id}", Senparc.Weixin.Config.TIME_OUT, openIds);
                    }
                    catch
                    {
                    }
                }
            }
            else if (args.MessageType == MpMessageType.image.ToString())
            {
                try
                {
                    result = GroupMessageApi.SendGroupMessageByOpenId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), GroupMessageType.image, args.ImageMediaID, $"MpMessage{args.Id}", Senparc.Weixin.Config.TIME_OUT, openIds);
                }
                catch
                {
                    try
                    {
                        result = GroupMessageApi.SendGroupMessageByOpenId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), GroupMessageType.image, args.ImageMediaID, $"MpMessage{args.Id}", Senparc.Weixin.Config.TIME_OUT, openIds);
                    }
                    catch
                    {
                    }
                }
            }
            else if (args.MessageType == MpMessageType.mpnews.ToString())
            {
                try
                {
                    result = GroupMessageApi.SendGroupMessageByOpenId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), GroupMessageType.mpnews, args.ArticleMediaID, $"MpMessage{args.Id}", Senparc.Weixin.Config.TIME_OUT, openIds);
                }
                catch
                {
                    try
                    {
                        result = GroupMessageApi.SendGroupMessageByOpenId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), GroupMessageType.mpnews, args.ArticleMediaID, $"MpMessage{args.Id}", Senparc.Weixin.Config.TIME_OUT, openIds);
                    }
                    catch
                    {
                    }
                }
            }
            else if (args.MessageType == MpMessageType.mpmultinews.ToString())
            {
                try
                {
                    result = GroupMessageApi.SendGroupMessageByOpenId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret), GroupMessageType.mpnews, args.ArticleGroupMediaID, $"MpMessage{args.Id}", Senparc.Weixin.Config.TIME_OUT, openIds);
                }
                catch
                {
                    try
                    {
                        result = GroupMessageApi.SendGroupMessageByOpenId(await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret, true), GroupMessageType.mpnews, args.ArticleGroupMediaID, $"MpMessage{args.Id}", Senparc.Weixin.Config.TIME_OUT, openIds);
                    }
                    catch
                    {
                    }
                }
            }
            await _mpMessageAppService.UpdateSendState(new List <MpMessageDto> {
                args
            });

            return(result);
        }
        /// <summary>
        /// 获取实体中的回复
        /// </summary>
        /// <param name="type"></param>
        /// <param name="imageMediaID"></param>
        /// <param name="content"></param>
        /// <param name="voiceMediaID"></param>
        /// <param name="videoID"></param>
        /// <param name="articleID"></param>
        /// <param name="articleGroupID"></param>
        /// <returns></returns>
        private async Task <IResponseMessageBase> GetEntityResponse(string type, string imageMediaID, string content, string voiceMediaID, string videoID, string articleID, string articleGroupID, RequestMessageText requestMessage = null)
        {
            if (type == ReplyMsgType.kf.ToString())
            {
                #region 春雨医生逻辑处理
                var chunyuHandler = _iocResolver.Resolve <IChunYuMessageHandler>();
                //如果春雨医生未结束,先引导用户主动关闭春雨医生会话
                if (await chunyuHandler.IsAsking(mpId, requestMessage.FromUserName))
                {
                    return(await chunyuHandler.CustomerNotice(mpId, requestMessage));
                }
                #endregion

                #region 客服逻辑处理
                var kefuMessageHandler = _iocResolver.Resolve <IKeFuMessageHandler>();
                return(await kefuMessageHandler.InCustomer(mpId, requestMessage));

                #endregion
            }
            else if (type == ReplyMsgType.doctor.ToString())
            {
                #region 客服逻辑处理
                var kefuMessageHandler = _iocResolver.Resolve <IKeFuMessageHandler>();
                if (await kefuMessageHandler.IsAsking(mpId, requestMessage.FromUserName))
                {
                    return(await kefuMessageHandler.ChunYuNotice(mpId, requestMessage));
                }
                #endregion

                #region 春雨医生逻辑处理
                var chunyuHandler = _iocResolver.Resolve <IChunYuMessageHandler>();
                return(await chunyuHandler.CreateProblem(mpId, requestMessage));

                #endregion
            }
            else if (type == ReplyMsgType.none.ToString())
            {
                return(base.CreateResponseMessage <ResponseMessageNoResponse>());
            }
            else if (type == ReplyMsgType.image.ToString())
            {
                var responseMessage = base.CreateResponseMessage <ResponseMessageImage>();
                responseMessage.Image.MediaId = imageMediaID;
                return(responseMessage);
            }
            else if (type == ReplyMsgType.text.ToString())
            {
                var responseMessage = base.CreateResponseMessage <ResponseMessageText>();
                responseMessage.Content = content;
                return(responseMessage);
            }
            else if (type == ReplyMsgType.voice.ToString())
            {
                var responseMessage = base.CreateResponseMessage <ResponseMessageVoice>();
                responseMessage.Voice.MediaId = voiceMediaID;
                return(responseMessage);
            }
            else if (type == ReplyMsgType.video.ToString())
            {
                var responseMessage = base.CreateResponseMessage <ResponseMessageVideo>();
                int vedioId         = 0;
                int.TryParse(videoID, out vedioId);
                var video = await _mpMediaVideoAppService.Get(new EntityDto <int> {
                    Id = vedioId
                });

                if (video != null)
                {
                    responseMessage.Video.MediaId     = video.MediaID;
                    responseMessage.Video.Title       = video.Title;
                    responseMessage.Video.Description = video.Description;
                    return(responseMessage);
                }
                else
                {
                    return(base.CreateResponseMessage <ResponseMessageNoResponse>());
                }
            }
            else if (type == ReplyMsgType.mpnews.ToString())
            {
                var responseMessage = base.CreateResponseMessage <ResponseMessageNews>();
                int articleId       = 0;
                int.TryParse(articleID, out articleId);
                var article = await _mpSelfArticleAppService.Get(new EntityDto <int> {
                    Id = articleId
                });

                if (article != null)
                {
                    responseMessage.Articles.Add(new Article()
                    {
                        Title       = article.Title,
                        Description = article.Description,
                        Url         = article.AUrl,
                        PicUrl      = article.FilePathOrUrl
                    });
                    return(responseMessage);
                }
                else
                {
                    return(base.CreateResponseMessage <ResponseMessageNoResponse>());
                }
            }
            else if (type == ReplyMsgType.mpmultinews.ToString())
            {
                var responseMessage = base.CreateResponseMessage <ResponseMessageNews>();
                int articleGroupId  = 0;
                int.TryParse(articleGroupID, out articleGroupId);
                var groupItems = await _mpSelfArticleGroupItemAppService.GetGroupItems(articleGroupId);

                if (!(groupItems == null || groupItems.Count() < 2))
                {
                    var articleIds = groupItems.Select(m => m.ArticleID).ToList();
                    var articles   = await _mpSelfArticleAppService.GetListByIds(articleIds);

                    foreach (var id in articleIds)
                    {
                        var item = articles.Where(m => m.Id == id).FirstOrDefault();
                        responseMessage.Articles.Add(new Article()
                        {
                            Title       = item.Title,
                            Description = item.Description,
                            Url         = item.AUrl,
                            PicUrl      = item.FilePathOrUrl
                        });
                    }

                    return(responseMessage);
                }
                else
                {
                    return(base.CreateResponseMessage <ResponseMessageNoResponse>());
                }
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Download(string mediaType, int id)
        {
            var    tempProfilePicturePath = "";
            string contentType            = "";

            if (mediaType == MpMessageType.video.ToString())
            {
                var data = await _mpMediaVideoAppService.Get(new Abp.Application.Services.Dto.EntityDto <int> {
                    Id = id
                });

                tempProfilePicturePath = data.FileID;
                var extraFileType = tempProfilePicturePath.Substring(tempProfilePicturePath.LastIndexOf(".") + 1);
                if (extraFileType == "mp4")
                {
                    contentType = "video/mpeg4";
                }
                else if (extraFileType == "rmvb")
                {
                    contentType = "application/vnd.rn-realmedia-vbr";
                }
                else if (extraFileType == "avi")
                {
                    contentType = "video/avi";
                }
                else if (extraFileType == "mpg" || extraFileType == "mpeg")
                {
                    contentType = "video/mpg";
                }
                else
                {
                    contentType = "application/file";
                }
            }
            if (mediaType == MpMessageType.voice.ToString())
            {
                var data = await _mpMediaVoiceAppService.Get(new Abp.Application.Services.Dto.EntityDto <int> {
                    Id = id
                });

                tempProfilePicturePath = data.FileID;
                var extraFileType = tempProfilePicturePath.Substring(tempProfilePicturePath.LastIndexOf(".") + 1);
                if (extraFileType == "mp3")
                {
                    contentType = "audio/mp3";
                }
                else if (extraFileType == "wma")
                {
                    contentType = "audio/x-ms-wma";
                }
                else if (extraFileType == "wav")
                {
                    contentType = "	audio/wav";
                }
                else
                {
                    contentType = "application/file";
                }
            }

            if (!string.IsNullOrWhiteSpace(tempProfilePicturePath))
            {
                var imgStream = new MemoryStream(System.IO.File.ReadAllBytes(tempProfilePicturePath));
                return(File(imgStream, contentType));
            }
            else
            {
                return(null);
            }
        }