Add AMap location and weather header with warm mini program theme
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sundynix/pets-be/internal/environment"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (a *API) environment(c *gin.Context) {
|
||||
lat, latErr := strconv.ParseFloat(c.Query("latitude"), 64)
|
||||
lon, lonErr := strconv.ParseFloat(c.Query("longitude"), 64)
|
||||
if latErr != nil || lonErr != nil {
|
||||
fail(c, http.StatusBadRequest, "请提供有效的定位坐标")
|
||||
return
|
||||
}
|
||||
result, err := a.env.Lookup(c.Request.Context(), lat, lon)
|
||||
if err != nil {
|
||||
status := http.StatusBadGateway
|
||||
if errors.Is(err, environment.ErrCoordinates) {
|
||||
status = http.StatusBadRequest
|
||||
}
|
||||
if errors.Is(err, environment.ErrNotConfigured) {
|
||||
status = http.StatusServiceUnavailable
|
||||
}
|
||||
fail(c, status, err.Error())
|
||||
return
|
||||
}
|
||||
ok(c, result)
|
||||
}
|
||||
@@ -12,18 +12,20 @@ import (
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/sundynix/pets-be/internal/config"
|
||||
"github.com/sundynix/pets-be/internal/environment"
|
||||
"github.com/sundynix/pets-be/internal/model"
|
||||
"github.com/sundynix/pets-be/internal/storage"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
env *environment.Client
|
||||
db *gorm.DB
|
||||
storage *storage.Storage
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
func New(db *gorm.DB, storage *storage.Storage, cfg *config.Config) *API {
|
||||
return &API{db: db, storage: storage, cfg: cfg}
|
||||
return &API{db: db, storage: storage, cfg: cfg, env: environment.New(cfg.AMap.Key)}
|
||||
}
|
||||
|
||||
func (a *API) Register(r *gin.Engine) {
|
||||
@@ -38,6 +40,7 @@ func (a *API) Register(r *gin.Engine) {
|
||||
|
||||
user := api.Group("")
|
||||
user.Use(a.requireRole("user"))
|
||||
user.GET("/environment", a.environment)
|
||||
user.GET("/me", a.me)
|
||||
user.GET("/pets", a.listPets)
|
||||
user.POST("/pets", a.createPet)
|
||||
|
||||
Reference in New Issue
Block a user