yanghui
2022-10-24 10df7e29b51d6a2efacc83d870856f57d97a9b66
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/WxXCXTempSend.java
@@ -5,13 +5,20 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import cn.hutool.core.util.StrUtil;
import com.panzhihua.common.controller.BaseController;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.user.SysAppConfigVO;
import com.panzhihua.common.redis.RedisUtils;
import com.panzhihua.common.service.user.UserService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
@@ -24,6 +31,9 @@
import lombok.extern.slf4j.Slf4j;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
@Slf4j
@Component
public class WxXCXTempSend extends BaseController {
@@ -33,9 +43,17 @@
    private static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
    private static String APP_IMAGE_URL = "https://www.psciio.com//idcard/8fa82cfba258498eab2fa818220fb592.jpg";
    private static String APP_IMAGE_NAME = "5.jpg";
    @Autowired
    private StringRedisTemplate redisTemplate;
    private static String WX_CALL_BACK = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=";
    //2小时
    private static final Long EXPIRE_TIME = 2L;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisUtils redisUtils;
    private static WxXCXTempSend wxXCXTempSend;
    /**
     * url转变为 MultipartFile对象
     *
@@ -83,41 +101,47 @@
     * @return
     */
    public String getAccessToken() throws Exception {
        // String accessToken = ;
        // Boolean aBoolean = redisTemplate.hasKey(SecurityConstants.APPLETS_ACCESS_TOKEN);
        // ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        // if(aBoolean){
        // Long expireTime = Long.parseLong(valueOperations.get(SecurityConstants.APPLETS_ACCESS_TOKEN_TIME));
        // if(expireTime <= System.currentTimeMillis()){
        // redisTemplate.delete(SecurityConstants.APPLETS_ACCESS_TOKEN);//如果过期则删除
        // redisTemplate.delete(SecurityConstants.APPLETS_ACCESS_TOKEN_TIME);//如果过期则删除
        //
        // valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN, accessToken);
        // valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN_TIME, System.currentTimeMillis() + 1100000 + "");
        // }else{
        // accessToken = valueOperations.get(SecurityConstants.APPLETS_ACCESS_TOKEN);
        // }
        // }else{
        // accessToken = getAppAccessToken();
        // valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN, accessToken);
        // valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN_TIME, System.currentTimeMillis() + 1100000 + "");
        // }
        return getAppAccessToken();
         String accessToken = "";
        String appId = this.getAppId();
        accessToken=wxXCXTempSend.stringRedisTemplate.boundValueOps("access_token:access_token:"+ appId).get();
        if (appId.equals(APP_ID)){
            accessToken = validAccessToken(accessToken, appId,APP_SECRET);
        }
        return accessToken;
    }
    public String getAppAccessToken() throws Exception {
    public String getAccessToken(String appId) throws Exception {
        String accessToken = "";
        accessToken=wxXCXTempSend.stringRedisTemplate.boundValueOps("access_token:access_token:"+appId).get();
        if (appId.equals(APP_ID)){
            accessToken = validAccessToken(accessToken, appId,APP_SECRET);
        }
        return accessToken;
    }
    private String validAccessToken(String accessToken, String appId,String appSecret) throws Exception {
        if (StrUtil.isEmpty(accessToken)) {
            //重新获取并设置到缓存 该方法暂时只用在花城
            accessToken = getAppAccessToken(appId, appSecret);
            wxXCXTempSend.stringRedisTemplate.opsForValue().set("access_token:access_token:" + appId, accessToken, EXPIRE_TIME,TimeUnit.HOURS);
        } else {
            String accessTokenUrl = WX_CALL_BACK + accessToken;
            String result = HttpClientUtil.httpGet(accessTokenUrl, null, null);
            Map<String, Object> resultMap = JSON.parseObject(result, Map.class);
            //如果Access_token过期也重新获取
            if (resultMap.containsKey("errcode")) {
                accessToken = getAppAccessToken(appId, appSecret);
                wxXCXTempSend.stringRedisTemplate.opsForValue().set("access_token:access_token:" + appId, accessToken, EXPIRE_TIME,TimeUnit.HOURS);
            }
        }
        return accessToken;
    }
    public String getAppAccessToken(String appId,String appSecret) throws Exception {
        String accessToken = "0";
        try {
            // 此处APP_ID APP_SECRET 在微信小程序后端可见
            // String accessTokenUrl = String.format(TEMP_URL, APP_ID, APP_SECRET);
            String appId = getAppId();
            String appSecret = getAppSecret();
            if(StringUtils.isEmpty(appId)){
                appId = APP_ID;
            }
            if(StringUtils.isEmpty(appSecret)){
                appSecret = APP_SECRET;
            }
            log.info("获取微信token参数:appid=" + appId + ",appSecret=" + appSecret);
            String accessTokenUrl = ACCESS_TOKEN_URL + "&appid=" + appId + "&secret=" + appSecret;
            String result = HttpClientUtil.httpGet(accessTokenUrl, null, null);
@@ -132,6 +156,50 @@
        return accessToken;
    }
    /**
     * 发布消息时重新设置access_token 防止过期
     * @param appId
     * @param appSecret
     * @throws Exception
     */
    public void setAppAccessTokenToCache(String appId,String appSecret) throws Exception{
        String accessToken = "0";
        try {
            log.info("获取微信token参数:appid=" + appId + ",appSecret=" + appSecret);
            String accessTokenUrl = ACCESS_TOKEN_URL + "&appid=" + appId + "&secret=" + appSecret;
            String result = HttpClientUtil.httpGet(accessTokenUrl, null, null);
            Map<String, Object> resultMap = JSON.parseObject(result, Map.class);
            if (resultMap.containsKey("access_token")) {
                accessToken = resultMap.get("access_token").toString();
                wxXCXTempSend.stringRedisTemplate.opsForValue().set("access_token:access_token:" + appId, accessToken, EXPIRE_TIME,TimeUnit.HOURS);
            }
        } catch (IOException ioe) {
            log.error("小程序http请求异常");
            ioe.printStackTrace();
        }
    }
    public String getAccessTokenNoValid(String appId) throws Exception {
        String accessToken = "";
        accessToken=wxXCXTempSend.stringRedisTemplate.boundValueOps("access_token:access_token:"+appId).get();
        return accessToken;
    }
    public String getWsAccessToken() throws Exception {
        String accessToken = "0";
        try {
//            log.info("获取微信token参数:appid=" + APP_ID + ",appSecret=" + APP_SECRET);
            String accessTokenUrl = ACCESS_TOKEN_URL + "&appid=wx98d62711dfbd8425"+ "&secret=97a2a10b990c2774ed279724337b5337";
            String result = HttpClientUtil.httpGet(accessTokenUrl, null, null);
            Map<String, Object> resultMap = JSON.parseObject(result, Map.class);
            if (resultMap.containsKey("access_token")) {
                accessToken = resultMap.get("access_token").toString();
            }
        } catch (IOException ioe) {
            log.error("小程序http请求异常");
            ioe.printStackTrace();
        }
        return accessToken;
    }
    /**
     * 获取西区社区通微信token
     * @return  西区社区通微信token
@@ -272,4 +340,13 @@
        return null;
    }
    @PostConstruct
    public void init() {
        wxXCXTempSend = this;
        wxXCXTempSend.redisTemplate = this.redisTemplate;
        wxXCXTempSend.stringRedisTemplate=this.stringRedisTemplate;
        wxXCXTempSend.redisUtils=this.redisUtils;
    }
}