使用binary-wang封装的工具包“判断用户是否关注公众号”
private boolean validWatch(String openId, String appId) {
WxMpUser wxUser = userBaseInfo(openId, appId);
if (wxUser != null) {
return wxUser.getSubscribe();
}
return false;
}
private WxMpUser userBaseInfo(String openId, String appId) {
WxMpUser wxMpUser = new WxMpUser();
try {
WxMpService mpService = WxMpConfiguration.getMpService(appId);
wxMpUser = mpService.getUserService().userInfo(openId);
return wxMpUser;
} catch (Exception e) {
log.error("获取微信用户【】错误" + e.getMessage());
}
return wxMpUser;
}
@Configuration
@AllArgsConstructor
@EnableConfigurationProperties(WxMpProperties.class)
public class WxMpConfiguration {
private static WxMpProperties properties ;
@Autowired
public void setProperties(WxMpProperties properties) {
WxMpConfiguration.properties = properties;
}
private static Map<String, WxMpService> mpServices = Maps.newHashMap();
public static Map<String, WxMpService> getMpServices() {
return mpServices;
}
public static WxMpService getMpService(String appId) {
return getMpServices().get(appId);
}
@ConfigurationProperties(prefix = "wx.mp")
@Data
public class WxMpProperties {
private List<MpConfig> configs;
public static class MpConfig {
private String appId;
private String secret;
private String token;
private String aesKey;
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getAesKey() {
return aesKey;
}
public void setAesKey(String aesKey) {
this.aesKey = aesKey;
}
}
public List<MpConfig> getConfigs() {
return configs;
}
public void setConfigs(List<MpConfig> configs) {
this.configs = configs;
}
@Override
public String toString() {
return JsonUtils.toJson(this);
}
}