api.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package common
  2. import (
  3. "Cold_Logistic/internal/pkg/common/codex"
  4. "Cold_Logistic/internal/pkg/common/global"
  5. "Cold_Logistic/internal/server/adapter/http/middleware"
  6. "Cold_Logistic/internal/server/application/commonsrv"
  7. "Cold_Logistic/internal/server/application/devicesrv"
  8. "Cold_Logistic/internal/server/infra/dao"
  9. "github.com/gin-gonic/gin"
  10. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  11. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  12. )
  13. func Register(r *gin.RouterGroup) {
  14. api := NewApi()
  15. common := r.Group("/common")
  16. common.Use(middleware.Auth())
  17. common.GET("/provinceList", api.provinceList) // 省市区列表
  18. common.GET("/sensorDataList", api.sensorDataList) // 设备数据列表
  19. common.GET("/logisticList", api.logisticList) // 设备数据列表
  20. return
  21. }
  22. type Api struct{}
  23. func NewApi() Api {
  24. return Api{}
  25. }
  26. func (api Api) provinceList(c *gin.Context) {
  27. srv := commonsrv.NewCommonService(dao.NewDataStore(global.CommonConnectRepoInst.StoreDB))
  28. data, err := srv.ProvinceList(c)
  29. if err != nil {
  30. core.WriteErrResponse(c, errors.WithCodeOnce(err, codex.ErrQueryFailed, ""))
  31. return
  32. }
  33. core.WriteResponse(c, nil, data)
  34. }
  35. // sensorDataList 设备数据列表
  36. func (api Api) sensorDataList(c *gin.Context) {
  37. req := devicesrv.SnDataListReqVO{}
  38. if err := c.BindJSON(&req); err != nil {
  39. core.WriteErrResponse(c, errors.WithCodeOnce(err, codex.ErrBindJSON, ""))
  40. return
  41. }
  42. if err := req.Validate(); err != nil {
  43. core.WriteErrResponse(c, errors.WithCodeOnce(err, codex.ErrParamValidate, "参数验证失败:"))
  44. return
  45. }
  46. srv := devicesrv.NewDeviceService(dao.NewDataStore(global.CommonConnectRepoInst.StoreDB))
  47. res, err := srv.SnDataList(c, req)
  48. if err != nil {
  49. core.WriteErrResponse(c, errors.WithCodeOnce(err, codex.ErrQueryFailed, ""))
  50. return
  51. }
  52. core.WriteResponse(c, nil, res)
  53. }
  54. // logisticList 物流公司列表
  55. func (api Api) logisticList(c *gin.Context) {
  56. req := commonsrv.LogisticListReqVO{}
  57. if err := c.BindJSON(&req); err != nil {
  58. core.WriteErrResponse(c, errors.WithCodeOnce(err, codex.ErrBindJSON, ""))
  59. return
  60. }
  61. srv := commonsrv.NewCommonService(dao.NewDataStore(global.CommonConnectRepoInst.StoreDB))
  62. res, err := srv.LogisticList(c, req)
  63. if err != nil {
  64. core.WriteErrResponse(c, errors.WithCodeOnce(err, codex.ErrQueryFailed, ""))
  65. return
  66. }
  67. core.WriteResponse(c, nil, res)
  68. }