コード例 #1
0
        /// <summary>
        /// 根据OpenID列表群发卡券消息(目前该接口仅支持填入非自定义code的卡券,自定义code的卡券需先进行code导入后调用。 )
        /// </summary>
        /// <param name="value">群发卡券时传入cardId</param>
        /// <param name="openIds">openId字符串数组(OpenID最少2个,最多10000个 )</param>
        /// <returns></returns>
        public ActionResult SendGroupMessageByOpenId(string value, string openIds)
        {
            if (!VerifyParam("value,openIds"))
            {
                return(ErrMsg());
            }

            string[] openIdArr = openIds.Split(',');

            var weChatConfig = GetWechatConfig();

            var ret = GroupMessageApi.SendGroupMessageByOpenId(weChatConfig.WeixinAppId, weChatConfig.WeixinCorpSecret, GroupMessageType.wxcard, value, 1000, openIdArr);

            if (ret.errcode == Weixin.ReturnCode.请求成功)
            {
                return(Json(new
                {
                    message = "",
                    success = true,
                    msg_id = ret.msg_id
                }));
            }
            else
            {
                return(Json(new
                {
                    message = ret.errmsg,
                    success = false
                }, JsonRequestBehavior.AllowGet));
            }
        }
コード例 #2
0
        //[TestMethod]
        public string SendImageByOpenIdTest()
        {
            string file = "";//文件路径,以下以图片为例

            string[] openIds = new string[] { _testOpenId };

            var accessToken = AccessTokenContainer.GetAccessToken(_appId);
            var mediaId     = MediaApi.UploadTemporaryMedia(accessToken, UploadMediaFileType.image, file).media_id;
            var result      = GroupMessageApi.SendGroupMessageByOpenId(accessToken, GroupMessageType.image, mediaId, Config.TIME_OUT, openIds);

            Assert.IsTrue(result.msg_id.Length > 0);

            return(result.msg_id);
        }
コード例 #3
0
        //[TestMethod]
        public string SendImageByOpenIdTest()
        {
            string file = "C:\\Users\\roman\\Pictures\\捕获.PNG";//文件路径,以下以图片为例

            string[] openIds = new string[] { _testOpenId, "oIskUw5ONFqLqb4XUyLHQ7Lu5t64" };

            var accessToken = AccessTokenContainer.GetAccessToken(_appId);
            var mediaId     = MediaApi.UploadTemporaryMedia(accessToken, UploadMediaFileType.image, file).media_id;
            var clientMsgId = DateTime.Now.Ticks.ToString();
            var result      = GroupMessageApi.SendGroupMessageByOpenId(accessToken, GroupMessageType.text, "你好lizejia", clientMsgId, Config.TIME_OUT, openIds);

            Assert.IsTrue(result.msg_id.Length > 0);

            return(result.msg_id);
        }
コード例 #4
0
ファイル: MediaTest.cs プロジェクト: zongningliu/WeiXinMPSDK
        public void AfterDeleteNewsTest()
        {
            var accessToken = AccessTokenContainer.GetAccessToken(_appId);

            var file   = @"E:\1.jpg";
            var result = MediaApi.UploadForeverMedia(accessToken, file);

            Assert.IsNotNull(result.media_id);

            var new1 = new NewsModel()
            {
                author             = "test",
                content            = "test",
                content_source_url = "http://qy.weiweihi.com/Content/Images/app/qyhelper.png",
                digest             = "test",
                show_cover_pic     = "1",
                thumb_media_id     = result.media_id,
                title = "test"
            };

            var new2 = new NewsModel()
            {
                author             = "test",
                content            = "test111",
                content_source_url = "http://qy.weiweihi.com/Content/Images/app/qyhelper.png",
                digest             = "test",
                show_cover_pic     = "1",
                thumb_media_id     = result.media_id,
                title = "test"
            };

            var result1 = MediaApi.UploadNews(accessToken, 10000, new1, new2);

            Assert.IsNotNull(result1.media_id);

            var clientMsgId = SystemTime.Now.Ticks.ToString();

            GroupMessageApi.SendGroupMessageByOpenId(accessToken, GroupMessageType.mpnews, result1.media_id, clientMsgId, 10000, "o3IHxjrPzMVZIJOgYMH1PyoTW_Tg", "o3IHxjrPzMVZIJOgYMH1PyoTW_Tg");
            //var result2 = MediaApi.UpdateForeverNews(accessToken, result1.media_id, 0, 10000, new2);

            MediaApi.DeleteForeverMedia(accessToken, result1.media_id);
            //Assert.AreEqual(result2.errcode, ReturnCode.请求成功);
        }
