liuxiulin 2 kuukautta sitten
commit
bda81c679c
8 muutettua tiedostoa jossa 321 lisäystä ja 0 poistoa
  1. 8 0
      .idea/.gitignore
  2. 18 0
      .idea/material_theme_project_new.xml
  3. 8 0
      .idea/modules.xml
  4. 9 0
      .idea/weather-api-sdk.iml
  5. 174 0
      api.go
  6. 15 0
      api_test.go
  7. 29 0
      go.mod
  8. 60 0
      go.sum

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 18 - 0
.idea/material_theme_project_new.xml

@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="MaterialThemeProjectNewConfig">
+    <option name="metadata">
+      <MTProjectMetadataState>
+        <option name="migrated" value="true" />
+        <option name="pristineConfig" value="false" />
+        <option name="userId" value="52f27171:182f6d84cf7:-8000" />
+        <option name="version" value="8.12.6" />
+      </MTProjectMetadataState>
+    </option>
+    <option name="titleBarState">
+      <MTProjectTitleBarConfigState>
+        <option name="overrideColor" value="false" />
+      </MTProjectTitleBarConfigState>
+    </option>
+  </component>
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/weather-api-sdk.iml" filepath="$PROJECT_DIR$/.idea/weather-api-sdk.iml" />
+    </modules>
+  </component>
+</project>

+ 9 - 0
.idea/weather-api-sdk.iml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="Go" enabled="true" />
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 174 - 0
api.go

