微信公众号和微信小程序结合
1.需要申请一个微信公众号并且资质审核通过,申请一个小程序也是要资质审核通过
2.申请一个微信开放平台,将上诉两个准备好的微信公众号和微信小程序绑定在这个微信开放平台上
绑定的公众号和其测试号
绑定的小程序
3.微信开发文档下载卡券开发资料并解压,将需要的信息复制到我们的项目中。
将以下几个类复制到项目中的合适位置
之后引入相应的包
// https://mvnrepository.com/artifact/org.json/json
compile group: 'org.json', name: 'json', version: '20180813'
接下来需要一个处理post请求的工具类
package com.community.utils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @Author zhaomengxia
* @create 2019/10/18 16:50
*/
public class HttpRequestUtil {
public static String response(String url,String content) {
String line = "";
String message = "";
String returnData = "";
boolean postState = false;
BufferedReader bufferedReader = null;
try {
URL urlObject = new URL(url);
HttpURLConnection urlConn = (HttpURLConnection) urlObject.openConnection();
urlConn.setDoOutput(true);
/*设定禁用缓存*/
urlConn.setRequestProperty("Cache-Control", "no-cache");
/*维持长连接*/
urlConn.setRequestProperty("Connection", "Keep-Alive");
/*设置字符集*/
urlConn.setRequestProperty("Charset", "UTF-8");
/*设定输出格式为json*/
urlConn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
/*设置使用POST的方式发送*/
urlConn.setRequestMethod("POST");
/*设置不使用缓存*/
urlConn.setUseCaches(false);
/*设置容许输出*/
urlConn.setDoOutput(true);
/*设置容许输入*/
urlConn.setDoInput(true);
urlConn.connect();
OutputStreamWriter outStreamWriter = new OutputStreamWriter(urlConn.getOutputStream(),"UTF-8");
outStreamWriter.write(content);
outStreamWriter.flush();
outStreamWriter.close();
/*若post失败*/
if((urlConn.getResponseCode() != 200)){
returnData = "{\"jsonStrStatus\":0,\"processResults\":[]}";
message = "发送POST失败!"+ "code="+urlConn.getResponseCode() + "," + "失败消息:"+ urlConn.getResponseMessage();
// 定义BufferedReader输入流来读取URL的响应
InputStream errorStream = urlConn.getErrorStream();
if(errorStream != null)
{
InputStreamReader inputStreamReader = new InputStreamReader(errorStream,"utf-8");
bufferedReader = new BufferedReader(inputStreamReader);
while ((line = bufferedReader.readLine()) != null) {
message += line;
}
inputStreamReader.close();
}
errorStream.close();
System.out.println("发送失败!错误信息为:"+message);
}else{
/*发送成功返回发送成功状态*/
postState = true;
// 定义BufferedReader输入流来读取URL的响应
InputStream inputStream = urlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
bufferedReader = new BufferedReader(inputStreamReader);
while ((line = bufferedReader.readLine()) != null) {
message += line;
}
returnData = message;
inputStream.close();
inputStreamReader.close();
System.out.println("发送POST成功!返回内容为:" + returnData);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return returnData;
}
}
}
其中:WxCardBaseInfo这个类是处理卡券(团购券,代金券,折扣券等)的通用字段为指定的json格式
WxCard是整理卡券创建的整个json字符串(可以根据不同的卡券类型进行创建相应的卡券),WxCardGroupon类继承了WxCard,就是创建团购券的。
下面这个运行出来的结果就是我们文档中看到的创建团购券时的post数据格式实例
import java.util.ArrayList;
import java.util.Calendar;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author jackylia