コード例 #5
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);
                }
            }
        }
コード例 #6
0
        public ActionResult Send(WeiChat_GroupMessageLog model)
        {
            var res = new AjaxResponse
            {
                Success = true,
                Message = "发送成功!"
            };

            if (string.IsNullOrWhiteSpace(model.MediaId))
            {
                res.Success = false;
                res.Message = "请选择素材!";
            }
            else
            {
                try
                {
                    var messageType = GroupMessageType.image;
                    switch (model.MessageType)
                    {
                    case SendMessageTypes.Text:
                        messageType = GroupMessageType.text;
                        break;

                    case SendMessageTypes.Image:
                        messageType = GroupMessageType.image;
                        break;

                    case SendMessageTypes.Voice:
                        messageType = GroupMessageType.voice;
                        break;

                    case SendMessageTypes.Video:
                        messageType = GroupMessageType.video;
                        break;

                    case SendMessageTypes.News:
                        messageType = GroupMessageType.mpnews;
                        break;

                    default:
                        break;
                    }
                    var q = db.WeiChat_Users.AsQueryable();
                    if (model.SexType != SexTypes.All)
                    {
                        var sexType = (WeChatSexTypes)((int)model.SexType - 1);
                        q = q.Where(p => p.Sex == sexType);
                        if (!string.IsNullOrEmpty(model.UserGroupIds))
                        {
                            //var groupId = Convert.ToInt32(model.UserGroupIds);
                            //q = q.Where(p => p.GroupIds.Contains(groupId));
                            q = q.Where(p => p.GroupIds.Contains(model.UserGroupIds));
                        }
                    }
                    switch (model.MessageType)
                    {
                    case SendMessageTypes.Text:
                    case SendMessageTypes.Image:
                    case SendMessageTypes.Voice:
                    case SendMessageTypes.Video:
                    case SendMessageTypes.News:
                    {
                        SetModel(model, default(long));
                        if (model.SexType != SexTypes.All)
                        {
                            var openIds = q.Select(p => p.OpenId).ToArray();
                            if ((openIds == null) || (openIds.Length == 0))
                            {
                                res.Success = false;
                                res.Message = "没有找到符合该条件的粉丝,请手动同步粉丝后或者修改条件后再试!";
                            }
                            var result = GroupMessageApi.SendGroupMessageByOpenId(AccessToken, messageType,
                                                                                  model.MediaId, 10000, openIds);
                            if (result.errcode != ReturnCode.请求成功)
                            {
                                res.Success = false;
                                res.Message = "发送失败!" + result.errmsg;
                            }
                        }
                        else
                        {
                            var result = GroupMessageApi.SendGroupMessageByGroupId(AccessToken, model.UserGroupIds,
                                                                                   model.MediaId, messageType, true);
                            if (result.errcode != ReturnCode.请求成功)
                            {
                                res.Success = false;
                                res.Message = "发送失败!" + result.errmsg;
                            }
                        }
                        model.IsSuccess = res.Success;
                        model.Message   = res.Message;
                        db.WeiChat_GroupMessageLogs.Add(model);
                        db.SaveChanges();
                        break;
                    }

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    res.Success     = false;
                    res.Message     = "发送失败!" + ex.Message;
                    model.IsSuccess = res.Success;
                    model.Message   = res.Message;
                    db.WeiChat_GroupMessageLogs.Add(model);
                    db.SaveChanges();
                }
            }
            return(Json(res));
        }
コード例 #7
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);
        }