作者: whooyun发表于: 2018-03-16 00:35
1、淘客系统中的推荐人或代理记录
2、店家发票
3、店铺会员
4、硬件设备一物一码
下面介绍公众号淘客系统中推荐人及代理对临时二维码的应用
1、根据用户的unionid生成二维码
2、下载场景二维码到服务器
3、上传临时二维码到公众号图片空间,并获取到mediaId
4、通过菜单的click事件发送给用户
5、其它用户扫描场景二维码成为下级
生成二维码
/**
* 换取二维码ticket
*
* @param access_token
* @param terminal_user_id
*/
public String getQrCode(String access_token, int terminal_user_id) {
StringBuffer bufferUrl = new StringBuffer("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=");
bufferUrl.append(access_token);
QrCodeDto qrCode = new QrCodeDto();
qrCode.setAction_name("QR_SCENE");
qrCode.setExpire_seconds(2592000);
QrCodeDto.Action_infoEntity a = new QrCodeDto.Action_infoEntity();
QrCodeDto.Action_infoEntity.SceneEntity s = new QrCodeDto.Action_infoEntity.SceneEntity();
s.setScene_id(terminal_user_id);
a.setScene(s);
qrCode.setAction_info(a);
Gson gson = new Gson();
String qrCodeParameter = gson.toJson(qrCode);
String qrTicketResult = HttpClientUtil.doPostJson(bufferUrl.toString(), qrCodeParameter);
System.out.println("换取二维码得到的值为==>" + qrTicketResult);
return qrTicketResult;
}
下载二维码图片
/**
* @param ticket
* @param imageName 需要保存的文件名,比如abc.jpg
* @throws Exception
* @Return 文件保存的路径 比如E:\fanbo2.0\123456.jpg
*/
public static String dowloadImage(String ticket, String imageName) throws Exception {
String wxUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket;
LOGGER.debug("下载图片请求地址==>{}", wxUrl);
//new一个URL对象
URL url = new URL(wxUrl);
//打开链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
//通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
//得到图片的二进制数据,以二进制封装得到数据,具有通用性
byte[] data = readInputStream(inStream);
//new一个文件对象用来保存图片,默认保存当前工程根目录
File imageFile = new File(imageName);
//创建输出流
FileOutputStream outStream = new FileOutputStream(imageFile);
String imageFilePath = imageFile.getAbsolutePath();
//写入数据
outStream.write(data);
//关闭输出流
outStream.close();
return imageFilePath;
}
上传二维码图片
/**
* 模拟表单进行多媒体传输
*
* @param fileName (文件名字)
* @param filePath (文件内容)
* @return
* @throws Exception
*/
public static String uploadMultimedia(String access_token, String fileName, String filePath) throws Exception {
String requestUrl = "https://api.weixin.qq.com/cgi-bin/media/upload?type=image&access_token=" + access_token;
String str = "";
String result = null;
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
throw new IOException("文件不存在");
}
try {
URL url = new URL(requestUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// 以Post方式提交表单,默认get方式
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
// post方式不能使用缓存
con.setUseCaches(false);
// 设置请求头信息
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
// 设置边界
String BOUNDARY = "----------" + System.currentTimeMillis();
con.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY);
StringBuilder sb = new StringBuilder();
// 必须多两道线
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"file\";filename=\""
+ fileName + "\"\r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
byte[] head = sb.toString().getBytes("utf-8");
OutputStream out = new DataOutputStream(con.getOutputStream());
out.write(head);
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线
out.write(foot);
out.flush();
out.close();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
if (result == null) {
result = buffer.toString();
}
} catch (IOException e) {
LOGGER.info("上传图片至微信临时空间出现异常==>{}", e);
e.printStackTrace();
throw new IOException("数据读取异常");
} finally {
if (reader != null) {
reader.close();
}
}
str = buffer.toString();
LOGGER.info("上传图片后返回后的字符串==>{}", str);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
return str;
}