longz
2026-04-27 4d4eb6e9addc4ce148e05f470beb016e507975ba
DriverYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/WeChatUtil.java
@@ -1,21 +1,27 @@
package com.stylefeng.guns.modular.system.util;
import cn.hutool.core.io.FileUtil;
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 org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.util.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
 * 微信工具类
@@ -23,12 +29,21 @@
@Component
public class WeChatUtil {
    private String wxAppletsAppid="wxccaecee088db18d5";
    private String wxAppletsAppid="wx20301e30167d920e";
    private String wxAppletsAppSecret="c02390b36817fae7ccd27f084dfbd294";
    private String wxAppletsAppSecret="4b8fe5cea20c4309fa996caa06ac9b4c";
    @Autowired
    private RestTemplate restTemplate;
    @Value("${spring.profiles.active}")
    private String activeProfile;
    @Value("${filePath}")
    private String filePath;
    @Value("${qyt.admin_url}")
    private String adminUrl;
    /**
@@ -135,7 +150,7 @@
     * @param page      跳转页 例如 pages/index/index
     * @param scene     参数 a=1&b=2
     */
    public InputStream getwxacodeunlimit(String page, String scene, String envVersion) throws Exception{
    public InputStream getwxacodeunlimit(String page, String scene, String envVersion){
        try {
            //String wxAppletsAccessToken = redisUtil.getValue("wxAppletsAccessToken");
            String wxAppletsAccessToken = getAccessToken2();
@@ -148,17 +163,42 @@
            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, new Object[0]);
            String body1 = exchange.getBody();
//            ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class, new Object[0]);
//            String body1 = exchange.getBody();
//            System.err.println(body1);
            ResponseEntity<byte[]> entity  = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
            byte[] body = entity.getBody();
            System.err.println(JSON.toJSON(entity));
            System.err.println(Base64.encodeBase64String(body));
            return new ByteArrayInputStream(body);
            byte[] result = entity.getBody();
            if (result == null || result.length == 0) {
                return null;
            }
            MediaType contentType = entity.getHeaders().getContentType();
            // 微信成功返回image/*;失败通常返回application/json
            if (contentType != null && !StringUtils.startsWithIgnoreCase(contentType.toString(), "image/")) {
                String errorBody = new String(result, "UTF-8");
                System.err.println("getwxacodeunlimit返回非图片内容: " + errorBody);
                return null;
            }
            return new ByteArrayInputStream(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public String getUnlimitQrcode(String appletPath, String scene){
        InputStream inputStream = getwxacodeunlimit(appletPath, scene, activeProfile.equals("prod") ? "release" : "trial");
        if (inputStream != null) {
            File file = FileUtil.writeFromStream(inputStream, new File(filePath + "/" + UUID.randomUUID().toString() + ".png"));
            HttpRequest post = HttpUtil.createPost(adminUrl + "/upload/image");
            post.form("file", file);
            try (HttpResponse execute = post.execute()) {
                int status = execute.getStatus();
                if (200 != status) {
                    throw new RuntimeException("上传图片异常");
                }
                return execute.body();
            }
        }
        return null;
    }
}