微信生成小程序码扫描唤醒小程序(2)
我做的是 _**带参数无限个数小程序码**,有什么问题或者错误,请及时指出!
(有些问题我还没有搞懂!但是代码能跑,小程序码能跳转!)
微信官方文档地址 微信公众平台配置说明
- 配置微信小程序后台(至今也不能跳转到开发版),登陆微信公众平台 ,在首页下面找到<开发>点击进去,然后在当前页面点击开发设置,滑到页面最下方
- 接下来看下具体的配置(只供参考,我也配的不是很明确,参考者自己去看下文档吧)
- 这里需要说下,调用微信接口生成小程序码需要accessToken,如何获取accessToken,这边我还是把代码贴一下吧
@PostMapping("getToken")
@ApiOperation(value = "获取AccessToken")
public JSONObject getAccessToken(@ReqHeader @ApiIgnore ApiReqHeader reqHeader) throws IOException {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
String APPID = "你的AppId";
String APPSECRET = "你的AppSecret";
String request_url = url.replace("APPID", APPID).replace("APPSECRET", APPSECRET);
URL conn_url = new URL(request_url);
HttpURLConnection conn = (HttpsURLConnection)conn_url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
conn.connect();
JSONObject jsonObject = null;
if(conn.getResponseCode()==200){
InputStream stream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(stream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
stream.close();
conn.disconnect();
jsonObject = JSONObject.parseObject(buffer.toString());
}
return jsonObject;
}
- 切记,如果AppId和AppSecret获取错了或者配置文件里的错了(scene中的参数没有的也是生成不了二维码的),你生成的二维码是没有用的
- accessToken有了就来调用微信的接口吧,
Controller
@PostMapping("createQRCodeMap/{accessToken}")
@ApiOperation(value = "生成二维码",notes="路径后面带AccessToken")
public ResultWrap<MemberInfoURLIO> createQRCodeMap(@ReqHeader ApiReqHeader reqHeader,@PathVariable("accessToken") String accessToken){
log.info("接收{}请求报文:{}", OperationType.CREATE, LogWriteUtils.getJsonStr(reqHeader));
String base64 = memberInfoService.createQRCodeMap(reqHeader,accessToken);
String path = fastDFSFileUploadManager.uploadBase64(base64);
String url = "http://***.115.137.33:880";
MemberInfoBO memberInfoBO = memberInfoService.updataInvitationCodeUrl(reqHeader, url.concat("/").concat(path));
MemberInfoURLIO memberInfoURLIO = POJOUtils.copyProperties(memberInfoBO, MemberInfoURLIO.class);
return RestParamsUtils.toRespWrapSucess(memberInfoURLIO);
}
Service
public String createQRCodeMap(ApiReqHeader reqHeader, String accessToken) {
MemberInfoPO memberInfoPO = memberInfoDao.findById(reqHeader.getUserId());
MemberInfoVO memberInfoVO = POJOUtils.copyProperties(memberInfoPO, MemberInfoVO.class);
String invitationCode = memberInfoVO.getInvitationCode();
String base64String = "";
try {
URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
JSONObject paramJson = new JSONObject();
paramJson.put("scene", invitationCode);
paramJson.put("path", "pages/index/index");
paramJson.put("width", 430);
printWriter.write(paramJson.toString());
printWriter.flush();
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int rc = 0;
while ((rc = bis.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] bytes = swapStream.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
String binary = encoder.encodeBuffer(bytes).trim();
String replaceAll = (binary).replaceAll(" +", "");
base64String = "data:image/png;base64,"+replaceAll;
System.out.println(base64String);
} catch (Exception e) {
e.printStackTrace();
}
return base64String;
}
成果
@Slf4j
public class Test1 {
public static void main(String[] args) {
try {
URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=你的accessToken");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
JSONObject paramJson = new JSONObject();
paramJson.put("scene", "12312312");
paramJson.put("path", "pages/index/index");
paramJson.put("width", 430);
printWriter.write(paramJson.toString());
printWriter.flush();
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int rc = 0;
while ((rc = bis.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
swapStream.flush();
byte[] bytes = swapStream.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
String binary = encoder.encodeBuffer(bytes).trim();
String replaceAll = (binary).replaceAll(" +", "");
String base64String = "data:image/png;base64,"+replaceAll;
System.out.println(base64String);
System.out.println("12312312");
System.out.println("12312312");
System.out.println("12312312");
} catch (Exception e) {
e.printStackTrace();
}
}
}
尾记
- 至今还搞不懂怎么跳转到开发版,因为跳转的是线上版本的,测试很不方便(我是后端我无所谓咯),在微信的开发者专区里面找了找,都说跳不了,但是微信官方的开发者说可以! :)