You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					
					
						
							223 lines
						
					
					
						
							7.5 KiB
						
					
					
				
			
		
		
	
	
							223 lines
						
					
					
						
							7.5 KiB
						
					
					
				package com.ynxbd.wx.config;
 | 
						|
 | 
						|
import com.ynxbd.common.helper.ProperHelper;
 | 
						|
import lombok.extern.slf4j.Slf4j;
 | 
						|
import org.apache.commons.lang3.ObjectUtils;
 | 
						|
 | 
						|
@Slf4j
 | 
						|
public class WeChatConfig {
 | 
						|
    private WeChatConfig() {
 | 
						|
    }
 | 
						|
 | 
						|
    //配置文件读取项
 | 
						|
    public static final String APP_ID;
 | 
						|
    public static final String APP_SECRET;
 | 
						|
    public static final String TOKEN;
 | 
						|
    public static final String AES_KEY;
 | 
						|
    public static final String MCH_ID;
 | 
						|
    public static final String MCH_KEY;
 | 
						|
    //
 | 
						|
    public static final String PASSWORD;
 | 
						|
    //
 | 
						|
    private static String BASE_URL;
 | 
						|
    private static String WEB_PATH;
 | 
						|
    private static String CACHE_WEB_URL = null;
 | 
						|
    // 开启预结算(第2开关)
 | 
						|
    public static boolean IS_RECIPE_PREPAY;
 | 
						|
 | 
						|
    static {
 | 
						|
        ProperHelper config = new ProperHelper().read("wx.properties");
 | 
						|
        IS_RECIPE_PREPAY = config.getBoolean("wx.is_recipe_prepay", false);
 | 
						|
 | 
						|
        APP_ID = config.getString("wx.appId");
 | 
						|
        APP_SECRET = config.getString("wx.appSecret");
 | 
						|
        TOKEN = config.getString("wx.token");
 | 
						|
        AES_KEY = config.getString("wx.aesKey");
 | 
						|
        BASE_URL = config.getString("wx.baseURL");
 | 
						|
        PASSWORD = config.getString("wx.password");
 | 
						|
        // 商户
 | 
						|
        MCH_ID = config.getString("wx.mchId");
 | 
						|
        MCH_KEY = config.getString("wx.mchKey");
 | 
						|
 | 
						|
        WEB_PATH = config.getString("wx.webPath");
 | 
						|
        if (WEB_PATH == null) {
 | 
						|
            WEB_PATH = "";
 | 
						|
        }
 | 
						|
 | 
						|
        if (BASE_URL == null) {
 | 
						|
            log.error("【微信】读取配置文件wx.properties失败");
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public static String getBaseUrl() {
 | 
						|
        if (BASE_URL == null) {
 | 
						|
            BASE_URL = "";
 | 
						|
        }
 | 
						|
 | 
						|
        if (BASE_URL.length() == 0) return BASE_URL;
 | 
						|
 | 
						|
        String path = BASE_URL;
 | 
						|
        String suffix = path.substring(path.length() - 1);
 | 
						|
        if (!"/".equals(suffix)) {
 | 
						|
            path += "/";
 | 
						|
        }
 | 
						|
        return path;
 | 
						|
    }
 | 
						|
 | 
						|
    public static String getHttpsBaseUrl() {
 | 
						|
        String url = getBaseUrl();
 | 
						|
        if (url != null && url.contains("http") && !url.contains("https")) {
 | 
						|
            url = url.replace("http", "https");
 | 
						|
        }
 | 
						|
        return url;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取配置中原本的域名
 | 
						|
     */
 | 
						|
    public static String getBaseDomain() {
 | 
						|
        if (BASE_URL.length() < 4) {
 | 
						|
            return BASE_URL;
 | 
						|
        }
 | 
						|
        return BASE_URL.substring(0, BASE_URL.length() - 4);
 | 
						|
    }
 | 
						|
 | 
						|
    // 域名
 | 
						|
    public static String getDomain(boolean isHttps) {
 | 
						|
        if (BASE_URL.length() < 4) {
 | 
						|
            return BASE_URL;
 | 
						|
        }
 | 
						|
 | 
						|
        String url = BASE_URL;
 | 
						|
        if (isHttps) {
 | 
						|
            int index = url.indexOf("http://");
 | 
						|
            if (index == 0) {
 | 
						|
                url = "https://" + url.substring(index + 7);
 | 
						|
            }
 | 
						|
        }
 | 
						|
        return url.substring(0, url.length() - 4);
 | 
						|
    }
 | 
						|
 | 
						|
    public static String getWebPath(boolean isPrefix, boolean isSuffix) {
 | 
						|
        if (WEB_PATH == null || WEB_PATH.length() == 0) return "/";
 | 
						|
 | 
						|
        String path = WEB_PATH;
 | 
						|
        String prefix = path.substring(0, 1);
 | 
						|
        if ("/".equals(prefix)) {
 | 
						|
            path = isPrefix ? path : path.substring(1);
 | 
						|
        } else {
 | 
						|
            path = isPrefix ? ("/" + path) : path;
 | 
						|
        }
 | 
						|
 | 
						|
        String suffix = path.substring(path.length() - 1);
 | 
						|
        if ("/".equals(suffix)) {
 | 
						|
            path = isSuffix ? path : path.substring(0, path.length() - 1);
 | 
						|
        } else {
 | 
						|
            path = isSuffix ? (path + "/") : path;
 | 
						|
        }
 | 
						|
        return path;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public static String getWebUrl() {
 | 
						|
        if (!ObjectUtils.isEmpty(CACHE_WEB_URL)) {
 | 
						|
            return CACHE_WEB_URL;
 | 
						|
        }
 | 
						|
        String webPath = getWebPath(false, true);
 | 
						|
        if ("/".equals(webPath)) {
 | 
						|
            webPath = "";
 | 
						|
        }
 | 
						|
        CACHE_WEB_URL = getBaseUrl() + webPath;
 | 
						|
        return CACHE_WEB_URL;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
//    public static String getAccessToken() {
 | 
						|
//        String accessToken = ACCESS_TOKEN_CACHE.get(ACCESS_TOKEN_KEY);
 | 
						|
//        if (ObjectUtils.isEmpty(accessToken)) {
 | 
						|
//            Token token = TokenAPI.token(getAppId(), getAppSecret());
 | 
						|
//            if (token == null) {
 | 
						|
//                return null;
 | 
						|
//            }
 | 
						|
//            accessToken = token.getAccess_token();
 | 
						|
//            if (token.getExpires_in() > 0 && !ObjectUtils.isEmpty(accessToken)) {
 | 
						|
//                ACCESS_TOKEN_CACHE.put("access_token", accessToken);
 | 
						|
//            }
 | 
						|
//        }
 | 
						|
//        return accessToken;
 | 
						|
//    }
 | 
						|
//
 | 
						|
//
 | 
						|
//    public static Map<String, Object> getWxConfig(String url) {
 | 
						|
//        Map<String, Object> map = new HashMap<>();
 | 
						|
//        // 时间戳
 | 
						|
//        String timestamp = Long.toString((new Date().getTime()) / 1000);
 | 
						|
//        // 随机串
 | 
						|
//        String nonceStr = UUID.randomUUID().toString();
 | 
						|
//        // 根据 token 获取 jstl
 | 
						|
//        String urlStr = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + getAccessToken() + "&msgInterface=jsapi";
 | 
						|
//        Result result = RequestHelper.get(urlStr, null);
 | 
						|
//        System.out.println(result);
 | 
						|
//        JSONObject resultJson = JSONObject.parseObject(JSON.toJSONString(result.getData()));
 | 
						|
//        String ticket = resultJson.getString("ticket");
 | 
						|
//        System.out.println(ticket);
 | 
						|
//        System.out.println("要加密的参数:" + nonceStr + " " + ticket + " " + timestamp + " " + url);
 | 
						|
//        String signature = getSign(nonceStr, ticket, timestamp, url);
 | 
						|
//        map.put("appId", appId);
 | 
						|
//        map.put("timestamp", timestamp);
 | 
						|
//        map.put("nonceStr", nonceStr);
 | 
						|
//        map.put("signature", signature);
 | 
						|
//        return map;
 | 
						|
//    }
 | 
						|
//
 | 
						|
//
 | 
						|
//    private static String getSign(String nonceStr, String jsapiTicket, String timestamp, String url) {
 | 
						|
//        String[] paramArr = new String[]{"jsapi_ticket=" + jsapiTicket, "timestamp=" + timestamp, "noncestr=" + nonceStr, "url=" + url};
 | 
						|
//        Arrays.sort(paramArr);
 | 
						|
//        // 将排序后的结果拼接成一个字符串
 | 
						|
//        String content = paramArr[0].concat("&" + paramArr[1]).concat("&" + paramArr[2]).concat("&" + paramArr[3]);
 | 
						|
//        String signature;
 | 
						|
//        try {
 | 
						|
//            MessageDigest md = MessageDigest.getInstance("SHA-1");
 | 
						|
//            // 对拼接后的字符串进行 sha1 加密
 | 
						|
//            System.out.println("拼接加密签名:" + content);
 | 
						|
//            byte[] digest = md.digest(content.getBytes());
 | 
						|
//            signature = byteToStr(digest); // 将 sha1 加密后的字符串与 signature 进行对比
 | 
						|
//            return signature;
 | 
						|
//        } catch (NoSuchAlgorithmException e) {
 | 
						|
//            e.printStackTrace();
 | 
						|
//        }
 | 
						|
//        return null;
 | 
						|
//    }
 | 
						|
//
 | 
						|
//    /**
 | 
						|
//     * 将字节数组转换为十六进制字符串
 | 
						|
//     *
 | 
						|
//     * @param byteArray 数据
 | 
						|
//     * @return
 | 
						|
//     */
 | 
						|
//    private static String byteToStr(byte[] byteArray) {
 | 
						|
//        StringBuilder digest = new StringBuilder();
 | 
						|
//        for (byte b : byteArray) {
 | 
						|
//            digest.append(byteToHexStr(b));
 | 
						|
//        }
 | 
						|
//
 | 
						|
//        return digest.toString();
 | 
						|
//    }
 | 
						|
//
 | 
						|
//    /**
 | 
						|
//     * 将字节转换为十六进制字符串
 | 
						|
//     *
 | 
						|
//     * @param dataByte 数据
 | 
						|
//     * @return
 | 
						|
//     */
 | 
						|
//    private static String byteToHexStr(byte dataByte) {
 | 
						|
//        char[] digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 | 
						|
//        char[] tempArr = new char[2];
 | 
						|
//        tempArr[0] = digit[(dataByte >>> 4) & 0X0F];
 | 
						|
//        tempArr[1] = digit[dataByte & 0X0F];
 | 
						|
//        return new String(tempArr);
 | 
						|
//    }
 | 
						|
 | 
						|
}
 | 
						|
 |