找到这里对应相关参数
public Result<?> getItemQrcode() { //获取到微信token方法在下下边 String accessToken = weChatService.getAccessToken(); // String generate = UUIDGenerator.generate(); long time = new Date().getTime(); // String xcxQrcodePath = "/xcxqrcode/" + generate.replace("-", "") + ".jpg"; Random random = new Random(); //xcxqrcode这个是自己文件上传目录里边的一个文件夹,本人生成到这个文件夹里边里,后面是图片的名字,记得不要重复哟!!! String xcxQrcodePath = "/xcxqrcode/" + random.nextInt(10000) +time + ".jpg"; // LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); //当前用户的信息 Agency agency = accountId.accountId(); //二维码生成的方法在下边 WeChatUtil.getWxacodeunlimit( //我要穿的参数 agency.getId(), //微信token 这个是必须传的开发文档上有说明 accessToken, //配置的文件上传的目录和你自己间的文件夹,还有生成的名字👇 uploadpath + xcxQrcodePath ); //这里是返回的图片路径,我这里是写死的,只是一种思路 String a = "http://dl.zx2017.com/agency1"+xcxQrcodePath ; //本人吧图片路径存到数据库里了,查的时候直接访问就行 agencyMapper.upqrCode(agency.getId(),a); return Result.OK(a); }
获取微信token的方法
/** * @Description: 微信操作 */ @Service public class WeChatServiceImpl implements IWeChatService { //配置文件里边配置上 /像这样👇 //微信appid @Value(value = "${wx.appid}") private String appid; //微信secret @Value(value = "${wx.secret}") private String secret; //accessTokenUrl @Value(value = "${wx.accessTokenUrl}") private String accessTokenUrl; @Autowired private RedisUtil redisUtil; @Override public String getAccessToken() { //获取微信token Object tokenRedisResult = redisUtil.get("accessToken"); String token = ""; if (oConvertUtils.isEmpty(tokenRedisResult)) { //掉方法传参数,方法在下边 token = WeChatUtil.getAccessToken(appid, secret, accessTokenUrl); redisUtil.set("accessToken", token); redisUtil.expire("accessToken", 7000); } else { token = tokenRedisResult.toString(); } return token; } }
public static String getAccessToken(String homeAppId, String appSecret, String accessTokenUrl) { String accessToken = ""; /**auth.getAccessToken 接口参数说明: 小程序appId=appid 小程序appSecret=secret 授权类型=grant_type (默认值=client_credential) */ String param = "appid=" + homeAppId + "&secret=" + appSecret + "&grant_type=client_credential"; Map<String, Object> paraMap = new HashMap<>(); String[] splitArr = StrUtil.split(param, "&"); for (String item : splitArr) { String[] key_value = StrUtil.split(item, "="); paraMap.put(key_value[0], key_value[1]); } try { String responseStr = HttpUtil.get(accessTokenUrl, paraMap); log.info("access_token接口返回信息:" + responseStr); if (oConvertUtils.isNotEmpty(responseStr)) { JSONObject jsonObject = JSONObject.parseObject(responseStr); accessToken = jsonObject.getString("access_token"); } } catch (Exception e) { log.error("access_token接口失败:" + accessTokenUrl + "; param: " + param); e.printStackTrace(); } return accessToken; }
生成二维码的方法
/** * 小程序二维码 */ public static void getWxacodeunlimit(String id, String accessToken, String outPath) { try { //这是没上线之前测试用的二维码 // URL url = new URL("https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken); //上线之后用这个 URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式 // conn.setConnectTimeout(10000);//连接超时 单位毫秒 // conn.setReadTimeout(2000);//读取超时 单位毫秒 // 发送POST请求必须设置如下两行 httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); // 获取URLConnection对象对应的输出流 PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream()); //line_color生效 /*paramJson.put("auto_color", false); JSONObject lineColor = new JSONObject(); lineColor.put("r", 0); lineColor.put("g", 0); lineColor.put("b", 0); paramJson.put("line_color", lineColor); */ JSONObject paramJson = new JSONObject(); //跳转页面 前端给的小程序地址,?后边是你们两个需要的参数 没上线之前在?后拼接 String page = "pages/userindex/index?scene="+id; //首页地址 //这些东西在开发文档上仔细核对,有些是必填 paramJson.put("path",page); paramJson.put("width", 600); //上线之后用这个 // paramJson.put("scene", id); paramJson.put("auto_color", false); paramJson.put("env_version", "trial"); paramJson.put("check_path", false); paramJson.put("is_hyaline", false); printWriter.write(paramJson.toString()); // flush输出流的缓冲 printWriter.flush(); printWriter.close(); httpURLConnection.connect(); //开始获取数据 BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream()); OutputStream os = new FileOutputStream(new File(outPath)); int len; byte[] arr = new byte[1024]; while ((len = bis.read(arr)) != -1) { os.write(arr, 0, len); os.flush(); } os.close(); } catch (Exception e) { e.printStackTrace(); } }
对应好!!!。。。
仅供参考