不想解释了,太累了,只是想说网上有很多错误的方法,腾讯也没有详细的介绍
1.用于获取access_token
1. 用于获取access_token
* @param APIKEY 小程序id
* @param SECRETKEY 小程序密钥
public static String postToken ( ) throws Exception {
String APIKEY = FileUtil. findValue ( "AppID" ) ;
String SECRETKEY = FileUtil. findValue ( "AppSecret" ) ;
String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APIKEY + "&secret=" + SECRETKEY;
URL url = new URL ( requestUrl) ;
HttpURLConnection connection = ( HttpURLConnection) url. openConnection ( ) ;
connection. setRequestMethod ( "POST" ) ;
connection. setRequestProperty ( "Content-Type" , "application/json" ) ;
connection. setRequestProperty ( "Connection" , "Keep-Alive" ) ;
connection. setUseCaches ( false ) ;
connection. setDoOutput ( true ) ;
connection. setDoInput ( true ) ;
DataOutputStream out = new DataOutputStream ( connection. getOutputStream ( ) ) ;
out. writeBytes ( "" ) ;
out. flush ( ) ;
out. close ( ) ;
connection. connect ( ) ;
BufferedReader in = null;
if ( requestUrl. contains ( "nlp" ) )
in = new BufferedReader ( new InputStreamReader ( connection. getInputStream ( ) , "GBK" ) ) ;
else
in = new BufferedReader ( new InputStreamReader ( connection. getInputStream ( ) , "UTF-8" ) ) ;
String result = "" ;
String getLine;
while ( ( getLine = in. readLine ( ) ) != null) {
result += getLine;
}
in. close ( ) ;
JSONObject jsonObject = JSON. parseObject ( result) ;
String accesstoken = jsonObject. getString ( "access_token" ) ;
return accesstoken;
}
2.生成带参小程序二维码
2. 生成带参小程序二维码
createQRCodeUrl= https: / / api. weixin. qq. com/ wxa/ getwxacodeunlimit? access_token=
* @param sceneStr 参数
* @param accessToken token
public static String getminiqrQr ( Map< String, Object> map) {
try {
String token = postToken ( ) ;
String qrCodeUrl = FileUtil. findValue ( "createQRCodeUrl" ) ;
URL url = new URL ( qrCodeUrl + token) ;
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" , map. get ( "item_id" ) + "shareId" + map. get ( "shareId" ) ) ;
paramJson. put ( "page" , "pages/item/detail/detail" ) ;
paramJson. put ( "width" , 258 ) ;
paramJson. put ( "auto_color" , true ) ;
printWriter. write ( paramJson. toString ( ) ) ;
printWriter. flush ( ) ;
BufferedInputStream bis = new BufferedInputStream ( httpURLConnection. getInputStream ( ) ) ;
String filePath = FileUtil. findValue ( "imgPath" )
+ FileUtil. findValue ( "qrCode1" )
+ map. get ( "shareId" ) + map. get ( "item_id" ) + ".png" ;
OutputStream os = new FileOutputStream ( new File ( filePath) ) ;
int len;
byte [ ] arr = new byte [ 1024 ] ;
while ( ( len = bis. read ( arr) ) != - 1 ) {
os. write ( arr, 0 , len) ;
os. flush ( ) ;
}
os. close ( ) ;
return filePath;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
return "" ;
}
工具类方法
public static String findValue ( String key) throws IOException {
Properties properties = new Properties ( ) ;
InputStream inputFile = null;
try {
ClassPathResource cpr = new ClassPathResource ( "config.properties" ) ;
inputFile = cpr. getInputStream ( ) ;
properties. load ( new InputStreamReader ( inputFile, "UTF-8" ) ) ;
String value = properties. getProperty ( key) ;
return value;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
if ( inputFile != null)
inputFile. close ( ) ;
}
return null;
}
小程序代码提取字段值
onLoad: function ( options) {
var a = options. scene;
}
点击微信开发工具, 直接打开你生成的正确二维码, 直接在你跳转的页面可以打印你输出的数据