ミニプログラムのサンコード生成は以下の方法で実現できます。
#制限付き Sun コードの生成public static String getAccessToken(String appid, String appsecret) { String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appsecret+""; String accessToken = null; try { String response = HttpClientUtil.getInstance().sendHttpsGet( requestUrl, null); JSONObject json = JSONObject.parseObject(response); accessToken = String.valueOf(json.get("access_token")); } catch (Exception e) { logger.error("getAccessToken error", e); } return accessToken; }
public static String generatLimitSunCode(WxScanCodeParam param) throws Exception { String token =param.getAccessToken(); Map<String, String> params = new HashMap<>(); params.put("path", param.getPath()); params.put("width", "430"); CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getwxacode?access_token="+token); httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json"); String body = JSON.toJSONString(params); StringEntity entity = new StringEntity(body); entity.setContentType("image/jpg"); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { HttpEntity entity2 = response.getEntity(); if(!entity2.getContentType().getValue().equals("image/jpeg")) { String result = EntityUtils.toString(entity2, "UTF-8"); logger.error("generate sun code error,http execute result:" + result); return null; } } else { logger.error("generate sun code error,http execute result:" + statusCode); } InputStream inputStream = response.getEntity().getContent(); // 保存图片到本地 int flag = saveImg(inputStream, param.getFilePath(), name); if (flag == 0) { throw new SysException("保存图片[" + name + "]失败"); } else { logger.info("太阳码[{}]生成成功", name); } return param.getFilePath() + File.separatorChar + name; }
/** * 生成无限制的小程序码 * @param param * @return * @throws Exception */ public static String generatUnlimitSunCode(WxScanCodeParam param) throws Exception { String token =param.getAccessToken(); Map<String, String> params = new HashMap<>(); params.put("scene", param.getScene()); params.put("page", param.getPath()); params.put("width", "430"); CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token); httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json"); String body = JSON.toJSONString(params); StringEntity entity = new StringEntity(body); entity.setContentType("image/jpg"); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { HttpEntity entity2 = response.getEntity(); if(!entity2.getContentType().getValue().equals("image/jpeg")) { String result = EntityUtils.toString(entity2, "UTF-8"); logger.error("generate sun code error,http execute result:" + result); return null; } } else { logger.error("generate sun code error,http execute result:" + statusCode); } InputStream inputStream = response.getEntity().getContent(); //太阳码写标题 String content=param.getWriteContent(); if(StringUtil.isNotEmpty(content) && param.isWrite()) { inputStream = ImageUtil.addImageTitle(param.getWriteContent(), inputStream, 450, 450); } String name = param.getFileName()+".jpg";//文件名加后缀,跟上面对应 int flag = saveImg(inputStream, param.getFilePath(), name);// 保存图片 if (flag == 0) { throw new SysException("保存图片[" + name + "]失败"); } else { logger.info("太阳码[{}]生成成功", name); } return param.getFilePath() + File.separatorChar + name; }
#
以上がJava で WeChat アプレットの Sun コードを生成する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。