生成二维码,跳转指定 url
npm install --save [email protected]
import QrcodeVue from 'qrcode.vue'
二维码
提示:扫码访问我的主页。
export default {
name: "qrcodeDemo",
components: {QrcodeVue},
data() {
return {
showPopup: false,//触发二维码
QRcodeTitle: '生成二维码',
qrcode: {
QRImgUrl: '',
QRlink: 'https://blog.csdn.net/ljz66254',
size: 200
},
}
},
mothod:{
showQRCode() {
this.QRcodeTitle = 'qrcoder of java porter';
this.showPopup = true;
}
}
修改前端代码获取后端连接
生成小程序码
public String getAccessToken() throws IOException {
String accessToken ="";
String tokenUrl ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;
String tokenJson =requestUtils.sendGet(tokenUrl);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode tokenJsonNode = objectMapper.readTree(tokenJson);
accessToken = tokenJsonNode.get("access_token").asText();
return accessToken;
}
package top.ljzstudy.controller;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import top.ljzstudy.utils.RequestUtils;
import top.ljzstudy.utils.Result;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Slf4j
@RestController("qrCodeJump/ScanQrCodeJumpController")
@RequestMapping("qrCodeJump/ScanQrCodeJump")
public class ScanQrCodeJumpController {
@Autowired
private RequestUtils requestUtils;
@Value("${wechatMini.appId}")
private String appid;
@Value("${wechatMini.appSecret}")
private String secret;
@PostMapping("createQrCode")
public Result createQrCode(@RequestParam Map<String,Object> params){
ByteArrayOutputStream os = null;
InputStream swapStream = null;
try {
String page = "pages/index/index";
String access_token =getAccessToken();
Map<String, Object> paraMap = new HashMap<>();
paraMap.put("scene", "1");
paraMap.put("page",page);
paraMap.put("width", 1280);
paraMap.put("auto_color", false);
paraMap.put("is_hyaline", false);
paraMap.put("check_path", false);
paraMap.put("env_version", "trial");
byte[] bytes = requestUtils.doImgPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token, paraMap);
String url = OSSFactory.build().uploadSuffix(bytes, "jpg");
System.out.println("url = " + url);
if(StringUtils.isNotBlank(url)){
return Result.success(url);
}else{
return Result.error("生成项目二维码失败");
}
} catch (Exception e) {
log.error("QrCodeApi productQR error:", e);
return Result.error("生成项目二维码失败");
}
}
}
二维码与小程序码的下载
mothod:{
downloadQRcode() {
const canvas = this.$refs.Qrcode
const dataURL = canvas.$el.childNodes[0].toDataURL('image/png');
// 创建一个链接元素
const link = document.createElement('a');
link.href = dataURL;
link.download = 'javaport的二维码.png';
// 模拟用户点击下载
link.click();
this.showPopup = false;
},
}
生成二维码,跳转指定的小程序的页面
package top.ljzstudy.controller;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import top.ljzstudy.utils.RequestUtils;
import top.ljzstudy.utils.Result;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Slf4j
@RestController("qrCodeJump/ScanQrCodeJumpController")
@RequestMapping("qrCodeJump/ScanQrCodeJump")
public class ScanQrCodeJumpController {
@Autowired
private RequestUtils requestUtils;
@Value("${wechatMini.appId}")
private String appid;
@Value("${wechatMini.appSecret}")
private String secret;
@PostMapping("createQrCode")
public Result createQrCode(@RequestParam Map<String,Object> params){
ByteArrayOutputStream os = null;
InputStream swapStream = null;
try {
String page = "pages/index/index.html";
String accessToken =getAccessToken();
String sendUrl = "https://open.weixin.qq.com/sns/getexpappinfo?appid=" + appid + "&path=" + page;
log.info("sendUrl = " + sendUrl);
if(StringUtils.isNotEmpty(sendUrl)){
return Result.success("operation successful",sendUrl);
}else{
new Result();
return Result.error("operation fail");
}
} catch (Exception e) {
log.error("QrCodeApi productQR error:", e);
return Result.error("生成项目二维码失败");
}
}
}
获取小程序的二维码
package top.ljzstudy.controller;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import top.ljzstudy.utils.RequestUtils;
import top.ljzstudy.utils.Result;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Slf4j
@RestController("qrCodeJump/ScanQrCodeJumpController")
@RequestMapping("qrCodeJump/ScanQrCodeJump")
public class ScanQrCodeJumpController {
@Autowired
private RequestUtils requestUtils;
@Value("${wechatMini.appId}")
private String appid;
@Value("${wechatMini.appSecret}")
private String secret;
@PostMapping("createQrCode")
public Result createQrCode(@RequestParam Map<String,Object> params){
ByteArrayOutputStream os = null;
InputStream swapStream = null;
try {
String page = "pages/index/index";
String access_token =getAccessToken();
Map<String, Object> paraMap = new HashMap<>();
paraMap.put("path",page);
paraMap.put("width", 600);
paraMap.put("env_version", "trial");
byte[] bytes = requestUtils.doImgPost("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+access_token, paraMap);
String qrCodeUrl = OSSFactory.build().uploadSuffix(bytes, "jpg");
System.out.println("qrCodeUrl = " + qrCodeUrl);
if(StringUtils.isNotEmpty(sendUrl)){
return Result.success("operation successful",sendUrl);
}else{
new Result();
return Result.error("operation fail");
}
} catch (Exception e) {
log.error("QrCodeApi productQR error:", e);
return Result.error("生成项目二维码失败");
}
}
}
涉及的微信开放接口
GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
GET https://open.weixin.qq.com/sns/getexpappinfo?appid=" + appid + "&path=" + path
POST https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN