Beispiel #1
0
        /// <summary>
        /// 发送客服消息
        /// </summary>
        /// <param name="strMsgType"></param>
        /// <param name="wechat"></param>
        /// <param name="strOpenids"></param>
        /// <param name="obj"></param>
        /// <param name="strTitle"></param>
        /// <param name="strComment"></param>
        /// <returns></returns>
        private static WxJsonResult SendMsgKF(Weixin.MP.GroupMessageType MsgType, SysWechatConfig wechat, string[] strOpenids, object obj, string strTitle = null, string strComment = null)
        {
            WxJsonResult Ret = null;

            foreach (var openId in strOpenids)
            {
                WxJsonResult returnResult = null;

                switch (MsgType)
                {
                case GroupMessageType.text:

                    returnResult = CustomApi.SendText(wechat.WeixinAppId, wechat.WeixinCorpSecret, openId, (string)obj);
                    break;
                //case GroupMessageType.mpnews:

                //    returnResult = CustomApi.SendNews(wechat.WeixinAppId, wechat.WeixinCorpSecret, openId, (List<Article>)obj);
                //    break;
                case GroupMessageType.mpnews:
                    returnResult = CustomApi.SendMpNews(wechat.WeixinAppId, wechat.WeixinCorpSecret, openId, (string)obj);
                    break;

                case GroupMessageType.image:
                    returnResult = CustomApi.SendImage(wechat.WeixinAppId, wechat.WeixinCorpSecret, openId, (string)obj);
                    break;

                case GroupMessageType.video:
                    returnResult = CustomApi.SendVideo(wechat.WeixinAppId, wechat.WeixinCorpSecret, openId, (string)obj, strTitle, strComment);
                    break;

                case GroupMessageType.voice:
                    returnResult = CustomApi.SendVoice(wechat.WeixinAppId, wechat.WeixinCorpSecret, openId, (string)obj);
                    break;
                }

                if (returnResult.errcode != ReturnCode.请求成功)
                {
                    Ret = returnResult;
                }
                else if (Ret == null)
                {
                    Ret = returnResult;
                }
            }

            return(Ret);
        }
Beispiel #2
0
        private static SendResult MPSendMessage(SysWechatConfig wechat, string value,
                                                Weixin.MP.GroupMessageType type, string strTags, string[] strOpenids, string[] PreviewOpenids, string VideoTitle, string VideoContent, bool isKefu)
        {
            SendResult returnResult = null;

            //客服消息
            if (isKefu)
            {
                WxJsonResult retCustom = SendMsgKF(type, wechat, strOpenids, value, VideoTitle, VideoContent);

                return(new SendResult()
                {
                    errcode = retCustom.errcode, errmsg = retCustom.errmsg
                });
            }

            //预览消息
            if (PreviewOpenids != null)
            {
                foreach (var openid in PreviewOpenids)
                {
                    returnResult = Innocellence.Weixin.MP.AdvancedAPIs.GroupMessageApi.SendGroupMessagePreview(wechat.WeixinAppId, wechat.WeixinCorpSecret, type, value, openid);
                }
                return(returnResult);
            }


            //其他正常消息
            if (strTags == "-1" || (null != strOpenids && strOpenids.Length == 1 && "@all".Equals(strOpenids[0]))) //ToAll
            {
                returnResult = Innocellence.Weixin.MP.AdvancedAPIs.GroupMessageApi.SendGroupMessageByGroupId(wechat.WeixinAppId, wechat.WeixinCorpSecret, "", value, type, true, false, 10000);
            }
            else if (!string.IsNullOrEmpty(strTags)) //To Tags
            {
                returnResult = Innocellence.Weixin.MP.AdvancedAPIs.GroupMessageApi.SendGroupMessageByGroupId(wechat.WeixinAppId, wechat.WeixinCorpSecret, strTags, value, type, false, false, 10000);
            }
            else if (strOpenids.Length > 0) //To openids
            {
                //一次最多1W人,超过1W,分批发
                int      iOpenidCount = strOpenids.Length;
                string[] TempOpenids;

                for (int i = 0; i < Math.Ceiling(iOpenidCount / 5000d); i++)
                {
                    int iTo = 5000 * (i + 1) > iOpenidCount ? (iOpenidCount - 5000 * i) : 5000;
                    TempOpenids = new string[iTo];

                    Array.Copy(strOpenids, 5000 * i, TempOpenids, 0, iTo);

                    if (type == GroupMessageType.video) //腾讯接口视频单独处理
                    {
                        returnResult = Innocellence.Weixin.MP.AdvancedAPIs.GroupMessageApi.SendVideoGroupMessageByOpenId(wechat.WeixinAppId, wechat.WeixinCorpSecret, VideoTitle, VideoContent, value, 10000, TempOpenids);
                    }
                    else
                    {
                        returnResult = Innocellence.Weixin.MP.AdvancedAPIs.GroupMessageApi.SendGroupMessageByOpenId(wechat.WeixinAppId, wechat.WeixinCorpSecret, type, value, 10000, TempOpenids);
                    }
                }
            }

            return(returnResult);
        }