⽣成⼩程序码getwxacodeunlimit的Buffer怎么处理官⽅⽂档(巨坑)返回的说是⼀个“Buffer”,究竟特么的是个啥?
写过后端的都知道,就是⼀个 byte[] ,即 字节数组。
以下上我此次需求相关的⼀些代码,放丢失。
细节也不组织了,亲测成功。
accessToken
@Component
@Slf4j
public class AccessTokenUtil {
@Value("${terminal.miniPro.attendance.settings.appId}")
private String attendanceMiniProAppId;
@Value("${terminal.miniPro.attendance.settings.secretKey}")
private String attendanceMiniProAppSecretKey;
@Value("${quest.url}")
private String attendanceMiniProGetAccessTokenUrl;
/**
* 官⽹提供的获取accessToken的办法
* developers.weixin.qq/miniprogram/dev/api-backend/open-api/AccessToken.html
* @return
*/
public AccessTokenRes getAccessToken(){
String realUrl = String.format(attendanceMiniProGetAccessTokenUrl,attendanceMiniProAppId,attendanceMiniProAppSecretKey); 
      String accessTokenRes = (realUrl, "");
AccessTokenRes accessTokenResDO = JsonUtil.json2Object(accessTokenRes, AccessTokenRes.class);
return accessTokenResDO;
}
}
⽣成⼩程序码、将byte[]上传到aliyun OSS、将byte[]转为base64之类
@Component
@Slf4j
public class QrCodeUtil {
/**
* ⼩程序码路径根⽬录
*/
@Value("${data.service.miniPro.qrcode.path}")
private String miniProQrCodePath;
@Value("${terminal.miniPro.quest.url}")
private String attendanceMiniProGetUnlimitedQrCodeUrl;
@Autowired
private XtOSSTool xtOSSTool;
/**
* developers.weixin.qq/miniprogram/dev/api-backend/open-api/Unlimited.html
*
*
* @param getQrCodeReq
* @param accessToken
*/
public QrCodeRes getUnlimitedQrCode(GetQrCodeReq getQrCodeReq, String accessToken) throws Exception {
//临时⽂件地址
File tempFile = null;
OutputStream output = null;
QrCodeRes result = new QrCodeRes();
byte[] byteArr = getQrCodeStream(getQrCodeReq, accessToken);
//估计byteArr.lenth ⼤于200的时候才是成功了;失败时候的信息较短
if (byteArr.length < 200) {
log.info("获取⼩程序码失败,getQrCodeReq = {}", getQrCodeReq);
ExceptionUtil.throwBizException(ErrorCode.PARAM_ERR);
}
try {
String filePath = miniProQrCodePath + "/" + UUID() + ".png";
tempFile = new File(filePath);
output = new FileOutputStream(tempFile);
output.write(byteArr);
String ossFileName = xtOSSTool.SimpleUpload(tempFile);
if (null == ossFileName || ossFileName.isEmpty()) {
log.info("获取⼩程序码oss上传失败,getQrCodeReq = {}", getQrCodeReq);
ExceptionUtil.throwBizException(ErrorCode.PARAM_ERR);
}
log.info("获取⼩程序码成功,ossFileName = {}", ossFileName);
result.setSuccess(true);
result.setFilePath(ossFileName);
return result;
} catch (Exception e) {
throw e;
} finally {
if (null != output) {
output.close();
}
//如果不为null,删除临时⽂件
if (null != tempFile) {
tempFile.delete();
}
}
}
/**
* 通⽤部分
*
* @param getQrCodeReq
* @param accessToken
* @return
*/写文章的小程序
public byte[] getQrCodeStream(GetQrCodeReq getQrCodeReq, String accessToken) throws Exception {
String realUrl = String.format(attendanceMiniProGetUnlimitedQrCodeUrl, accessToken);
String realUrl = String.format(attendanceMiniProGetUnlimitedQrCodeUrl, accessToken);        Map<String, String> params = new HashMap<>();
String encodeScene = Scene();
params.put("scene", encodeScene);
params.put("page", Page());
params.put("width", "120");
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
String paramStr = Json(params);
byte[] result = httpPostWithJSON(realUrl, paramStr);
return result;
}
/**
* 来⾃地址:blog.sina/s/blog_56a68d550102ybos.html
*
* @param url
* @param json
* @return
* @throws Exception
*/
public byte[] httpPostWithJSON(String url, String json) throws Exception {
byte[] result = null;
HttpClient httpClient = ate().build();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
StringEntity se = new StringEntity(json);
se.setContentType("application/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "UTF-8"));
httpPost.setEntity(se);
HttpResponse response = ute(httpPost);
if (response != null) {
HttpEntity resEntity = Entity();
if (resEntity != null) {
//              instreams = Content();
result = ByteArray(resEntity);
}
}
httpPost.abort();
return result;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。