获取小程序二维码
@ApiOperation(value="获取小程序二维码", notes="width,path,path,grantType")
@PostMapping(value="/getWxCord")
public Result<String> getAppQrCord(@RequestParam(name = "width", defaultValue = "430") String width ,String path,String grantType){
String url = "https://api.weixin.qq.com/wxa/getwxacode";
String urls = "https://api.weixin.qq.com/cgi-bin/token";
String appid = "密钥";
String secret = "密钥";
String accessToken = WxTkenUtil.getAccessToken(urls, grantType, appid, secret);
// if(accessToken == null){
// return Result.error("access_token获取失败");
// }
String wxCode = getWxCode(url, path, width,accessToken);
return Result.OK("请求成功",wxCode);
}
package org.jeecg.modules.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.RestUtil;
import org.jeecg.config.sign.util.HttpUtils;
import javax.xml.transform.Result;
public class WxTkenUtil {
public static String getAccessToken(String url,String grantType,String appid,String secret){
String access_token ="";
String tokenUrl = url +"?grant_type="+grantType+"&appid="+appid +"&secret="+ secret;
Object result = RestUtil.get(tokenUrl);
JSONObject jsons = JSONObject.parseObject(result.toString());
String expires_in = jsons.getString("expires_in");
if(StringUtils.isNotEmpty(expires_in) && Integer.parseInt(expires_in) == 7200){
access_token = jsons.getString("access_token");
}
return access_token;
}
}
public String getWxCode (String url, String path,String width,String access_token){
String[] str = path.split("[?]");
path = str[0];
String scene = str[1];
url = url+ "?access_token="+ access_token;
JSONObject jsonObject = new JSONObject();
jsonObject.put("scene",scene);
jsonObject.put("path",path);
jsonObject.put("width",Integer.parseInt(width));
jsonObject.put("auto_color",false);
Map<String,Object> line_color = new HashMap<>();
line_color.put("r",0);
line_color.put("g",0);
line_color.put("b",0);
jsonObject.put("line_color",line_color);
// RestTemplate restTemplate = new RestTemplate();
// restTemplate.post
//
// PrintWriter printWriter = new PrintWriter(new OutputStream() {
// @Override
// public void write(int b) throws IOException {
//
// }
// });
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
// NOTE: 建议指定charset=utf-8。低于4.4.6版本的HttpCore,不能正确的设置字符集,可能导致签名错误
httpPost.addHeader("Content-Type", "application/json");
httpPost.addHeader("Accept", "application/json");
// ---------------------------核心认证 end---------------------------------------------------------------
httpPost.setEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
// 发起转账请求
CloseableHttpResponse response = httpclient.execute(httpPost);
// 获取返回的数据
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
// 从流中获取数据
byte[] data = new byte[(int) entity.getContentLength()];
int offset = 0;
int readBytes = 0;
do {
readBytes = inputStream.read(data, offset, 2048);
offset += readBytes;
} while (readBytes != -1);
// Base64 编码
Base64.Encoder encoder = Base64.getEncoder();
String result = encoder.encodeToString(data);
// System.out.println(result);
// 关闭资源
response.close();
httpclient.close();
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
// JSONObject instreams = RestUtil.post(url, jsonObject);
// return instreams;
}```