小程序单次点击获取多次formId,发送模板消息

小程序单次点击获取多次formId,发送模板消息

        页面的 <form/> 组件,属性report-submittrue时,可以声明为需发模板消息,此时点击按钮提交表单可以获取formId,用于发送模板消息。或者当用户完成支付行为,可以获取prepay_id用于发送模板消息。

        此处的一个formId在7天内可以推送一次模板消息。  但有时我们需要用户一次点击之后,可以多次给用户发送推送消息(提醒)。就需要在用户点击一次后,获取多个formId,然后保存到后台。

直接上代码

    亲测可用

  1. wxml代码    

单次点击获取单个formid

<form bindsubmit="formSubmit" report-submit="true">
  <button formType="submit" class='btn'>
    <view class="vv">
      fff
    </view>
  </button>
</form>

单次点击获取多个formid,下面代码是单次点击获取5个formid。要获取多少个formid,则复制多少次,内嵌在里面。

<form bindsubmit="formSubmit" report-submit="true">
  <button formType="submit" class='btn'>
    <view class="vv">
      <form bindsubmit="formSubmit" report-submit="true">
        <button formType="submit" class='btn'>
          <view class="vv">
            <form bindsubmit="formSubmit" report-submit="true">
              <button formType="submit" class='btn'>
                <view class="vv">
                  <form bindsubmit="formSubmit" report-submit="true">
                    <button formType="submit" class='btn'>
                      <view class="vv">
                        <form bindsubmit="formSubmit" report-submit="true">
                          <button formType="submit" class='btn'>
                            <view class="vv">
                              获取formId
                            </view>
                          </button>
                        </form>
                      </view>
                    </button>
                  </form>
                </view>
              </button>
            </form>
          </view>
        </button>
      </form>
    </view>
  </button>
</form>

点击按钮,则可获得5次formid。

2.wxss代码

.btn {
  width: 0rpx;
  height: 0rpx;
  margin: 0;
  padding: 0;
  border-radius: 0;
  position:absolute;
  background: rgba(0, 155, 0, 0.5);
  top: 0;
}

.vv {
  width: 200rpx;
  height: 200rpx;
  background: rgba(0, 155, 0, 0.5);
  position: fixed;
  top: 0;
}

3.js代码,在这里可以加上后台保存formId的方法,在有效期(7天)内用这些formId给用户发送模板消息(提醒)。

formSubmit:function(e){
    console.log(e.detail.formId);
  }

4.java后端代码

public static void main(String[] args) {
		Map<String, Object> params = new HashMap<>();
		//发送模版消息
		String accessToken = "accessToken";
		params.put("touser", "需要发送的openid或unionid");
		params.put("template_id", "模板id");
		params.put("page", "pages/index/index");
		params.put("form_id", "获取的formid");
		params.put("data", buildTemplateInfo());
		params.put("color", "");
		params.put("emphasis_keyword", "keyword1.DATA");
		String res = SendHttpReqUtil.sendPost("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token="+accessToken, params);
		System.out.println(res);
	}
	private static Map<String, Object> buildTemplateInfo(){
		Map<String, Object> templateInfo =new HashMap<>();
		Map<String, Object> info =new HashMap<>();
		info.put("value", "大吉大利,今晚吃鸡");
		templateInfo.put("keyword1", info);
		info =new HashMap<>();
		info.put("value", "你的好友已完成评价");
		templateInfo.put("keyword2", info);
		info =new HashMap<>();
		info.put("value", "orderId0000001");
		templateInfo.put("keyword3", info);
		info =new HashMap<>();
		info.put("value", "这是评价评估");
		templateInfo.put("keyword4", info);
		return templateInfo;
	}
 /** 
     * 发送HttpPost请求 
     *  
     * @param strURL 
     *            服务地址 
     * @param params 
     *  
     * @return 成功:返回json字符串<br/> 
     */  
    public static String sendPost(String strURL, Map<String, Object> params) {  
        try {  
            URL url = new URL(strURL);// 创建连接  
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
            connection.setDoOutput(true);  
            connection.setDoInput(true);  
            connection.setUseCaches(false);  
            connection.setInstanceFollowRedirects(true);  
            connection.setRequestMethod("POST"); // 设置请求方式  
            connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式  
            connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式  
            connection.connect();  
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码  
            out.append(JsonUtil.getJsonStr(params));
            out.flush();  
            out.close();  
  
            int code = connection.getResponseCode();  
            InputStream is = null;  
            if (code == 200) {  
                is = connection.getInputStream();  
            } else {  
                is = connection.getErrorStream();  
            }  
  
            // 读取响应  
            int length = (int) connection.getContentLength();// 获取长度  
            if (length != -1) {  
                byte[] data = new byte[length];  
                byte[] temp = new byte[512];  
                int readLen = 0;  
                int destPos = 0;  
                while ((readLen = is.read(temp)) > 0) {  
                    System.arraycopy(temp, 0, data, destPos, readLen);  
                    destPos += readLen;  
                }  
                String result = new String(data, "UTF-8"); // utf-8编码  
                return result;  
            }  
  
        } catch (IOException e) {  
        	logger.error("Exception occur when send http post request!", e);  
        }  
        return "error"; // 自定义错误信息  
    }  
获取formid 借鉴了  http://www.51xuediannao.com/xiaochengxu/0cba78ba.html

猜你喜欢

转载自blog.csdn.net/u013786328/article/details/80913182