Pu Zhibing
2024-12-25 0d49fd3475ffef0d26c8b68c0c8d5ae04fff2386
DriverAHTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/WeChatUtil.java
@@ -1,7 +1,12 @@
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.stylefeng.guns.core.util.ToolUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
@@ -13,10 +18,13 @@
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
 * 微信工具类
@@ -32,6 +40,9 @@
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RedisUtil redisUtil;
@@ -110,7 +121,32 @@
        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 (公众号)
@@ -118,7 +154,7 @@
     * @return
     */
    public String getAccessToken(){
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc89ecab90d24edd8&secret=bca863174d22736a4a9758a67484947e";
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxAppletsAppid + "&secret=" + wxAppletsAppSecret;
        String backData = restTemplate.getForObject(url, String.class);
        String accessToken = JSONObject.parseObject(backData).getString("access_token");
        return accessToken;
@@ -167,32 +203,34 @@
    }
    public String rid(String path, Integer driverId) throws Exception{
        // 获取access_token
        String accessToken = getAccessToken();
        // 替换CREATE_CODE_URL
        String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+accessToken;
        // 发送请求
        String stringApiResult = null;
        InputStream release = getwxacodeunlimit(path, "driverId=" + driverId, EnvVersion.RELEASE.getVersion());
        String png = ObsUploadUtil.obsUpload("png", release);
        return png;
    }
    public InputStream getwxacodeunlimit(String page, String scene, String envVersion) throws Exception{
        try {
            HashMap<String, String> map = new HashMap<>();
            map.put("path", path+"?driverId="+driverId);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity requestEntity = new HttpEntity(map, headers);
            ResponseEntity<byte[]> rep = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class);
            byte[] bytes = rep.getBody();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            MultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), byteArrayInputStream);
//            ResultUtil resultUtil = userInfoController.uploadImg(file, null, null);
            String s = OssUploadUtil.ossUpload(file,driverId);
            System.err.println("===url"+s);
//            StringBuilder sb = new StringBuilder(stringApiResult);
//            stringApiResult = sb.toString();
            return s;
        } catch (Exception e) {
            String wxAppletsAccessToken = getWxAppletsAccessToken();
            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + wxAppletsAccessToken;
            Map<String, Object> param = new HashMap<>();
            param.put("scene", scene);
            param.put("page", page);
            param.put("env_version", envVersion);
            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<byte[]> entity  = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
            byte[] body = entity.getBody();
            String s = new String(body, Charset.defaultCharset());
            System.err.println(s);
            return new ByteArrayInputStream(body);
        }catch (Exception e){
            e.printStackTrace();
        }
        return stringApiResult;
        return null;
    }
}