Pu Zhibing
2024-12-24 d09284975427910441eeaf59d34a4d50d2fd5fdc
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,7 +172,7 @@
    }
    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);
@@ -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);