二维码的两种生成方式:
1、临时二维码,是有过期时间的,最长可以设置为在二维码生成后的30天(即2592000秒)后过期,但能够生成较多数量。临时二维码主要用于帐号绑定等不要求二维码永久保存的业务场景 2、永久二维码,是无过期时间的,但数量较少(目前为最多10万个)。永久二维码主要用于适用于帐号绑定、用户来源统计等场景。
/**
* 创建二维码服务路径
*/
private final static String CREATE_URL="http://api.weixin.qq.com/cgi-bin/qrcode/create";
/**
* 获取二维码服务路径
*/
private final static String GET_URL="http://mp.weixin.qq.com/cgi-bin/showqrcode";
/**
* 生成临时二维码
* @param sceneid
* @param token
* @param times
* @return
*/
public PatientTicket createLsTicket(String sceneid,String token,int times){
PatientTicket t=null;
try{
String tourl=CREATE_URL+"?access_token="+token;
HashMap<String,Object> params=new HashMap<String,Object>();
params.put("action_name", "QR_SCENE");
HashMap<String, PatientScene> semap=new HashMap<String,PatientScene>();
semap.put("scene", new PatientScene(sceneid));
params.put("action_info", semap);
params.put("expire_seconds", times);
String msg=new Gson().toJson(params);
String result=new HttpRequest().post(msg, tourl, null);
if(StringUtils.isNotBlank(result)&&result.contains("ticket")){
JSONTicket jt=new Gson().fromJson(result, JSONTicket.class);
t=new PatientTicket();
t.setTicket(jt.getTicket());
t.setSceneid(sceneid);
}
}catch(Exception e){
LogPrintUtil.printExceptionLog(logger, e);
}
return t;
}
/**
* 生成临时二维码
* @param sceneid
* @param token
* @param times
* @return
*/
public PatientTicket createLsTicket(String sceneid,String token,int times){
PatientTicket t=null;
try{
String tourl=CREATE_URL+"?access_token="+token;
HashMap<String,Object> params=new HashMap<String,Object>();
params.put("action_name", "QR_SCENE");
HashMap<String, PatientScene> semap=new HashMap<String,PatientScene>();
semap.put("scene", new PatientScene(sceneid));
params.put("action_info", semap);
params.put("expire_seconds", times);
String msg=new Gson().toJson(params);
String result=new HttpRequest().post(msg, tourl, null);
if(StringUtils.isNotBlank(result)&&result.contains("ticket")){
JSONTicket jt=new Gson().fromJson(result, JSONTicket.class);
t=new PatientTicket();
t.setTicket(jt.getTicket());
t.setSceneid(sceneid);
}
}catch(Exception e){
LogPrintUtil.printExceptionLog(logger, e);
}
return t;
}