分享功能生成小程序二维码
生活随笔
收集整理的這篇文章主要介紹了
分享功能生成小程序二维码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、創建工具類HttpRequestUtil
/** *生成小程序二維碼工具類 */ public class HttpRequestUtil {/* 訪問小程序獲取數據的doGet方法* @param url 要訪問的地址* @param param 訪問的參數 * @return*/public static String doGet(String url, Map<String, String> param) {// 創建Httpclient對象RequestConfig config = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(60000).build();CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();String resultString = "";CloseableHttpResponse response = null;try {// 創建uriURIBuilder builder = new URIBuilder(url);if (param != null) {for (String key : param.keySet()) {builder.addParameter(key, param.get(key));}}URI uri = builder.build();// 創建http GET請求HttpGet httpGet = new HttpGet(uri);// 執行請求response = httpclient.execute(httpGet);// 判斷返回狀態是否為200if (response.getStatusLine().getStatusCode() == 200) {resultString = EntityUtils.toString(response.getEntity(), "UTF-8");}} catch (Exception e) {e.printStackTrace();} finally {try {if (response != null) {response.close();}httpclient.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}/***調用doGet方法*/public static String doGet(String url) {return doGet(url, null);}/*** 根據小程序APPID和SECRET獲取微信小程序token* @param APPID 小程序APPID* @param SECRET 小程序SECRET * @return* @throws JSONException*/public static String getWxAccessToken(String APPID, String SECRET) throws JSONException {String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECRET;String json = HttpRequestUtil.doGet(url);JSONObject obj = new JSONObject(json);return (String) obj.get("access_token");}/*** 生成帶參小程序二維碼** @param url 帶token的小程序生成二維碼地址* @param paraMap 生成二維碼的參數,是一個Map,存儲要跳轉的網頁URL和二維碼寬度等等* @return*/public static byte[] getminiqrQr(String url, Map<String, Object> paraMap) throws Exception {byte[] result = null;HttpPost httpPost = new HttpPost(url);httpPost.addHeader("Content-Type", "application/json");// 設置請求的參數JSONObject postData = new JSONObject();for (Map.Entry<String, Object> entry : paraMap.entrySet()) {postData.put(entry.getKey(), entry.getValue());}httpPost.setEntity(new StringEntity(postData.toString(), "UTF-8"));HttpClient httpClient = HttpClientBuilder.create().build();HttpResponse response = httpClient.execute(httpPost);HttpEntity entity = response.getEntity();result = EntityUtils.toByteArray(entity);return result;}/*** 下載帶參小程序二維碼* @param response* @param wxurl 要跳轉的頁面url* @param width 生成二維碼寬度* @param APPID 小程序APPID* @param SECRET 小程序SECRET * @throws Exception*/public static void downloadQrCode(HttpServletResponse response, String wxurl, int width, String APPID, String SECRET) throws Exception {//獲取AccessTokenString accessToken =getWxAccessToken(APPID, SECRET);byte[] qrCodeBytes = null;Map<String, Object> paraMap = new HashMap();String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=";url += accessToken;//二維碼攜帶參數 不超過32位 參數類型必須是字符串paraMap.put("path", wxurl); //存入的參數paraMap.put("width",width);qrCodeBytes=getminiqrQr(url,paraMap);response.setContentType("image/jpg");// 寫入response的輸出流中OutputStream stream = response.getOutputStream();stream.write(qrCodeBytes);stream.flush();stream.close();}}二、創建Controller提供生成二維碼接口
@Controller @RequestMapping(value = "/share") public class JobManagementController {//不同小程序的APPID和SECRET是不一樣的,不要直接復制粘貼,然后就說不行public static final String APPID="wxac123456789154";public static final String SECRET="14521545dffjsdd5f45d4fds5f4sd1";/*** 生成崗位詳情二維碼*/@GetMapping(value = "/code")public void policyCode(String uuid, HttpServletResponse response, HttpServletRequest request) {try {String wxurl = "/wx/job/getJobInfo/" + uuid; //掃描二維碼后要跳轉的頁面接口HttpRequestUtil.downloadQrCode(response,wxurl, 280, APPID, SECRET);}catch (Exception e){e.printStackTrace();}} }總結
以上是生活随笔為你收集整理的分享功能生成小程序二维码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 主流OLAP系统对比总结
- 下一篇: 哈工大刘宏伟《计算机组成原理》课程框架总