微信消息推送
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.

124 lines
3.6 KiB

4 years ago
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;
4 years ago
import org.springframework.util.ResourceUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
4 years ago
/**
* 获取有关appId跟secret等相关配置信息
*
* @author 李进才
* 单例模式-静态代码块
*/
@Slf4j
public class IniConfig {
private IniConfig() {
}
private static final Wini WINI;
private static final String IP;
public static final Boolean TEMPLATE_VERSION;
public static final String WECHAT_UI_PATH;
public static final String DOMAIN;
public static final String TOKEN_URL;
public static final String LEADER_TIME;
public static final String TODAY_REG_TIME_MOM;
public static final String TODAY_REG_TIME_NOON;
4 years ago
// 静态内部类保证Wini只实例化一次
static {
WINI = initConfig();
IP = IPHelper.getIp();
log.info("IP:{}", IP);
TEMPLATE_VERSION = Objects.equals(getInstance("templateVersion"), "new");
WECHAT_UI_PATH = getInstance("weChatUIPath");
DOMAIN = getInstance("domain");
TOKEN_URL = getInstance("token_url");
LEADER_TIME = getInstance("leaderTime");
TODAY_REG_TIME_MOM = getInstance("todayRegTimeMom");
TODAY_REG_TIME_NOON = getInstance("todayRegTimeNoon");
4 years ago
}
/**
* 获取项目的配置信息
*/
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.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.WECHAT_UI_PATH;
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 ? "/" : "");
}
4 years ago
public static void main(String[] args) {
String wxWebPath = getWxWebPath(true);
System.out.println(wxWebPath);
4 years ago
}
}