示例#1
0
        /// <summary>
        /// 公众号创建菜单
        ///  https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
        /// </summary>
        /// <param name="menus"></param>
        /// <returns>调用是否成功</returns>
        public bool CreateMenus(List <MenuBase> menus)
        {
            if (menus.Count > 3)
            {
                throw new Exception("菜单超限");
            }


            MenuContainerModel container = new MenuContainerModel();

            container.button = menus;

            string postData   = ZTImage.Json.JsonBuilder.ToJsonString(container);
            string url        = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + this.GetAccessToken();
            string returnData = string.Empty;

            try
            {
                returnData = ZTImage.HttpEx.SyncPost(url, postData, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                ZTImage.Log.Trace.Error("请求微信接口错误", ex);
                return(false);
            }

            WeChatReturnModel ret = ZTImage.Json.JsonParser.ToObject <WeChatReturnModel>(returnData);

            if (ret == null)
            {
                ZTImage.Log.Trace.Error("返回消息没有成功转化为json,data:" + returnData);
                return(false);
            }

            if (ret.errcode == 0)
            {
                return(true);
            }

            ZTImage.Log.Trace.Warn("设置菜单返回值为:" + returnData);
            return(false);
        }
示例#2
0
        /// <summary>
        /// 发送模板消息
        /// https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
        /// </summary>
        /// <returns></returns>
        public bool SendTemplateMessage(string touser, string template_id, string url, string dataJson)
        {
            string messageJson = "{\"touser\":\"" + touser + "\",\"template_id\":\"" + template_id + "\",";

            if (!string.IsNullOrEmpty(url))
            {
                messageJson += "\"url\":\"" + url + "\",";
            }
            messageJson += "\"data\":" + dataJson + "}";

            string posturl    = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + this.GetAccessToken();
            string returnData = string.Empty;

            try
            {
                returnData = ZTImage.HttpEx.SyncPost(posturl, messageJson, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                ZTImage.Log.Trace.Error("请求微信发送模板消息接口错误", ex);
                return(false);
            }

            WeChatReturnModel ret = ZTImage.Json.JsonParser.ToObject <WeChatReturnModel>(returnData);

            if (ret == null)
            {
                ZTImage.Log.Trace.Error("返回消息没有成功转化为json,data:" + returnData);
                return(false);
            }

            if (ret.errcode == 0)
            {
                return(true);
            }

            ZTImage.Log.Trace.Warn("发送模板消息返回值为:" + returnData);
            return(false);
        }