172 lines
4.4 KiB
Go
172 lines
4.4 KiB
Go
// Code generated by hertz generator.
|
||
|
||
package weather_and_earthquake
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"weather_and_earthquake/internal/biz"
|
||
"weather_and_earthquake/internal/data"
|
||
|
||
"github.com/cloudwego/hertz/pkg/app"
|
||
"github.com/cloudwego/hertz/pkg/protocol/consts"
|
||
weather_and_earthquake "weather_and_earthquake/biz/model/weather_and_earthquake"
|
||
|
||
logs "github.com/zxysilent/logs"
|
||
)
|
||
|
||
// QueryMethod .
|
||
// @router / [GET]
|
||
func QueryMethod(ctx context.Context, c *app.RequestContext) {
|
||
var err error
|
||
var req weather_and_earthquake.QueryReq
|
||
err = c.BindAndValidate(&req)
|
||
if err != nil {
|
||
c.String(consts.StatusBadRequest, err.Error())
|
||
return
|
||
}
|
||
|
||
logs.Info("QueryMethod req: %v", req)
|
||
resp := new(weather_and_earthquake.QueryResp)
|
||
switch req.Op {
|
||
case 0:
|
||
// 获取地震信息
|
||
resp.Code = 0
|
||
resp.EarthquakeInformations = biz.GetEarthquakeInfo()
|
||
case 1:
|
||
// 通过ip地址查天气,不需要其他参数
|
||
ip := c.ClientIP()
|
||
GetWeatherConditionByIPAddr(ctx, resp, ip)
|
||
case 2:
|
||
// 通过 location key 查询天气,需要提供 location key
|
||
if len(req.LocationKey) == 0 {
|
||
resp.Code = -1
|
||
} else {
|
||
resp.LocationKey = req.LocationKey
|
||
GetWeatherConditionByLocationKey(ctx, resp, req.LocationKey)
|
||
}
|
||
case 3:
|
||
// 通过地名查询天气
|
||
if len(req.Addr) == 0 {
|
||
resp.Code = -1
|
||
} else {
|
||
GetWeatherConditionByAddress(ctx, resp, req.Addr)
|
||
}
|
||
default:
|
||
resp.Code = -1
|
||
}
|
||
|
||
c.JSON(consts.StatusOK, resp)
|
||
}
|
||
|
||
func GetWeatherConditionByLocationKey(ctx context.Context, resp *weather_and_earthquake.QueryResp, key string) {
|
||
resp.Code = 0
|
||
condition, err := data.GetWeatherCondition(ctx, key)
|
||
if err == nil && len(condition) > 0 {
|
||
resp.CurrentConditions = condition
|
||
} else {
|
||
condition, err = biz.GetCurrentConditions(key)
|
||
if err != nil {
|
||
resp.Code = 1
|
||
resp.CurrentConditions = ""
|
||
logs.Errorf("GetWeatherConditionByLocationKey err: %v\n", err)
|
||
} else {
|
||
resp.CurrentConditions = condition
|
||
err = data.SetWeatherCondition(ctx, key, condition)
|
||
if err != nil {
|
||
logs.Errorf("Set Weather Condition err: %v\n", err)
|
||
}
|
||
}
|
||
}
|
||
|
||
forecasts, err := data.GetDailyForecasts(ctx, key)
|
||
if err == nil && len(forecasts) > 0 {
|
||
resp.DailyForecasts = forecasts
|
||
} else {
|
||
forecasts, err = biz.GetDailyForecasts(key)
|
||
if err != nil {
|
||
resp.Code = 1
|
||
resp.DailyForecasts = ""
|
||
logs.Errorf("Get daily forecasts failed: %v\n", err)
|
||
} else {
|
||
resp.DailyForecasts = forecasts
|
||
err = data.SetDailyForecasts(ctx, key, forecasts)
|
||
if err != nil {
|
||
logs.Errorf("Set daily forecasts failed: %v\n", err)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
func GetWeatherConditionByIPAddr(ctx context.Context, resp *weather_and_earthquake.QueryResp, ip string) {
|
||
key, err := biz.GetLocationByIPAddr(ip)
|
||
if err != nil {
|
||
resp.Code = 2
|
||
logs.Errorf("Query location for ip[%s] failed: %v\n", ip, err)
|
||
return
|
||
}
|
||
|
||
resp.Code = 0
|
||
resp.LocationKey = key
|
||
condition, err := data.GetWeatherCondition(ctx, key)
|
||
if err == nil && len(condition) > 0 {
|
||
resp.CurrentConditions = condition
|
||
} else {
|
||
condition, err = biz.GetCurrentConditions(key)
|
||
if err != nil {
|
||
resp.Code = 1
|
||
resp.CurrentConditions = ""
|
||
logs.Errorf("Get current conditions failed: %v\n", err)
|
||
} else {
|
||
resp.CurrentConditions = condition
|
||
err = data.SetWeatherCondition(ctx, key, condition)
|
||
if err != nil {
|
||
logs.Errorf("Set weather condition failed: %v\n", err)
|
||
}
|
||
}
|
||
}
|
||
|
||
forecasts, err := data.GetDailyForecasts(ctx, key)
|
||
if err == nil && len(forecasts) > 0 {
|
||
resp.DailyForecasts = forecasts
|
||
} else {
|
||
forecasts, err = biz.GetDailyForecasts(key)
|
||
if err != nil {
|
||
resp.Code = 1
|
||
resp.DailyForecasts = ""
|
||
logs.Errorf("Get daily forecasts failed: %v\n", err)
|
||
} else {
|
||
resp.DailyForecasts = forecasts
|
||
err = data.SetDailyForecasts(ctx, key, forecasts)
|
||
if err != nil {
|
||
logs.Errorf("Set daily forecasts failed: %v\n", err)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
func GetWeatherConditionByAddress(ctx context.Context, resp *weather_and_earthquake.QueryResp, address string) {
|
||
content, err := biz.GetLocationsByName(address)
|
||
if err != nil || len(content) == 0 {
|
||
resp.Code = 2
|
||
return
|
||
}
|
||
|
||
cities := make([]biz.CityInfo, 0)
|
||
err = json.Unmarshal([]byte(content), &cities)
|
||
if err != nil {
|
||
resp.Code = 2
|
||
return
|
||
}
|
||
|
||
if len(cities) == 1 {
|
||
// 只找到一个对应的地点,就返回这个地点的结果
|
||
city := cities[0]
|
||
resp.LocationKey = city.Key
|
||
GetWeatherConditionByLocationKey(ctx, resp, city.Key)
|
||
} else {
|
||
resp.Code = 3
|
||
resp.Locations = content
|
||
}
|
||
}
|