Beispiel #1
0
        /// <summary>
        /// 发送语音消息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="openId"></param>
        /// <param name="mediaId"></param>
        /// <returns></returns>
        public static bool SendVoice(string companyId, string accessToken, string openId, string mediaId)
        {
            var data = new
            {
                touser  = openId,
                msgtype = "voice",
                voice   = new
                {
                    media_id = mediaId
                }
            };

            ResError result = WxHttp.Post(WxUrl.SendMsg.ToFormat(accessToken), data);

            if (result.errcode == ResCode.请求成功)
            {
                return(true);
            }
            if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效)
            {
                WX_ApiConfig config         = WXApiConfigServices.QueryWXApiConfig(companyId);
                var          newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true);
                ResError     newResult      = WxHttp.Post(WxUrl.SendMsg.ToFormat(newAccessToken), data);
                if (newResult.errcode == ResCode.请求成功)
                {
                    return(true);
                }
            }
            TxtLogServices.WriteTxtLogEx("WeiXinBase", "发送语音消息错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg);
            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// 删除群发
        /// 请注意,只有已经发送成功的消息才能删除删除消息只是将消息的图文详情页失效,已经收到的用户,还是能在其本地看到消息卡片。
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="msgId">发送出去的消息ID</param>
        /// <returns></returns>
        public static bool SendAllDelete(string companyId, string accessToken, long msgId)
        {
            var data = new
            {
                msgid = msgId
            };

            ResError result = WxHttp.Post(WxUrl.SendAllDelete.ToFormat(accessToken), data);

            if (result.errcode == ResCode.请求成功)
            {
                return(true);
            }
            if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效)
            {
                WX_ApiConfig config         = WXApiConfigServices.QueryWXApiConfig(companyId);
                var          newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true);
                ResError     newResult      = WxHttp.Post(WxUrl.SendAllDelete.ToFormat(newAccessToken), data);
                if (newResult.errcode == ResCode.请求成功)
                {
                    return(true);
                }
            }
            TxtLogServices.WriteTxtLogEx("WeiXinBase", "删除群发发生错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg);
            return(false);
        }
Beispiel #3
0
        public static bool SendTemplateMessage <T>(string companyId, string accessToken, string openId, string templateId, string topcolor, T data)
        {
            var pdata = new Templete
            {
                template_id = templateId,
                topcolor    = topcolor,
                touser      = openId,
                data        = data
            };
            ResError result = WxHttp.Post(WxUrl.TemplateSend.ToFormat(accessToken), pdata);

            if (result.errcode == ResCode.请求成功)
            {
                return(true);
            }
            if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效)
            {
                WX_ApiConfig config         = WXApiConfigServices.QueryWXApiConfig(companyId);
                var          newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true);
                ResError     newResult      = WxHttp.Post(WxUrl.TemplateSend.ToFormat(newAccessToken), pdata);
                if (newResult.errcode == ResCode.请求成功)
                {
                    return(true);
                }
            }
            TxtLogServices.WriteTxtLogEx("WeiXinBase", "发送模板消息错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg);
            return(false);
        }
Beispiel #4
0
        /// <summary>
        /// 移动用户分组
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="openId"></param>
        /// <param name="toGroupId"></param>
        /// <returns></returns>
        public static bool UpdateUserGroup(string companyId, string accessToken, string openId, int toGroupId)
        {
            var data = new
            {
                openid     = openId,
                to_groupid = toGroupId
            };
            ResError result = WxHttp.Post(WxUrl.UpdateUserGroup.ToFormat(accessToken), data);

            if (result.errcode == ResCode.请求成功)
            {
                return(true);
            }
            if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效)
            {
                WX_ApiConfig config         = WXApiConfigServices.QueryWXApiConfig(companyId);
                var          newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true);
                ResError     newResult      = WxHttp.Post(WxUrl.UpdateUserGroup.ToFormat(newAccessToken), data);
                if (newResult.errcode == ResCode.请求成功)
                {
                    return(true);
                }
            }
            TxtLogServices.WriteTxtLogEx("WeiXinBase", "移动用户分组发生错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg);
            return(false);
        }
