| | |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.WechatUtils; |
| | | import com.ruoyi.system.domain.SysTask; |
| | | import com.ruoyi.system.domain.SysTaskEmergency; |
| | | import com.ruoyi.system.domain.SysConfig; |
| | | import com.ruoyi.system.mapper.SysConfigMapper; |
| | | import com.ruoyi.system.mapper.SysTaskEmergencyMapper; |
| | | import com.ruoyi.system.mapper.SysTaskMapper; |
| | | import com.ruoyi.system.domain.SysTask; |
| | | import com.ruoyi.system.domain.SysTaskEmergency; |
| | | import com.ruoyi.system.mapper.SysUserMapper; |
| | | import com.ruoyi.system.service.IWechatTaskNotifyService; |
| | | 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; |
| | |
| | | @Autowired |
| | | private WechatConfig wechatConfig; |
| | | |
| | | @Autowired |
| | | private ISysConfigService configService; |
| | | |
| | | @Autowired |
| | | private SysConfigMapper configMapper; |
| | | |
| | | |
| | | /** |
| | | * 获取应用级微信AccessToken(带缓存) |
| | | * 优先从sys_config读取并判断有效期;过期则重新获取并写回sys_config |
| | | */ |
| | | private String getAppAccessToken() { |
| | | 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; |
| | | } 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); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 发送任务通知消息给指定用户列表 |
| | | * |
| | |
| | | // 查询急救信息 |
| | | SysTaskEmergency emergency = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(taskId); |
| | | |
| | | // 获取微信AccessToken |
| | | String accessToken = WechatUtils.getAccessToken(wechatConfig.getAppId(), wechatConfig.getAppSecret()); |
| | | // 获取微信AccessToken(走应用级缓存) |
| | | String accessToken = getAppAccessToken(); |
| | | if (StringUtils.isEmpty(accessToken)) { |
| | | log.error("获取微信AccessToken失败,无法发送任务通知"); |
| | | return 0; |