C#微信小程序订阅消息

小程序订阅消息官方配置教程文档链接:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/subscribe-message.html

wxml页面代码:

<view>

<button class="btn" type="primary" bindtap="subscribe">订阅消息</button>

</view>

js页面代码:

subscribe: function(e) {

wx.requestSubscribeMessage({

tmplIds: ['XXXXXXXXXXXXXXXXXXX'],//模板id

success:function(res){

wx.request({

            url: 'xxxxxxxxxxxxxxxxxxxxxxxxx',//接口地址

            data: { },

            success: function(res) {

                 console.log("订阅成功");

            }

          });

        }

    })

}

后端代码(C#)(这里我以“天气提醒”模板为示例,在接口中写了一个方法如下):

#region 发送订阅消息===============================

        /// <summary>

        /// 发送订阅消息

       /// <param name="touser">用户openid</param>

        /// <param name="city">城市</param>

        /// <param name="location">地址</param>

        /// <param name="weather">天气</param>

        /// <returns>返回值{"errcode":0,"errmsg":"ok"}</returns>

        /// </summary>

        public void Send_Subscribe(string touser, string city, string location, string weather)

        {

            string access_token = "Access_Token";//Access_Token

            string _url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + access_token;

            string timenow = Convert.ToDateTime(DateTime.Now).ToString("yyyy年MM月dd日");

            //json参数

            string jsonParam = "{\"touser\": \"" + touser + "\",\"template_id\": \"XXXXXXXXXXXXXX\",\"page\": \"pages/weather/weather\",\"data\": {\"date1\": { \"value\": \"" + timenow + "\"},\"phrase2\": {\"value\": \"" + city + "\"},\"phrase3\": {\"value\": \"" + location + "\"},\"character_string4\": {\"value\": \"" + weather + "\"}}}";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);

            request.Method = "POST";

            request.Timeout = 5000;

            request.ContentType = "application/json;charset=UTF-8";

            byte[] byteData = Encoding.UTF8.GetBytes(jsonParam);

            int length = byteData.Length;

            request.ContentLength = length;

            using (Stream writer = request.GetRequestStream())

            {

                writer.Write(byteData, 0, length);

                writer.Close();

            }

            string jsonStrings = string.Empty;

            using (HttpWebResponse responses = (HttpWebResponse)request.GetResponse())

            {

                using (Stream streams = responses.GetResponseStream())

                {

                    using (StreamReader readers = new StreamReader(streams, System.Text.Encoding.UTF8))

                    {

                        jsonStrings = readers.ReadToEnd();

                        responses.Close();

                        streams.Close();

                        readers.Close();

                    }

                }

            }

            //{"errcode":0,"errmsg":"ok"}

            JObject jo = (JObject)JsonConvert.DeserializeObject(jsonStrings);

            string errcode = jo["errcode"].ToString();

            string errmsg = jo["errmsg"].ToString();

            //写日志

            LogUtils.Write(errcode + "," + errmsg);

        }

        #endregion

 

示例:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值