扫码加群:
生成带参数的二维码
为了满足用户渠道推广分析和用户帐号绑定等场景的需要,公众平台提供了生成带参数二维码的接口。使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送。
目前有2中类型的二维码:
1.临时二维码,是有过期时间的,最长可以设置为在二维码生成后的30天(即2592000秒)后过期,但能够生成较多数量。临时二维码主要用于帐号绑定等不要求二维码永久保存的业务场景
2.永久二维码,是无过期时间的,但数量较少(目前为最多10万个)。永久二维码主要用于适用于帐号绑定、用户来源统计等场景。
用户扫描带场景值二维码时,可能推送以下两种事件:
如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。
如果用户已经关注公众号,在用户扫描后会自动进入会话,微信也会将带场景值扫描事件推送给开发者。
不说了,马赛克了
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 * <p>Discription:[临时二维码]</p> * Created on 2018年3月2日 * @param wxQrcode * @param json * @return * @author:[soong] */ Json<QrcodeRespInfo> createTemporaryQrcode(WxQrcode wxQrcode, Json<QrcodeRespInfo> json); /** * V1.0 * <p>Discription:[永久二维码]</p> * Created on 2018年3月2日 * @param wxQrcode * @param json * @return * @author:[soong] */ Json<Object> createQrcode(WxQrcode wxQrcode, Json<Object> json); }
public class WxQrcodeServiceImpl implements WxQrcodeService { @Override public Json<QrcodeRespInfo> createTemporaryQrcode(WxQrcode wxQrcode, Json<QrcodeRespInfo> json) { Map<String, Object> params = new HashMap<String, Object>(); Map<String, String> sceneStrMap = new HashMap<String, String>(); Map<String, Object> sceneMap = new HashMap<String, Object>(); 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<Object> createQrcode(WxQrcode wxQrcode, Json<Object> json) { Map<String, Object> params = new HashMap<String, Object>(); Map<String, String> sceneStrMap = new HashMap<String, String>(); Map<String, Object> sceneMap = new HashMap<String, Object>(); 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<QrcodeRespInfo> json = new Json<QrcodeRespInfo>(); 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; /** * * <p>Discription:[创建带参临时二维码]</p> * Created on 2018年3月2日 * @param memberInfo * @param request * @param response * @return * @author:[soong] */ @RequestMapping(value = "createTemporaryQrcode", method = RequestMethod.POST) @ResponseBody public Json<QrcodeRespInfo> createTemporaryQrcode(WxQrcode wxQrcode, HttpServletRequest request, HttpServletResponse response) { Json<QrcodeRespInfo> json = new Json<QrcodeRespInfo>(); if (StringUtils.isBlank(wxQrcode.getSceneStr())) { json.setFalid(Json.PARAM_EXCEPTION, "参数异常:sceneStr不能为空"); } try { json = wxQrcodeService.createTemporaryQrcode(wxQrcode, json); } catch (BusinessException e) { e.printStackTrace(); } return json; } }
再说一遍,扫码加群