Generate QR code for WeChat applet

First of all, let's talk about some digressions. I am a novice in the java industry. Because I changed my career to do java, I will not mention my previous work.

I have written a few articles before, but I just read them and I thought they were relatively low, so I deleted them, so it is also the first essay to enter the java industry. Please bear with me, thank you in advance.

The reason why I talk about the QR code of the applet is because this business has really troubled me for several days, and the applet is still very popular recently. After the payment function is completed, the payment function will be briefly stroked.

Our company's business is separated from the front and back ends. I am responsible for the back-end java, so I mainly talk about the java part.

Back to the topic, describe the business, use the front-end of the applet to pass parameters to the back-end, the back-end generates a QR code, save it locally, and the front-end calls the local QR code image to display, and the user can scan the QR code to get the corresponding interface. The process of obtaining the QR code at the back end: send the url+appid and secret to the server to obtain the token, and then send the token and scene parameters to the server to obtain the QR code parameters, and the parameters are saved in the image format. Paste the main code:

//Process the main business in the controller

public R createRoomCode(int roomId) throws IOException {
//获取token_access
String params=grant_type_code+"&appid="+app_id+"&secret="+secret;
String sr = HttpRequest.sendGet("https://api.weixin.qq.com/cgi-bin/token?grant_type", params);
JSONObject json=JSONObject.fromObject(sr);
String token_access=sr.substring(sr.indexOf(":")+2,sr.indexOf(",")-1);
String sceneStr=""+roomId;
HttpUtil.getminiqrQr(sceneStr,token_access);
return R.ok("succ");
}
//getminiqrQr class processes the returned QR code parameters to save the picture
public class HttpUtil {

public static Map getminiqrQr(String sceneStr, String accessToken) {
RestTemplate rest = new RestTemplate();
InputStream inputStream = null;
OutputStream outputStream = null;
try {
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken;
Map<String,Object> param = new HashMap<>();
param.put("scene", sceneStr);
param.put("path", "pages/bookinglist/bookinglist");
param.put("width", 300);
param.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);
param.put("line_color", line_color);
System.out.println("Call the generated WeChat URL interface to pass parameters:" + 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]);
System.out.println("Call the applet to generate WeChat permanent applet code URL interface return result:" + entity.getBody());
byte[] result = entity.getBody ();
inputStream = new ByteArrayInputStream(result);

File file = new File("D:/workSpace/wechat2.0/03-Source/java2.0/room/pic/1.png");
if (!file.exists()){
file.createNewFile();
}
outputStream = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.flush();
} catch (Exception e) {
System.out.println("调用小程序生成微信永久小程序码URL接口异常");
} finally {
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}

The pit below is inexperience. After I start the back-end server, the front-end cannot access the path I specified above to save the image. Later, I asked the big guy to create a new static file under the resources folder of the ieda project. folder, put the picture in it, and then you can request the path format on the web page, such as https://domain name:port number/project name/picture name.png, you can access the

front end and directly access this path to preview the QR code .

The reference page can't be found, but thanks to the big guys on the Internet and the big guys in the company.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324845599&siteId=291194637