技术栈
- Spring Boot
- Maven
- MySQL
- MyBtis-Plus
- Shiro
- JavaScript
- JQuery
- Ajax
- BootStrap
- Vue.js
- apispace天气查询接口
功能设计
-
后台管理系统
-
管理人员登录
-
城市管理
-
城市天气日志
-
城市天气定时更新
-
城市天气手动更新
-
天气资讯
-
日志管理
-
-
网站部分
-
天气查询
-
天气资讯查询
-
天气资讯阅读
-
效果展示
代码实现
查询天气
package com.bishe.app.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bishe.app.anno.AopLog;
import com.bishe.app.anno.AttributeValue;
import com.bishe.app.pojo.WeatherLog;
import com.bishe.app.pojo.vo.ResponseResult;
import com.bishe.app.service.WeatherLogService;
import com.bishe.app.utils.AttributeValueUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* @author ZhenHua Zou
* @create 2023/3/15
* @since 1.0.0
*/
@RestController
@RequestMapping("weatherLog")
public class WeatherLogController extends BaseController{
@Autowired
private WeatherLogService weatherLogService;
@RequestMapping(value = "/getPageWeatherLog/{currentPage}/{pageSize}",method = RequestMethod.GET)
@AopLog(description = "查询分页城市天气纪录")
public ResponseResult getPageWeatherLog(@PathVariable Integer currentPage, @PathVariable Integer pageSize, @RequestParam String weatherLogName){
IPage<WeatherLog> weatherLogIPage = weatherLogService.getPageWeatherLog(currentPage,pageSize,weatherLogName);
Class clazz = WeatherLog.class;
List<String> strings = getAttributeValueList(clazz);
return ResponseResult.success("",weatherLogIPage, (ArrayList) strings);
}
@RequestMapping(value = "/deleteWeatherLog",method = RequestMethod.DELETE)
@AopLog(description = "删除城市天气纪录")
public ResponseResult deleteWeatherLog(Integer... ids){
Integer a = weatherLogService.deleteWeatherLog(ids);
return successOrFail(a);
}
@RequestMapping(value = "/updateWeatherLog/{cityCode}",method = RequestMethod.POST)
@AopLog(description = "手动更新城市天气")
public ResponseResult updateWeatherLog(@PathVariable String cityCode) throws IOException {
Integer a = weatherLogService.updateWeatherLog(cityCode);
return successOrFail(a);
}
}
调用接口查询天气
package com.bishe.app.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bishe.app.mapper.CityMapper;
import com.bishe.app.mapper.WeatherLogMapper;
import com.bishe.app.pojo.City;
import com.bishe.app.pojo.WeatherLog;
import com.bishe.app.service.QueryService;
import com.bishe.app.service.WeatherService;
import com.bishe.app.utils.CurrentIpUtil;
import com.bishe.app.utils.HttpClientToInterface;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Map;
/**
* @author ZhenHua Zou
* @create 2023/3/12
* @since 1.0.0
*/
@Service
public class QueryServiceImpl implements QueryService {
@Autowired
private CurrentIpUtil currentIpUtil;
@Autowired
private CityMapper cityMapper;
@Autowired
private WeatherLogMapper weatherLogMapper;
@Value("${ip.requestAddress}")
private String ipRequestAddress;
@Autowired
private WeatherService weatherService;
@Override
public Object queryCurrentAddressWeather() throws IOException {
//获取当前IP
String ip = currentIpUtil.getCurrentIp();
//解析当前IP位置
String result = HttpClientToInterface.doGet(ipRequestAddress,"UTF-8");
System.out.println(result);
JSONObject jsonObject = JSONObject.parseObject(result);
System.out.println(JSONObject.parseObject(jsonObject.getString("info")).getString("city"));
String cityName = JSONObject.parseObject(jsonObject.getString("info")).getString("city");
QueryWrapper<City> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(City::getCityName,cityName);
String cityCode = cityMapper.selectOne(queryWrapper).getCityCode();
JSONObject realtimeMap = weatherService.queryWeather(cityCode);
System.out.println(realtimeMap);
System.out.println(realtimeMap.get("vis"));
WeatherLog weatherLog = new WeatherLog();
weatherLog.setVis(Integer.valueOf(realtimeMap.get("vis").toString()));
weatherLog.setUv(Integer.valueOf(realtimeMap.get("uv").toString()));
weatherLog.setTemp(Double.valueOf(realtimeMap.get("temp").toString()));
weatherLog.setCode(realtimeMap.get("code").toString());
weatherLog.setWindClass(realtimeMap.get("wind_class").toString());
weatherLog.setWindDir(realtimeMap.get("wind_dir").toString());
weatherLog.setClouds(Integer.valueOf(realtimeMap.get("clouds").toString()));
weatherLog.setPressure(Integer.valueOf(realtimeMap.get("pressure").toString()));
weatherLog.setFeelsLike(Integer.valueOf(realtimeMap.get("feels_like").toString()));
weatherLog.setPrec(Double.valueOf(realtimeMap.get("prec").toString()));
weatherLog.setRh(Integer.valueOf(realtimeMap.get("rh").toString()));
weatherLog.setWindSpeed(Double.valueOf(realtimeMap.get("wind_speed").toString()));
weatherLog.setText(realtimeMap.get("text").toString());
weatherLog.setCityName(cityName);
weatherLog.setCityCode(cityCode);
weatherLog.setUpdateTime(new Date());
weatherLog.setDate(new Date());
weatherLogMapper.insert(weatherLog);
return weatherLog;
}
@Override
public Object queryFifteenDaysWeather(String cityCode) throws IOException {
if ("".equals(cityCode)||"undefined".equals(cityCode)){
//获取当前IP
String ip = currentIpUtil.getCurrentIp();
//解析当前IP位置
String result = HttpClientToInterface.doGet(ipRequestAddress,"UTF-8");
System.out.println(result);
JSONObject jsonObject = JSONObject.parseObject(result);
System.out.println(JSONObject.parseObject(jsonObject.getString("info")).getString("city"));
String cityName = JSONObject.parseObject(jsonObject.getString("info")).getString("city");
QueryWrapper<City> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(City::getCityName,cityName);
cityCode = cityMapper.selectOne(queryWrapper).getCityCode();
}
JSONObject realtimeMap = weatherService.queryFifteenDaysWeather(cityCode);
return realtimeMap;
}
}
如何获取
关注下方微信公众号
版权声明:本文为weixin_44828258原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。