2
0

2 Commits edbc0cf060 ... cf1f77f090

Autor SHA1 Nachricht Datum
  lihf cf1f77f090 refactor(common): 重构 PageParams.GetPageParams 方法 vor 2 Monaten
  lihf a3e11ce4c1 refactor(config): 将 FileSource 类型移动到新文件 file.go vor 2 Monaten
3 geänderte Dateien mit 65 neuen und 49 gelöschten Zeilen
  1. 34 21
      common/page.go
  2. 31 0
      config/file.go
  3. 0 28
      config/source.go

+ 34 - 21
common/page.go

@@ -7,28 +7,41 @@ type PageParams struct {
 	Last     bool  `protobuf:"varint,4,opt,name=last,proto3" json:"last"`
 }
 
-//GetPageParams 从0开始的分页
-func (this *PageParams) GetPageParams() (limit int, offset int) {
-	_offset := this.Page * this.PageSize
-	if this.Total <= this.PageSize {
-		this.Page = 0
-		this.Last = true
-		return int(this.Total), 0
+// GetPageParams 从0开始的分页计算(返回limit和offset)
+func (p *PageParams) GetPageParams() (limit int, offset int) {
+	// 1. 校正无效参数
+	if p.Page < 0 {
+		p.Page = 0 // 页码为负时重置为第0页
 	}
-	_start := (this.Page + 1) * this.PageSize
+	if p.PageSize <= 0 {
+		p.PageSize = 10 // 页大小无效时用默认值
+	}
+
+	// 2. 处理总数据为0的情况
+	if p.Total <= 0 {
+		p.Last = true
+		return 0, 0
+	}
+
+	// 3. 计算基础偏移量和最大可显示的页码
+	offset64 := p.Page * p.PageSize
+	maxPage := (p.Total - 1) / p.PageSize // 最后一页的页码(从0开始)
 
-	if this.Total-_start < 0 {
-		this.Last = true
-		pageCount := this.Total / this.PageSize
-		this.Page = pageCount
-		num := this.Total - pageCount*this.PageSize
-		if num == 0 {
-			pageCount = pageCount - 1
-			return int(this.PageSize), int(pageCount * this.PageSize)
-		}
-		return int(num), int(pageCount * this.PageSize)
-	} else if this.Total-_start == 0 {
-		this.Last = true
+	// 4. 如果当前页超出最大页码,强制跳转到最后一页
+	if p.Page > maxPage {
+		p.Page = maxPage
+		offset64 = p.Page * p.PageSize
 	}
-	return int(this.PageSize), int(_offset)
+
+	// 5. 计算实际返回的条数(最后一页可能不足一页)
+	remaining := p.Total - offset64
+	if remaining < p.PageSize {
+		limit = int(remaining)
+		p.Last = true // 剩余数据不足一页,说明是最后一页
+	} else {
+		limit = int(p.PageSize)
+		p.Last = p.Page == maxPage // 当前页等于最大页时才是最后一页
+	}
+
+	return limit, int(offset64)
 }

+ 31 - 0
config/file.go

@@ -0,0 +1,31 @@
+package config
+
+import (
+	"fmt"
+	"github.com/go-kratos/kratos/v2/config"
+	"github.com/go-kratos/kratos/v2/config/file"
+)
+
+type FileSource struct {
+	Format Format
+	Path   string
+}
+
+func (s *FileSource) NewSource() (config.Source, error) {
+	source := file.NewSource(s.Path)
+	return source, nil
+}
+
+func (s *FileSource) Validate() bool {
+	if !s.Format.Validate() {
+		return false
+	}
+	if s.Path == "" {
+		return false
+	}
+	return true
+}
+
+func (s *FileSource) String() string {
+	return fmt.Sprintf("file source format:%v, path:%v", s.Format, s.Path)
+}

+ 0 - 28
config/source.go

@@ -1,9 +1,7 @@
 package config
 
 import (
-	"fmt"
 	"github.com/go-kratos/kratos/v2/config"
-	"github.com/go-kratos/kratos/v2/config/file"
 )
 
 type Source interface {
@@ -30,29 +28,3 @@ func (f Format) Validate() bool {
 	_, ok := formatMap[f]
 	return ok
 }
-
-// =======================================
-
-type FileSource struct {
-	Format Format
-	Path   string
-}
-
-func (s *FileSource) NewSource() (config.Source, error) {
-	source := file.NewSource(s.Path)
-	return source, nil
-}
-
-func (s *FileSource) Validate() bool {
-	if !s.Format.Validate() {
-		return false
-	}
-	if s.Path == "" {
-		return false
-	}
-	return true
-}
-
-func (s *FileSource) String() string {
-	return fmt.Sprintf("file source format:%v, path:%v", s.Format, s.Path)
-}