知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏壧峁┍憬莸闹С郑?
您當(dāng)前位置>首頁 » 新聞資訊 » 小程序相關(guān) >
如何生成微信小程序碼(獲取微信小程序碼)
發(fā)表時間:2020-10-23
發(fā)布人:葵宇科技
瀏覽次數(shù):67
前言
在微信小程序的某些業(yè)務(wù)場景中,需要用戶微信掃碼后直接進(jìn)入到小程序的某個頁面(有時小程序碼還需攜帶一些參數(shù)),在這種場景下,就需要生成小程序碼。
流程
1、獲取小程序接口調(diào)用憑證(accesstoken)
2、獲取小程序二維碼(獲取成功后,微信服務(wù)端將返回二進(jìn)制字節(jié)流,因此需要創(chuàng)建一個文件保存圖片)
代碼案例
1、引入fastjson依賴
<!-- json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
2、Java代碼示例
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
/**
* @description 獲取小程序二維碼
* @author: liyinlong
* @date 2020/10/23 10:11
*/
public class GetMiniQrcode {
public static void main(String[] args) {
String appid = "";
String secret = "";
String token = getAccessToken(appid, secret);
//發(fā)送json請求,對象必須封裝成json格式
JSONObject params = new JSONObject();
params.put("scene","xiaozhugedeboke");//參數(shù)
params.put("page","pages/verifycode/verifycode");
//注意!一定要將對象轉(zhuǎn)成字符串
String param = params.toJSONString();
String file = "e://a.png";
getQrCode("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token, param, file);
}
/**
* @param url 發(fā)送請求的URL
* @param param 請求參數(shù)
* @return 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果
* @description 向指定 URL 發(fā)送POST方法的請求
* @author: liyinlong
* @date 2020-01-05 21:00
*/
public static String getQrCode(String url, String param,String file) {
System.out.println("\n==============================POST請求開始==============================");
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection();
// 設(shè)置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//設(shè)置Content-type 為 application/json
conn.addRequestProperty("Content-type", "application/json");
// 發(fā)送POST請求必須設(shè)置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection對象對應(yīng)的輸出流
out = new PrintWriter(conn.getOutputStream());
// 發(fā)送請求參數(shù)
out.print(param);
// flush輸出流的緩沖
out.flush();
// 定義BufferedReader輸入流來讀取URL的響應(yīng)
InputStream inputStream = conn.getInputStream();
FileOutputStream 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();
outputStream.close();
} catch (Exception e) {
System.out.println("發(fā)送 POST 請求出現(xiàn)異常!" + e);
e.printStackTrace();
}
//使用finally塊來關(guān)閉輸出流、輸入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println("url:" + url);
System.out.println("POST請求結(jié)果:" + result);
System.out.println("==============================POST請求結(jié)束==============================\n");
return result;
}
/**
* @author: liyinlong
* @description 獲取小程序接口調(diào)用憑證
* 官方文檔: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
* 接口調(diào)用url https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
* @date 2020/10/23 10:31
* @param appid 小程序appid
* @param appsecert 小程序密鑰
* @return 小程序接口調(diào)用憑證accesstoken
*/
public static String getAccessToken(String appid,String appsecert) {
System.out.println("\n==============================GET請求開始==============================");
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid
+ "&secret=" + appsecert;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection();
// 設(shè)置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//設(shè)置Content-type 為 application/json
conn.addRequestProperty("Content-type", "application/json");
// 發(fā)送POST請求必須設(shè)置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發(fā)送 GET 請求出現(xiàn)異常!" + e);
e.printStackTrace();
}
//使用finally塊來關(guān)閉輸出流、輸入流
finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println("url:" + url);
System.out.println("GET請求結(jié)果:" + result);
JSONObject res = JSONObject.parseObject(result);
System.out.println("==============================GET請求結(jié)束==============================\n");
return res.getString("access_token");
}
}
相關(guān)案例查看更多
相關(guān)閱讀
- 開發(fā)制作小程序
- 制作一個小程序
- 云南網(wǎng)站建設(shè)首選
- 網(wǎng)站建設(shè)選
- 網(wǎng)站優(yōu)化公司
- 報(bào)廢車
- 做小程序被騙
- 小程序分銷商城
- 商標(biāo)
- 江蘇小程序開發(fā)
- 楚雄網(wǎng)站建設(shè)公司
- 小程序制作
- 網(wǎng)站建設(shè)招商
- 網(wǎng)站建設(shè)首選公司
- 微信分銷
- 小程序的開發(fā)公司
- 云南花農(nóng)小程序
- 網(wǎng)站開發(fā)哪家好
- 網(wǎng)站建設(shè)高手
- 云南網(wǎng)站優(yōu)化公司
- 小程序開發(fā)費(fèi)用
- 網(wǎng)站建設(shè)專業(yè)品牌
- 網(wǎng)站建設(shè)特性
- 小程序生成海報(bào)
- 昆明網(wǎng)站設(shè)計(jì)
- web教程
- 汽車拆解管理系統(tǒng)
- 小程序技術(shù)
- 云南省城鄉(xiāng)建設(shè)廳網(wǎng)站
- 昆明網(wǎng)站開發(fā)