// 二维码地址(小程序码,永久有效,数量暂无限制)
public final String WECHAT_ACODE_UNLIMIT_URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=";
public void getWxAcodePic(Integer param) {
RestTemplate restTemplate = new RestTemplate();
/**
* 组装地址
* 访问需要在地址中加入access_token,这里不详细写获取过程
*/
StringBuilder postUrl = new StringBuilder(WECHAT_ACODE_UNLIMIT_URL);
postUrl.append(tokenService.getWechatAccessToken());
try {
// 组装接口参数,这里做测试只写一个简单的int参数,根据需要自行修改
String reqParamJson = this.buildAcodeUnlimitParam(param);
HttpHeaders headers = new HttpHeaders();
//定义请求参数类型,这里用json所以是MediaType.APPLICATION_JSON
headers.setContentType(MediaType.APPLICATION_JSON);
//RestTemplate带参传的时候要用HttpEntity<?>对象传递
HttpEntity<String> request = new HttpEntity<>(reqParamJson, headers);
ResponseEntity<Resource> resp = restTemplate.postForEntity(postUrl.toString(), request, Resource.class);
Resource RespBody = resp.getBody();
// 这里需要判断,返回的是不是流,如果不是流,则会返回错误信息,这里省略
// 返回成功
if(HttpStatus.OK.equals(resp.getStatusCode()) && null != RespBody) {
// 组装文件路径
String filePath = ImageBean.IMAGE_ORDER_ACODE_PATH + order.getOpenId() + order.getEventId() + ImageBean.IMAGE_TYPE_JPG;
File acodePic = new File(filePath);
FileOutputStream fileOutStream = new FileOutputStream(acodePic);
int i;
while((i = in.read()) != -1 ){
fileOutStream.write(i);
}
// 释放资源
fileOutStream.flush();
fileOutStream.close();
in.close();
}
} catch (IOException e) {
// 会有json解析等错误,需要处理,自行斟酌
e.printStackTrace();
}
}
/**
* 二维码参数组装
*
* @return
* @throws JsonProcessingException
*/
public String buildAcodeUnlimitParam(Integer orderId) throws JsonProcessingException {
Map<String, String> reqParam = new HashMap<>();
reqParam.put("page", "pages/info/info");
reqParam.put("scene", orderId.toString());
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(reqParam);
}
tokenService中access_token的获取方法
// 微信登录请求参数
public static final String WECHAT_APP_ID = "ur app id";
public static final String WECHAT_SECRET = "ur secret";
// 微信accessToken获取地址
public static final String WECHAT_ACCESS_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
/** 微信accessToken缓存 */
public static String WECHAT_ACCESS_TOKEN_CACHE = null;
public static Long WECHAT_ACCESS_TOKEN_CACHE_TIME = null;
@Override
public String getWechatAccessToken() {
String accessToken = null;
Long now = System.currentTimeMillis();
if(null != WECHAT_ACCESS_TOKEN_CACHE
&& null != WECHAT_ACCESS_TOKEN_CACHE_TIME
&& 5000000 > now - WECHAT_ACCESS_TOKEN_CACHE_TIME) {
return WECHAT_ACCESS_TOKEN_CACHE;
} else {
RestTemplate restTemplate = new RestTemplate();
// 组装接口地址
StringBuilder accessUrl = new StringBuilder(WECHAT_ACCESS_URL);
accessUrl.append("&appid=").append(WECHAT_APP_ID);
accessUrl.append("&secret=").append(WECHAT_SECRET);
String resp = restTemplate.getForObject(accessUrl.toString(), String.class);
ObjectMapper mapper = new ObjectMapper();
try {
@SuppressWarnings("unchecked")
Map<String, String> jsonMap = mapper.readValue(resp, Map.class);
accessToken = jsonMap.get("access_token");
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
WECHAT_ACCESS_TOKEN_CACHE = accessToken;
WECHAT_ACCESS_TOKEN_CACHE_TIME = now;
}
return accessToken;
}
留言