@@ -0,0 +1,174 @@
+package api
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"github.com/gogf/gf/v2/frame/g"
+	"strconv"
+	"time"
+)
+
+const (
+	api    = "https://api.seniverse.com/v3/weather/now.json?language=zh-Hans&unit=c&location="
+	dayApi = "https://api.seniverse.com/v3/weather/daily.json?language=zh-Hans&unit=c&start=-1&days=5&location="
+	airApi = "https://api.seniverse.com/v3/air/now.json?language=zh-Hans&scope=city&location="
+)
+
+// WeatherResp 天气
+type WeatherResp struct {
+	Results []struct {
+		Location struct {
+			Id             string `json:"id"`
+			Name           string `json:"name"`
+			Country        string `json:"country"`
+			Path           string `json:"path"`
+			Timezone       string `json:"timezone"`
+			TimezoneOffset string `json:"timezone_offset"`
+		} `json:"location"`
+		Now struct {
+			Text                string `json:"text"`
+			Code                string `json:"code"`
+			Temperature         string `json:"temperature"`
+			FeelsLike           string `json:"feels_like"`
+			Pressure            string `json:"pressure"`
+			Humidity            string `json:"humidity"`
+			Visibility          string `json:"visibility"`
+			WindDirection       string `json:"wind_direction"`
+			WindDirectionDegree string `json:"wind_direction_degree"`
+			WindSpeed           string `json:"wind_speed"`
+			WindScale           string `json:"wind_scale"`
+			Clouds              string `json:"clouds"`
+			DewPoint            string `json:"dew_point"`
+		} `json:"now"`
+		LastUpdate time.Time `json:"last_update"`
+	}
+}
+
+type XinZhiDayWeatherInfo struct {
+	Results []struct {
+		Location struct {
+			Id             string `json:"id"`
+			Name           string `json:"name"`
+			Country        string `json:"country"`
+			Path           string `json:"path"`
+			Timezone       string `json:"timezone"`
+			TimezoneOffset string `json:"timezone_offset"`
+		} `json:"location"`
+		Daily []struct {
+			Date                string `json:"date"`
+			TextDay             string `json:"text_day"`
+			CodeDay             string `json:"code_day"`
+			TextNight           string `json:"text_night"`
+			CodeNight           string `json:"code_night"`
+			High                string `json:"high"`
+			Low                 string `json:"low"`
+			Rainfall            string `json:"rainfall"`
+			Precip              string `json:"precip"`
+			WindDirection       string `json:"wind_direction"`
+			WindDirectionDegree string `json:"wind_direction_degree"`
+			WindSpeed           string `json:"wind_speed"`
+			WindScale           string `json:"wind_scale"`
+			Humidity            string `json:"humidity"`
+		} `json:"daily"`
+		LastUpdate time.Time `json:"last_update"`
+	} `json:"results"`
+}
+
+type AirInfo struct {
+	Results []struct {
+		Location struct {
+			Id             string `json:"id"`
+			Name           string `json:"name"`
+			Country        string `json:"country"`
+			Path           string `json:"path"`
+			Timezone       string `json:"timezone"`
+			TimezoneOffset string `json:"timezone_offset"`
+		} `json:"location"`
+		Air struct {
+			City struct {
+				Aqi              string    `json:"aqi"`               // 空气质量指数(AQI)是描述空气质量状况的定量指数
+				Pm25             string    `json:"pm25"`              // PM2.5颗粒物(粒径小于等于2.5μm)1小时平均值。单位:μg/m³
+				Pm10             string    `json:"pm10"`              // PM10颗粒物(粒径小于等于10μm)1小时平均值。单位:μg/m³
+				So2              string    `json:"so2"`               // 二氧化硫1小时平均值。单位:μg/m³
+				No2              string    `json:"no2"`               // 二氧化氮1小时平均值。单位:μg/m³
+				Co               string    `json:"co"`                // 一氧化碳1小时平均值。单位:mg/m³
+				O3               string    `json:"o3"`                // 臭氧1小时平均值。单位:μg/m³
+				PrimaryPollutant string    `json:"primary_pollutant"` // 首要污染物
+				Quality          string    `json:"quality"`           // 空气质量类别,有“优、良、轻度污染、中度污染、重度污染、严重污染”6类
+				LastUpdate       time.Time `json:"last_update"`       // 数据发布时间
+			} `json:"city"`
+		} `json:"air"`
+		LastUpdate time.Time `json:"last_update"`
+	}
+}
+
+type WeatherInfoResp struct {
+	Location    string `json:"location"`
+	Text        string `json:"text"`
+	Code        string `json:"code"`
+	Temperature int    `json:"temperature"`
+	Humidity    int    `json:"humidity"`
+	AQI         int    `json:"aqi"`
+	PM25        int    `json:"pm25"`
+	PM10        int    `json:"pm10"`
+	Quality     string `json:"quality"`
+}
+
+func GetWeatherInfo(location, key string) (*WeatherInfoResp, error) {
+	result := new(WeatherInfoResp)
+	info, err := GetWeatherInfoByLocation(location, key)
+	if err != nil {
+		return nil, err
+	}
+	result.Location = info.Results[0].Location.Name
+	result.Text = info.Results[0].Now.Text
+	result.Code = info.Results[0].Now.Code
+	result.Temperature, _ = strconv.Atoi(info.Results[0].Now.Temperature)
+	result.Humidity, _ = strconv.Atoi(info.Results[0].Now.Humidity)
+	airInfo, err := GetAirInfoByLocation(location, key)
+	if err != nil {
+		return nil, err
+	}
+	result.PM25, _ = strconv.Atoi(airInfo.Results[0].Air.City.Pm25)
+	result.AQI, _ = strconv.Atoi(airInfo.Results[0].Air.City.Aqi)
+	result.PM10, _ = strconv.Atoi(airInfo.Results[0].Air.City.Pm10)
+	result.Quality = airInfo.Results[0].Air.City.Quality
+	return result, nil
+}
+
+func GetWeatherInfoByLocation(location, key string) (*WeatherResp, error) {
+	ctx := context.Background()
+	url := fmt.Sprintf("%s%s&key=%s", api, location, key)
+	res := g.Client().GetVar(ctx, url)
+	var result WeatherResp
+	err := res.Scan(&result)
+	if err != nil {
+		return nil, errors.New("天气查询失败")
+	}
+	return &result, nil
+}
+
+func GetDayWeatherInfoByLocation(location, key string) (*XinZhiDayWeatherInfo, error) {
+	ctx := context.Background()
+	url := fmt.Sprintf("%s%s&key=%s", dayApi, location, key)
+	res := g.Client().GetVar(ctx, url)
+	var result XinZhiDayWeatherInfo
+	err := res.Scan(&result)
+	if err != nil {
+		return nil, errors.New("天气查询失败")
+	}
+	return &result, nil
+}
+
+func GetAirInfoByLocation(location, key string) (*AirInfo, error) {
+	ctx := context.Background()
+	url := fmt.Sprintf("%s%s&key=%s", airApi, location, key)
+	res := g.Client().GetVar(ctx, url)
+	var result AirInfo
+	err := res.Scan(&result)
+	if err != nil {
+		return nil, errors.New("天气查询失败")
+	}
+	return &result, nil
+}

