小程序获取二维码跳转地址
/**
* 小程序获取二维码
* @param
* @throws IOException
*/
@ApiOperation("小程序获取二维码")
@GetMapping(value = "/xcx/qryQrCode")
public String qryQrCode() throws Exception {
String accessToken = AccessToken.getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+accessToken;
Map<String,Object> param = new HashMap<>();
param.put("path", "/pages/index/index");// 路径 如果没有默认跳转到首页面微信小程序发布后才可以使用不能添加参数
param.put("width", 430); //二维码尺寸
param.put("is_hyaline", true); // 是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码 参数仅对小程序码生效
param.put("auto_color", true); // 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 参数仅对小程序码生效
Map<String,Object> line_color = new HashMap<>();
line_color.put("r", 0);
line_color.put("g", 0);
line_color.put("b", 0);
param.put("line_color", line_color);
String filePath = ImgUtils.createQRCode(url,param);
if (StringUtils.isEmpty(filePath))
throw new ServiceException("获取二维码失败,请重新获取");
User users = new User();
users.setId(user.getId());
users.setQrCodeUrl(filePath);
userService.updateUser(users);
return filePath;
}
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.io.*;
import java.util.Map;
/**
*
* @author admin
*/
public class ImgUtils {
private static final Logger log = LoggerFactory.getLogger(ImgUtils.class);
public static String createQRCode(String url, Map<String,Object> param) {
RestTemplate rest = new RestTemplate();
InputStream inputStream = null;
FileOutputStream os = null;
String filename = null;
String imgpath = "/home/xcx/upload";
try {
log.info("url: "+url);
log.info("调用生成微信URL接口传参:" + param);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity requestEntity = new HttpEntity(param, headers);
ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
log.info("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody());
byte[] result = entity.getBody();
log.info(Base64.encodeBase64String(result));
inputStream = new ByteArrayInputStream(result);
filename = DateUtils.datePath() + "/" + IdUtils.fastUUID().replaceAll("-","") + ".png"; // 下载路径及下载图片名称
File file = new File(imgpath + "/" + filename);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
os = new FileOutputStream(file, true);
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 开始读取
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (Exception e) {
log.error("调用小程序生成微信永久小程序码URL接口异常",e);
} finally {
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return filename;
}
}
public String getAccessToken() {
String APP_ID = "";
String SECRET = "";
String url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", APP_ID, SECRET);
//HTTP请求
JSONObject jsonObject = Utils.requestHttp(url, HttpMethod.GET, new JSONObject());
if (ObjectUtils.isNotEmpty(jsonObject)) {
return accessToken = jsonObject.getString("access_token");
}
return null;
}
import com.alibaba.fastjson2.JSONObject;
import java.nio.charset.StandardCharsets;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
public class Utils {
/**
* 发送http请求
*
* @param url 请求地址
* @param method 请求方式
* @param json 请求参数
* @return 返回json对象
*/
public static JSONObject requestHttp(String url, HttpMethod method, JSONObject json) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
org.springframework.http.HttpEntity<String> entity = new org.springframework.http.HttpEntity<String>(json.toString(), headers);
ResponseEntity<String> response = restTemplate.exchange(url, method, entity, String.class);
return JSONObject.parseObject(response.getBody());
}
}