service_auth.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package middleware
  2. import (
  3. "cold-logistics/common/middleware/handler"
  4. "errors"
  5. "github.com/gin-gonic/gin"
  6. "gogs.baozhida.cn/zoie/OAuth-core/api"
  7. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  8. "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  9. "gorm.io/gorm"
  10. )
  11. // AuthCheckService 服务检查中间件
  12. func AuthCheckService() gin.HandlerFunc {
  13. return func(c *gin.Context) {
  14. serviceId := c.Request.Header.Get("serviceId")
  15. log := api.GetRequestLogger(c)
  16. ormDB, err := pkg.GetOrm(c)
  17. if err != nil {
  18. log.Errorf("get db error, %s", err.Error())
  19. response.Error(c, 500, err, "数据库连接获取失败")
  20. return
  21. }
  22. service := handler.SysService{}
  23. err = ormDB.Table("sys_service").Where("id = ? ", serviceId).First(&service).Error
  24. if err != nil {
  25. log.Errorf("get service error, %s", err.Error())
  26. if errors.Is(err, gorm.ErrRecordNotFound) {
  27. response.Error(c, 400, err, "未认证")
  28. return
  29. }
  30. response.Error(c, 500, err, "数据库连接获取失败")
  31. return
  32. }
  33. if service.Status == 1 {
  34. response.Error(c, 400, err, "服务已停用")
  35. return
  36. }
  37. c.Next()
  38. }
  39. }