Beispiel #5
0
        /// <summary>
        /// 发送音乐消息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="openId"></param>
        /// <param name="title">音乐标题(非必须)</param>
        /// <param name="description">音乐描述(非必须)</param>
        /// <param name="musicUrl">音乐链接</param>
        /// <param name="hqMusicUrl">高品质音乐链接,wifi环境优先使用该链接播放音乐</param>
        /// <param name="thumbMediaId">视频缩略图的媒体ID</param>
        /// <returns></returns>
        public static bool SendMusic(string companyId, string accessToken, string openId, string title, string description, string musicUrl, string hqMusicUrl, string thumbMediaId)
        {
            var data = new
            {
                touser  = openId,
                msgtype = "music",
                music   = new
                {
                    title          = title,
                    description    = description,
                    musicurl       = musicUrl,
                    hqmusicurl     = hqMusicUrl,
                    thumb_media_id = thumbMediaId
                }
            };
            ResError result = WxHttp.Post(WxUrl.SendMsg.ToFormat(accessToken), data);

            if (result.errcode == ResCode.请求成功)
            {
                return(true);
            }

            if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效)
            {
                WX_ApiConfig config         = WXApiConfigServices.QueryWXApiConfig(companyId);
                var          newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true);
                ResError     newResult      = WxHttp.Post(WxUrl.SendMsg.ToFormat(newAccessToken), data);
                if (newResult.errcode == ResCode.请求成功)
                {
                    return(true);
                }
            }
            TxtLogServices.WriteTxtLogEx("WeiXinBase", "发送音乐消息错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg);
            return(false);
        }
Beispiel #6
0
        /// <summary>
        /// 媒体文件上传接口
        ///注意事项
        ///1.上传的媒体文件限制:
        ///图片(image) : 1MB,支持JPG格式
        ///语音(voice):1MB,播放长度不超过60s,支持MP4格式
        ///视频(video):10MB,支持MP4格式
        ///缩略图(thumb):64KB,支持JPG格式
        ///2.媒体文件在后台保存时间为3天,即3天后media_id失效
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="type">上传文件类型</param>
        /// <param name="fileName">上传文件完整路径+文件名</param>
        /// <returns></returns>
        //public static MediaUpload UploadMediaFile(string accessToken, MediaType type, string fileName)
        //{
        //    var fileStream = FileService.GetFileStream(fileName);
        //    var url = WxUrl.UploadMedia.ToFormat(accessToken, type.ToString().ToLower());
        //    var data = new
        //    {
        //        filename = Path.GetFileName(fileName),
        //        filelength = fileStream != null ? fileStream.Length : 0
        //    };
        //    return WxHttp.Post<MediaUpload>(url, data, fileStream);
        //}
        public static MediaUpload UploadMediaFile(string accessToken, MediaType type, string fileName)
        {
            //var fileStream = FileService.GetFileStream(fileName);
            var url = WxUrl.UploadMedia.ToFormat(accessToken, type.ToString().ToLower());

            //url += string.Format("&filename={0}&filelength={1}", Path.GetFileName(fileName), fileStream != null ? fileStream.Length : 0);
            return(WxHttp.Post <MediaUpload>(url, fileName));
        }
Beispiel #7
0
        /// <summary>
        /// 查询用户所在分组
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="openId"></param>
        /// <returns></returns>
        public static GroupId GetGroupId(string accessToken, string openId)
        {
            var url  = WxUrl.GetUserGroupId.ToFormat(accessToken);
            var data = new
            {
                openid = openId
            };

            return(WxHttp.Post <GroupId>(url, data));
        }
Beispiel #8
0
        /// <summary>
        /// 创建分组
        /// </summary>
        /// <returns></returns>
        public static GroupCreate CreateGroup(string accessToken, string name)
        {
            var url  = WxUrl.CreateGroup.ToFormat(accessToken);
            var data = new
            {
                group = new
                {
                    name = name
                }
            };

            return(WxHttp.Post <GroupCreate>(url, data));
        }
Beispiel #9
0
        /// <summary>
        /// 根据OpenID列表群发
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="mediaId">用于群发的图文消息的media_id</param>
        /// <param name="toOpenIds">填写图文消息的接收者,一串OpenID列表,OpenID最少1个,最多10000个</param>
        /// <param name="msgtype">群发的消息类型,图文消息为mpnews</param>
        /// <returns></returns>
        public static SendAllResult SendAll(string accessToken, string mediaId, IEnumerable <string> toOpenIds, string msgtype = "mpnews")
        {
            var url  = WxUrl.SendAllToOpenId.ToFormat(accessToken);
            var data = new
            {
                touser = toOpenIds,
                mpnews = new
                {
                    media_id = mediaId
                },
                msgtype = msgtype
            };

            return(WxHttp.Post <SendAllResult>(url, data));
        }
