添加换取二维码图片url地址的重构方法(可以选择是否生成压缩的网址)

This commit is contained in:
BinaryWang 2016-06-21 11:46:48 +08:00
parent 5358d68fff
commit bc22b03ea3
2 changed files with 24 additions and 3 deletions

View File

@ -511,6 +511,17 @@ public interface WxMpService {
*/
public File qrCodePicture(WxMpQrCodeTicket ticket) throws WxErrorException;
/**
* <pre>
* 换取二维码图片url地址可以选择是否生成压缩的网址
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/18/167e7d94df85d8389df6c94a7a8f78ba.html">生成带参数的二维码</a>
* </pre>
* @param ticket 二维码ticket
* @param needShortUrl 是否需要压缩的二维码地址
* @return
* @throws WxErrorException
*/
public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException;
/**
* <pre>
* 换取二维码图片url地址

View File

@ -519,17 +519,27 @@ public class WxMpServiceImpl implements WxMpService {
}
@Override
public String qrCodePictureUrl(String ticket) throws WxErrorException {
public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException {
String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s";
try {
return this.shortUrl(String.format(url,
URLEncoder.encode(ticket, Charsets.UTF_8.name())));
String resultUrl = String.format(url,
URLEncoder.encode(ticket, Charsets.UTF_8.name()));
if(needShortUrl){
return this.shortUrl(resultUrl);
}
return resultUrl;
} catch (UnsupportedEncodingException e) {
WxError error = WxError.newBuilder().setErrorCode(-1)
.setErrorMsg(e.getMessage()).build();
throw new WxErrorException(error);
}
}
@Override
public String qrCodePictureUrl(String ticket) throws WxErrorException {
return qrCodePictureUrl(ticket, false);
}
@Override
public String shortUrl(String long_url) throws WxErrorException {