微信公众平台 添加模板消息

小程序端

提交按钮必须是form提交

wxml页

<form bind:submit="testSubmit" report-submit>
  <button formType="submit" class="weui-btn" id=\'tixian\' bindtap="tixianComfirm" disabled=\'{{IsCanClick}}\' hover-class="none">
    确定提现
  </button>
</form>

 js页

//发送推送消息
var touser = that.data.openid;//接收者的openid
dataS.touser = touser;
dataS.template_id = "Qv3x9PsGlIJ5IUsfKxzFn_g4e97dRf5hhRok9CCOJps";//模板ID
dataS.page = "pages/my/my";//跳转页面
dataS.form_id = fId;//formID
var datav1 = new Object();
datav1.value = wx.getStorageSync(\'ShopName\');
var datav2 = new Object();
datav2.value = that.data.Banlance +"元";
var datav3 = new Object();
datav3.value = that.data.inputMoney + "元";
var datav4 = new Object();
datav4.value = parseFloat(that.data.Fee) * 100 + "%";
var datav5 = new Object();
var date =new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours() > 9 ? date.getHours() : "0" + date.getHours();
var minute = date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes();
var datetime = year.toString()+"年"+month.toString()+"月"+day.toString()+"日 "+hour.toString()+":"+minute.toString()+"";
console.log(datetime);
datav5.value = datetime;
var data1 = new Object();
data1.keyword1 = new Object();
data1.keyword2 = new Object();
data1.keyword3 = new Object();
data1.keyword4 = new Object();
data1.keyword5 = new Object();
data1.keyword1 = datav1;
data1.keyword2 = datav2;
data1.keyword3 = datav3;
data1.keyword4 = datav4;
data1.keyword5 = datav5;
dataS.data = data1;
var ss = JSON.stringify(dataS);
var param1 = {
action: "PushWx",
msg: ss
}
  wx.request({
            method: \'GET\',
            dataType: "json",
            header: {
              \'content-type\': \'application/json\' // 默认值
            },
            data: param1,
            url: app.data.https,
            success: function (res) {
              if (res.data.Status > 0) {
              }
            }
          });

服务器端

private void PushWx(HttpContext context)
        {
            string msg = "";
            if (!string.IsNullOrEmpty(context.Request["msg"])) { msg = Hxzs.Common.ParemConverter.ObjectToString(context.Request["msg"]); }
            new Honszeal.Business.classes.spacelife.PushWx().Send(1, msg);
        }
View Code
public void Send(int Type, string pd)
        {
            Hxzs.Common.LogUtil.WriteLog(pd);
            var l = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" + getaccess_token();
            StringBuilder result = Hxzs.Common.HttpRequest.PostPageContent(l, "", pd, "UTF-8");
            if (!string.IsNullOrEmpty(result.ToString()))
            {
                returnjson json = JsonConvert.DeserializeObject<returnjson>(result.ToString());
                Hxzs.Common.LogUtil.WriteLog(result.ToString());
            }

            //var result = pd.ToJson(); //Newtonsoft.Json.JsonConvert.SerializeObject(tempData);

        }
View Code

获取access_token

         /// <summary>
        /// 获取access_token
        /// </summary>
        /// <returns></returns>
        public static string getaccess_token()
        {
            string access_token = "";
            string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx49ebae7d3a19117f&secret=a3ed5d2fd9ff30cddf55bc37ecf85832";
            WebRequest wReq = WebRequest.Create(url);
            WebResponse wResp = wReq.GetResponse();
            Stream respStream = wResp.GetResponseStream();
            using (StreamReader reader = new StreamReader(respStream, Encoding.GetEncoding("UTF-8")))
            {
                var result = reader.ReadToEnd(); //result就是返回值
                try
                {
                    GetWxModel m = Newtonsoft.Json.JsonConvert.DeserializeObject<GetWxModel>(result);
                    access_token = m.access_token;
                }
                catch (Exception ex)
                {
                    Hxzs.Common.LogUtil.WriteLog(ex.Message);
                }
            }
            Hxzs.Common.LogUtil.WriteLog("access_token:" + access_token);
            return access_token;
        }
View Code