Beispiel #10
0
        /// <summary>
        /// 根据分组进行群发
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="groupId">群发到的分组的group_id</param>
        /// <param name="mediaId">用于群发的消息的media_id</param>
        /// <param name="msgtype">群发的消息类型,图文消息为mpnews</param>
        /// <returns></returns>
        public static SendAllResult SendAll(string accessToken, string groupId, string mediaId, string msgtype = "mpnews")
        {
            var url  = WxUrl.SendAll.ToFormat(accessToken);
            var data = new
            {
                filter = new
                {
                    group_id = groupId
                },
                mpnews = new
                {
                    media_id = mediaId
                },
                msgtype = msgtype
            };

            return(WxHttp.Post <SendAllResult>(url, data));
        }
Beispiel #11
0
        /// <summary>
        /// 创建二维码
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="sceneId">场景值ID,临时二维码时为32位整型,永久二维码时最大值为1000</param>
        /// <param name="expireSeconds">该二维码有效时间,以秒为单位。 最大不超过1800。不填和0时为永久二维码</param>
        /// <returns></returns>
        public static QrCode CreateQrCode(string companyId, string accessToken, int sceneId, int expireSeconds = 0)
        {
            var    url  = WxUrl.CreateQrCode.ToFormat(accessToken);
            var    guid = Guid.NewGuid().ToString("N");
            object data;

            if (expireSeconds > 0)
            {
                data = new
                {
                    expire_seconds = expireSeconds,
                    action_name    = "QR_SCENE",
                    action_info    = new
                    {
                        scene = new
                        {
                            scene_id = sceneId
                        }
                    }
                };
                //LogServices.WriteTxtLogEx("CreateQrCode", "Create sceneId:{0},expire_seconds:{1},guid:{2}", sceneId, expireSeconds, guid);
            }
            else
            {
                data = new
                {
                    action_name = "QR_LIMIT_SCENE",
                    action_info = new
                    {
                        scene = new
                        {
                            scene_id = sceneId
                        }
                    }
                };
                //LogServices.WriteTxtLogEx("CreateQrCode", "Create static sceneId:{0},guid:{1}", sceneId, guid);
            }
            var qrCode = WxHttp.Post <QrCode>(url, data);

            //LogServices.WriteTxtLogEx("CreateQrCode", "Create sceneId:{0},guid:{1},ticket:{2}", sceneId, guid, qrCode.Ticket);
            return(qrCode);
        }
Beispiel #12
0
        /// <summary>
        /// 创建菜单
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="mainButton"></param>
        /// <returns></returns>
        public static bool CreateMenu(string companyId, string accessToken, MainButton mainButton)
        {
            ResError result = WxHttp.Post(WxUrl.CreateMenu.ToFormat(accessToken), mainButton);

            if (result.errcode == ResCode.请求成功)
            {
                return(true);
            }
            if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效)
            {
                WX_ApiConfig config         = WXApiConfigServices.QueryWXApiConfig(companyId);
                var          newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true);
                ResError     newResult      = WxHttp.Post(WxUrl.CreateMenu.ToFormat(newAccessToken), mainButton);
                if (newResult.errcode == ResCode.请求成功)
                {
                    return(true);
                }
            }
            TxtLogServices.WriteTxtLogEx("WeiXinBase", "微信创建菜单错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg);
            return(false);
        }
Beispiel #13
0
        /// <summary>
        /// 发送图文消息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="openId"></param>
        /// <param name="articles"></param>
        /// <returns></returns>
        public static bool SendNews(string companyId, string accessToken, string openId, List <Article> articles)
        {
            var data = new
            {
                touser  = openId,
                msgtype = "news",
                news    = new
                {
                    articles = articles.Select(z => new
                    {
                        title       = z.Title,
                        description = z.Description,
                        url         = z.Url,
                        picurl      = z.PicUrl//图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80
                    }).ToList()
                }
            };
            ResError result = WxHttp.Post(WxUrl.SendMsg.ToFormat(accessToken), data);

            if (result.errcode == ResCode.请求成功)
            {
                return(true);
            }

            if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效)
            {
                WX_ApiConfig config         = WXApiConfigServices.QueryWXApiConfig(companyId);
                var          newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true);
                ResError     newResult      = WxHttp.Post(WxUrl.SendMsg.ToFormat(newAccessToken), data);
                if (newResult.errcode == ResCode.请求成功)
                {
                    return(true);
                }
            }
            TxtLogServices.WriteTxtLogEx("WeiXinBase", "发送图文消息错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg);
            return(false);
        }
Beispiel #14
0
        /// <summary>
        /// 上传图文消息素材
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="news"></param>
        /// <returns></returns>
        public static SendAllNewsCreate UploadNews(string accessToken, SendAllNews news)
        {
            var url = WxUrl.SendAllUploadNews.ToFormat(accessToken);

            return(WxHttp.Post <SendAllNewsCreate>(url, news));
        }