1,首先,必须根据小程序的appid和appSecret获取到accessToken
2,然后再根据accessToken去获取指定页面的小程序二维码
/** * 1,第一步 获取 accessToken * 2,**appId,和appSecret是小程序的**,不是公众号的,注意 * @return */ public String getAccessToken(){ String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret; JSONObject jsonObject = restTemplate.getForObject(url, JSONObject.class); String accessToken = ""; if (String.valueOf(jsonObject.get("expires_in")).equals("7200")){ accessToken = String.valueOf(jsonObject.get("access_token")); redisService.set(RedisConstants.WX_ACCESS_TOKEN, accessToken, 7200); } return accessToken; }
/** * 2,第一步 根据token获取二维码 * @param accessToken * @return */ public String getActivityQrCodeByAccessToken(String accessToken, LyCreateActivityQrCodeDto lyCreateActivityQrCodeDto) throws Exception { /********* 封装请求参数 **********/ Map<String, Object> paraMap = new HashMap<>(); //二维码携带参数 不超过32位 参数类型必须是字符串 paraMap.put("scene", lyCreateActivityQrCodeDto.getScene()); //二维码跳转页面 paraMap.put("page", lyCreateActivityQrCodeDto.getPage()); //二维码的宽度 paraMap.put("width", 450); //自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 paraMap.put("auto_color", false); //是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码 paraMap.put("is_hyaline", false); //要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop" paraMap.put("env_version", "trial"); //检查page 是否存在,为 true 时 page 必须是已经发布的小程序存在的页面(否则报错);为 false 时允许小程序未发布或者 page 不存在, 但page 有数量上限(60000个)请勿滥用 paraMap.put("check_path", false); /********* 执行post请求微信 获取二维码数据流 **********/ byte[] result = WxUtil.getQrCodeByParam("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken, paraMap); // 将获取的二维码流,存入oss,这步看具体情况,我这里是将返回的流存入了oss,然后获取的是oss给的图片地址,也可以直接返回这个base64位的字节数组,img标签展示的时候,需要加上base64处理,一样可以显示处理二维码 return QrCodeUtils.getQrCodeUrlByByteStream(result); }
package com.sport.sportadminserver.common.utils; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.ConnectionPoolTimeoutException; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import java.net.SocketTimeoutException; import java.util.Map; /** * @Description: 访问外部接口工具类 * @Author: zdj * @Date: 2021/12/08 * @version: 1.0.0 */ public class WxUtil { /** * 一物一码 * 获取小程序二维码数据流 * @param url 请求路径 * @param paraMap 其他参数 * @return */ public static byte[] getQrCodeByParam(String url, Map<String, Object> paraMap) { byte[] result = null; HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/json"); try { // 设置请求的参数 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); } catch (ConnectionPoolTimeoutException e) { e.printStackTrace(); } catch (ConnectTimeoutException e) { e.printStackTrace(); } catch (SocketTimeoutException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { httpPost.releaseConnection(); } return result; } }
结果