| | |
| | | import com.ruoyi.system.domain.SysTaskEmergency; |
| | | import com.ruoyi.system.mapper.SysUserMapper; |
| | | import com.ruoyi.system.service.IWechatTaskNotifyService; |
| | | import com.ruoyi.system.service.IWechatAccessTokenService; |
| | | import com.ruoyi.system.service.ISysConfigService; |
| | | import com.ruoyi.system.mapper.SysConfigMapper; |
| | | import com.ruoyi.system.domain.SysConfig; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | private WechatConfig wechatConfig; |
| | | |
| | | @Autowired |
| | | private ISysConfigService configService; |
| | | private IWechatAccessTokenService wechatAccessTokenService; |
| | | |
| | | @Autowired |
| | | private SysConfigMapper configMapper; |
| | | private ISysConfigService configService; |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取应用级微信AccessToken(带缓存) |
| | | * 优先从sys_config读取并判断有效期;过期则重新获取并写回sys_config |
| | | * 检查是否启用订阅消息发送 |
| | | * |
| | | * @return true=启用,false=禁用 |
| | | */ |
| | | private String getAppAccessToken() { |
| | | private boolean isSubscribeMessageEnabled() { |
| | | try { |
| | | String appId = wechatConfig.getAppId(); |
| | | String tokenKey = "weixin.access_token." + appId; |
| | | String expireKey = "weixin.access_token_expires." + appId; |
| | | |
| | | String cachedToken = configService.selectConfigByKey(tokenKey); |
| | | String cachedExpireStr = configService.selectConfigByKey(expireKey); |
| | | long now = System.currentTimeMillis(); |
| | | long expireTs = 0L; |
| | | if (StringUtils.isNotEmpty(cachedExpireStr)) { |
| | | try { |
| | | expireTs = Long.parseLong(cachedExpireStr); |
| | | } catch (NumberFormatException e) { |
| | | expireTs = 0L; |
| | | } |
| | | } |
| | | |
| | | // 缓存有效且未过期(预留60秒安全边界) |
| | | if (StringUtils.isNotEmpty(cachedToken) && expireTs > now + 60000L) { |
| | | return cachedToken; |
| | | } |
| | | |
| | | // 重新获取,并写入sys_config |
| | | String newToken = WechatUtils.getAccessToken(wechatConfig.getAppId(), wechatConfig.getAppSecret()); |
| | | if (StringUtils.isEmpty(newToken)) { |
| | | return null; |
| | | } |
| | | long newExpireTs = now + 7200L * 1000L; // 7200秒 |
| | | upsertConfig(tokenKey, newToken); |
| | | upsertConfig(expireKey, String.valueOf(newExpireTs)); |
| | | return newToken; |
| | | String enabled = configService.selectConfigByKey("wechat.subscribe.message.enabled"); |
| | | return "true".equalsIgnoreCase(enabled); |
| | | } catch (Exception e) { |
| | | log.error("获取应用级微信AccessToken失败", e); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据configKey写入或更新sys_config |
| | | */ |
| | | private void upsertConfig(String key, String value) { |
| | | SysConfig exist = configMapper.checkConfigKeyUnique(key); |
| | | if (exist != null && exist.getConfigId() != null) { |
| | | exist.setConfigValue(value); |
| | | configMapper.updateConfig(exist); |
| | | } else { |
| | | SysConfig cfg = new SysConfig(); |
| | | cfg.setConfigKey(key); |
| | | cfg.setConfigName(key); |
| | | cfg.setConfigValue(value); |
| | | cfg.setConfigType("Y"); // 内置参数 |
| | | configMapper.insertConfig(cfg); |
| | | log.warn("获取订阅消息开关配置失败,默认启用", e); |
| | | return true; // 默认启用 |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | public int sendTaskNotifyMessage(Long taskId, List<Long> userIds, Long excludeUserId) { |
| | | // 检查订阅消息开关 |
| | | if (!isSubscribeMessageEnabled()) { |
| | | log.info("订阅消息发送已关闭,跳过发送,taskId={}", taskId); |
| | | return 0; |
| | | } |
| | | if (taskId == null || userIds == null || userIds.isEmpty()) { |
| | | log.warn("发送微信任务通知参数不完整,taskId={}, userIds={}", taskId, userIds); |
| | | return 0; |
| | |
| | | SysTaskEmergency emergency = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(taskId); |
| | | |
| | | // 获取微信AccessToken(走应用级缓存) |
| | | String accessToken = getAppAccessToken(); |
| | | String accessToken = wechatAccessTokenService.getAppAccessToken(); |
| | | if (StringUtils.isEmpty(accessToken)) { |
| | | log.error("获取微信AccessToken失败,无法发送任务通知"); |
| | | return 0; |