puzhibing
2023-08-16 d5b3e5a413bcfccba294793ee093722f31b2448a
user/guns-admin/src/main/java/com/supersavedriving/user/modular/system/util/weChat/WeChatUtil.java
@@ -80,7 +80,7 @@
                                e.printStackTrace();
                            }
                        }
                    }, 0, 7000000);
                    }, 0, 6000000);
                }catch (Exception e){
                    e.printStackTrace();
                }
@@ -412,4 +412,42 @@
        String body1 = exchange.getBody();
        return body1;
    }
    /**
     * 获取小程序urllink码
     * env_version 默认值"release"。要打开的小程序版本。正式版为 "release",体验版为"trial",开发版为"develop",仅在微信外打开时生效。
     * @return
     */
    public String getUrlLink(String path, String query, String env_version) throws Exception{
        String wxAppletsAccessToken = redisUtil.getValue("wxAppletsAccessToken");
        String url = "https://api.weixin.qq.com/wxa/generate_urllink?access_token=" + wxAppletsAccessToken;
        Map<String, Object> param = new HashMap<>();
        param.put("path", path);
        param.put("query", query);
        param.put("is_expire", true);
        param.put("expire_type", 0);
        param.put("expire_time", System.currentTimeMillis() + 60 * 60 * 1000);
        param.put("env_version", env_version);
        HttpHeaders httpHeaders = new HttpHeaders();
        MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8");
        httpHeaders.setContentType(type);
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(param, httpHeaders);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        String body1 = exchange.getBody();
        JSONObject jsonObject = JSON.parseObject(body1);
        Integer errcode = jsonObject.getInteger("errcode");
        if(null != errcode && 40001 == errcode){
            wxAppletsAccessToken = getWxAppletsAccessToken();
            if(ToolUtil.isEmpty(wxAppletsAccessToken)){
                System.err.println("获取微信小程序access_token失败");
                return "获取微信小程序access_token失败";
            }
            redisUtil.setStrValue("wxAppletsAccessToken", wxAppletsAccessToken, 7000);
            return getUrlLink(path, query, env_version);
        }
        return body1;
    }
}