Search in sources :

Example 1 with ApiException

use of com.yh.weatherpush.exception.ApiException in project weather-push by yangh124.

the class SchTaskServiceImpl method updateTask.

@Transactional(rollbackFor = Exception.class)
@Override
public void updateTask(Long id, UpdateTaskDTO dto) {
    SchTask task = super.getById(id);
    Integer status = dto.getStatus();
    String cronExp = dto.getCronExp();
    List<Long> tagIds = dto.getTagIds();
    if (ObjectUtil.isNull(status) || StrUtil.isBlank(cronExp) || CollUtil.isEmpty(tagIds)) {
        throw new ApiException("参数错误!");
    }
    BeanUtil.copyProperties(dto, task);
    super.updateById(task);
    taskRelTagService.remove(new QueryWrapper<TaskRelTag>().lambda().eq(TaskRelTag::getTaskId, id));
    List<TaskRelTag> list = new ArrayList<>(tagIds.size());
    for (Long tagId : tagIds) {
        TaskRelTag taskRelTag = new TaskRelTag();
        taskRelTag.setTaskId(id);
        taskRelTag.setTagId(tagId);
        taskRelTag.setCtime(LocalDateTime.now());
        list.add(taskRelTag);
    }
    taskRelTagService.saveBatch(list);
    if (StrUtil.isNotBlank(cronExp)) {
        QuartzBean quartzBean = new QuartzBean();
        quartzBean.setId(String.valueOf(id));
        quartzBean.setCronExp(cronExp);
        quartzClient.update(scheduler, quartzBean);
    }
    if (ObjectUtil.isNotNull(status)) {
        if (0 == status) {
            quartzClient.start(scheduler, String.valueOf(id));
        } else {
            quartzClient.stop(scheduler, String.valueOf(id));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SchTask(com.yh.weatherpush.entity.SchTask) TaskRelTag(com.yh.weatherpush.entity.TaskRelTag) ApiException(com.yh.weatherpush.exception.ApiException) QuartzBean(com.yh.weatherpush.dto.QuartzBean) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ApiException

use of com.yh.weatherpush.exception.ApiException in project weather-push by yangh124.

the class WeatherServiceImpl method getTodayWeather.

@Override
public Map<Integer, String> getTodayWeather(List<Tag> tags) {
    Map<Integer, String> res = new HashMap<>(tags.size());
    for (Tag tag : tags) {
        Integer tagid = tag.getTagId();
        String tagname = tag.getTagName();
        String code = tag.getCode();
        // 实时天气
        String weatherUrl = hfConfig.getGetUrl();
        weatherUrl = weatherUrl.replace("code", code);
        ResponseEntity<HfWeatherResp> weatherResponse = restTemplate.getForEntity(weatherUrl, HfWeatherResp.class);
        HfWeatherResp weatherBody = weatherResponse.getBody();
        if (null == weatherBody) {
            throw new ApiException("获取天气异常!");
        }
        // 天气指数
        String weatherIndexUrl = hfConfig.getIndexUrl();
        weatherIndexUrl = weatherIndexUrl.replace("code", code);
        ResponseEntity<HfWeatherIndexResp> weatherIndexResponse = restTemplate.getForEntity(weatherIndexUrl, HfWeatherIndexResp.class);
        HfWeatherIndexResp weatherIndexBody = weatherIndexResponse.getBody();
        if (null == weatherIndexBody) {
            throw new ApiException("获取天气指数异常!");
        }
        // 24小时天气
        String weatherHourUrl = hfConfig.getHourUrl();
        weatherHourUrl = weatherHourUrl.replace("code", code);
        ResponseEntity<HfWeatherHourResp> weatherHourResponse = restTemplate.getForEntity(weatherHourUrl, HfWeatherHourResp.class);
        HfWeatherHourResp weatherHourBody = weatherHourResponse.getBody();
        if (null == weatherHourBody) {
            throw new ApiException("获取天气指数异常!");
        }
        String s = covertWeatherData(tagname, weatherBody.getNow(), weatherIndexBody.getDaily(), weatherHourBody.getHourly());
        res.put(tagid, s);
    }
    return res;
}
Also used : HashMap(java.util.HashMap) Tag(com.yh.weatherpush.entity.Tag) ApiException(com.yh.weatherpush.exception.ApiException)

Example 3 with ApiException

use of com.yh.weatherpush.exception.ApiException in project weather-push by yangh124.

the class WeatherServiceImpl method getWeatherWarn.

@Override
public Map<Integer, String> getWeatherWarn(List<Tag> tags) {
    Map<Integer, String> map = new HashMap<>();
    for (Tag tag : tags) {
        String url = hfConfig.getWarnUrl();
        url = url.replace("code", tag.getCode());
        ResponseEntity<HfWeatherWarnResp> response = restTemplate.getForEntity(url, HfWeatherWarnResp.class);
        HfWeatherWarnResp body = response.getBody();
        if (null == body) {
            throw new ApiException("获取天气预警失败!");
        }
        String code = body.getCode();
        if (!"200".equals(code)) {
            throw new ApiException("获取天气预警失败! -> " + body);
        }
        String fxLink = body.getFxLink();
        List<WeatherWarn> warning = body.getWarning();
        if (CollectionUtils.isEmpty(warning)) {
            continue;
        }
        WeatherWarn weatherWarn = warning.get(0);
        String id = weatherWarn.getId();
        Boolean exist;
        try {
            exist = redisTemplate.hasKey(id);
            if (null != exist && exist) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace();
            continue;
        }
        Boolean res;
        try {
            res = redisTemplate.opsForValue().setIfAbsent(id, id);
            if (null != res && res) {
                String text = weatherWarn.getText();
                String sb = text + "\n详细信息请查看 -> " + "<a href=\"" + fxLink + "\">和风天气</a>";
                map.put(tag.getTagId(), sb);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) Tag(com.yh.weatherpush.entity.Tag) ApiException(com.yh.weatherpush.exception.ApiException) ApiException(com.yh.weatherpush.exception.ApiException)

Example 4 with ApiException

use of com.yh.weatherpush.exception.ApiException in project weather-push by yangh124.

the class AdminServiceImpl method login.

@Override
public String login(LoginParam param) {
    String token = null;
    // 密码需要客户端加密后传递
    try {
        UserDetails userDetails = loadUserByUsername(param.getUsername());
        if (!passwordEncoder.matches(param.getPassword(), userDetails.getPassword())) {
            throw new ApiException("密码不正确");
        }
        if (!userDetails.isEnabled()) {
            throw new ApiException("帐号已被禁用");
        }
        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authentication);
        token = jwtTokenUtil.generateToken(userDetails);
    } catch (AuthenticationException e) {
        log.warn("登录异常:{}", e.getMessage());
    }
    return token;
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) AdminUserDetails(com.yh.weatherpush.dto.AdminUserDetails) AuthenticationException(org.springframework.security.core.AuthenticationException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) ApiException(com.yh.weatherpush.exception.ApiException)

Example 5 with ApiException

use of com.yh.weatherpush.exception.ApiException in project weather-push by yangh124.

the class QywxServiceImpl method delTagMembers.

@Override
public void delTagMembers(TagMembersParam param) {
    String url = qywxConfig.getTag().getDelTagUserUrl();
    String token = getOtherToken();
    url = url.replace("ACCESS_TOKEN", token);
    ResponseEntity<JSONObject> response = restTemplate.postForEntity(url, param, JSONObject.class);
    JSONObject body = response.getBody();
    if (null == body) {
        throw new ApiException("删除失败!");
    }
    Integer errcode = body.getInteger("errcode");
    if (!errcode.equals(0)) {
        throw new ApiException("删除失败! -> " + body.getString("errmsg"));
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) ApiException(com.yh.weatherpush.exception.ApiException)

Aggregations

ApiException (com.yh.weatherpush.exception.ApiException)17 JSONObject (com.alibaba.fastjson.JSONObject)7 Tag (com.yh.weatherpush.entity.Tag)5 HashMap (java.util.HashMap)3 JSONArray (com.alibaba.fastjson.JSONArray)2 TaskRelTag (com.yh.weatherpush.entity.TaskRelTag)2 AdminUserDetails (com.yh.weatherpush.dto.AdminUserDetails)1 QuartzBean (com.yh.weatherpush.dto.QuartzBean)1 Admin (com.yh.weatherpush.entity.Admin)1 SchTask (com.yh.weatherpush.entity.SchTask)1 ArrayList (java.util.ArrayList)1 CacheEvict (org.springframework.cache.annotation.CacheEvict)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 AuthenticationException (org.springframework.security.core.AuthenticationException)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1 Transactional (org.springframework.transaction.annotation.Transactional)1