package com.ynxbd.push.config; import com.ynxbd.push.helper.IPHelper; import com.ynxbd.push.helper.ProperHelper; import lombok.extern.slf4j.Slf4j; import org.ini4j.Wini; import org.springframework.util.ObjectUtils; import org.springframework.util.ResourceUtils; import java.io.IOException; import java.io.InputStream; /** * 获取有关appId跟secret等相关配置信息 * * @author 李进才 * 单例模式-静态代码块 */ @Slf4j public class IniConfig { private IniConfig() { } private static final Wini WINI; private static final String IP; // 静态内部类保证Wini只实例化一次 static { WINI = initConfig(); IP = IPHelper.getIp(); log.info("IP:{}", IP); } /** * 获取项目的配置信息 */ public synchronized static Wini initConfig() { try { Wini wini = new Wini(); try (InputStream in = IniConfig.class.getClassLoader().getResourceAsStream("weChat.ini")) { wini.load(in); } catch (IOException e) { e.printStackTrace(); } return wini; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 获取项目的配置信息 * * @param key 配置信息的关键词 */ public static String getInstance(String key) { return WINI.fetch(IP, key); } /** * 测试是否读取成功 */ public static String test() { return WINI.fetch("10.20.10.60", "name"); } // public static void main(String[] args) { // System.out.println(test()); // } /** * 获取微信页面链接 * * @param isSuffix 是否需要后缀/ */ public static String getWxWebPath(boolean isSuffix) { String domain = IniConfig.getInstance("domain"); if (!ObjectUtils.isEmpty(domain)) { String suffix = domain.substring(domain.length() - 1); if (suffix.equals("/")) { domain = domain.substring(0, domain.length() - 1); } } String weChatUIPath = IniConfig.getInstance("weChatUIPath"); if (!ObjectUtils.isEmpty(weChatUIPath) && weChatUIPath.length() > 1) { String prefix = weChatUIPath.substring(0, 1); String suffix = weChatUIPath.substring(weChatUIPath.length() - 1); if (!prefix.equals("/")) { weChatUIPath = "/" + weChatUIPath; } if (suffix.equals("/")) { weChatUIPath = weChatUIPath.substring(0, weChatUIPath.length() - 1); } } else { weChatUIPath = ""; } return domain + "/wx" + weChatUIPath + (isSuffix ? "/" : ""); } public static void main(String[] args) { String wxWebPath = getWxWebPath(false); System.out.println(wxWebPath); } }