Пример #1
0
        /// <summary>
        /// 发送模板消息
        /// </summary>
        /// <param name="token"></param>
        /// <param name="msg">json字符串</param>
        /// <returns></returns>
        public static bool Send(string token, string msg)
        {
            string url = "https://api.weixin.qq.com/cgi-bin/message/template/send";

            url += "?access_token=" + token;

            var res = new HttpUtil( ).PostJson(url, msg);

            Log.Logger.Log("[wx: Send发送模板消息] " + url + "#" + res);

            try
            {
                TemplateMsgResult wx_tmpl_res = JsonUtil.FromJson <TemplateMsgResult>(res);
                if (wx_tmpl_res != null && wx_tmpl_res.errcode == 0)
                {
                    return(true);
                }
                else
                {
                    Log.Logger.Log("[wx: Send发送模板消息 失败] " + url + "#" + res);
                    return(false);
                }
            }
            catch (Exception ex)
            {
                Log.Logger.Log("[wx: Send发送模板消息 错误] " + url + "#" + ex.Message);
                return(false);
            }
        }
Пример #2
0
        /// <summary>
        /// 发送模版消息
        /// </summary>
        /// <param name="templateMsg">发送消息内容</param>
        /// <param name="config">配置</param>
        /// <returns>结果</returns>
        public static Result SendTemplateMessage <T>(TemplateMsg <T> templateMsg, Config config)
        {
            Result            res        = new Result();
            var               configData = config;
            string            url        = TenpayUtil.GetTemplateUrl(configData, false);
            var               strSendRes = HttpHelp.PostXmlToUrl(url, JsonConvert.SerializeObject(templateMsg));
            TemplateMsgResult sendRes    = JsonConvert.DeserializeObject <TemplateMsgResult>(strSendRes);

            res.IsSucceed = sendRes.ErrorMsg.Trim().ToLower() == "ok";
            res.Message   = sendRes.ErrorMsg;
            if (sendRes.ErrorCode == 40001)
            {
                string            url1        = TenpayUtil.GetTemplateUrl(configData, true);
                var               strSendRes1 = HttpHelp.PostXmlToUrl(url1, JsonConvert.SerializeObject(templateMsg));
                TemplateMsgResult sendRes1    = JsonConvert.DeserializeObject <TemplateMsgResult>(strSendRes1);
                res.IsSucceed = sendRes1.ErrorMsg.Trim().ToLower() == "ok";
                res.Message   = sendRes1.ErrorMsg;
            }
            return(res);
        }