+ 15 - 0
api_test.go

@@ -0,0 +1,15 @@
+package api
+
+import (
+	"fmt"
+	"github.com/gogf/gf/v2/encoding/gjson"
+	"testing"
+)
+
+func TestGetWeatherInfo(t *testing.T) {
+	weatherInfo, err := GetWeatherInfo("上海", "SgqF9ZGBUj7R0dLAb")
+	if err != nil {
+		fmt.Println(err)
+	}
+	fmt.Println(gjson.New(weatherInfo).String())
+}

+ 29 - 0
go.mod

@@ -0,0 +1,29 @@
+module weather-api-sdk
+
+require github.com/gogf/gf/v2 v2.6.1
+
+require (
+	github.com/BurntSushi/toml v1.2.0 // indirect
+	github.com/clbanning/mxj/v2 v2.7.0 // indirect
+	github.com/fatih/color v1.15.0 // indirect
+	github.com/fsnotify/fsnotify v1.7.0 // indirect
+	github.com/go-logr/logr v1.2.4 // indirect
+	github.com/go-logr/stdr v1.2.2 // indirect
+	github.com/gorilla/websocket v1.5.0 // indirect
+	github.com/grokify/html-strip-tags-go v0.0.1 // indirect
+	github.com/magiconair/properties v1.8.6 // indirect
+	github.com/mattn/go-colorable v0.1.13 // indirect
+	github.com/mattn/go-isatty v0.0.20 // indirect
+	github.com/mattn/go-runewidth v0.0.15 // indirect
+	github.com/olekukonko/tablewriter v0.0.5 // indirect
+	github.com/rivo/uniseg v0.4.4 // indirect
+	go.opentelemetry.io/otel v1.14.0 // indirect
+	go.opentelemetry.io/otel/sdk v1.14.0 // indirect
+	go.opentelemetry.io/otel/trace v1.14.0 // indirect
+	golang.org/x/net v0.17.0 // indirect
+	golang.org/x/sys v0.13.0 // indirect
+	golang.org/x/text v0.13.0 // indirect
+	gopkg.in/yaml.v3 v3.0.1 // indirect
+)
+
+go 1.22

+ 60 - 0
go.sum

@@ -0,0 +1,60 @@
+github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
+github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
+github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
+github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
+github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
+github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
+github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
+github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
+github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
+github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
+github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
+github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
+github.com/gogf/gf/v2 v2.6.1 h1:n/cfXM506WjhPa6Z1CEDuHNM1XZ7C8JzSDPn2AfuxgQ=
+github.com/gogf/gf/v2 v2.6.1/go.mod h1:x2XONYcI4hRQ/4gMNbWHmZrNzSEIg20s2NULbzom5k0=
+github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
+github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
+github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
+github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
+github.com/grokify/html-strip-tags-go v0.0.1 h1:0fThFwLbW7P/kOiTBs03FsJSV9RM2M/Q/MOnCQxKMo0=
+github.com/grokify/html-strip-tags-go v0.0.1/go.mod h1:2Su6romC5/1VXOQMaWL2yb618ARB8iVo6/DR99A6d78=
+github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
+github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
+github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
+github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
+github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
+github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
+github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
+github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
+github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
+github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
+github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
+github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
+github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
+github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
+github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
+github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
+go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM=
+go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU=
+go.opentelemetry.io/otel/sdk v1.14.0 h1:PDCppFRDq8A1jL9v6KMI6dYesaq+DFcDZvjsoGvxGzY=
+go.opentelemetry.io/otel/sdk v1.14.0/go.mod h1:bwIC5TjrNG6QDCHNWvW4HLHtUQ4I+VQDsnjhvyZCALM=
+go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyKcFq/M=
+go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8=
+golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
+golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
+golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
+golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
+golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=