WEQServer/biz/handler/weather_and_earthquake/weather_and_earthquake_serv...

169 lines
4.3 KiB
Go
Raw Normal View History

2025-10-31 06:42:43 +00:00
// Code generated by hertz generator.
package weather_and_earthquake
import (
"context"
2025-11-03 11:12:39 +00:00
"encoding/json"
"weather_and_earthquake/internal/biz"
"weather_and_earthquake/internal/data"
2025-10-31 06:42:43 +00:00
"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"
2025-10-31 06:42:43 +00:00
)
// 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)
2025-10-31 06:42:43 +00:00
resp := new(weather_and_earthquake.QueryResp)
2025-11-03 11:12:39 +00:00
switch req.Op {
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
}
2025-10-31 06:42:43 +00:00
c.JSON(consts.StatusOK, resp)
}
2025-11-03 11:12:39 +00:00
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 {
2025-11-04 03:45:51 +00:00
//logs.Info("缓存命中:" + key)
2025-11-03 11:12:39 +00:00
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
}
}