扫码加群:
微信公众平台技术文档二维码部分如下:如果用户已经关注公众号,在用户扫描后会自动进入会话,微信也会将带场景值扫描事件推送给开发者。
不说了,马赛克了
public class WxQrcode {
/**有效时间:秒为单位*/
private Integer expireSeconds;
/**二维码类型*/
private String actionName;
/**场景值ID:非0整型*/
private Integer sceneId;
/**场景之ID:字符串*/
private String sceneStr;
/**二维码详细信息*/
private String actionInfo;
public WxQrcode() {
super();
}
public Integer getExpireSeconds() {
return expireSeconds;
}
public void setExpireSeconds(Integer expireSeconds) {
this.expireSeconds = expireSeconds;
}
public String getActionName() {
return actionName;
}
public void setActionName(String actionName) {
this.actionName = actionName;
}
public Integer getSceneId() {
return sceneId;
}
public void setSceneId(Integer sceneId) {
this.sceneId = sceneId;
}
public String getSceneStr() {
return sceneStr;
}
public void setSceneStr(String sceneStr) {
this.sceneStr = sceneStr;
}
public String getActionInfo() {
return actionInfo;
}
public void setActionInfo(String actionInfo) {
this.actionInfo = actionInfo;
}
}
public class QrcodeRespInfo {
/**获取的二维码ticket,凭借此ticket可以在有效时间内换取二维码*/
private String ticket;
/**该二维码有效时间,以秒为单位。 最大不超过2592000(即30天)*/
private Integer expireSeconds;
/**二维码图片解析后的地址,开发者可根据该地址自行生成需要的二维码图片*/
private String url;
public QrcodeRespInfo() {
super();
}
public String getTicket() {
return ticket;
}
public void setTicket(String ticket) {
this.ticket = ticket;
}
public Integer getExpireSeconds() {
return expireSeconds;
}
public void setExpireSeconds(Integer expireSeconds) {
this.expireSeconds = expireSeconds;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
public enum QrcodeReqUrl {
/**POST请求:临时二维码*/
TEMPORARY_QRCODE_CREATE ("createTemporaryQrcode", "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=ACCESS_TOKEN"),
QRCODE_CREATE ("createQrcode", "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=ACCESS_TOKEN"),
/**GET请求*/
SHOW_QRCODE ("showQrcode", "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET");
private QrcodeReqUrl(String method, String url) {
this.method = method;
this.url = url;
}
/**请求路径*/
private String url;
/**请求方法*/
private String method;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
}
public class QrcodeConstants {
/**二维码类型数据字典*/
public static final String QR_CODE_TYPE = "qr_code_type";
/**临时二维码*/
public static final String QR_SCENE = "QR_SCENE";
/**临时二维码:字符串形式*/
public static final String QR_STR_SCENE = "QR_STR_SCENE";
/**永久二维码*/
public static final String QR_LIMIT_SCENE = "QR_LIMIT_SCENE";
/**永久二维码:字符串形式*/
public static final String QR_LIMIT_STR_SCENE = "QR_LIMIT_STR_SCENE";
}
public interface WxQrcodeService {
/***
* V1.0
* Discription:[临时二维码]
* Created on 2018年3月2日
* @param wxQrcode
* @param json
* @return
* @author:[soong]
*/
Json createTemporaryQrcode(WxQrcode wxQrcode, Json json);
/**
* V1.0
* Discription:[永久二维码]
* Created on 2018年3月2日
* @param wxQrcode
* @param json
* @return
* @author:[soong]
*/
Json
public class WxQrcodeServiceImpl implements WxQrcodeService {
@Override
public Json createTemporaryQrcode(WxQrcode wxQrcode, Json json) {
Map params = new HashMap();
Map sceneStrMap = new HashMap();
Map sceneMap = new HashMap();
try {
Integer expire_seconds = wxQrcode.getExpireSeconds();
String action_name = QrcodeConstants.QR_STR_SCENE;
String scene_str = wxQrcode.getSceneStr();
sceneStrMap.put("scene_str", scene_str);
sceneMap.put("scene", sceneStrMap);
//参数封装
params.put("expire_seconds", expire_seconds);
params.put("action_name", action_name);
params.put("action_info", sceneMap);
//执行结果
String result = HttpClientUtil.post(QrcodeReqUrl.TEMPORARY_QRCODE_CREATE.getUrl(), params);
System.out.println(result);
QrcodeRespInfo respInfo = (QrcodeRespInfo) JsonMapper.fromJsonString(result, QrcodeRespInfo.class);
json.setSuccess(Json.SUCCESS, "临时二维码创建成功", respInfo);
} catch (Exception e) {
e.printStackTrace();
json.setFalid(Json.BUSINESS_EXCEPTION, "临时二维码创建异常:" + e.getMessage());;
}
return json;
}
@Override
public Json createQrcode(WxQrcode wxQrcode, Json json) {
Map params = new HashMap();
Map sceneStrMap = new HashMap();
Map sceneMap = new HashMap();
try {
String action_name = QrcodeConstants.QR_LIMIT_STR_SCENE;
String scene_str = wxQrcode.getSceneStr();
sceneStrMap.put("scene_str", scene_str);
sceneMap.put("scene", sceneStrMap);
//参数封装
params.put("action_name", action_name);
params.put("action_info", sceneMap);
//执行结果
String result = HttpClientUtil.post(QrcodeReqUrl.QRCODE_CREATE.getUrl(), params);
System.out.println(result);
QrcodeRespInfo respInfo = (QrcodeRespInfo) JsonMapper.fromJsonString(result, QrcodeRespInfo.class);
json.setSuccess(Json.SUCCESS, "永久二维码创建成功", respInfo);
} catch (Exception e) {
e.printStackTrace();
json.setFalid(Json.BUSINESS_EXCEPTION, "永久二维码创建异常:" + e.getMessage());;
}
return json;
}
public static void main(String[] args){
Json json = new Json();
WxQrcodeServiceImpl impl = new WxQrcodeServiceImpl();
WxQrcode wxQrcode = new WxQrcode();
wxQrcode.setExpireSeconds(3600);
wxQrcode.setSceneStr("olTBd0VsnavWUFb2TqEHoTjI1xmo");
impl.createTemporaryQrcode(wxQrcode, json);
}
}
public class WxQrcodeController extends BaseController{
@Autowired
private WxQrcodeService wxQrcodeService;
/**
*
* Discription:[创建带参临时二维码]
* Created on 2018年3月2日
* @param memberInfo
* @param request
* @param response
* @return
* @author:[soong]
*/
@RequestMapping(value = "createTemporaryQrcode", method = RequestMethod.POST)
@ResponseBody
public Json createTemporaryQrcode(WxQrcode wxQrcode, HttpServletRequest request, HttpServletResponse response) {
Json json = new Json();
if (StringUtils.isBlank(wxQrcode.getSceneStr())) {
json.setFalid(Json.PARAM_EXCEPTION, "参数异常:sceneStr不能为空");
}
try {
json = wxQrcodeService.createTemporaryQrcode(wxQrcode, json);
} catch (BusinessException e) {
e.printStackTrace();
}
return json;
}
}
再说一遍,扫码加群