From 6c0d8fb2db64b8982c4162c7527e72841547110b Mon Sep 17 00:00:00 2001 From: Pu Zhibing <393733352@qq.com> Date: 星期五, 27 十二月 2024 15:28:50 +0800 Subject: [PATCH] 修改推单规则 --- UserAHTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/WeChatUtil.java | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 42 insertions(+), 7 deletions(-) diff --git a/UserAHTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/WeChatUtil.java b/UserAHTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/WeChatUtil.java index 65056ae..c6fdbd0 100644 --- a/UserAHTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/WeChatUtil.java +++ b/UserAHTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/WeChatUtil.java @@ -1,5 +1,8 @@ package com.stylefeng.guns.modular.system.util; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper; @@ -44,6 +47,9 @@ @Autowired private RestTemplate restTemplate; + + @Autowired + private RedisUtil redisUtil; @@ -56,6 +62,7 @@ String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxAppletsAppid + "&secret=" + wxAppletsAppSecret + "&js_code=" + jscode + "&grant_type=authorization_code"; String forObject = restTemplate.getForObject(url, String.class); + System.err.println(forObject); JSONObject jsonObject = JSON.parseObject(forObject); int errcode = jsonObject.getIntValue("errcode"); Map<String, String> map = new HashMap<>(); @@ -122,7 +129,35 @@ String ticket = JSONObject.parseObject(backData).getString("ticket"); return ticket; } - + + + + public String getWxAppletsAccessToken(){ + String wxAppletsAccessToken = redisUtil.getValue("wxAppletsAccessToken"); + if(ToolUtil.isNotEmpty(wxAppletsAccessToken)){ + return wxAppletsAccessToken; + } + String appletsAccessToken = getAppletsAccessToken(); + redisUtil.setStrValue("wxAppletsAccessToken", appletsAccessToken, 7200); + return appletsAccessToken; + } + + /** + * 获取微信小程序token + * @return + */ + public String getAppletsAccessToken() { + String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxAppletsAppid + "&secret=" + wxAppletsAppSecret; + HttpRequest get = HttpUtil.createGet(url); + HttpResponse response = get.execute(); + if(response.getStatus() != 200){ + return ""; + } + JSONObject jsonObject = JSON.parseObject(response.body()); + return jsonObject.getString("access_token"); + } + + /*** * 获取acess_token (公众号) @@ -137,14 +172,14 @@ } public String getUrlLink(String path, String query) throws Exception{ - String wxAppletsAccessToken = getAccessToken(); + String wxAppletsAccessToken = getWxAppletsAccessToken(); String url = "https://api.weixin.qq.com/wxa/generate_urllink?access_token=" + wxAppletsAccessToken; Map<String, Object> param = new HashMap<>(); param.put("expire_type", 1); param.put("expire_interval", 1); param.put("path", path); param.put("query", query); - param.put("env_version", "trial"); + param.put("env_version", "release"); HttpHeaders httpHeaders = new HttpHeaders(); MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8"); httpHeaders.setContentType(type); @@ -155,7 +190,7 @@ } public String getPhone(String code) throws Exception{ - String wxAppletsAccessToken = getAccessToken(); + String wxAppletsAccessToken = getWxAppletsAccessToken(); String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + wxAppletsAccessToken; Map<String, Object> param = new HashMap<>(); param.put("code",code); @@ -181,16 +216,16 @@ @Autowired private UserInfoController userInfoController; - public String rid(String path, String query) throws Exception{ + public String rid(String path, Integer driverId) throws Exception{ // 获取access_token - String accessToken = getAccessToken(); + String accessToken = getWxAppletsAccessToken(); // 替换CREATE_CODE_URL String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+accessToken; // 发送请求 String stringApiResult = null; try { HashMap<String, String> map = new HashMap<>(); - map.put("path", path+"?driverId=979"); + map.put("path", path+"?driverId=" + driverId); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity requestEntity = new HttpEntity(map, headers); -- Gitblit v1.7.1