Pu Zhibing
2025-04-22 fd7b8fb7c89832c28a838b0449bbb8a392433ee2
ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/util/weChat/WeChatUtil.java
@@ -1,15 +1,32 @@
package com.ruoyi.account.util.weChat;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.Header;
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.ruoyi.account.util.ObsUploadUtil;
import com.ruoyi.common.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHeaders;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
 * 微信工具类
@@ -24,14 +41,16 @@
    @Value("${wx.appletsAppSecret}")
    private String wxAppletsAppSecret;
    @Value("{wx.officialAccountAppSecret}")
    private String officialAccountAppSecret;
    @Value("${wx.appid}")
//    @Value("${wx.appid}")
    private String webAppId;
    @Value("${wx.appSecret}")
//    @Value("${wx.appSecret}")
    private String webAppSecret;
    @Resource
    private RedisService redisService;
    /**
@@ -39,7 +58,7 @@
     * @param jscode
     * @return
     */
    public Map<String, Object> code2Session(String jscode) throws Exception{
    public Map<String, Object> code2Session(String jscode) {
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxAppletsAppid + "&secret=" + wxAppletsAppSecret
                + "&js_code=" + jscode + "&grant_type=authorization_code";
        HttpRequest get = HttpUtil.createGet(url);
@@ -72,13 +91,24 @@
        }
        return null;
    }
    public String getWxAppletsAccessToken(){
        Object wxAppletsAccessToken = redisService.getCacheObject("wxAppletsAccessToken");
        if(null != wxAppletsAccessToken){
            return wxAppletsAccessToken.toString();
        }
        String appletsAccessToken = getAppletsAccessToken();
        redisService.setCacheObject("wxAppletsAccessToken", appletsAccessToken, 7200L, TimeUnit.SECONDS);
        return appletsAccessToken;
    }
    /**
     * 获取微信小程序token
     * @return
     */
    public String getWxAppletsAccessToken() throws Exception{
    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();
@@ -169,35 +199,48 @@
    
//    /**
//     * 获取小程序二维码
//     * @param page      跳转页 例如 pages/index/index
//     * @param scene     参数 a=1&b=2
//     */
//    public InputStream getwxacodeunlimit(String page, String scene){
//        try {
//            String token = getWxAppletsAccessToken();
//            if(StringUtils.isEmpty(token)){
//                System.err.println("获取接口调用凭证失败");
//            }
//            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token;
//            Map<String, Object> param = new HashMap<>();
//            param.put("scene", scene);
//            param.put("page", page);
//            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, 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(Base64.encodeBase64String(body));
//            return new ByteArrayInputStream(body);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return null;
//    }
    /**
     * 获取小程序二维码
     * @param page      跳转页 例如 pages/index/index
     * @param scene     参数 a=1&b=2
     */
    public InputStream getwxacodeunlimit(String page, String scene, EnvVersion env_version){
        try {
            String token = getWxAppletsAccessToken();
            if(StringUtils.isEmpty(token)){
                System.err.println("获取接口调用凭证失败");
            }
            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token;
            Map<String, Object> param = new HashMap<>();
            param.put("scene", scene);
            param.put("page", page);
            param.put("env_version", env_version.getVersion());
            HttpRequest post = HttpUtil.createPost(url);
            post.header(Header.CONTENT_TYPE, "application/json;charset=UTF-8");
            post.body(JSON.toJSONString(param));
            HttpResponse execute = post.execute();
            byte[] bytes = execute.bodyBytes();
            String body1 = execute.body();
            return new ByteArrayInputStream(bytes);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 获取微信小程序二维码
     * @param page
     * @param scene
     * @param filePath
     * @return
     */
    public String getwxacodeunlimit(String page, String scene, EnvVersion env_version, String filePath){
        InputStream getwxacodeunlimit = getwxacodeunlimit(page, scene, env_version);
//        File file = FileUtil.writeFromStream(getwxacodeunlimit, new File(filePath));
//        return file.getPath();
        return ObsUploadUtil.obsUpload("png", getwxacodeunlimit);
    }
}